home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / j_tools.zip / jconv.c < prev    next >
C/C++ Source or Header  |  1998-01-14  |  41KB  |  1,381 lines

  1. /*
  2. Program: jconv.c
  3. Version: 3.0
  4. Date:    July 1, 1993
  5. Author:  Ken R. Lunde, Adobe Systems Incorporated
  6.   EMAIL: lunde@mv.us.adobe.com
  7.   MAIL : 1585 Charleston Road, P.O. Box 7900, Mountain View, CA 94039-7900
  8. Type:    A tool for converting the Japanese code of Japanese textfiles.
  9. Code:    ANSI C (portable)
  10.  
  11. PORTABILITY:
  12. This source code was written so that it would be portable on C compilers which
  13. conform to the ANSI C standard. It has been tested on a variety of compilers.
  14.  
  15. I used THINK C and GNU C as my development platforms. I left in the Macintosh-
  16. specific lines of code so that it would be easier to enhance/debug this tool
  17. later. For those of you who wish to use this tool on the Macintosh, simply
  18. add the ANSI library to the THINK C project, and then build the application.
  19. Be sure that THINK_C has been defined, though, as the conditional compilation
  20. depends on it. You then have a double-clickable application, which when
  21. launched, will greet you with a Macintosh-style interface.
  22.  
  23. DISTRIBUTION AND RESTRICTIONS ON USAGE:
  24.  1) Please give this source code away to your friends at no charge.
  25.  2) Please try to compile this source code on various platforms to check for
  26.     portablity, and please report back to me with any results be they good or
  27.     bad. Suggestions are always welcome.
  28.  3) Only use this tool on a copy of a file -- do not use an original. This
  29.     is just common sense.
  30.  4) This source code or a compiled version may be bundled with commercial
  31.     software as long as the author is notified beforehand. The author's name
  32.     should also be mentioned in the credits.
  33.  5) Feel free to use any of the algorithms for your own work. Many of them are
  34.     being used in other tools I have written.
  35.  6) The most current version can be obtained by requesting a copy directly
  36.     from me.
  37.  
  38. DESCRIPTION:
  39.  1) Supports Shift-JIS, EUC, New-JIS, Old-JIS, and NEC-JIS for both input and
  40.     output.
  41.  2) Automatically detects infile's Japanese code (the ability to force an
  42.     input Japanese code is also supported through a command-line option).
  43.  3) The ability to convert Shift-JIS and EUC half-width katakana into full-
  44.     width equivalents. Note that half-width katakana includes other symbols
  45.     such as a Japanese period, Japanese comma, center dot, etc.
  46.  4) Supports conversion between the same code (i.e., EUC -> EUC, Shift-JIS ->
  47.     Shift-JIS, etc.). This is useful as a filter for converting half-width
  48.     katakana to their full-width equivalents.
  49.  5) If the infile does not contain any Japanese, then its contents are
  50.     echoed to the outfile. It will also try to repair the file as though
  51.     it had stripped escape characters (see #6 below).
  52.  6) The functionality of my other tool called repair-jis.c is included in
  53.     this tool by using the "-r[CODE]" option. This will recover stripped
  54.     escape characters, then convert the file to be in CODE format.
  55.  
  56. FILE HANDLING:
  57.  1) Specifying the infile is optional as one can redirect stdin and stdout.
  58.  2) Specifying the outfile is also optional. If none is specified, the
  59.     tool will semi-intelligently change the file's name. The tool simply
  60.     simply scans the outname, finds the last period in it, terminates
  61.     the string at that point, and tacks on one of seven possible extensions.
  62.     Here are some example command lines, and the resulting outfile names:
  63.  
  64.     a) jconv -oe sig.jpn                  = sig.euc
  65.     b) jconv sig.jpn                      = sig.sjs (defaulted to Shift-JIS)
  66.     c) jconv -oj sig.jpn.txt              = sig.jpn.new
  67.     d) jconv -oo sig                      = sig.old
  68.  
  69.     This is very useful for MS-DOS users since a filename such as sig.jpn.new
  70.     will not result in converting a file called sig.jpn.
  71.  
  72.     Also note that if the outfile and infile have the same name, the tool
  73.     will not work, and data will be lost. I tried to build safe-guards against
  74.     this. For example, note how this tool will change the outfile name so
  75.     that it does not overwrite the infile:
  76.     
  77.     a) jconv -f sig.sjs                   = sig-.sjs
  78.     b) jconv -f sig.sjs sig.sjs           = sig-.sjs
  79.     c) jconv sig-.sjs                     = sig--.sjs
  80.     
  81.     If only the infile is given, a hyphen is inserted after the last period,
  82.     and the extension is then reattached. If the outfile is specified by the
  83.     user, then it will search for the last period (if any), attach a hyphen,
  84.     and finally attach the proper extension). This sort of protection is NOT
  85.     available from this tool if stdin/stdout are used.
  86.  3) If you want to specify an infile, but to have the output to go to stdout,
  87.     you have two options:
  88.  
  89.     a) jconv < sig.jpn                    <= redirect stdin for infile
  90.     b) jconv sig.jpn -                    <= specify infile, then use "-" to
  91.                                              indictate stdout for output
  92.  
  93.  4) When using pipes under UNIX, it is very important to use the "-iCODE"
  94.     option so that the automatic code detection routines are skipped. This
  95.     is because the tool attempts to use the rewind() function, and this
  96.     has no effect on some machines if input is coming from a pipe. One can
  97.     easily use the "-c" option to find out what the infile code is, then
  98.     apply it in a pipe. Here are some examples (they assume that the infile
  99.     code is EUC):
  100.  
  101.     a) cat japan.txt | jconv -oj          <= does not work
  102.     a) cat japan.txt | jconv -ie -oj      <= works
  103. */
  104.  
  105. #ifdef THINK_C
  106. #include <console.h>
  107. #include <stdlib.h>
  108. #endif
  109. #include <stdio.h>
  110. #include <string.h>
  111. #include <ctype.h>
  112. #ifndef THINK_C
  113. #include <errno.h>
  114. #endif
  115.  
  116. #define INPUT       1
  117. #define OUTPUT      2
  118. #define REPAIR      3
  119. #define NOTSET      0
  120. #define NEW         1
  121. #define OLD         2
  122. #define NEC         3
  123. #define EUC         4
  124. #define SJIS        5
  125. #define EUCORSJIS   6
  126. #define ASCII       7
  127. #define NUL         0
  128. #define NL          10
  129. #define CR          13
  130. #define ESC         27
  131. #define SS2         142
  132. #define TRUE        1
  133. #define FALSE       0
  134. #define PERIOD      '.'
  135. #define SJIS1(A)    ((A >= 129 && A <= 159) || (A >= 224 && A <= 239))
  136. #define SJIS2(A)    (A >= 64 && A <= 252)
  137. #define HANKATA(A)  (A >= 161 && A <= 223)
  138. #define ISEUC(A)    (A >= 161 && A <= 254)
  139. #define ISMARU(A)   (A >= 202 && A <= 206)
  140. #define ISNIGORI(A) ((A >= 182 && A <= 196) || (A >= 202 && A <= 206) || (A == 179))
  141. #ifndef SEEK_CUR
  142. #define SEEK_CUR    1
  143. #endif
  144. /* The following 8 lines of code are used to establish the default output codes
  145.  * when using the "-o[CODE]" or "-r[CODE]" options. They are self-explanatory,
  146.  * and easy to change.
  147.  */
  148. #define DEFAULT_O   SJIS     /* default output code for "-o[CODE]" option */
  149. #define DEFAULT_OS  ".sjs"   /* default file extension for "-o[CODE]" option */
  150. #define DEFAULT_OKI ""       /* default kanji-in code for "-o[CODE]" option */
  151. #define DEFAULT_OKO ""       /* default kanji-out code for "-o[CODE]" option */
  152. #define DEFAULT_R   NEW      /* default output code for "-r[CODE]" option */
  153. #define DEFAULT_RS  ".new"   /* default file extension for "-r[CODE]" option */
  154. #define DEFAULT_RKI "$B"     /* default kanji-in code for "-r[CODE]" option */
  155. #define DEFAULT_RKO "(J"     /* default kanji-out code for "-r[CODE]" option */
  156.  
  157. void han2zen(FILE *in,int *p1,int *p2,int incode);
  158. void sjis2jis(int *p1,int *p2);
  159. void jis2sjis(int *p1,int *p2);
  160. void shift2seven(FILE *in,FILE *out,int incode,char ki[],char ko[]);
  161. void shift2euc(FILE *in,FILE *out,int incode,int tofullsize);
  162. void euc2seven(FILE *in,FILE *out,int incode,char ki[],char ko[]);
  163. void euc2euc(FILE *in,FILE *out,int incode,int tofullsize);
  164. void shift2shift(FILE *in,FILE *out,int incode,int tofullsize);
  165. void euc2shift(FILE *in,FILE *out,int incode,int tofullsize);
  166. void seven2shift(FILE *in,FILE *out);
  167. void seven2euc(FILE *in,FILE *out);
  168. void seven2seven(FILE *in,FILE *out,char ki[],char ko[]);
  169. void dohelp(char toolname[]);
  170. void dojistable(void);
  171. void doeuctable(void);
  172. void dosjistable(void);
  173. void jisrepair(FILE *in,FILE *out,int verbose,int outcode,char ki[],char ko[]);
  174. void removeescape(FILE *in,FILE *out,int verbose,int forcedelesc);
  175. void printcode(int code);
  176. int toup(int data);
  177. int SkipESCSeq(FILE *in,int temp,int *intwobyte);
  178. int DetectCodeType(FILE *in);
  179. int getcode(char extension[],int data,char ki[],char ko[],int doing);
  180. #ifdef THINK_C
  181. int ccommand(char ***p);
  182. #endif
  183.  
  184. void main(int argc,char **argv)
  185. {
  186.   FILE *in,*out;
  187. #ifndef THINK_C
  188.   int rc;
  189. #endif
  190.   int tempincode,incode,doing = FALSE,forcedelesc = FALSE;
  191.   int makeoutfile = TRUE,outcode = NOTSET,verbose = FALSE,delesc = FALSE;
  192.   int repairjis = FALSE,tofullsize = FALSE,setincode = FALSE,docheck = FALSE;
  193.   char infilename[100],outfilename[100],extension[10],ki[10],ko[10],toolname[100];
  194.  
  195. #ifdef THINK_C
  196.   argc = ccommand(&argv);
  197. #endif
  198.  
  199.   strcpy(toolname,*argv);
  200.   while (--argc > 0 && (*++argv)[0] == '-')
  201.     switch (toup(*++argv[0])) {
  202.       case 'C' :
  203.         docheck = TRUE;
  204.         break;
  205.       case 'F' :
  206.         tofullsize = TRUE;
  207.         break;
  208.       case 'H' :
  209.         dohelp(toolname);
  210.         break;
  211.       case 'I' :
  212.         setincode = TRUE;
  213.         doing = INPUT;
  214.         incode = getcode(extension,toup(*++argv[0]),ki,ko,doing);
  215.         break;
  216.       case 'O' :
  217.         doing = OUTPUT;
  218.         outcode = getcode(extension,toup(*++argv[0]),ki,ko,doing);
  219.         break;
  220.       case 'R' :
  221.         repairjis = TRUE;
  222.         doing = REPAIR;
  223.         outcode = getcode(extension,toup(*++argv[0]),ki,ko,doing);
  224.         break;
  225.       case 'S' :
  226.         delesc = TRUE;
  227.         strcpy(extension,".rem");
  228.         if (toup(*++argv[0]) == 'F')
  229.           forcedelesc = TRUE;
  230.         break;
  231.       case 'T' :
  232.         switch (toup(*++argv[0])) {
  233.           case 'E' :
  234.             doeuctable();
  235.             break;
  236.           case 'J' :
  237.           case 'N' :
  238.           case 'O' :
  239.             dojistable();
  240.             break;
  241.           case 'S' :
  242.             dosjistable();
  243.             break;
  244.           default :
  245.             dojistable();
  246.             dosjistable();
  247.             doeuctable();
  248.             break;
  249.         }
  250.         exit(0);
  251.         break;
  252.       case 'V' :
  253.         verbose = TRUE;
  254.         break;
  255.       default :
  256.         fprintf(stderr,"Illegal option \"-%c\"! Try using the \"-h\" option for help.\n",*argv[0]);
  257.         fprintf(stderr,"Usage: %s [-options] [infile] [outfile]\nExiting...\n",toolname);
  258.         exit(1);
  259.         break;
  260.     }
  261.   if (repairjis && delesc) {
  262.     fprintf(stderr,"Error! Both \"-r\" and \"-s\" options cannot be selected! Exiting...\n");
  263.     exit(1);
  264.   }
  265.   if (outcode == NOTSET && !repairjis && !delesc) {
  266.     strcpy(ki,DEFAULT_OKI);
  267.     strcpy(ko,DEFAULT_OKO);
  268.     strcpy(extension,DEFAULT_OS);
  269.     outcode = DEFAULT_O;
  270.   }
  271.   if (argc == 0) {
  272. #ifndef THINK_C
  273.     rc = lseek(0,0,SEEK_CUR);
  274.     if (rc == -1 && errno == ESPIPE && !setincode && !delesc && !docheck && !repairjis) {
  275.       fprintf(stderr,"Cannot automatically detect input code from a pipe!\n");
  276.       fprintf(stderr,"Try \"-c\" to determine input code, then apply it to \"iCODE\" option.\n");
  277.       fprintf(stderr,"Exiting...\n");
  278.       exit(1);
  279.     }
  280. #endif
  281.     in = stdin;
  282.     out = stdout;
  283.   }
  284.   else if (argc > 0) {
  285.     if (argc == 1) {
  286.       strcpy(infilename,*argv);
  287.       if (strchr(*argv,PERIOD) != NULL)
  288.         *strrchr(*argv,PERIOD) = '\0';
  289.       strcpy(outfilename,*argv);
  290.       strcat(outfilename,extension);
  291.       if (!strcmp(infilename,outfilename)) {
  292.         if (strchr(outfilename,PERIOD) != NULL)
  293.           *strrchr(outfilename,PERIOD) = '\0';
  294.         strcat(outfilename,"-");
  295.         strcat(outfilename,extension);
  296.       }
  297.       if (verbose && !docheck)
  298.         fprintf(stderr,"Output file will be named %s\n",outfilename);
  299.     }
  300.     else if (argc > 1) {
  301.       strcpy(infilename,*argv++);
  302.       if (*argv[0] == '-') {
  303.         out = stdout;
  304.         makeoutfile = FALSE;
  305.       }
  306.       else {
  307.         strcpy(outfilename,*argv);
  308.         if (!strcmp(infilename,outfilename)) {
  309.           if (strchr(outfilename,PERIOD) != NULL)
  310.             *strrchr(outfilename,PERIOD) = '\0';
  311.           strcat(outfilename,"-");
  312.           strcat(outfilename,extension);
  313.         }
  314.       }
  315.     }
  316.     if ((in = fopen(infilename,"r")) == NULL) {
  317.       fprintf(stderr,"Cannot open %s! Exiting...\n",infilename);
  318.       exit(1);
  319.     }
  320.     if (!docheck && makeoutfile)
  321.       if ((out = fopen(outfilename,"w")) == NULL) {
  322.         fprintf(stderr,"Cannot open %s! Exiting...\n",outfilename);
  323.         exit(1);
  324.       }
  325.   }
  326.   if (repairjis) {
  327.     jisrepair(in,out,verbose,outcode,ki,ko);
  328.     exit(0);
  329.   }
  330.   if (delesc) {
  331.     removeescape(in,out,verbose,forcedelesc);
  332.     exit(0);
  333.   }
  334.   tempincode = incode;
  335.   if (setincode && verbose) {
  336.     fprintf(stderr,"User-selected input code: ");
  337.     printcode(incode);
  338.   }
  339.   if (verbose && !docheck) {
  340.     fprintf(stderr,"User-selected output code: ");
  341.     printcode(outcode);
  342.   }
  343.   if (setincode && verbose)
  344.     ;
  345.   else if (!setincode || docheck || verbose) {
  346.     incode = DetectCodeType(in);
  347.     if (docheck || verbose) {
  348.       if (setincode && docheck)
  349.         fprintf(stderr,"NOTE: The selected \"-iCODE\" option was ignored\n");
  350.       fprintf(stderr,"Detected input code: ");
  351.       printcode(incode);
  352.       if (docheck)
  353.         exit(0);
  354.     }
  355.     rewind(in);
  356.   }
  357.   if (setincode)
  358.     incode = tempincode;
  359.   switch (incode) {
  360.     case NOTSET :
  361.       fprintf(stderr,"Unknown input code! Exiting...\n");
  362.       exit(1);
  363.       break;
  364.     case EUCORSJIS :
  365.       fprintf(stderr,"Ambiguous (Shift-JIS or EUC) input code!\n");
  366.       fprintf(stderr,"Try using the \"-iCODE\" option to specify either Shift-JIS or EUC.\n");
  367.       fprintf(stderr,"Exiting...\n");
  368.       exit(1);
  369.       break;
  370.     case ASCII :
  371.       fprintf(stderr,"Since detected input code is ASCII, it may be damaged New- or Old-JIS\n");
  372.       fprintf(stderr,"Trying to repair...\n");
  373.       jisrepair(in,out,verbose,outcode,ki,ko);
  374.       break;
  375.     case NEW :
  376.     case OLD :
  377.     case NEC :
  378.       switch (outcode) {
  379.         case NEW :
  380.         case OLD :
  381.         case NEC :
  382.           seven2seven(in,out,ki,ko);
  383.           break;
  384.         case EUC :
  385.           seven2euc(in,out);
  386.           break;
  387.         case SJIS :
  388.           seven2shift(in,out);
  389.           break;
  390.       }
  391.       break;
  392.     case EUC :
  393.       switch (outcode) {
  394.         case NEW :
  395.         case OLD :
  396.         case NEC :
  397.           euc2seven(in,out,incode,ki,ko);
  398.           break;
  399.         case EUC :
  400.           euc2euc(in,out,incode,tofullsize);
  401.           break;
  402.         case SJIS :
  403.           euc2shift(in,out,incode,tofullsize);
  404.           break;
  405.       }
  406.       break;
  407.     case SJIS :
  408.       switch (outcode) {
  409.         case NEW :
  410.         case OLD :
  411.         case NEC :
  412.           shift2seven(in,out,incode,ki,ko);
  413.           break;
  414.         case EUC :
  415.           shift2euc(in,out,incode,tofullsize);
  416.           break;
  417.         case SJIS :
  418.           shift2shift(in,out,incode,tofullsize);
  419.           break;
  420.       }
  421.       break;
  422.   }
  423.   exit(0);
  424. }
  425.  
  426. int toup(int data)
  427. {
  428.   if (islower(data))
  429.     return (toupper(data));
  430.   else
  431.     return data;
  432. }
  433.  
  434. void printcode(int code)
  435. {
  436.   switch (code) {
  437.     case OLD :
  438.       fprintf(stderr,"Old-JIS\n");
  439.       break;
  440.     case NEW :
  441.       fprintf(stderr,"New-JIS\n");
  442.       break;
  443.     case NEC :
  444.       fprintf(stderr,"NEC-JIS\n");
  445.       break;
  446.     case EUC :
  447.       fprintf(stderr,"EUC\n");
  448.       break;
  449.     case SJIS :
  450.       fprintf(stderr,"Shift-JIS\n");
  451.       break;
  452.     case EUCORSJIS :
  453.       fprintf(stderr,"ambiguous (Shift-JIS or EUC)\n");
  454.       break;
  455.     case ASCII :
  456.       fprintf(stderr,"ASCII (no Japanese)\n");
  457.       break;
  458.     case NOTSET :
  459.       fprintf(stderr,"unknown\n");
  460.       break;
  461.     default :
  462.       break;
  463.   }
  464. }
  465.  
  466. int getcode(char extension[],int data,char ki[],char ko[],int doing)
  467. {
  468.   if (data == 'E') {
  469.     if (doing == OUTPUT || doing == REPAIR)
  470.       strcpy(extension,".euc");
  471.     return EUC;
  472.   }
  473.   else if (data == 'S') {
  474.     if ((doing == OUTPUT) || (doing == REPAIR))
  475.       strcpy(extension,".sjs");
  476.     return SJIS;
  477.   }
  478.   else if (data == 'J') {
  479.     if (doing == OUTPUT || doing == REPAIR) {
  480.       strcpy(ki,"$B");
  481.       strcpy(ko,"(J");
  482.       strcpy(extension,".new");
  483.     }
  484.     return NEW;
  485.   }
  486.   else if (data == 'O') {
  487.     if (doing == OUTPUT || doing == REPAIR) {
  488.       strcpy(ki,"$@");
  489.       strcpy(ko,"(J");
  490.       strcpy(extension,".old");
  491.     }
  492.     return OLD;
  493.   }
  494.   else if (data == 'N') {
  495.     if (doing == OUTPUT || doing == REPAIR) {
  496.       strcpy(ki,"K");
  497.       strcpy(ko,"H");
  498.       strcpy(extension,".nec");
  499.     }
  500.     return NEC;
  501.   }
  502.   else {
  503.     if (doing == INPUT) {
  504.       fprintf(stderr,"Missing or invalid user-selected input code! Exiting...\n");
  505.       exit(1);
  506.     }
  507.     else if (doing == OUTPUT) {
  508.       strcpy(ki,DEFAULT_OKI);
  509.       strcpy(ko,DEFAULT_OKO);
  510.       strcpy(extension,DEFAULT_OS);
  511.       return DEFAULT_O;
  512.     }
  513.     else if (doing == REPAIR) {
  514.       strcpy(ki,DEFAULT_RKI);
  515.       strcpy(ko,DEFAULT_RKO);
  516.       strcpy(extension,DEFAULT_RS);
  517.       return DEFAULT_R;
  518.     }
  519.   }
  520. }
  521.  
  522. void dojistable(void)
  523. {
  524.   printf("JIS Code Specifications:\n");
  525.   printf("                             DECIMAL                  HEXADECIMAL\n");
  526.   printf("Two-byte character escape sequences\n");
  527.   printf(" JIS C 6226-1978 (Old-JIS)   027 036 064              1B 24 40\n");
  528.   printf(" JIS C 6226-1978 (NEC-JIS)   027 075                  1B 4B\n");
  529.   printf(" JIS X 0208-1983 (New-JIS)   027 036 066              1B 24 42\n");
  530.   printf(" JIS X 0208-1990             027 038 064 027 036 066  1B 26 40 1B 24 42\n");
  531.   printf(" JIS X 0212-1990             027 036 040 068          1B 24 28 44\n");
  532.   printf("Two-byte characters\n");
  533.   printf(" first byte range            033-126                  21-7E\n");
  534.   printf(" second byte range           033-126                  21-7E\n");
  535.   printf("One-byte character escape sequences\n");
  536.   printf(" JIS-Roman                   027 040 074              1B 28 4A\n");
  537.   printf(" JIS-Roman (NEC)             027 072                  1B 48\n");
  538.   printf(" ASCII                       027 040 066              1B 28 42\n");
  539.   printf(" half-width katakana         027 040 073              1B 28 49\n");
  540.   printf("JIS7 half-width katakana\n");
  541.   printf(" byte range                  033-095                  21-5F\n");
  542.   printf("JIS8 half-width katakana\n");
  543.   printf(" shift-out                   014                      0E\n");
  544.   printf(" byte range                  161-223                  A1-DF\n");
  545.   printf(" shift-in                    015                      0F\n\n");
  546.   printf("NOTE: This version of the tool does not support the escape sequences\n");
  547.   printf("      for JIS X 0212-1990 and JIS X 0208-1990, nor any of the half-\n");
  548.   printf("      width katakana specifications\n\n\n");
  549. }
  550.  
  551. void doeuctable(void)
  552. {
  553.   printf("EUC Code Specifications (Packed format):\n");
  554.   printf("                             DECIMAL                  HEXADECIMAL\n");
  555.   printf("Code set 0 (ASCII)\n");
  556.   printf(" byte range                  033-126                  21-7E\n");
  557.   printf("Code set 1 (JIS X 0208-1990)\n");
  558.   printf(" first byte range            161-254                  A1-FE\n");
  559.   printf(" second byte range           161-254                  A1-FE\n");
  560.   printf("Code set 2 (Half-width katakana)\n");
  561.   printf(" first byte                  142                      8E\n");
  562.   printf(" second byte range           161-223                  A1-DF\n");
  563.   printf("Code set 3 (JIS X 0212-1990)\n");
  564.   printf(" first byte                  143                      8F\n");
  565.   printf(" second byte range           161-254                  A1-FE\n");
  566.   printf(" third byte range            161-254                  A1-FE\n\n");
  567.   printf("EUC Code Specifications (Complete 2-byte format):\n");
  568.   printf("                             DECIMAL                  HEXADECIMAL\n");
  569.   printf("Code set 0 (ASCII)\n");
  570.   printf(" first byte                  000                      00\n");
  571.   printf(" second byte range           033-126                  21-7E\n");
  572.   printf("Code set 1 (JIS X 0208-1990)\n");
  573.   printf(" first byte range            161-254                  A1-FE\n");
  574.   printf(" second byte range           161-254                  A1-FE\n");
  575.   printf("Code set 2 (Half-width katakana)\n");
  576.   printf(" first byte                  000                      00\n");
  577.   printf(" second byte range           161-223                  A1-DF\n");
  578.   printf("Code set 3 (JIS X 0212-1990)\n");
  579.   printf(" first byte range            161-254                  A1-FE\n");
  580.   printf(" second byte range           033-126                  21-7E\n\n");
  581.   printf("NOTE: This version of the tool does not support code set 3 nor\n");
  582.   printf("      the Complete 2-byte format\n\n\n");
  583. }
  584.  
  585. void dosjistable(void)
  586. {
  587.   printf("Shift-JIS Code Specifications:\n");
  588.   printf("                             DECIMAL                  HEXADECIMAL\n");
  589.   printf("Two-byte characters\n");
  590.   printf(" first byte range            129-159, 224-239         81-9F, E0-EF\n");
  591.   printf(" second byte range           064-126, 128-252         40-7E, 80-FC\n");
  592.   printf("One-byte characters\n");
  593.   printf(" Half-width katakana         161-223                  A1-DF\n");
  594.   printf(" ASCII/JIS-Roman             033-126                  21-7E\n\n\n");
  595. }
  596.  
  597. void dohelp(char toolname[])
  598. {
  599.   printf("** %s v3.0 (July 1, 1993) **\n\n",toolname);
  600.   printf("Written by Ken R. Lunde, Adobe Systems Incorporated\nlunde@mv.us.adobe.com\n\n");
  601.   printf("Usage: %s [-options] [infile] [outfile]\n\n",toolname);
  602.   printf("Tool description: This tool is a utility for converting the Japanese code of\n");
  603.   printf("textfiles, and supports Shift-JIS, EUC, New-JIS, Old-JIS, and NEC-JIS for\n");
  604.   printf("both input and output. It can also display a file's input code, repair\n");
  605.   printf("damaged Old- or New-JIS files, and display the specifications for any of the\n");
  606.   printf("handled codes.\n\n");
  607.   printf("Options include:\n\n");
  608.   printf("  -c        Displays the detected input code, then exits -- the types\n");
  609.   printf("            reported include EUC, Shift-JIS, New-JIS, Old-JIS, NEC-JIS, ASCII\n");
  610.   printf("            (no Japanese), ambiguous (Shift-JIS or EUC), and unknown (note\n");
  611.   printf("            that this option overrides \"-iCODE\")\n");
  612.   printf("  -f        Converts half-width katakana to their full-width equivalents (this\n");
  613.   printf("            option is forced when output code is New-, Old-, or NEC-JIS)\n");
  614.   printf("  -h        Displays this help page, then exits\n");
  615.   printf("  -iCODE    Forces input code to be recognized as CODE\n");
  616.   printf("  -o[CODE]  Output code set to CODE (default is Shift-JIS if this option is\n");
  617.   printf("            not specified, or if the specified CODE is invalid)\n");
  618.   printf("  -r[CODE]  Repairs damaged New- and Old-JIS encoded files by restoring lost\n");
  619.   printf("            escape characters, then converts it to the CODE specified (the\n");
  620.   printf("            default is to convert the file to New-JIS if CODE is not\n");
  621.   printf("            specified -- cannot be used in conjunction with \"-s\")\n");
  622.   printf("  -s[f]     Removes escape characters from valid escape sequences of New- and\n");
  623.   printf("            Old-JIS encoded files -- \"f\" will force all escape characters\n");
  624.   printf("            to be removed (default extension is .rem -- cannot be used in\n");
  625.   printf("            conjunction with \"-r\")\n");
  626.   printf("  -t[CODE]  Prints a table listing the specifications for the specified CODE,\n");
  627.   printf("            then exits (all code tables will be displayed if CODE is not\n");
  628.   printf("            specified, or if CODE is invalid)\n");
  629.   printf("  -v        Verbose mode -- displays information such as automatically\n");
  630.   printf("            generated file names, detected input code, number of escape\n");
  631.   printf("            characters restored/removed, etc.\n\n");
  632.   printf("NOTE: CODE has five possible values (and default outfile extensions):\n");
  633.   printf("      \"e\" = EUC (.euc); \"s\" = Shift-JIS (.sjs); \"j\" = New-JIS (.new);\n");
  634.   printf("      \"o\" = Old-JIS (.old); and \"n\" = NEC-JIS (.nec)\n\n");
  635.   exit(0);
  636. }
  637.  
  638. int SkipESCSeq(FILE *in,int temp,int *intwobyte)
  639. {
  640.   int tempdata = *intwobyte;
  641.  
  642.   if (temp == '$' || temp == '(')
  643.     fgetc(in);
  644.   if (temp == 'K' || temp == '$')
  645.     *intwobyte = TRUE;
  646.   else
  647.     *intwobyte = FALSE;
  648.   if (tempdata == *intwobyte)
  649.     return FALSE;
  650.   else
  651.     return TRUE;
  652. }
  653.  
  654. void removeescape(FILE *in,FILE *out,int verbose,int forcedelesc)
  655. {
  656.   int p1,p2,p3;
  657.   unsigned long count = 0,other = 0;
  658.  
  659.   while ((p1 = fgetc(in)) != EOF) {
  660.     if (p1 == ESC) {
  661.       p2 = fgetc(in);
  662.       if (p2 == '(') {
  663.         p3 = fgetc(in);
  664.         switch (p3) {
  665.           case 'J' :
  666.           case 'B' :
  667.           case 'H' :
  668.             fprintf(out,"%c%c",p2,p3);
  669.             count++;
  670.             break;
  671.           default :
  672.             if (forcedelesc)
  673.               fprintf(out,"%c%c",p2,p3);
  674.             else
  675.               fprintf(out,"%c%c%c",p1,p2,p3);
  676.             other++;
  677.             break;
  678.         }
  679.       }
  680.       else if (p2 == '$') {
  681.         p3 = fgetc(in);
  682.         switch (p3) {
  683.           case 'B' :
  684.           case '@' :
  685.             fprintf(out,"%c%c",p2,p3);
  686.             count++;
  687.             break;
  688.           default :
  689.             if (forcedelesc)
  690.               fprintf(out,"%c%c",p2,p3);
  691.             else
  692.               fprintf(out,"%c%c%c",p1,p2,p3);
  693.             other++;
  694.             break;
  695.         }
  696.       }
  697.       else {
  698.         if (forcedelesc)
  699.           fprintf(out,"%c",p2);
  700.         else
  701.           fprintf(out,"%c%c",p1,p2);
  702.         other++;
  703.       }
  704.     }
  705.     else
  706.       fprintf(out,"%c",p1);
  707.   }
  708.   if (verbose) {
  709.     fprintf(stderr,"Number of valid escape characters removed: %lu\n",count);
  710.     if (forcedelesc)
  711.       fprintf(stderr,"Number of other escape characters forced removed: %lu\n",other);
  712.     else
  713.       fprintf(stderr,"Number of other escape characters detected: %lu\n",other);
  714.   }
  715. }
  716.  
  717. void jisrepair(FILE *in,FILE *out,int verbose,int outcode,char ki[],char ko[])
  718. {
  719.   int p1,p2,p3,intwobyte = FALSE;
  720.   unsigned long count = 0;
  721.  
  722.   while ((p1 = fgetc(in)) != EOF) {
  723.     if (intwobyte) {
  724.       if (p1 == ESC) {
  725.         p2 = fgetc(in);
  726.         if (p2 == '(') {
  727.           p3 = fgetc(in);
  728.           switch (p3) {
  729.             case 'J' :
  730.             case 'B' :
  731.             case 'H' :
  732.               intwobyte = FALSE;
  733.               switch (outcode) {
  734.                 case NEC :
  735.                 case NEW :
  736.                 case OLD :
  737.                   fprintf(out,"%c%s",ESC,ko);
  738.                   break;
  739.                 default :
  740.                   break;
  741.               }
  742.               break;
  743.             default :
  744.               fprintf(out,"%c%c%c",p1,p2,p3);
  745.               break;
  746.           }
  747.         }
  748.         else if (p2 == 'H') {
  749.           intwobyte = FALSE;
  750.           switch (outcode) {
  751.             case NEC :
  752.             case NEW :
  753.             case OLD :
  754.               fprintf(out,"%c%s",ESC,ko);
  755.               break;
  756.             default :
  757.               break;
  758.           }
  759.         }
  760.         else
  761.           fprintf(out,"%c%c",p1,p2);
  762.       }
  763.       else if (p1 == '(') {
  764.         p2 = fgetc(in);
  765.         switch (p2) {
  766.           case 'J' :
  767.           case 'B' :
  768.           case 'H' :
  769.             intwobyte = FALSE;
  770.             switch (outcode) {
  771.               case NEC :
  772.               case NEW :
  773.               case OLD :
  774.                 fprintf(out,"%c%s",ESC,ko);
  775.                 break;
  776.               default :
  777.                 break;
  778.             }
  779.             count++;
  780.             break;
  781.           default :
  782.             switch (outcode) {
  783.               case NEC :
  784.               case NEW :
  785.               case OLD :
  786.                 fprintf(out,"%c%c",p1,p2);
  787.                 break;
  788.               case EUC :
  789.                 p1 += 128;
  790.                 p2 += 128;
  791.                 fprintf(out,"%c%c",p1,p2);
  792.                 break;
  793.               case SJIS :
  794.                 jis2sjis(&p1,&p2);
  795.                 fprintf(out,"%c%c",p1,p2);
  796.                 break;
  797.             }
  798.             break;
  799.         }
  800.       }
  801.       else if (p1 == NL) {
  802.         switch (outcode) {
  803.           case NEC :
  804.           case NEW :
  805.           case OLD :
  806.             fprintf(out,"%c%s%c",ESC,ko,p1);
  807.             break;
  808.           default :
  809.             fprintf(out,"%c",p1);
  810.             break;
  811.         }
  812.         count++;
  813.         intwobyte = FALSE;
  814.       }
  815.       else {
  816.         p2 = fgetc(in);
  817.         switch (outcode) {
  818.           case NEC :
  819.           case NEW :
  820.           case OLD :
  821.             fprintf(out,"%c%c",p1,p2);
  822.             break;
  823.           case EUC :
  824.             p1 += 128;
  825.             p2 += 128;
  826.             fprintf(out,"%c%c",p1,p2);
  827.             break;
  828.           case SJIS :
  829.             jis2sjis(&p1,&p2);
  830.             fprintf(out,"%c%c",p1,p2);
  831.             break;
  832.         }
  833.       }
  834.     }
  835.     else {
  836.       if (p1 == ESC) {
  837.         p2 = fgetc(in);
  838.         if (p2 == '$') {
  839.           p3 = fgetc(in);
  840.           switch (p3) {
  841.             case 'B' :
  842.             case '@' :
  843.               intwobyte = TRUE;
  844.               switch (outcode) {
  845.                 case NEC :
  846.                 case NEW :
  847.                 case OLD :
  848.                   fprintf(out,"%c%s",ESC,ki);
  849.                   break;
  850.                 default :
  851.                   break;
  852.               }
  853.               break;
  854.             default :
  855.               fprintf(out,"%c%c%c",p1,p2,p3);
  856.               break;
  857.           }
  858.         }
  859.         else if (p2 == 'K') {
  860.           intwobyte = TRUE;
  861.           switch (outcode) {
  862.             case NEC :
  863.             case NEW :
  864.             case OLD :
  865.               fprintf(out,"%c%s",ESC,ki);
  866.               break;
  867.             default :
  868.               break;
  869.           }
  870.         }
  871.         else
  872.           fprintf(out,"%c%c",p1,p2);
  873.       }
  874.       else if (p1 == '$') {
  875.         p2 = fgetc(in);
  876.         switch (p2) {
  877.           case 'B' :
  878.           case '@' :
  879.             intwobyte = TRUE;
  880.             switch (outcode) {
  881.               case NEC :
  882.               case NEW :
  883.               case OLD :
  884.                 fprintf(out,"%c%s",ESC,ki);
  885.                 break;
  886.               default :
  887.                 break;
  888.             }
  889.             count++;
  890.             break;
  891.           default :
  892.             switch (outcode) {
  893.               case NEC :
  894.               case NEW :
  895.               case OLD :
  896.                 fprintf(out,"%c%c",p1,p2);
  897.                 break;
  898.               case EUC :
  899.                 fprintf(out,"%c%c",p1,p2);
  900.                 break;
  901.               case SJIS :
  902.                 fprintf(out,"%c%c",p1,p2);
  903.                 break;
  904.             }
  905.             break;
  906.         }
  907.       }
  908.       else
  909.         fprintf(out,"%c",p1);
  910.     }
  911.   }
  912.   if (intwobyte) {
  913.     switch (outcode) {
  914.       case NEC :
  915.       case NEW :
  916.       case OLD :
  917.         fprintf(out,"%c%s",ESC,ko);
  918.         count++;
  919.         break;
  920.       default :
  921.         break;
  922.     }
  923.   }
  924.   if (verbose)
  925.     fprintf(stderr,"Number of escape characters restored: %lu\n",count);
  926. }
  927.  
  928. void sjis2jis(int *p1, int *p2)
  929. {
  930.   unsigned char c1 = *p1;
  931.   unsigned char c2 = *p2;
  932.   int adjust = c2 < 159;
  933.   int rowOffset = c1 < 160 ? 112 : 176;
  934.   int cellOffset = adjust ? (c2 > 127 ? 32 : 31) : 126;
  935.  
  936.   *p1 = ((c1 - rowOffset) << 1) - adjust;
  937.   *p2 -= cellOffset;
  938. }
  939.  
  940. void jis2sjis(int *p1, int *p2)
  941. {
  942.   unsigned char c1 = *p1;
  943.   unsigned char c2 = *p2;
  944.   int rowOffset = c1 < 95 ? 112 : 176;
  945.   int cellOffset = c1 % 2 ? (c2 > 95 ? 32 : 31) : 126;
  946.  
  947.   *p1 = ((c1 + 1) >> 1) + rowOffset;
  948.   *p2 += cellOffset;
  949. }
  950.  
  951. void shift2seven(FILE *in,FILE *out,int incode,char ki[],char ko[])
  952. {
  953.   int p1,p2,intwobyte = FALSE;
  954.  
  955.   while ((p1 = fgetc(in)) != EOF) {
  956.     if (p1 == NL || p1 == CR) {
  957.       if (intwobyte) {
  958.         intwobyte = FALSE;
  959.         fprintf(out,"%c%s",ESC,ko);
  960.       }
  961.       fprintf(out,"%c",p1);
  962.     }
  963.     else if (SJIS1(p1)) {
  964.       p2 = fgetc(in);
  965.       if (SJIS2(p2)) {
  966.         sjis2jis(&p1,&p2);
  967.         if (!intwobyte) {
  968.           intwobyte = TRUE;
  969.           fprintf(out,"%c%s",ESC,ki);
  970.         }
  971.       }
  972.       fprintf(out,"%c%c",p1,p2);
  973.     }
  974.     else if (HANKATA(p1)) {
  975.       han2zen(in,&p1,&p2,incode);
  976.       sjis2jis(&p1,&p2);
  977.       if (!intwobyte) {
  978.         intwobyte = TRUE;
  979.         fprintf(out,"%c%s",ESC,ki);
  980.       }
  981.       fprintf(out,"%c%c",p1,p2);
  982.     }
  983.     else {
  984.       if (intwobyte) {
  985.         intwobyte = FALSE;
  986.         fprintf(out,"%c%s",ESC,ko);
  987.       }
  988.       fprintf(out,"%c",p1);
  989.     }
  990.   }
  991.   if (intwobyte)
  992.     fprintf(out,"%c%s",ESC,ko);
  993. }
  994.  
  995. void shift2euc(FILE *in,FILE *out,int incode,int tofullsize)
  996. {
  997.   int p1,p2;
  998.   
  999.   while ((p1 = fgetc(in)) != EOF) {
  1000.     if (SJIS1(p1)) {
  1001.       p2 = fgetc(in);
  1002.       if (SJIS2(p2)) {
  1003.         sjis2jis(&p1,&p2);
  1004.         p1 += 128;
  1005.         p2 += 128;
  1006.       }
  1007.       fprintf(out,"%c%c",p1,p2);
  1008.     }
  1009.     else if (HANKATA(p1)) {
  1010.       if (tofullsize) {
  1011.         han2zen(in,&p1,&p2,incode);
  1012.         sjis2jis(&p1,&p2);
  1013.         p1 += 128;
  1014.         p2 += 128;
  1015.       }
  1016.       else {
  1017.         p2 = p1;
  1018.         p1 = SS2;
  1019.       }
  1020.       fprintf(out,"%c%c",p1,p2);
  1021.     }
  1022.     else
  1023.       fprintf(out,"%c",p1);
  1024.   }
  1025. }
  1026.  
  1027. void euc2seven(FILE *in,FILE *out,int incode,char ki[],char ko[])
  1028. {
  1029.   int p1,p2,intwobyte = FALSE;
  1030.  
  1031.   while ((p1 = fgetc(in)) != EOF) {
  1032.     if (p1 == NL || p1 == CR) {
  1033.       if (intwobyte) {
  1034.         intwobyte = FALSE;
  1035.         fprintf(out,"%c%s",ESC,ko);
  1036.       }
  1037.       fprintf(out,"%c",p1);
  1038.     }
  1039.     else {
  1040.       if (ISEUC(p1)) {
  1041.         p2 = fgetc(in);
  1042.         if (ISEUC(p2)) {
  1043.           p1 -= 128;
  1044.           p2 -= 128;
  1045.           if (!intwobyte) {
  1046.             intwobyte = TRUE;
  1047.             fprintf(out,"%c%s",ESC,ki);
  1048.           }
  1049.         }
  1050.         fprintf(out,"%c%c",p1,p2);
  1051.       }
  1052.       else if (p1 == SS2) {
  1053.         p2 = fgetc(in);
  1054.         if (HANKATA(p2)) {
  1055.           p1 = p2;
  1056.           han2zen(in,&p1,&p2,incode);
  1057.           sjis2jis(&p1,&p2);
  1058.           if (!intwobyte) {
  1059.             intwobyte = TRUE;
  1060.             fprintf(out,"%c%s",ESC,ki);
  1061.           }
  1062.         }
  1063.         fprintf(out,"%c%c",p1,p2);
  1064.       }
  1065.       else {
  1066.         if (intwobyte) {
  1067.           intwobyte = FALSE;
  1068.           fprintf(out,"%c%s",ESC,ko);
  1069.         }
  1070.         fprintf(out,"%c",p1);
  1071.       }
  1072.     }
  1073.   }
  1074.   if (intwobyte)
  1075.     fprintf(out,"%c%s",ESC,ko);
  1076. }
  1077.  
  1078. void euc2shift(FILE *in,FILE *out,int incode,int tofullsize)
  1079. {
  1080.   int p1,p2;
  1081.  
  1082.   while ((p1 = fgetc(in)) != EOF) {
  1083.     if (ISEUC(p1)) {
  1084.       p2 = fgetc(in);
  1085.       if (ISEUC(p2)) {
  1086.         p1 -= 128;
  1087.         p2 -= 128;
  1088.         jis2sjis(&p1,&p2);
  1089.       }
  1090.       fprintf(out,"%c%c",p1,p2);
  1091.     }
  1092.     else if (p1 == SS2) {
  1093.       p2 = fgetc(in);
  1094.       if (HANKATA(p2)) {
  1095.         p1 = p2;
  1096.         if (tofullsize) {
  1097.           han2zen(in,&p1,&p2,incode);
  1098.           fprintf(out,"%c%c",p1,p2);
  1099.         }
  1100.         else {
  1101.           fprintf(out,"%c",p1);
  1102.         }
  1103.       }
  1104.       else
  1105.         fprintf(out,"%c%c",p1,p2);
  1106.     }
  1107.     else
  1108.       fprintf(out,"%c",p1);
  1109.   }
  1110. }
  1111.  
  1112. void euc2euc(FILE *in,FILE *out,int incode,int tofullsize)
  1113. {
  1114.   int p1,p2;
  1115.  
  1116.   while ((p1 = fgetc(in)) != EOF) {
  1117.     if (ISEUC(p1)) {
  1118.       p2 = fgetc(in);
  1119.       if (ISEUC(p2))
  1120.         fprintf(out,"%c%c",p1,p2);
  1121.     }
  1122.     else if (p1 == SS2) {
  1123.       p2 = fgetc(in);
  1124.       if (HANKATA(p2) && tofullsize) {
  1125.         p1 = p2;
  1126.         han2zen(in,&p1,&p2,incode);
  1127.         sjis2jis(&p1,&p2);
  1128.         p1 += 128;
  1129.         p2 += 128;
  1130.       }
  1131.       fprintf(out,"%c%c",p1,p2);
  1132.     }
  1133.     else
  1134.       fprintf(out,"%c",p1);
  1135.   }
  1136. }
  1137.  
  1138. void shift2shift(FILE *in,FILE *out,int incode,int tofullsize)
  1139. {
  1140.   int p1,p2;
  1141.   
  1142.   while ((p1 = fgetc(in)) != EOF) {
  1143.     if (SJIS1(p1)) {
  1144.       p2 = fgetc(in);
  1145.       if (SJIS2(p2))
  1146.         fprintf(out,"%c%c",p1,p2);
  1147.     }
  1148.     else if (HANKATA(p1) && tofullsize) {
  1149.       han2zen(in,&p1,&p2,incode);
  1150.       fprintf(out,"%c%c",p1,p2);
  1151.     }
  1152.     else
  1153.       fprintf(out,"%c",p1);
  1154.   }
  1155. }
  1156.  
  1157. void seven2shift(FILE *in,FILE *out)
  1158. {
  1159.   int temp,p1,p2,intwobyte = FALSE;
  1160.  
  1161.   while ((p1 = fgetc(in)) != EOF) {
  1162.     if (p1 == ESC) {
  1163.       temp = fgetc(in);
  1164.       SkipESCSeq(in,temp,&intwobyte);
  1165.     }
  1166.     else if (p1 == NL || p1 == CR) {
  1167.       if (intwobyte)
  1168.         intwobyte = FALSE;
  1169.       fprintf(out,"%c",p1);
  1170.     }
  1171.     else {
  1172.       if (intwobyte) {
  1173.         p2 = fgetc(in);
  1174.         jis2sjis(&p1,&p2);
  1175.         fprintf(out,"%c%c",p1,p2);
  1176.       }
  1177.       else
  1178.         fprintf(out,"%c",p1);
  1179.     }
  1180.   }
  1181. }
  1182.   
  1183. void seven2euc(FILE *in,FILE *out)
  1184. {
  1185.   int temp,p1,p2,intwobyte = FALSE;
  1186.  
  1187.   while ((p1 = fgetc(in)) != EOF) {
  1188.     if (p1 == ESC) {
  1189.       temp = fgetc(in);
  1190.       SkipESCSeq(in,temp,&intwobyte);
  1191.     }
  1192.     else if (p1 == NL || p1 == CR) {
  1193.       if (intwobyte)
  1194.         intwobyte = FALSE;
  1195.       fprintf(out,"%c",p1);
  1196.     }
  1197.     else {
  1198.       if (intwobyte) {
  1199.         p2 = fgetc(in);
  1200.         p1 += 128;
  1201.         p2 += 128;
  1202.         fprintf(out,"%c%c",p1,p2);
  1203.       }
  1204.       else
  1205.         fprintf(out,"%c",p1);
  1206.     }
  1207.   }
  1208. }
  1209.  
  1210. void seven2seven(FILE *in,FILE *out,char ki[],char ko[])
  1211. {
  1212.   int temp,p1,p2,change,intwobyte = FALSE;
  1213.  
  1214.   while ((p1 = fgetc(in)) != EOF) {
  1215.     if (p1 == ESC) {
  1216.       temp = fgetc(in);
  1217.       change = SkipESCSeq(in,temp,&intwobyte);
  1218.       if ((intwobyte) && (change))
  1219.         fprintf(out,"%c%s",ESC,ki);
  1220.       else if (change)
  1221.         fprintf(out,"%c%s",ESC,ko);
  1222.     }
  1223.     else if (p1 == NL || p1 == CR) {
  1224.       if (intwobyte) {
  1225.         intwobyte = FALSE;
  1226.         fprintf(out,"%c%s",ESC,ko);
  1227.       }
  1228.       fprintf(out,"%c",p1);
  1229.     }
  1230.     else {
  1231.       if (intwobyte) {
  1232.         p2 = fgetc(in);
  1233.         fprintf(out,"%c%c",p1,p2);
  1234.       }
  1235.       else
  1236.         fprintf(out,"%c",p1);
  1237.     }
  1238.   }
  1239.   if (intwobyte)
  1240.     fprintf(out,"%c%s",ESC,ko);
  1241. }
  1242.  
  1243. int DetectCodeType(FILE *in)
  1244. {
  1245.   int c = 0,whatcode = ASCII;
  1246.  
  1247.   while ((whatcode == EUCORSJIS || whatcode == ASCII) && c != EOF) {
  1248.     if ((c = fgetc(in)) != EOF) {
  1249.       if (c == ESC) {
  1250.         c = fgetc(in);
  1251.         if (c == '$') {
  1252.           c = fgetc(in);
  1253.           if (c == 'B')
  1254.             whatcode = NEW;
  1255.           else if (c == '@')
  1256.             whatcode = OLD;
  1257.         }
  1258.         else if (c == 'K')
  1259.           whatcode = NEC;
  1260.       }
  1261.       else if ((c >= 129 && c <= 141) || (c >= 143 && c <= 159))
  1262.         whatcode = SJIS;
  1263.       else if (c == SS2) {
  1264.         c = fgetc(in);
  1265.         if ((c >= 64 && c <= 126) || (c >= 128 && c <= 160) || (c >= 224 && c <= 252))
  1266.           whatcode = SJIS;
  1267.         else if (c >= 161 && c <= 223)
  1268.           whatcode = EUCORSJIS;
  1269.       }
  1270.       else if (c >= 161 && c <= 223) {
  1271.         c = fgetc(in);
  1272.         if (c >= 240 && c <= 254)
  1273.           whatcode = EUC;
  1274.         else if (c >= 161 && c <= 223)
  1275.           whatcode = EUCORSJIS;
  1276.         else if (c >= 224 && c <= 239) {
  1277.           whatcode = EUCORSJIS;
  1278.           while (c >= 64 && c != EOF && whatcode == EUCORSJIS) {
  1279.             if (c >= 129) {
  1280.               if (c <= 141 || (c >= 143 && c <= 159))
  1281.                 whatcode = SJIS;
  1282.               else if (c >= 253 && c <= 254)
  1283.                 whatcode = EUC;
  1284.             }
  1285.             c = fgetc(in);
  1286.           }
  1287.         }
  1288.         else if (c <= 159)
  1289.           whatcode = SJIS;
  1290.       }
  1291.       else if (c >= 240 && c <= 254)
  1292.         whatcode = EUC;
  1293.       else if (c >= 224 && c <= 239) {
  1294.         c = fgetc(in);
  1295.         if ((c >= 64 && c <= 126) || (c >= 128 && c <= 160))
  1296.           whatcode = SJIS;
  1297.         else if (c >= 253 && c <= 254)
  1298.           whatcode = EUC;
  1299.         else if (c >= 161 && c <= 252)
  1300.           whatcode = EUCORSJIS;
  1301.       }
  1302.     }
  1303.   }
  1304.   return whatcode;
  1305. }
  1306.  
  1307. void han2zen(FILE *in,int *p1,int *p2,int incode)
  1308. {
  1309.   int tmp = *p1,junk,maru = FALSE,nigori = FALSE;
  1310.   int mtable[][2] = {
  1311.     {129,66},{129,117},{129,118},{129,65},{129,69},{131,146},{131,64},
  1312.     {131,66},{131,68},{131,70},{131,72},{131,131},{131,133},{131,135},
  1313.     {131,98},{129,91},{131,65},{131,67},{131,69},{131,71},{131,73},
  1314.     {131,74},{131,76},{131,78},{131,80},{131,82},{131,84},{131,86},
  1315.     {131,88},{131,90},{131,92},{131,94},{131,96},{131,99},{131,101},
  1316.     {131,103},{131,105},{131,106},{131,107},{131,108},{131,109},
  1317.     {131,110},{131,113},{131,116},{131,119},{131,122},{131,125},
  1318.     {131,126},{131,128},{131,129},{131,130},{131,132},{131,134},
  1319.     {131,136},{131,137},{131,138},{131,139},{131,140},{131,141},
  1320.     {131,143},{131,147},{129,74},{129,75}
  1321.   };
  1322.  
  1323.   if (incode == SJIS) {
  1324.     *p2 = fgetc(in);
  1325.     if (*p2 == 222) {
  1326.       if (ISNIGORI(*p1))
  1327.         nigori = TRUE;
  1328.       else
  1329.         ungetc(*p2,in);
  1330.     }
  1331.     else if (*p2 == 223) {
  1332.       if (ISMARU(*p1))
  1333.         maru = TRUE;
  1334.       else
  1335.         ungetc(*p2,in);
  1336.     }
  1337.     else
  1338.       ungetc(*p2,in);
  1339.   }
  1340.   else if (incode == EUC) {
  1341.     junk = fgetc(in);
  1342.     if (junk == SS2) {
  1343.       *p2 = fgetc(in);
  1344.       if (*p2 == 222) {
  1345.         if (ISNIGORI(*p1))
  1346.           nigori = TRUE;
  1347.         else {
  1348.           ungetc(*p2,in);
  1349.           ungetc(junk,in);
  1350.         }
  1351.       }
  1352.       else if (*p2 == 223) {
  1353.         if (ISMARU(*p1))
  1354.           maru = TRUE;
  1355.         else {
  1356.           ungetc(*p2,in);
  1357.           ungetc(junk,in);
  1358.         }
  1359.       }
  1360.       else {
  1361.         ungetc(*p2,in);
  1362.         ungetc(junk,in);
  1363.       }
  1364.     }
  1365.     else
  1366.       ungetc(junk,in);
  1367.   }
  1368.   if (*p1 >= 161 && *p1 <= 223) {
  1369.     *p1 = mtable[tmp - 161][0];
  1370.     *p2 = mtable[tmp - 161][1];
  1371.   }
  1372.   if (nigori) {
  1373.     if ((*p2 >= 74 && *p2 <= 103) || (*p2 >= 110 && *p2 <= 122))
  1374.       (*p2)++;
  1375.     else if (*p1 == 131 && *p2 == 69)
  1376.       *p2 = 148;
  1377.   }
  1378.   else if (maru && *p2 >= 110 && *p2 <= 122)
  1379.     *p2 += 2;
  1380. }
  1381.