1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
#ifdef USE_UNICODE
#include "wc.h"
#include "ucs.h"
#include "utf7.h"
#include "wtf.h"
#define SD WC_UTF7_MAP_SET_D
#define SO WC_UTF7_MAP_SET_O
#define SB WC_UTF7_MAP_SET_B
#define BB WC_UTF7_MAP_BASE64
#define BP WC_UTF7_MAP_PLUS
#define BM WC_UTF7_MAP_MINUS
#define CD (WC_UTF7_MAP_SET_D | WC_UTF7_MAP_C0)
#define CB (WC_UTF7_MAP_SET_B | WC_UTF7_MAP_C0)
#define C1 WC_UTF7_MAP_C1
wc_uint8 WC_UTF7_MAP[ 0x100 ] = {
/* TAB NL CR */
CB, CB, CB, CB, CB, CB, CB, CB, CB, CD, CD, CB, CB, CD, CB, CB,
/* */
CB, CB, CB, CB, CB, CB, CB, CB, CB, CB, CB, CB, CB, CB, CB, CB,
/* SP ! " # $ % & ' ( ) * + , - . / */
SD, SO, SO, SO, SO, SO, SO, SD, SD, SD, SO, BP, SD, BM, SD, BB,
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, SD, SO, SO, SO, SO, SD,
/* @ A B C D E F G H I J K L M N O */
BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB,
/* P Q R S T U V W X Y Z [ \ ] ^ _ */
BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, SO, SB, SO, SO, SO,
/* ` a b c d e f g h i j k l m n o */
SO, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB,
/* p q r s t u v w x y z { | } ~ DEL */
BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, BB, SO, SO, SO, SB, CB,
C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1,
C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1,
C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1,
C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1,
C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1,
C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1,
C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1,
C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, C1,
};
static char c_base64_map[ 0x60 ] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
};
static char base64_c_map[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define BASE64_C(x) base64_c_map[(x)]
#define C_BASE64(x) c_base64_map[(x) - 0x20]
Str
wc_conv_from_utf7(Str is, wc_ces ces)
{
Str os;
wc_uchar *sp = (wc_uchar *)is->ptr;
wc_uchar *ep = sp + is->length;
wc_uchar *p;
int state = WC_UTF7_NOSTATE;
wc_uint32 b, high = 0;
wc_status st;
for (p = sp; p < ep && *p < 0x80 && *p != WC_C_UTF7_PLUS; p++)
;
if (p == ep)
return is;
os = Strnew_size(is->length + is->length / 3);
if (p > sp)
Strcat_charp_n(os, is->ptr, (int)(p - sp));
st.tag = NULL;
st.ntag = 0;
for (; p < ep; p++) {
switch (state) {
case WC_UTF7_NOSTATE:
if (*p == WC_C_UTF7_PLUS) {
state = WC_UTF7_PLUS;
st.shift = 16;
st.base = 0;
high = 0;
continue;
}
break;
case WC_UTF7_PLUS:
if (*p == WC_C_UTF7_MINUS)
wtf_push_ucs(os, (wc_uint32)WC_C_UTF7_PLUS, &st);
case WC_UTF7_BASE64:
switch (WC_UTF7_MAP[*p]) {
case BB: /* [A-Za-z0-9/] */
case BP: /* '+' */
b = C_BASE64(*p);
st.shift -= 6;
if (st.shift <= 0) {
st.base |= b >> (- st.shift);
if (st.base >= WC_C_UCS2_SURROGATE &&
st.base < WC_C_UCS2_SURROGATE_LOW) {
if (! high)
high = st.base;
else
high = 0; /* error */
} else if (st.base >= WC_C_UCS2_SURROGATE_LOW &&
st.base <= WC_C_UCS2_SURROGATE_END) {
if (high)
wtf_push_ucs(os, wc_utf16_to_ucs(high, st.base), &st);
/* else; */ /* error */
high = 0;
} else if (st.base != WC_C_UCS2_BOM)
wtf_push_ucs(os, st.base, &st);
st.shift += 16;
st.base = 0;
}
st.base |= (b << st.shift) & 0xffff;
state = WC_UTF7_BASE64;
continue;
case BM: /* '-' */
state = WC_UTF7_NOSTATE;
continue;
}
}
switch (WC_UTF7_MAP[*p]) {
case CD:
case CB:
Strcat_char(os, (char)*p);
break;
case C1:
wtf_push_unknown(os, p, 1);
break;
default:
wtf_push_ucs(os, (wc_uint32)*p, &st);
break;
}
}
return os;
}
static void
wc_push_ucs_to_utf7(Str os, wc_uint32 ucs, wc_status *st)
{
if (ucs > WC_C_UNICODE_END)
return;
if (ucs > WC_C_UCS2_END) {
ucs = wc_ucs_to_utf16(ucs);
wc_push_ucs_to_utf7(os, ucs >> 16, st);
wc_push_ucs_to_utf7(os, ucs & 0xffff, st);
return;
}
if (ucs < 0x80) {
switch (WC_UTF7_MAP[ucs]) {
case BB:
case BM:
case SD:
case CD:
if (st->state == WC_UTF7_BASE64) {
Strcat_char(os, BASE64_C(st->base));
Strcat_char(os, WC_C_UTF7_MINUS);
st->state = WC_UTF7_NOSTATE;
}
Strcat_char(os, (char)ucs);
return;
case BP:
if (st->state == WC_UTF7_BASE64) {
Strcat_char(os, BASE64_C(st->base));
Strcat_char(os, WC_C_UTF7_MINUS);
st->state = WC_UTF7_NOSTATE;
}
Strcat_char(os, WC_C_UTF7_PLUS);
Strcat_char(os, WC_C_UTF7_MINUS);
return;
}
}
if (st->state == WC_UTF7_BASE64 && st->shift) {
st->shift += 16;
st->base |= ucs >> st->shift;
Strcat_char(os, BASE64_C(st->base));
} else {
if (st->state != WC_UTF7_BASE64) {
Strcat_char(os, WC_C_UTF7_PLUS);
st->state = WC_UTF7_BASE64;
}
st->shift = 16;
st->base = 0;
}
st->shift -= 6;
Strcat_char(os, BASE64_C((ucs >> st->shift) & 0x3f));
st->shift -= 6;
Strcat_char(os, BASE64_C((ucs >> st->shift) & 0x3f));
if (st->shift) {
st->shift -= 6;
st->base = (ucs << (- st->shift)) & 0x3f;
}
return;
}
static int
wc_push_tag_to_utf7(Str os, int ntag, wc_status *st)
{
char *p;
if (ntag) {
p = wc_ucs_get_tag(ntag);
if (p == NULL)
ntag = 0;
}
if (ntag) {
wc_push_ucs_to_utf7(os, WC_C_LANGUAGE_TAG, st);
for (; *p; p++)
wc_push_ucs_to_utf7(os, WC_C_LANGUAGE_TAG0 | *p, st);
} else
wc_push_ucs_to_utf7(os, WC_C_CANCEL_TAG, st);
return ntag;
}
void
wc_push_to_utf7(Str os, wc_wchar_t cc, wc_status *st)
{
char *p;
while (1) {
switch (WC_CCS_SET(cc.ccs)) {
case WC_CCS_UCS4:
if (cc.code > WC_C_UNICODE_END) {
cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
continue;
}
case WC_CCS_US_ASCII:
case WC_CCS_UCS2:
if (st->ntag)
st->ntag = wc_push_tag_to_utf7(os, 0, st);
wc_push_ucs_to_utf7(os, cc.code, st);
return;
case WC_CCS_UCS_TAG:
if (WcOption.use_language_tag && wc_ucs_tag_to_tag(cc.code) != st->ntag)
st->ntag = wc_push_tag_to_utf7(os, wc_ucs_tag_to_tag(cc.code), st);
wc_push_ucs_to_utf7(os, wc_ucs_tag_to_ucs(cc.code), st);
return;
case WC_CCS_ISO_8859_1:
if (st->ntag)
st->ntag = wc_push_tag_to_utf7(os, 0, st);
wc_push_ucs_to_utf7(os, cc.code | 0x80, st);
return;
case WC_CCS_UNKNOWN_W:
if (!WcOption.no_replace) {
if (st->ntag)
st->ntag = wc_push_tag_to_utf7(os, 0, st);
for (p = WC_REPLACE_W; *p; p++)
wc_push_ucs_to_utf7(os, (wc_uint32)*p, st);
}
return;
case WC_CCS_UNKNOWN:
if (!WcOption.no_replace) {
if (st->ntag)
st->ntag = wc_push_tag_to_utf7(os, 0, st);
for (p = WC_REPLACE; *p; p++)
wc_push_ucs_to_utf7(os, (wc_uint32)*p, st);
}
return;
default:
if (WcOption.ucs_conv &&
(cc.code = wc_any_to_ucs(cc)) != WC_C_UCS4_ERROR)
cc.ccs = WC_CCS_UCS2;
else
cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
continue;
}
}
}
void
wc_push_to_utf7_end(Str os, wc_status *st)
{
if (st->ntag)
st->ntag = wc_push_tag_to_utf7(os, 0, st);
if (st->state == WC_UTF7_BASE64) {
if (st->shift)
Strcat_char(os, BASE64_C(st->base));
Strcat_char(os, WC_C_UTF7_MINUS);
}
return;
}
Str
wc_char_conv_from_utf7(wc_uchar c, wc_status *st)
{
static Str os;
static wc_uint32 high;
wc_uint32 b;
if (st->state == -1) {
st->state = WC_UTF7_NOSTATE;
os = Strnew_size(8);
}
switch (st->state) {
case WC_UTF7_NOSTATE:
if (c == WC_C_UTF7_PLUS) {
st->state = WC_UTF7_PLUS;
st->shift = 16;
st->base = 0;
high = 0;
return NULL;
}
break;
case WC_UTF7_PLUS:
if (c == WC_C_UTF7_MINUS) {
wtf_push_ucs(os, (wc_uint32)WC_C_UTF7_PLUS, st);
st->state = -1;
return os;
}
case WC_UTF7_BASE64:
switch (WC_UTF7_MAP[c]) {
case BB: /* [A-Za-z0-9/] */
case BP: /* '+' */
b = C_BASE64(c);
st->shift -= 6;
if (st->shift <= 0) {
st->base |= b >> (- st->shift);
if (st->base >= WC_C_UCS2_SURROGATE &&
st->base < WC_C_UCS2_SURROGATE_LOW) {
if (! high)
high = st->base;
else
high = 0; /* error */
} else if (st->base >= WC_C_UCS2_SURROGATE_LOW &&
st->base <= WC_C_UCS2_SURROGATE_END) {
if (high)
wtf_push_ucs(os, wc_utf16_to_ucs(high, st->base), st);
/* else; */ /* error */
high = 0;
} else if (st->base != WC_C_UCS2_BOM)
wtf_push_ucs(os, st->base, st);
st->shift += 16;
st->base = 0;
}
st->base |= (b << st->shift) & 0xffff;
st->state = WC_UTF7_BASE64;
return os;
case BM: /* '-' */
st->state = -1;
return NULL;
}
}
switch (WC_UTF7_MAP[c]) {
case CD:
case CB:
Strcat_char(os, (char)c);
break;
case C1:
break;
default:
wtf_push_ucs(os, (wc_uint32)c, st);
break;
}
st->state = -1;
return os;
}
#endif
|