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