Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.8 KiB

  1. /*
  2. * mkunitab - Convert JIS code to Unicode.
  3. *
  4. * TODO:
  5. *
  6. * HISTORY:
  7. *
  8. * 9/4/98 yasuho Created.
  9. */
  10. #include <stdio.h>
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include <windef.h>
  15. #include <winbase.h>
  16. #include <winnls.h>
  17. static char buf[256];
  18. static void mkunitab(char *name);
  19. static void jis2sjis(BYTE jis[], BYTE sjis[]);
  20. static void usage();
  21. static void fatal(char *s);
  22. static void error(char *s);
  23. void __cdecl main(int argc, char *argv[])
  24. {
  25. argc--, argv++;
  26. if (argc == 0)
  27. usage();
  28. while (argc--)
  29. mkunitab(*argv++);
  30. exit(0);
  31. }
  32. static void mkunitab(char *name)
  33. {
  34. BYTE jis[2];
  35. BYTE sjis[4];
  36. WORD v, n, len;
  37. INT v1, v2;
  38. WCHAR uni[2];
  39. FILE *fp;
  40. if ((fp = fopen(name, "r")) == NULL)
  41. fatal(name);
  42. while (fgets(buf, sizeof buf, fp)) {
  43. len = strlen(buf);
  44. n = 0;
  45. while (isxdigit(buf[n]))
  46. n++;
  47. if (n == 0)
  48. continue;
  49. if (n != 2 && n != 4)
  50. error("Invalid format");
  51. if (sscanf(buf, "%x %d %d", &v, &v1, &v2) != 3)
  52. error("Invalid format");
  53. // SBCS Vertical font doesn't lying
  54. if (v <= 0xFF)
  55. v2 = -v1;
  56. jis[0] = HIBYTE(v);
  57. jis[1] = LOBYTE(v);
  58. jis2sjis(jis, sjis);
  59. sjis[2] = 0;
  60. if (!MultiByteToWideChar(CP_ACP, 0, (const char *)sjis, 2,
  61. uni, sizeof uni))
  62. error("MultiByteToWideChar: fail");
  63. printf("%04x%8d%8d\n", uni[0], v1, v2);
  64. }
  65. fclose(fp);
  66. }
  67. static void jis2sjis(BYTE jis[], BYTE sjis[])
  68. {
  69. BYTE h, l;
  70. h = jis[0];
  71. l = jis[1];
  72. if (h == 0) {
  73. sjis[0] = l;
  74. sjis[1] = 0;
  75. return;
  76. }
  77. l += 0x1F;
  78. if (h & 0x01)
  79. h >>= 1;
  80. else {
  81. h >>= 1;
  82. l += 0x5E;
  83. h--;
  84. }
  85. if (l >= 0x7F)
  86. l++;
  87. if (h < 0x2F)
  88. h += 0x71;
  89. else
  90. h += 0xB1;
  91. sjis[0] = h;
  92. sjis[1] = l;
  93. }
  94. static void usage()
  95. {
  96. fprintf(stderr, "Usage: mkunitab file[...]\n");
  97. exit(1);
  98. }
  99. static void fatal(char *s)
  100. {
  101. fprintf(stderr, "mkunitab: ");
  102. perror(s);
  103. exit(1);
  104. }
  105. static void error(char *s)
  106. {
  107. fprintf(stderr, "mkunitab: %s\n", s);
  108. exit(1);
  109. }