Human68k CUI emulator with sound.
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.

sjis.c 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "sjis.h"
  4. #include "utf8.h"
  5. int sjis_strlen(uint8_t *data, int len) {
  6. int l = 0;
  7. for(int i = 0; i < len; i++) {
  8. l++;
  9. if(data[i] >= 0x80) {
  10. l++;
  11. i++;
  12. }
  13. }
  14. return l;
  15. }
  16. int sjis_to_utf8(uint8_t *sjis_data, int sjis_len, uint8_t *utf8_data, int utf8_len) {
  17. if(sjis_len < 1) return 0;
  18. if(utf8_len < 1) return -1;
  19. int j = 0, last_byte = 0;
  20. for(int i = 0; i < sjis_len; i++) {
  21. uint8_t b = sjis_data[i];
  22. if(last_byte == 0 && SJIS_FIRST_CHAR(b)) {
  23. last_byte = b;
  24. } else {
  25. char buf[4];
  26. int l = utf8_encode(sjis_char_to_unicode((last_byte << 8) | b), buf);
  27. if(j + l >= utf8_len - 1)
  28. return j;
  29. strcpy((char *)utf8_data + j, buf);
  30. j += l;
  31. last_byte = 0;
  32. }
  33. }
  34. return j;
  35. }
  36. int sjis_print_utf8(uint8_t *sjis_data, int sjis_len) {
  37. return sjis_write_utf8(stdout, sjis_data, sjis_len);
  38. }
  39. int sjis_write_utf8(FILE *f, uint8_t *sjis_data, int sjis_len) {
  40. if(sjis_len < 1) return 0;
  41. int j = 0, last_byte = 0;
  42. for(int i = 0; i < sjis_len; i++) {
  43. uint8_t b = sjis_data[i];
  44. if(last_byte == 0 && SJIS_FIRST_CHAR(b)) {
  45. last_byte = b;
  46. } else {
  47. char buf[4];
  48. int l = utf8_encode(sjis_char_to_unicode((last_byte << 8) | b), buf);
  49. if(l) fwrite(buf, l, 1, f);
  50. j += l;
  51. last_byte = 0;
  52. }
  53. }
  54. return j;
  55. }
  56. uint16_t jis_to_sjis(uint16_t val) {
  57. uint8_t j2 = val & 0xff;
  58. uint8_t j1 = val >> 8;
  59. uint8_t s1, s2;
  60. if(33 <= j1 && j1 <= 94)
  61. s1 = (j1 + 1) / 2 + 112;
  62. else
  63. s1 = (j1 + 1) / 2 + 176;
  64. if(j1 & 1)
  65. s2 = j2 + 31 + (j2 / 96);
  66. else
  67. s2 = j2 + 126;
  68. return (s1 << 8) | s2;
  69. }
  70. uint16_t jis_from_sjis(uint16_t val) {
  71. int status = 0;
  72. uint8_t j1 = 0;
  73. uint8_t j2 = 0;
  74. uint8_t b1 = val >> 8;
  75. uint8_t b2 = val & 0xff;
  76. if((b1 >= 0x81 && b1 <= 0x84) || (b1 >= 0x87 && b1 <= 0x9f)) {
  77. j1 = 2 * (b1 - 0x70) - 1;
  78. if (b2 >= 0x40 && b2 <= 0x9e) {
  79. j2 = b2 - 31;
  80. if (j2 > 95)
  81. j2 -= 1;
  82. status = 1;
  83. } else if (b2 >= 0x9f && b2 <= 0xfc) {
  84. j2 = b2 - 126;
  85. j1 += 1;
  86. status = 1;
  87. }
  88. } else if (b1 >= 0xe0 && b1 <= 0xef) {
  89. j1 = 2 * (b1 - 0xb0) - 1;
  90. if (b2 >= 0x40 && b2 <= 0x9e) {
  91. j2 = b2 - 31;
  92. if (j2 > 95)
  93. j2 -= 1;
  94. status = 1;
  95. } else if (b2 >= 0x9f && b2 <= 0xfc) {
  96. j2 = b2 - 126;
  97. j1 += 1;
  98. status = 1;
  99. }
  100. }
  101. if(status == 0) return 0;
  102. return (j1 << 8) | j2;
  103. }