--- t:src/codepage.c	Tue Sep 03 10:49:22 2002
+++ codepage.c	Mon Feb 10 11:18:20 2003
@@ -312,33 +312,42 @@
 176, 168, 255, 251, 216, 248, 166, 160, 
 };
 
+unsigned int unity[256];
 
 FILE *infile;
 FILE *outfile;;
 int unicode = 0;
+int utf8 = 0;
 int ch;
 
 typedef struct codepage_s {
     const char *name;	    /* name of code page */
     unsigned int *table;    /* code page translation table */
-    int unicode;	    /* non-zero if output is Unicode */
+    int unicode;	    /* non-zero if output is Unicode UCS16 */
+    int utf8;	    	    /* non-zero if output is Unicode UTF-8 */
 } codepage_t;
 
 codepage_t cptable[] = {
-    {"1252_437", cp1252_to_437, 0},	/* MS-DOS United States */
-    {"1252_850", cp1252_to_850, 0},	/* MS-DOS Multilingual (Latin I) */
-    {"850_1252", cp850_to_1252, 0},	/* MS-DOS Multilingual (Latin I) */
-    {"1253_869", cp1253_to_869, 0},	 /* IBM Modern Greek */
-    {"1253_28597",  cp1253_to_28597, 0}, /* ISO-8859-7 Greek */
-    {"1253_8859-7", cp1253_to_28597, 0}, /* ISO-8859-7 Greek */
-    {"1253_uc", cp1253_to_unicode, 1},   /* Greek to Unicode */
-    {"1250_852", cp1250_to_852, 0},	/* MS-DOS Eastern Europe (Latin 2) */
-    {"852_1250", cp852_to_1250, 0},	/* MS-DOS Eastern Europe (Latin 2) */
+    {"1252_437", cp1252_to_437, 0, 0},	/* MS-DOS United States */
+    {"1252_850", cp1252_to_850, 0, 0},	/* MS-DOS Multilingual (Latin I) */
+    {"1252_uc", unity, 1, 0},		/* Latin1 to Unicode UCS16 */
+    {"1252_utf8", unity, 0, 1},		/* Latin1 to Unicode UTF-8 */
+    {"850_1252", cp850_to_1252, 0, 0},	/* MS-DOS Multilingual (Latin I) */
+    {"1253_869", cp1253_to_869, 0, 0},	 /* IBM Modern Greek */
+    {"1253_28597",  cp1253_to_28597, 0, 0}, /* ISO-8859-7 Greek */
+    {"1253_8859-7", cp1253_to_28597, 0, 0}, /* ISO-8859-7 Greek */
+    {"1253_uc", cp1253_to_unicode, 1, 0},   /* Greek to Unicode */
+    {"1253_utf8", cp1253_to_unicode, 0, 1}, /* Greek to Unicode UTF-8*/
+    {"1250_852", cp1250_to_852, 0, 0},	/* MS-DOS Eastern Europe (Latin 2) */
+    {"852_1250", cp852_to_1250, 0, 0},	/* MS-DOS Eastern Europe (Latin 2) */
     {NULL, NULL}};
 
 int main(int argc, char *argv[])
 {
     int i;
+    unsigned int uch;
+    for (i=0; i<sizeof(unity)/sizeof(unity[0]); i++)
+	unity[i] = i;
 
     cp = NULL;
     if (argc >= 4) {
@@ -346,6 +355,7 @@
 	    if (strcmp(argv[1], cptable[i].name)==0) {
 		cp = cptable[i].table;
 		unicode = cptable[i].unicode;
+		utf8 = cptable[i].utf8;
 		break;
 	    }
 	}
@@ -384,7 +394,24 @@
     }
     while ( (ch = fgetc(infile)) != EOF ) {
 	if ((ch >=0) && (ch <= 0xff)) {
-	    if (unicode) {
+	    if (utf8) {
+		uch = cp[(unsigned int)ch];
+		if (uch <= 0x7f)
+	            fputc(uch & 0x7f, outfile);
+		else if (uch <= 0x7ff) {
+		    fputc(0xc0 | ((uch >> 6) & 0x1f), outfile);
+		    fputc(0x80 | (uch & 0x3f), outfile);
+		}
+		else if (uch <= 0x7ff) {
+		    fputc(0xc0 | ((uch >> 12) & 0xf), outfile);
+		    fputc(0x80 | ((uch >> 6) & 0x3f), outfile);
+		    fputc(0x80 | (uch & 0x3f), outfile);
+		}
+		else {
+		    fputc('?', outfile);
+		}
+	    }
+	    else if (unicode) {
 	        fputc((char)( cp[(unsigned int)ch]       & 0xff), outfile);
 	        fputc((char)((cp[(unsigned int)ch] >> 8) & 0xff), outfile);
 	    }
