Shift-JIS JISの相互変換

/* * Japanese Detection and Converstion Functions */ #define HANKATA(A) ( (A >= 0xA1) && (A <= 0xDF)) // 半角カタカナ #define ISEUC(A) ( (A >= 0xA1) && (A <= 0xFE)) #define NOTEUC(A,B) (((A >= 0x81) && (A <= 0x9F)) && ((B >= 0x40) && (B <= 0xA0))) #define SJIS1(A) (((A >= 0x81) && (A <= 0x9F)) || ((A >= 0xE0) && (A <= 0xEF))) #define SJIS2(A) ( (A >= 0x40) && (A <= 0xFC)) #define ISMARU(A) ( (A >= 0xCA) && (A <= 0xCE)) // 半角パピプぺポ #define ISNIGORI(A) (((A >= 0xB6) && (A <= 0xC4)) || ((A >= 0xCA) && (A <= 0xCE))) //濁り ガ-ゴ ザ-ゾ ダ-ド バ-ボ static inline void jis2sjis(uchar *p1, uchar *p2) { uchar c1 = *p1; uchar c2 = *p2; int row = c1 < 0x5F ? 0x70 : 0xB0; int cell = c1 % 2 ? 0x1F + (c2 > 0x5F) : 0x7E; *p1 = (uchar)(((c1 + 1) >> 1) + row); *p2 = (uchar)(c2 + cell); } int JISToSJIS(const char *input, char *output) { int inpos, outpos, shifted = FALSE; uchar p1, p2; for (inpos = outpos = 0; input[inpos]; inpos++) { p1 = input[inpos]; if (p1 == ESC) { if ((p2 = input[++inpos]) == 0) { return NG; } if (p2 == '$' || p2 == '(') { inpos++; } shifted = (p2 == 'K' || p2 =='$') ? TRUE : FALSE; } else { if (shifted) { if ((p2 = input[++inpos]) == 0) { return NG; } jis2sjis(&p1, &p2); output[outpos++] = p1; output[outpos++] = p2; } else { output[outpos++] = p1; } } } output[outpos] = 0x00; return outpos; } static inline void sjis2jis(uchar *p1, uchar *p2) { uchar c1 = *p1; uchar c2 = *p2; int shift = c2 < - 0x9F; int row = c1 < 0xA0 ? 0x70 : 0xB0; int cell = shift ? (0x1F + (c2 > 0x7F)): 0x7E; *p1 = (uchar)(((c1 - row) << 1) - shift); *p2 -= cell; } static int han2zen(uchar *p1, uchar *p2) { static const uchar char1[] = { // 全角カナ 1st byte 0x81,0x81,0x81,0x81,0x81, // 。°′、・ 0x83,0x83,0x83,0x83,0x83,0x83, // ヲァィゥェォ 0x83,0x83,0x83,0x83,0x81, // ャュョッー 0x83,0x83,0x83,0x83,0x83, // アイウエオ 0x83,0x83,0x83,0x83,0x83, // カキクケコ 0x83,0x83,0x83,0x83,0x83, // サシスセソ 0x83,0x83,0x83,0x83,0x83, // タチツテト 0x83,0x83,0x83,0x83,0x83, // ナニヌネノ 0x83,0x83,0x83,0x83,0x83, // ハヒフヘホ 0x83,0x83,0x83,0x83,0x83, // マミムメモ 0x83,0x83,0x83, // ヤユヨ 0x83,0x83,0x83,0x83,0x83, // ラリルレロ 0x83,0x83,0x81,0x81 }; // ワンカガ static const uchar char2[] = { // 全角カナ 2nd byte 0x42,0x75,0x76,0x41,0x45, // 。°′、・ 0x92,0x40,0x42,0x44,0x46, 0x48, // ヲァィゥェォ 0x83,0x85,0x87,0x62,0x5B, // ャュョッー 0x41,0x43,0x45,0x47,0x49, // アイウエオ 0x4A,0x4C,0x4E,0x50,0x52, // カキクケコ 0x54,0x56,0x58,0x5A,0x5C, // サシスセソ 0x5E,0x60,0x63,0x65,0x67, // タチツテト 0x69,0x6A,0x6B,0x6C,0x6D, // ナニヌネノ 0x6E,0x71,0x74,0x77,0x7A, // ハヒフヘホ 0x7D,0x7E,0x80,0x81,0x82, // マミムメモ 0x84,0x86,0x88, // ヤユヨ 0x89,0x8A,0x8B,0x8C,0x8D, // ラリルレロ 0x8F,0x93,0x4A,0x4B }; // ワンカガ int maru = FALSE, nigori = FALSE; if ((*p2 == 0xDE) && ((ISNIGORI(*p1) || (*p1 == 0xB3)))) { nigori = TRUE; } if ((*p2 == 0xDF) && (ISMARU(*p1))) { maru = TRUE; } if (maru == FALSE && nigori == FALSE) { return 0; } if (*p1 >= 0xA1 && *p1 <= 0xDF) { uchar index = *p1 - 0xA1; *p1 = char1[index]; *p2 = char2[index]; } if (nigori) { // カ ト ハ ホ if (((*p2 >= 0x4A) && (*p2 <= 0x67)) || ((*p2 >= 0x6E) && (*p2 <= 0x7A))){ (*p2)++; } else if ((*p1 == 0x83) && (*p2 == 0x45)) { // ゥ゙→ヴ *p2 = 0x94; } } else if (maru && (*p2 >= 0x6E) && (*p2 <= 0x7A)) { // ハ→パ *p2 += 2; } return 1; } int SJISToJIS(const char *input, char *output) { int inpos, outpos, shifted = FALSE; uchar p1, p2; for (inpos = outpos = 0; input[inpos]; inpos++) { p1 = input[inpos] & 0xff; if (p1 == LF || p1 == CR) { if (shifted) { shifted = FALSE; output[outpos++] = ESC; output[outpos++] = '('; output[outpos++] = 'B'; } output[outpos++] = p1; continue; } if (SJIS1(p1)) { if ((p2 = input[++inpos]) == 0) { return NG; } if (SJIS2(p2)) { sjis2jis(&p1,&p2); if (!shifted) { shifted = TRUE; output[outpos++] = ESC; output[outpos++] = '$'; output[outpos++] = 'B'; } } output[outpos++]=p1; output[outpos++]=p2; } else { if (HANKATA(p1)) { if ((p2 = input[inpos + 1]) == 0) { return NG; } inpos+=han2zen(&p1,&p2); sjis2jis(&p1,&p2); if (!shifted) { shifted = TRUE; output[outpos++] = ESC; output[outpos++] = '$'; output[outpos++] = 'B'; } output[outpos++]=p1; output[outpos++]=p2; } else { if (shifted) { shifted = FALSE; output[outpos++] = ESC; output[outpos++] = '('; output[outpos++] = 'B'; } output[outpos++]=p1; } } } if (shifted) { output[outpos++] = ESC; output[outpos++] = '('; output[outpos++] = 'B'; } output[outpos] = 0x00; return outpos; }

你可能感兴趣的:(c,input,byte,output,X86)