| 1 |
/*********************************************************************** |
| 2 |
* |
| 3 |
* file: translate.c |
| 4 |
* |
| 5 |
* キーバイト列を、 CP932 に従って可視文字列に変換する |
| 6 |
* (たしか)鳥屋クライアントで使ってるものと同一。 |
| 7 |
* |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "cp932.h" |
| 13 |
#include "translate.h" |
| 14 |
|
| 15 |
/*************************************************************** |
| 16 |
* |
| 17 |
* 可視文字列に変換 |
| 18 |
* n = 0, flag !=0 で呼ぶこと |
| 19 |
* |
| 20 |
* ptr で指される文字列は、変換に成功したら書き換えられる |
| 21 |
* |
| 22 |
* 成功したら flg, 失敗したら 0 を返す。 |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
unsigned |
| 27 |
translate(unsigned char *ptr, |
| 28 |
int n, |
| 29 |
unsigned flag) |
| 30 |
{ |
| 31 |
int r; |
| 32 |
unsigned char buf[32]; |
| 33 |
unsigned s0 = (n == 1 || n == 2 ? 0x00 : 0x80); |
| 34 |
unsigned s1 = (n == 0 || n == 1 ? 0x00 : 0x80); |
| 35 |
unsigned c0 = ptr[n] << 8; |
| 36 |
unsigned c1 = ptr[n + 1]; |
| 37 |
unsigned cs0 = c0 ^ (s0 << 8); |
| 38 |
unsigned cs1 = c1 ^ s1; |
| 39 |
|
| 40 |
if (n >= 8) |
| 41 |
return flag; |
| 42 |
|
| 43 |
if (n == 7) |
| 44 |
{ |
| 45 |
int i; |
| 46 |
/* 最後の1文字 */ |
| 47 |
if (!(ptr[7] & 0x7F)) |
| 48 |
return flag; |
| 49 |
|
| 50 |
/* 半角空白の場合、錯誤予防のため |
| 51 |
もう1文字足してあげる */ |
| 52 |
if (ptr[7] == 0x20) |
| 53 |
{ |
| 54 |
if ((0x21 <= ptr[6] && ptr[6] <= 0x7E) |
| 55 |
|| (0xA1 <= ptr[6] && ptr[6] <= 0xDF)) |
| 56 |
ptr[8] = ptr[6]; |
| 57 |
else |
| 58 |
ptr[8] = '?'; /* てきとうな1文字 */ |
| 59 |
return flag; |
| 60 |
} |
| 61 |
|
| 62 |
for (i = 0x00; i <= 0x7E; i++) |
| 63 |
{ |
| 64 |
if (cp932[c0 | i] & KCLS_K1) |
| 65 |
{ |
| 66 |
ptr[8] = i; |
| 67 |
return flag; |
| 68 |
} |
| 69 |
if (cp932[c0 | (0x80 + i)] & KCLS_K1) |
| 70 |
{ |
| 71 |
ptr[8] = 0x80 + i; |
| 72 |
return flag; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
ptr[7] ^= 0x80; c0 = ptr[7] << 8; |
| 77 |
for (i = 0x00; i <= 0x7E; i++) |
| 78 |
{ |
| 79 |
if (cp932[c0 | i] & KCLS_K1) |
| 80 |
{ |
| 81 |
ptr[8] = i; |
| 82 |
return flag; |
| 83 |
} |
| 84 |
if (cp932[c0 | (0x80 + i)] & KCLS_K1) |
| 85 |
{ |
| 86 |
ptr[8] = 0x80 + i; |
| 87 |
return flag; |
| 88 |
} |
| 89 |
} |
| 90 |
ptr[7] ^= 0x80; c0 = ptr[7] << 8; |
| 91 |
} |
| 92 |
else if ((ptr[n] & 0x7F) == 0x00 |
| 93 |
&& (ptr[n + 1] & 0x7F) != 0x00) |
| 94 |
{ |
| 95 |
/* BSD鯖では失敗 */ |
| 96 |
return 0; |
| 97 |
} |
| 98 |
else if (n == 6 |
| 99 |
&& (ptr[6] | 0x80) == 0x81 |
| 100 |
&& (ptr[7] & 0x7F) == 0x40) |
| 101 |
{ |
| 102 |
/* 末尾が0x8140(全角空白)になりそうなときは |
| 103 |
てきとうな1文字をケツに足しといてあげる */ |
| 104 |
ptr[6] = 0x81; |
| 105 |
ptr[7] = 0x40; |
| 106 |
ptr[8] = 0x40; /* @ */ |
| 107 |
return flag; |
| 108 |
} |
| 109 |
|
| 110 |
/* K1 */ |
| 111 |
if (cp932[c0 | c1] & KCLS_K1) |
| 112 |
{ |
| 113 |
r = translate(ptr, n + 2, flag); |
| 114 |
if (r) |
| 115 |
return r; |
| 116 |
} |
| 117 |
if (s0 |
| 118 |
&& cp932[cs0 | c1] & KCLS_K1) |
| 119 |
{ |
| 120 |
memcpy(buf, ptr, sizeof(buf)); |
| 121 |
buf[n] ^= s0; |
| 122 |
r = translate(buf, n + 2, flag); |
| 123 |
if (r) |
| 124 |
{ |
| 125 |
memcpy(ptr, buf, sizeof(buf)); |
| 126 |
return r; |
| 127 |
} |
| 128 |
} |
| 129 |
if (s1 |
| 130 |
&& cp932[c0 | cs1] & KCLS_K1) |
| 131 |
{ |
| 132 |
memcpy(buf, ptr, sizeof(buf)); |
| 133 |
buf[n + 1] ^= s1; |
| 134 |
r = translate(buf, n + 2, flag); |
| 135 |
if (r) |
| 136 |
{ |
| 137 |
memcpy(ptr, buf, sizeof(buf)); |
| 138 |
return r; |
| 139 |
} |
| 140 |
} |
| 141 |
if (s0 && s1 |
| 142 |
&& cp932[cs0 | cs1] & KCLS_K1) |
| 143 |
{ |
| 144 |
memcpy(buf, ptr, sizeof(buf)); |
| 145 |
buf[n] ^= s0; |
| 146 |
buf[n + 1] ^= s1; |
| 147 |
r = translate(buf, n + 2, flag); |
| 148 |
if (r) |
| 149 |
{ |
| 150 |
memcpy(ptr, buf, sizeof(buf)); |
| 151 |
return r; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/* AN */ |
| 156 |
if (cp932[c0] & (KCLS_AN | KCLS_KA)) |
| 157 |
{ |
| 158 |
r = translate(ptr, n + 1, flag); |
| 159 |
if (r) |
| 160 |
return r; |
| 161 |
} |
| 162 |
if (s0 && cp932[cs0] & (KCLS_AN | KCLS_KA)) |
| 163 |
{ |
| 164 |
memcpy(buf, ptr, sizeof(buf)); |
| 165 |
buf[n] ^= s0; |
| 166 |
r = translate(buf, n + 1, flag); |
| 167 |
if (r) |
| 168 |
{ |
| 169 |
memcpy(ptr, buf, sizeof(buf)); |
| 170 |
return r; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
/* KG */ |
| 175 |
/* KD */ |
| 176 |
/* AD */ |
| 177 |
|
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
* Local Variables: |
| 183 |
* tab-width: 4 |
| 184 |
* End: |
| 185 |
* |
| 186 |
* EOF */ |