Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * encoding.c : implements the encoding conversion functions needed for XML |
| 3 | * |
| 4 | * Related specs: |
| 5 | * rfc2044 (UTF-8 and UTF-16) F. Yergeau Alis Technologies |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 6 | * rfc2781 UTF-16, an encoding of ISO 10646, P. Hoffman, F. Yergeau |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 7 | * [ISO-10646] UTF-8 and UTF-16 in Annexes |
| 8 | * [ISO-8859-1] ISO Latin-1 characters codes. |
| 9 | * [UNICODE] The Unicode Consortium, "The Unicode Standard -- |
| 10 | * Worldwide Character Encoding -- Version 1.0", Addison- |
| 11 | * Wesley, Volume 1, 1991, Volume 2, 1992. UTF-8 is |
| 12 | * described in Unicode Technical Report #4. |
| 13 | * [US-ASCII] Coded Character Set--7-bit American Standard Code for |
| 14 | * Information Interchange, ANSI X3.4-1986. |
| 15 | * |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 16 | * Original code for IsoLatin1 and UTF-16 by "Martin J. Duerst" <duerst@w3.org> |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 17 | * |
| 18 | * See Copyright for the status of this software. |
| 19 | * |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 20 | * Daniel.Veillard@w3.org |
| 21 | */ |
| 22 | |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 23 | #ifdef WIN32 |
| 24 | #include "win32config.h" |
| 25 | #else |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 26 | #include "config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
| 32 | #ifdef HAVE_CTYPE_H |
| 33 | #include <ctype.h> |
| 34 | #endif |
Daniel Veillard | 6d3bf1f | 1999-12-16 17:52:19 +0000 | [diff] [blame] | 35 | #ifdef HAVE_STDLIB_H |
| 36 | #include <stdlib.h> |
| 37 | #endif |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 38 | #include <libxml/xmlversion.h> |
| 39 | #ifdef LIBXML_ICONV_ENABLED |
| 40 | #ifdef HAVE_ERRNO_H |
| 41 | #include <errno.h> |
| 42 | #endif |
| 43 | #endif |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 44 | #include <libxml/encoding.h> |
| 45 | #include <libxml/xmlmemory.h> |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 46 | #ifdef LIBXML_HTML_ENABLED |
| 47 | #include <libxml/HTMLparser.h> |
| 48 | #endif |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 49 | #include <libxml/xmlerror.h> |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 50 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 51 | xmlCharEncodingHandlerPtr xmlUTF16LEHandler = NULL; |
| 52 | xmlCharEncodingHandlerPtr xmlUTF16BEHandler = NULL; |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 53 | |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 54 | typedef struct _xmlCharEncodingAlias xmlCharEncodingAlias; |
| 55 | typedef xmlCharEncodingAlias *xmlCharEncodingAliasPtr; |
| 56 | struct _xmlCharEncodingAlias { |
| 57 | const char *name; |
| 58 | const char *alias; |
| 59 | }; |
| 60 | |
| 61 | static xmlCharEncodingAliasPtr xmlCharEncodingAliases = NULL; |
| 62 | static int xmlCharEncodingAliasesNb = 0; |
| 63 | static int xmlCharEncodingAliasesMax = 0; |
| 64 | |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 65 | #ifdef LIBXML_ICONV_ENABLED |
| 66 | #if 0 |
| 67 | #define DEBUG_ENCODING /* Define this to get encoding traces */ |
| 68 | #endif |
| 69 | #endif |
| 70 | |
| 71 | static int xmlLittleEndian = 1; |
| 72 | |
Daniel Veillard | 0ba4d53 | 1998-11-01 19:34:31 +0000 | [diff] [blame] | 73 | /* |
| 74 | * From rfc2044: encoding of the Unicode values on UTF-8: |
| 75 | * |
| 76 | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
| 77 | * 0000 0000-0000 007F 0xxxxxxx |
| 78 | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
| 79 | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
| 80 | * |
| 81 | * I hope we won't use values > 0xFFFF anytime soon ! |
| 82 | */ |
| 83 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 84 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 85 | * xmlGetUTF8Char: |
| 86 | * @utf: a sequence of UTF-8 encoded bytes |
| 87 | * @len: a pointer to @bytes len |
| 88 | * |
| 89 | * Read one UTF8 Char from @utf |
| 90 | * |
| 91 | * Returns the char value or -1 in case of error and update @len with the |
| 92 | * number of bytes used |
| 93 | */ |
| 94 | int |
| 95 | xmlGetUTF8Char(const unsigned char *utf, int *len) { |
| 96 | unsigned int c; |
| 97 | |
| 98 | if (utf == NULL) |
| 99 | goto error; |
| 100 | if (len == NULL) |
| 101 | goto error; |
| 102 | if (*len < 1) |
| 103 | goto error; |
| 104 | |
| 105 | c = utf[0]; |
| 106 | if (c & 0x80) { |
| 107 | if (*len < 2) |
| 108 | goto error; |
| 109 | if ((utf[1] & 0xc0) != 0x80) |
| 110 | goto error; |
| 111 | if ((c & 0xe0) == 0xe0) { |
| 112 | if (*len < 3) |
| 113 | goto error; |
| 114 | if ((utf[2] & 0xc0) != 0x80) |
| 115 | goto error; |
| 116 | if ((c & 0xf0) == 0xf0) { |
| 117 | if (*len < 4) |
| 118 | goto error; |
| 119 | if ((c & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80) |
| 120 | goto error; |
| 121 | *len = 4; |
| 122 | /* 4-byte code */ |
| 123 | c = (utf[0] & 0x7) << 18; |
| 124 | c |= (utf[1] & 0x3f) << 12; |
| 125 | c |= (utf[2] & 0x3f) << 6; |
| 126 | c |= utf[3] & 0x3f; |
| 127 | } else { |
| 128 | /* 3-byte code */ |
| 129 | *len = 3; |
| 130 | c = (utf[0] & 0xf) << 12; |
| 131 | c |= (utf[1] & 0x3f) << 6; |
| 132 | c |= utf[2] & 0x3f; |
| 133 | } |
| 134 | } else { |
| 135 | /* 2-byte code */ |
| 136 | *len = 2; |
| 137 | c = (utf[0] & 0x1f) << 6; |
| 138 | c |= utf[1] & 0x3f; |
| 139 | } |
| 140 | } else { |
| 141 | /* 1-byte code */ |
| 142 | *len = 1; |
| 143 | } |
| 144 | return(c); |
| 145 | |
| 146 | error: |
| 147 | *len = 0; |
| 148 | return(-1); |
| 149 | } |
| 150 | |
| 151 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 152 | * xmlCheckUTF8: Check utf-8 string for legality. |
| 153 | * @utf: Pointer to putative utf-8 encoded string. |
| 154 | * |
| 155 | * Checks @utf for being valid utf-8. @utf is assumed to be |
| 156 | * null-terminated. This function is not super-strict, as it will |
| 157 | * allow longer utf-8 sequences than necessary. Note that Java is |
| 158 | * capable of producing these sequences if provoked. Also note, this |
| 159 | * routine checks for the 4-byte maxiumum size, but does not check for |
| 160 | * 0x10ffff maximum value. |
| 161 | * |
| 162 | * Return value: true if @utf is valid. |
| 163 | **/ |
| 164 | int |
| 165 | xmlCheckUTF8(const unsigned char *utf) |
| 166 | { |
| 167 | int ix; |
| 168 | unsigned char c; |
| 169 | |
| 170 | for (ix = 0; (c = utf[ix]);) { |
| 171 | if (c & 0x80) { |
| 172 | if ((utf[ix + 1] & 0xc0) != 0x80) |
| 173 | return(0); |
| 174 | if ((c & 0xe0) == 0xe0) { |
| 175 | if ((utf[ix + 2] & 0xc0) != 0x80) |
| 176 | return(0); |
| 177 | if ((c & 0xf0) == 0xf0) { |
| 178 | if ((c & 0xf8) != 0xf0 || (utf[ix + 3] & 0xc0) != 0x80) |
| 179 | return(0); |
| 180 | ix += 4; |
| 181 | /* 4-byte code */ |
| 182 | } else |
| 183 | /* 3-byte code */ |
| 184 | ix += 3; |
| 185 | } else |
| 186 | /* 2-byte code */ |
| 187 | ix += 2; |
| 188 | } else |
| 189 | /* 1-byte code */ |
| 190 | ix++; |
| 191 | } |
| 192 | return(1); |
| 193 | } |
| 194 | |
| 195 | /** |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 196 | * asciiToUTF8: |
| 197 | * @out: a pointer to an array of bytes to store the result |
| 198 | * @outlen: the length of @out |
| 199 | * @in: a pointer to an array of ASCII chars |
| 200 | * @inlen: the length of @in |
| 201 | * |
| 202 | * Take a block of ASCII chars in and try to convert it to an UTF-8 |
| 203 | * block of chars out. |
| 204 | * Returns 0 if success, or -1 otherwise |
| 205 | * The value of @inlen after return is the number of octets consumed |
| 206 | * as the return value is positive, else unpredictiable. |
| 207 | * The value of @outlen after return is the number of ocetes consumed. |
| 208 | */ |
| 209 | int |
| 210 | asciiToUTF8(unsigned char* out, int *outlen, |
| 211 | const unsigned char* in, int *inlen) { |
| 212 | unsigned char* outstart = out; |
| 213 | const unsigned char* base = in; |
| 214 | const unsigned char* processed = in; |
| 215 | unsigned char* outend = out + *outlen; |
| 216 | const unsigned char* inend; |
| 217 | unsigned int c; |
| 218 | int bits; |
| 219 | |
| 220 | inend = in + (*inlen); |
| 221 | while ((in < inend) && (out - outstart + 5 < *outlen)) { |
| 222 | c= *in++; |
| 223 | |
| 224 | /* assertion: c is a single UTF-4 value */ |
| 225 | if (out >= outend) |
| 226 | break; |
| 227 | if (c < 0x80) { *out++= c; bits= -6; } |
| 228 | else { |
| 229 | *outlen = out - outstart; |
| 230 | *inlen = processed - base; |
| 231 | return(-1); |
| 232 | } |
| 233 | |
| 234 | for ( ; bits >= 0; bits-= 6) { |
| 235 | if (out >= outend) |
| 236 | break; |
| 237 | *out++= ((c >> bits) & 0x3F) | 0x80; |
| 238 | } |
| 239 | processed = (const unsigned char*) in; |
| 240 | } |
| 241 | *outlen = out - outstart; |
| 242 | *inlen = processed - base; |
| 243 | return(0); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * UTF8Toascii: |
| 248 | * @out: a pointer to an array of bytes to store the result |
| 249 | * @outlen: the length of @out |
| 250 | * @in: a pointer to an array of UTF-8 chars |
| 251 | * @inlen: the length of @in |
| 252 | * |
| 253 | * Take a block of UTF-8 chars in and try to convert it to an ASCII |
| 254 | * block of chars out. |
| 255 | * |
| 256 | * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise |
| 257 | * The value of @inlen after return is the number of octets consumed |
| 258 | * as the return value is positive, else unpredictiable. |
| 259 | * The value of @outlen after return is the number of ocetes consumed. |
| 260 | */ |
| 261 | int |
| 262 | UTF8Toascii(unsigned char* out, int *outlen, |
| 263 | const unsigned char* in, int *inlen) { |
| 264 | const unsigned char* processed = in; |
| 265 | const unsigned char* outend; |
| 266 | const unsigned char* outstart = out; |
| 267 | const unsigned char* instart = in; |
| 268 | const unsigned char* inend; |
| 269 | unsigned int c, d; |
| 270 | int trailing; |
| 271 | |
| 272 | if (in == NULL) { |
| 273 | /* |
| 274 | * initialization nothing to do |
| 275 | */ |
| 276 | *outlen = 0; |
| 277 | *inlen = 0; |
| 278 | return(0); |
| 279 | } |
| 280 | inend = in + (*inlen); |
| 281 | outend = out + (*outlen); |
| 282 | while (in < inend) { |
| 283 | d = *in++; |
| 284 | if (d < 0x80) { c= d; trailing= 0; } |
| 285 | else if (d < 0xC0) { |
| 286 | /* trailing byte in leading position */ |
| 287 | *outlen = out - outstart; |
| 288 | *inlen = processed - instart; |
| 289 | return(-2); |
| 290 | } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; } |
| 291 | else if (d < 0xF0) { c= d & 0x0F; trailing= 2; } |
| 292 | else if (d < 0xF8) { c= d & 0x07; trailing= 3; } |
| 293 | else { |
| 294 | /* no chance for this in Ascii */ |
| 295 | *outlen = out - outstart; |
| 296 | *inlen = processed - instart; |
| 297 | return(-2); |
| 298 | } |
| 299 | |
| 300 | if (inend - in < trailing) { |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | for ( ; trailing; trailing--) { |
| 305 | if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80)) |
| 306 | break; |
| 307 | c <<= 6; |
| 308 | c |= d & 0x3F; |
| 309 | } |
| 310 | |
| 311 | /* assertion: c is a single UTF-4 value */ |
| 312 | if (c < 0x80) { |
| 313 | if (out >= outend) |
| 314 | break; |
| 315 | *out++ = c; |
| 316 | } else { |
| 317 | /* no chance for this in Ascii */ |
| 318 | *outlen = out - outstart; |
| 319 | *inlen = processed - instart; |
| 320 | return(-2); |
| 321 | } |
| 322 | processed = in; |
| 323 | } |
| 324 | *outlen = out - outstart; |
| 325 | *inlen = processed - instart; |
| 326 | return(0); |
| 327 | } |
| 328 | |
| 329 | /** |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 330 | * isolat1ToUTF8: |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 331 | * @out: a pointer to an array of bytes to store the result |
| 332 | * @outlen: the length of @out |
| 333 | * @in: a pointer to an array of ISO Latin 1 chars |
| 334 | * @inlen: the length of @in |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 335 | * |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 336 | * Take a block of ISO Latin 1 chars in and try to convert it to an UTF-8 |
| 337 | * block of chars out. |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 338 | * Returns 0 if success, or -1 otherwise |
| 339 | * The value of @inlen after return is the number of octets consumed |
| 340 | * as the return value is positive, else unpredictiable. |
| 341 | * The value of @outlen after return is the number of ocetes consumed. |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 342 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 343 | int |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 344 | isolat1ToUTF8(unsigned char* out, int *outlen, |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 345 | const unsigned char* in, int *inlen) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 346 | unsigned char* outstart = out; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 347 | const unsigned char* base = in; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 348 | const unsigned char* processed = in; |
| 349 | unsigned char* outend = out + *outlen; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 350 | const unsigned char* inend; |
| 351 | unsigned int c; |
| 352 | int bits; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 353 | |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 354 | inend = in + (*inlen); |
| 355 | while ((in < inend) && (out - outstart + 5 < *outlen)) { |
| 356 | c= *in++; |
| 357 | |
| 358 | /* assertion: c is a single UTF-4 value */ |
| 359 | if (out >= outend) |
| 360 | break; |
| 361 | if (c < 0x80) { *out++= c; bits= -6; } |
| 362 | else { *out++= ((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 363 | |
| 364 | for ( ; bits >= 0; bits-= 6) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 365 | if (out >= outend) |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 366 | break; |
| 367 | *out++= ((c >> bits) & 0x3F) | 0x80; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 368 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 369 | processed = (const unsigned char*) in; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 370 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 371 | *outlen = out - outstart; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 372 | *inlen = processed - base; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 373 | return(0); |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 376 | /** |
| 377 | * UTF8Toisolat1: |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 378 | * @out: a pointer to an array of bytes to store the result |
| 379 | * @outlen: the length of @out |
| 380 | * @in: a pointer to an array of UTF-8 chars |
| 381 | * @inlen: the length of @in |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 382 | * |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 383 | * Take a block of UTF-8 chars in and try to convert it to an ISO Latin 1 |
| 384 | * block of chars out. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 385 | * |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 386 | * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 387 | * The value of @inlen after return is the number of octets consumed |
| 388 | * as the return value is positive, else unpredictiable. |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 389 | * The value of @outlen after return is the number of ocetes consumed. |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 390 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 391 | int |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 392 | UTF8Toisolat1(unsigned char* out, int *outlen, |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 393 | const unsigned char* in, int *inlen) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 394 | const unsigned char* processed = in; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 395 | const unsigned char* outend; |
| 396 | const unsigned char* outstart = out; |
| 397 | const unsigned char* instart = in; |
| 398 | const unsigned char* inend; |
| 399 | unsigned int c, d; |
| 400 | int trailing; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 401 | |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 402 | if (in == NULL) { |
| 403 | /* |
| 404 | * initialization nothing to do |
| 405 | */ |
| 406 | *outlen = 0; |
| 407 | *inlen = 0; |
| 408 | return(0); |
| 409 | } |
| 410 | inend = in + (*inlen); |
| 411 | outend = out + (*outlen); |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 412 | while (in < inend) { |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 413 | d = *in++; |
| 414 | if (d < 0x80) { c= d; trailing= 0; } |
| 415 | else if (d < 0xC0) { |
| 416 | /* trailing byte in leading position */ |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 417 | *outlen = out - outstart; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 418 | *inlen = processed - instart; |
| 419 | return(-2); |
| 420 | } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; } |
| 421 | else if (d < 0xF0) { c= d & 0x0F; trailing= 2; } |
| 422 | else if (d < 0xF8) { c= d & 0x07; trailing= 3; } |
| 423 | else { |
| 424 | /* no chance for this in IsoLat1 */ |
| 425 | *outlen = out - outstart; |
| 426 | *inlen = processed - instart; |
| 427 | return(-2); |
| 428 | } |
| 429 | |
| 430 | if (inend - in < trailing) { |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | for ( ; trailing; trailing--) { |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 435 | if (in >= inend) |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 436 | break; |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 437 | if (((d= *in++) & 0xC0) != 0x80) { |
| 438 | *outlen = out - outstart; |
| 439 | *inlen = processed - instart; |
| 440 | return(-2); |
| 441 | } |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 442 | c <<= 6; |
| 443 | c |= d & 0x3F; |
| 444 | } |
| 445 | |
| 446 | /* assertion: c is a single UTF-4 value */ |
| 447 | if (c <= 0xFF) { |
| 448 | if (out >= outend) |
| 449 | break; |
| 450 | *out++ = c; |
| 451 | } else { |
| 452 | /* no chance for this in IsoLat1 */ |
| 453 | *outlen = out - outstart; |
| 454 | *inlen = processed - instart; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 455 | return(-2); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 456 | } |
| 457 | processed = in; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 458 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 459 | *outlen = out - outstart; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 460 | *inlen = processed - instart; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 461 | return(0); |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 464 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 465 | * UTF16LEToUTF8: |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 466 | * @out: a pointer to an array of bytes to store the result |
| 467 | * @outlen: the length of @out |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 468 | * @inb: a pointer to an array of UTF-16LE passwd as a byte array |
| 469 | * @inlenb: the length of @in in UTF-16LE chars |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 470 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 471 | * Take a block of UTF-16LE ushorts in and try to convert it to an UTF-8 |
| 472 | * block of chars out. This function assume the endian properity |
| 473 | * is the same between the native type of this machine and the |
| 474 | * inputed one. |
| 475 | * |
| 476 | * Returns the number of byte written, or -1 by lack of space, or -2 |
| 477 | * if the transcoding fails (for *in is not valid utf16 string) |
| 478 | * The value of *inlen after return is the number of octets consumed |
| 479 | * as the return value is positive, else unpredictiable. |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 480 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 481 | int |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 482 | UTF16LEToUTF8(unsigned char* out, int *outlen, |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 483 | const unsigned char* inb, int *inlenb) |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 484 | { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 485 | unsigned char* outstart = out; |
| 486 | const unsigned char* processed = inb; |
| 487 | unsigned char* outend = out + *outlen; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 488 | unsigned short* in = (unsigned short*) inb; |
| 489 | unsigned short* inend; |
| 490 | unsigned int c, d, inlen; |
| 491 | unsigned char *tmp; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 492 | int bits; |
| 493 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 494 | if ((*inlenb % 2) == 1) |
| 495 | (*inlenb)--; |
| 496 | inlen = *inlenb / 2; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 497 | inend = in + inlen; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 498 | while ((in < inend) && (out - outstart + 5 < *outlen)) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 499 | if (xmlLittleEndian) { |
| 500 | c= *in++; |
| 501 | } else { |
| 502 | tmp = (unsigned char *) in; |
| 503 | c = *tmp++; |
| 504 | c = c | (((unsigned int)*tmp) << 8); |
| 505 | in++; |
| 506 | } |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 507 | if ((c & 0xFC00) == 0xD800) { /* surrogates */ |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 508 | if (in >= inend) { /* (in > inend) shouldn't happens */ |
| 509 | break; |
| 510 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 511 | if (xmlLittleEndian) { |
| 512 | d = *in++; |
| 513 | } else { |
| 514 | tmp = (unsigned char *) in; |
| 515 | d = *tmp++; |
| 516 | d = d | (((unsigned int)*tmp) << 8); |
| 517 | in++; |
| 518 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 519 | if ((d & 0xFC00) == 0xDC00) { |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 520 | c &= 0x03FF; |
| 521 | c <<= 10; |
| 522 | c |= d & 0x03FF; |
| 523 | c += 0x10000; |
| 524 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 525 | else { |
| 526 | *outlen = out - outstart; |
| 527 | *inlenb = processed - inb; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 528 | return(-2); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 529 | } |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 532 | /* assertion: c is a single UTF-4 value */ |
| 533 | if (out >= outend) |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 534 | break; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 535 | if (c < 0x80) { *out++= c; bits= -6; } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 536 | else if (c < 0x800) { *out++= ((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 537 | else if (c < 0x10000) { *out++= ((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
| 538 | else { *out++= ((c >> 18) & 0x07) | 0xF0; bits= 12; } |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 539 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 540 | for ( ; bits >= 0; bits-= 6) { |
| 541 | if (out >= outend) |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 542 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 543 | *out++= ((c >> bits) & 0x3F) | 0x80; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 544 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 545 | processed = (const unsigned char*) in; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 546 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 547 | *outlen = out - outstart; |
| 548 | *inlenb = processed - inb; |
| 549 | return(0); |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 552 | /** |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 553 | * UTF8ToUTF16LE: |
| 554 | * @outb: a pointer to an array of bytes to store the result |
| 555 | * @outlen: the length of @outb |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 556 | * @in: a pointer to an array of UTF-8 chars |
| 557 | * @inlen: the length of @in |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 558 | * |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 559 | * Take a block of UTF-8 chars in and try to convert it to an UTF-16LE |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 560 | * block of chars out. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 561 | * |
Daniel Veillard | 1e346af | 1999-02-22 10:33:01 +0000 | [diff] [blame] | 562 | * Returns the number of byte written, or -1 by lack of space, or -2 |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 563 | * if the transcoding failed. |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 564 | */ |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 565 | int |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 566 | UTF8ToUTF16LE(unsigned char* outb, int *outlen, |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 567 | const unsigned char* in, int *inlen) |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 568 | { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 569 | unsigned short* out = (unsigned short*) outb; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 570 | const unsigned char* processed = in; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 571 | unsigned short* outstart= out; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 572 | unsigned short* outend; |
| 573 | const unsigned char* inend= in+*inlen; |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 574 | unsigned int c, d; |
| 575 | int trailing; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 576 | unsigned char *tmp; |
| 577 | unsigned short tmp1, tmp2; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 578 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 579 | if (in == NULL) { |
| 580 | /* |
| 581 | * initialization, add the Byte Order Mark |
| 582 | */ |
| 583 | if (*outlen >= 2) { |
| 584 | outb[0] = 0xFF; |
| 585 | outb[1] = 0xFE; |
| 586 | *outlen = 2; |
| 587 | *inlen = 0; |
| 588 | #ifdef DEBUG_ENCODING |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 589 | xmlGenericError(xmlGenericErrorContext, |
| 590 | "Added FFFE Byte Order Mark\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 591 | #endif |
| 592 | return(2); |
| 593 | } |
| 594 | *outlen = 0; |
| 595 | *inlen = 0; |
| 596 | return(0); |
| 597 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 598 | outend = out + (*outlen / 2); |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 599 | while (in < inend) { |
| 600 | d= *in++; |
| 601 | if (d < 0x80) { c= d; trailing= 0; } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 602 | else if (d < 0xC0) { |
| 603 | /* trailing byte in leading position */ |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 604 | *outlen = (out - outstart) * 2; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 605 | *inlen = processed - in; |
| 606 | return(-2); |
| 607 | } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; } |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 608 | else if (d < 0xF0) { c= d & 0x0F; trailing= 2; } |
| 609 | else if (d < 0xF8) { c= d & 0x07; trailing= 3; } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 610 | else { |
| 611 | /* no chance for this in UTF-16 */ |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 612 | *outlen = (out - outstart) * 2; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 613 | *inlen = processed - in; |
| 614 | return(-2); |
| 615 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 616 | |
| 617 | if (inend - in < trailing) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 618 | break; |
| 619 | } |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 620 | |
| 621 | for ( ; trailing; trailing--) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 622 | if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80)) |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 623 | break; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 624 | c <<= 6; |
| 625 | c |= d & 0x3F; |
| 626 | } |
| 627 | |
| 628 | /* assertion: c is a single UTF-4 value */ |
| 629 | if (c < 0x10000) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 630 | if (out >= outend) |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 631 | break; |
| 632 | if (xmlLittleEndian) { |
| 633 | *out++ = c; |
| 634 | } else { |
| 635 | tmp = (unsigned char *) out; |
| 636 | *tmp = c ; |
| 637 | *(tmp + 1) = c >> 8 ; |
| 638 | out++; |
| 639 | } |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 640 | } |
| 641 | else if (c < 0x110000) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 642 | if (out+1 >= outend) |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 643 | break; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 644 | c -= 0x10000; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 645 | if (xmlLittleEndian) { |
| 646 | *out++ = 0xD800 | (c >> 10); |
| 647 | *out++ = 0xDC00 | (c & 0x03FF); |
| 648 | } else { |
| 649 | tmp1 = 0xD800 | (c >> 10); |
| 650 | tmp = (unsigned char *) out; |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 651 | *tmp = (unsigned char) tmp1; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 652 | *(tmp + 1) = tmp1 >> 8; |
| 653 | out++; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 654 | |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 655 | tmp2 = 0xDC00 | (c & 0x03FF); |
| 656 | tmp = (unsigned char *) out; |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 657 | *tmp = (unsigned char) tmp2; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 658 | *(tmp + 1) = tmp2 >> 8; |
| 659 | out++; |
| 660 | } |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 661 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 662 | else |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 663 | break; |
| 664 | processed = in; |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 665 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 666 | *outlen = (out - outstart) * 2; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 667 | *inlen = processed - in; |
| 668 | return(0); |
Daniel Veillard | 891e404 | 1998-10-19 00:43:02 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 671 | /** |
| 672 | * UTF16BEToUTF8: |
| 673 | * @out: a pointer to an array of bytes to store the result |
| 674 | * @outlen: the length of @out |
| 675 | * @inb: a pointer to an array of UTF-16 passwd as a byte array |
| 676 | * @inlenb: the length of @in in UTF-16 chars |
| 677 | * |
| 678 | * Take a block of UTF-16 ushorts in and try to convert it to an UTF-8 |
| 679 | * block of chars out. This function assume the endian properity |
| 680 | * is the same between the native type of this machine and the |
| 681 | * inputed one. |
| 682 | * |
| 683 | * Returns the number of byte written, or -1 by lack of space, or -2 |
| 684 | * if the transcoding fails (for *in is not valid utf16 string) |
| 685 | * The value of *inlen after return is the number of octets consumed |
| 686 | * as the return value is positive, else unpredictiable. |
| 687 | */ |
| 688 | int |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 689 | UTF16BEToUTF8(unsigned char* out, int *outlen, |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 690 | const unsigned char* inb, int *inlenb) |
| 691 | { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 692 | unsigned char* outstart = out; |
| 693 | const unsigned char* processed = inb; |
| 694 | unsigned char* outend = out + *outlen; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 695 | unsigned short* in = (unsigned short*) inb; |
| 696 | unsigned short* inend; |
| 697 | unsigned int c, d, inlen; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 698 | unsigned char *tmp; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 699 | int bits; |
| 700 | |
| 701 | if ((*inlenb % 2) == 1) |
| 702 | (*inlenb)--; |
| 703 | inlen = *inlenb / 2; |
| 704 | inend= in + inlen; |
| 705 | while (in < inend) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 706 | if (xmlLittleEndian) { |
| 707 | tmp = (unsigned char *) in; |
| 708 | c = *tmp++; |
| 709 | c = c << 8; |
| 710 | c = c | (unsigned int) *tmp; |
| 711 | in++; |
| 712 | } else { |
| 713 | c= *in++; |
| 714 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 715 | if ((c & 0xFC00) == 0xD800) { /* surrogates */ |
| 716 | if (in >= inend) { /* (in > inend) shouldn't happens */ |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 717 | *outlen = out - outstart; |
| 718 | *inlenb = processed - inb; |
| 719 | return(-2); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 720 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 721 | if (xmlLittleEndian) { |
| 722 | tmp = (unsigned char *) in; |
| 723 | d = *tmp++; |
| 724 | d = d << 8; |
| 725 | d = d | (unsigned int) *tmp; |
| 726 | in++; |
| 727 | } else { |
| 728 | d= *in++; |
| 729 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 730 | if ((d & 0xFC00) == 0xDC00) { |
| 731 | c &= 0x03FF; |
| 732 | c <<= 10; |
| 733 | c |= d & 0x03FF; |
| 734 | c += 0x10000; |
| 735 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 736 | else { |
| 737 | *outlen = out - outstart; |
| 738 | *inlenb = processed - inb; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 739 | return(-2); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 740 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /* assertion: c is a single UTF-4 value */ |
| 744 | if (out >= outend) |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 745 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 746 | if (c < 0x80) { *out++= c; bits= -6; } |
| 747 | else if (c < 0x800) { *out++= ((c >> 6) & 0x1F) | 0xC0; bits= 0; } |
| 748 | else if (c < 0x10000) { *out++= ((c >> 12) & 0x0F) | 0xE0; bits= 6; } |
| 749 | else { *out++= ((c >> 18) & 0x07) | 0xF0; bits= 12; } |
| 750 | |
| 751 | for ( ; bits >= 0; bits-= 6) { |
| 752 | if (out >= outend) |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 753 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 754 | *out++= ((c >> bits) & 0x3F) | 0x80; |
| 755 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 756 | processed = (const unsigned char*) in; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 757 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 758 | *outlen = out - outstart; |
| 759 | *inlenb = processed - inb; |
| 760 | return(0); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | /** |
| 764 | * UTF8ToUTF16BE: |
| 765 | * @outb: a pointer to an array of bytes to store the result |
| 766 | * @outlen: the length of @outb |
| 767 | * @in: a pointer to an array of UTF-8 chars |
| 768 | * @inlen: the length of @in |
| 769 | * |
| 770 | * Take a block of UTF-8 chars in and try to convert it to an UTF-16BE |
| 771 | * block of chars out. |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 772 | * |
| 773 | * Returns the number of byte written, or -1 by lack of space, or -2 |
| 774 | * if the transcoding failed. |
| 775 | */ |
| 776 | int |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 777 | UTF8ToUTF16BE(unsigned char* outb, int *outlen, |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 778 | const unsigned char* in, int *inlen) |
| 779 | { |
| 780 | unsigned short* out = (unsigned short*) outb; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 781 | const unsigned char* processed = in; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 782 | unsigned short* outstart= out; |
| 783 | unsigned short* outend; |
| 784 | const unsigned char* inend= in+*inlen; |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 785 | unsigned int c, d; |
| 786 | int trailing; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 787 | unsigned char *tmp; |
| 788 | unsigned short tmp1, tmp2; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 789 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 790 | if (in == NULL) { |
| 791 | /* |
| 792 | * initialization, add the Byte Order Mark |
| 793 | */ |
| 794 | if (*outlen >= 2) { |
| 795 | outb[0] = 0xFE; |
| 796 | outb[1] = 0xFF; |
| 797 | *outlen = 2; |
| 798 | *inlen = 0; |
| 799 | #ifdef DEBUG_ENCODING |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 800 | xmlGenericError(xmlGenericErrorContext, |
| 801 | "Added FEFF Byte Order Mark\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 802 | #endif |
| 803 | return(2); |
| 804 | } |
| 805 | *outlen = 0; |
| 806 | *inlen = 0; |
| 807 | return(0); |
| 808 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 809 | outend = out + (*outlen / 2); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 810 | while (in < inend) { |
| 811 | d= *in++; |
| 812 | if (d < 0x80) { c= d; trailing= 0; } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 813 | else if (d < 0xC0) { |
| 814 | /* trailing byte in leading position */ |
| 815 | *outlen = out - outstart; |
| 816 | *inlen = processed - in; |
| 817 | return(-2); |
| 818 | } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 819 | else if (d < 0xF0) { c= d & 0x0F; trailing= 2; } |
| 820 | else if (d < 0xF8) { c= d & 0x07; trailing= 3; } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 821 | else { |
| 822 | /* no chance for this in UTF-16 */ |
| 823 | *outlen = out - outstart; |
| 824 | *inlen = processed - in; |
| 825 | return(-2); |
| 826 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 827 | |
| 828 | if (inend - in < trailing) { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 829 | break; |
| 830 | } |
| 831 | |
| 832 | for ( ; trailing; trailing--) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 833 | if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80)) break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 834 | c <<= 6; |
| 835 | c |= d & 0x3F; |
| 836 | } |
| 837 | |
| 838 | /* assertion: c is a single UTF-4 value */ |
| 839 | if (c < 0x10000) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 840 | if (out >= outend) break; |
| 841 | if (xmlLittleEndian) { |
| 842 | tmp = (unsigned char *) out; |
| 843 | *tmp = c >> 8; |
| 844 | *(tmp + 1) = c; |
| 845 | out++; |
| 846 | } else { |
| 847 | *out++ = c; |
| 848 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 849 | } |
| 850 | else if (c < 0x110000) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 851 | if (out+1 >= outend) break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 852 | c -= 0x10000; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 853 | if (xmlLittleEndian) { |
| 854 | tmp1 = 0xD800 | (c >> 10); |
| 855 | tmp = (unsigned char *) out; |
| 856 | *tmp = tmp1 >> 8; |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 857 | *(tmp + 1) = (unsigned char) tmp1; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 858 | out++; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 859 | |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 860 | tmp2 = 0xDC00 | (c & 0x03FF); |
| 861 | tmp = (unsigned char *) out; |
| 862 | *tmp = tmp2 >> 8; |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 863 | *(tmp + 1) = (unsigned char) tmp2; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 864 | out++; |
| 865 | } else { |
| 866 | *out++ = 0xD800 | (c >> 10); |
| 867 | *out++ = 0xDC00 | (c & 0x03FF); |
| 868 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 869 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 870 | else |
| 871 | break; |
| 872 | processed = in; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 873 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 874 | *outlen = (out - outstart) * 2; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 875 | *inlen = processed - in; |
| 876 | return(0); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 877 | } |
Daniel Veillard | 97b5877 | 1998-10-20 06:14:16 +0000 | [diff] [blame] | 878 | |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 879 | /** |
| 880 | * xmlDetectCharEncoding: |
| 881 | * @in: a pointer to the first bytes of the XML entity, must be at least |
| 882 | * 4 bytes long. |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 883 | * @len: pointer to the length of the buffer |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 884 | * |
| 885 | * Guess the encoding of the entity using the first bytes of the entity content |
| 886 | * accordingly of the non-normative appendix F of the XML-1.0 recommendation. |
| 887 | * |
| 888 | * Returns one of the XML_CHAR_ENCODING_... values. |
| 889 | */ |
| 890 | xmlCharEncoding |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 891 | xmlDetectCharEncoding(const unsigned char* in, int len) |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 892 | { |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 893 | if (len >= 4) { |
| 894 | if ((in[0] == 0x00) && (in[1] == 0x00) && |
| 895 | (in[2] == 0x00) && (in[3] == 0x3C)) |
| 896 | return(XML_CHAR_ENCODING_UCS4BE); |
| 897 | if ((in[0] == 0x3C) && (in[1] == 0x00) && |
| 898 | (in[2] == 0x00) && (in[3] == 0x00)) |
| 899 | return(XML_CHAR_ENCODING_UCS4LE); |
| 900 | if ((in[0] == 0x00) && (in[1] == 0x00) && |
| 901 | (in[2] == 0x3C) && (in[3] == 0x00)) |
| 902 | return(XML_CHAR_ENCODING_UCS4_2143); |
| 903 | if ((in[0] == 0x00) && (in[1] == 0x3C) && |
| 904 | (in[2] == 0x00) && (in[3] == 0x00)) |
| 905 | return(XML_CHAR_ENCODING_UCS4_3412); |
| 906 | if ((in[0] == 0x4C) && (in[1] == 0x6F) && |
| 907 | (in[2] == 0xA7) && (in[3] == 0x94)) |
| 908 | return(XML_CHAR_ENCODING_EBCDIC); |
| 909 | if ((in[0] == 0x3C) && (in[1] == 0x3F) && |
| 910 | (in[2] == 0x78) && (in[3] == 0x6D)) |
| 911 | return(XML_CHAR_ENCODING_UTF8); |
| 912 | } |
| 913 | if (len >= 2) { |
| 914 | if ((in[0] == 0xFE) && (in[1] == 0xFF)) |
| 915 | return(XML_CHAR_ENCODING_UTF16BE); |
| 916 | if ((in[0] == 0xFF) && (in[1] == 0xFE)) |
| 917 | return(XML_CHAR_ENCODING_UTF16LE); |
| 918 | } |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 919 | return(XML_CHAR_ENCODING_NONE); |
| 920 | } |
| 921 | |
| 922 | /** |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 923 | * xmlCleanupEncodingAliases: |
| 924 | * |
| 925 | * Unregisters all aliases |
| 926 | */ |
| 927 | void |
| 928 | xmlCleanupEncodingAliases(void) { |
| 929 | int i; |
| 930 | |
| 931 | if (xmlCharEncodingAliases == NULL) |
| 932 | return; |
| 933 | |
| 934 | for (i = 0;i < xmlCharEncodingAliasesNb;i++) { |
| 935 | if (xmlCharEncodingAliases[i].name != NULL) |
| 936 | xmlFree((char *) xmlCharEncodingAliases[i].name); |
| 937 | if (xmlCharEncodingAliases[i].alias != NULL) |
| 938 | xmlFree((char *) xmlCharEncodingAliases[i].alias); |
| 939 | } |
| 940 | xmlCharEncodingAliasesNb = 0; |
| 941 | xmlCharEncodingAliasesMax = 0; |
| 942 | xmlFree(xmlCharEncodingAliases); |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * xmlGetEncodingAlias: |
| 947 | * @alias: the alias name as parsed, in UTF-8 format (ASCII actually) |
| 948 | * |
| 949 | * Lookup an encoding name for the given alias. |
| 950 | * |
| 951 | * Returns NULL if not found the original name otherwise |
| 952 | */ |
| 953 | const char * |
| 954 | xmlGetEncodingAlias(const char *alias) { |
| 955 | int i; |
| 956 | char upper[100]; |
| 957 | |
| 958 | if (alias == NULL) |
| 959 | return(NULL); |
| 960 | |
| 961 | if (xmlCharEncodingAliases == NULL) |
| 962 | return(NULL); |
| 963 | |
| 964 | for (i = 0;i < 99;i++) { |
| 965 | upper[i] = toupper(alias[i]); |
| 966 | if (upper[i] == 0) break; |
| 967 | } |
| 968 | upper[i] = 0; |
| 969 | |
| 970 | /* |
| 971 | * Walk down the list looking for a definition of the alias |
| 972 | */ |
| 973 | for (i = 0;i < xmlCharEncodingAliasesNb;i++) { |
| 974 | if (!strcmp(xmlCharEncodingAliases[i].alias, upper)) { |
| 975 | return(xmlCharEncodingAliases[i].name); |
| 976 | } |
| 977 | } |
| 978 | return(NULL); |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * xmlAddEncodingAlias: |
| 983 | * @name: the encoding name as parsed, in UTF-8 format (ASCII actually) |
| 984 | * @alias: the alias name as parsed, in UTF-8 format (ASCII actually) |
| 985 | * |
| 986 | * Registers and alias @alias for an encoding named @name. Existing alias |
| 987 | * will be overwritten. |
| 988 | * |
| 989 | * Returns 0 in case of success, -1 in case of error |
| 990 | */ |
| 991 | int |
| 992 | xmlAddEncodingAlias(const char *name, const char *alias) { |
| 993 | int i; |
| 994 | char upper[100]; |
| 995 | |
| 996 | if ((name == NULL) || (alias == NULL)) |
| 997 | return(-1); |
| 998 | |
| 999 | for (i = 0;i < 99;i++) { |
| 1000 | upper[i] = toupper(alias[i]); |
| 1001 | if (upper[i] == 0) break; |
| 1002 | } |
| 1003 | upper[i] = 0; |
| 1004 | |
| 1005 | if (xmlCharEncodingAliases == NULL) { |
| 1006 | xmlCharEncodingAliasesNb = 0; |
| 1007 | xmlCharEncodingAliasesMax = 20; |
| 1008 | xmlCharEncodingAliases = (xmlCharEncodingAliasPtr) |
| 1009 | xmlMalloc(xmlCharEncodingAliasesMax * sizeof(xmlCharEncodingAlias)); |
| 1010 | if (xmlCharEncodingAliases == NULL) |
| 1011 | return(-1); |
| 1012 | } else if (xmlCharEncodingAliasesNb >= xmlCharEncodingAliasesMax) { |
| 1013 | xmlCharEncodingAliasesMax *= 2; |
| 1014 | xmlCharEncodingAliases = (xmlCharEncodingAliasPtr) |
| 1015 | xmlRealloc(xmlCharEncodingAliases, |
| 1016 | xmlCharEncodingAliasesMax * sizeof(xmlCharEncodingAlias)); |
| 1017 | } |
| 1018 | /* |
| 1019 | * Walk down the list looking for a definition of the alias |
| 1020 | */ |
| 1021 | for (i = 0;i < xmlCharEncodingAliasesNb;i++) { |
| 1022 | if (!strcmp(xmlCharEncodingAliases[i].alias, upper)) { |
| 1023 | /* |
| 1024 | * Replace the definition. |
| 1025 | */ |
| 1026 | xmlFree((char *) xmlCharEncodingAliases[i].name); |
| 1027 | xmlCharEncodingAliases[i].name = xmlMemStrdup(name); |
| 1028 | return(0); |
| 1029 | } |
| 1030 | } |
| 1031 | /* |
| 1032 | * Add the definition |
| 1033 | */ |
| 1034 | xmlCharEncodingAliases[xmlCharEncodingAliasesNb].name = xmlMemStrdup(name); |
| 1035 | xmlCharEncodingAliases[xmlCharEncodingAliasesNb].alias = xmlMemStrdup(upper); |
| 1036 | xmlCharEncodingAliasesNb++; |
| 1037 | return(0); |
| 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * xmlDelEncodingAlias: |
| 1042 | * @alias: the alias name as parsed, in UTF-8 format (ASCII actually) |
| 1043 | * |
| 1044 | * Unregisters an encoding alias @alias |
| 1045 | * |
| 1046 | * Returns 0 in case of success, -1 in case of error |
| 1047 | */ |
| 1048 | int |
| 1049 | xmlDelEncodingAlias(const char *alias) { |
| 1050 | int i; |
| 1051 | |
| 1052 | if (alias == NULL) |
| 1053 | return(-1); |
| 1054 | |
| 1055 | if (xmlCharEncodingAliases == NULL) |
| 1056 | return(-1); |
| 1057 | /* |
| 1058 | * Walk down the list looking for a definition of the alias |
| 1059 | */ |
| 1060 | for (i = 0;i < xmlCharEncodingAliasesNb;i++) { |
| 1061 | if (!strcmp(xmlCharEncodingAliases[i].alias, alias)) { |
| 1062 | xmlFree((char *) xmlCharEncodingAliases[i].name); |
| 1063 | xmlFree((char *) xmlCharEncodingAliases[i].alias); |
| 1064 | xmlCharEncodingAliasesNb--; |
| 1065 | memmove(&xmlCharEncodingAliases[i], &xmlCharEncodingAliases[i + 1], |
| 1066 | sizeof(xmlCharEncodingAlias) * (xmlCharEncodingAliasesNb - i)); |
| 1067 | return(0); |
| 1068 | } |
| 1069 | } |
| 1070 | return(-1); |
| 1071 | } |
| 1072 | |
| 1073 | /** |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1074 | * xmlParseCharEncoding: |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1075 | * @name: the encoding name as parsed, in UTF-8 format (ASCII actually) |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1076 | * |
| 1077 | * Conpare the string to the known encoding schemes already known. Note |
| 1078 | * that the comparison is case insensitive accordingly to the section |
| 1079 | * [XML] 4.3.3 Character Encoding in Entities. |
| 1080 | * |
| 1081 | * Returns one of the XML_CHAR_ENCODING_... values or XML_CHAR_ENCODING_NONE |
| 1082 | * if not recognized. |
| 1083 | */ |
| 1084 | xmlCharEncoding |
Daniel Veillard | 011b63c | 1999-06-02 17:44:04 +0000 | [diff] [blame] | 1085 | xmlParseCharEncoding(const char* name) |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1086 | { |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1087 | const char *alias; |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1088 | char upper[500]; |
| 1089 | int i; |
| 1090 | |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1091 | if (name == NULL) |
| 1092 | return(XML_CHAR_ENCODING_NONE); |
| 1093 | |
| 1094 | /* |
| 1095 | * Do the alias resolution |
| 1096 | */ |
| 1097 | alias = xmlGetEncodingAlias(name); |
| 1098 | if (alias != NULL) |
| 1099 | name = alias; |
| 1100 | |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1101 | for (i = 0;i < 499;i++) { |
| 1102 | upper[i] = toupper(name[i]); |
| 1103 | if (upper[i] == 0) break; |
| 1104 | } |
| 1105 | upper[i] = 0; |
| 1106 | |
| 1107 | if (!strcmp(upper, "")) return(XML_CHAR_ENCODING_NONE); |
| 1108 | if (!strcmp(upper, "UTF-8")) return(XML_CHAR_ENCODING_UTF8); |
| 1109 | if (!strcmp(upper, "UTF8")) return(XML_CHAR_ENCODING_UTF8); |
| 1110 | |
| 1111 | /* |
| 1112 | * NOTE: if we were able to parse this, the endianness of UTF16 is |
| 1113 | * already found and in use |
| 1114 | */ |
| 1115 | if (!strcmp(upper, "UTF-16")) return(XML_CHAR_ENCODING_UTF16LE); |
| 1116 | if (!strcmp(upper, "UTF16")) return(XML_CHAR_ENCODING_UTF16LE); |
| 1117 | |
| 1118 | if (!strcmp(upper, "ISO-10646-UCS-2")) return(XML_CHAR_ENCODING_UCS2); |
| 1119 | if (!strcmp(upper, "UCS-2")) return(XML_CHAR_ENCODING_UCS2); |
| 1120 | if (!strcmp(upper, "UCS2")) return(XML_CHAR_ENCODING_UCS2); |
| 1121 | |
| 1122 | /* |
| 1123 | * NOTE: if we were able to parse this, the endianness of UCS4 is |
| 1124 | * already found and in use |
| 1125 | */ |
| 1126 | if (!strcmp(upper, "ISO-10646-UCS-4")) return(XML_CHAR_ENCODING_UCS4LE); |
| 1127 | if (!strcmp(upper, "UCS-4")) return(XML_CHAR_ENCODING_UCS4LE); |
| 1128 | if (!strcmp(upper, "UCS4")) return(XML_CHAR_ENCODING_UCS4LE); |
| 1129 | |
| 1130 | |
| 1131 | if (!strcmp(upper, "ISO-8859-1")) return(XML_CHAR_ENCODING_8859_1); |
| 1132 | if (!strcmp(upper, "ISO-LATIN-1")) return(XML_CHAR_ENCODING_8859_1); |
| 1133 | if (!strcmp(upper, "ISO LATIN 1")) return(XML_CHAR_ENCODING_8859_1); |
| 1134 | |
| 1135 | if (!strcmp(upper, "ISO-8859-2")) return(XML_CHAR_ENCODING_8859_2); |
| 1136 | if (!strcmp(upper, "ISO-LATIN-2")) return(XML_CHAR_ENCODING_8859_2); |
| 1137 | if (!strcmp(upper, "ISO LATIN 2")) return(XML_CHAR_ENCODING_8859_2); |
| 1138 | |
| 1139 | if (!strcmp(upper, "ISO-8859-3")) return(XML_CHAR_ENCODING_8859_3); |
| 1140 | if (!strcmp(upper, "ISO-8859-4")) return(XML_CHAR_ENCODING_8859_4); |
| 1141 | if (!strcmp(upper, "ISO-8859-5")) return(XML_CHAR_ENCODING_8859_5); |
| 1142 | if (!strcmp(upper, "ISO-8859-6")) return(XML_CHAR_ENCODING_8859_6); |
| 1143 | if (!strcmp(upper, "ISO-8859-7")) return(XML_CHAR_ENCODING_8859_7); |
| 1144 | if (!strcmp(upper, "ISO-8859-8")) return(XML_CHAR_ENCODING_8859_8); |
| 1145 | if (!strcmp(upper, "ISO-8859-9")) return(XML_CHAR_ENCODING_8859_9); |
| 1146 | |
| 1147 | if (!strcmp(upper, "ISO-2022-JP")) return(XML_CHAR_ENCODING_2022_JP); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1148 | if (!strcmp(upper, "SHIFT_JIS")) return(XML_CHAR_ENCODING_SHIFT_JIS); |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1149 | if (!strcmp(upper, "EUC-JP")) return(XML_CHAR_ENCODING_EUC_JP); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1150 | |
| 1151 | #ifdef DEBUG_ENCODING |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1152 | xmlGenericError(xmlGenericErrorContext, "Unknown encoding %s\n", name); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1153 | #endif |
Daniel Veillard | 27d8874 | 1999-05-29 11:51:49 +0000 | [diff] [blame] | 1154 | return(XML_CHAR_ENCODING_ERROR); |
| 1155 | } |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1156 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1157 | /** |
| 1158 | * xmlGetCharEncodingName: |
| 1159 | * @enc: the encoding |
| 1160 | * |
| 1161 | * The "canonical" name for XML encoding. |
| 1162 | * C.f. http://www.w3.org/TR/REC-xml#charencoding |
| 1163 | * Section 4.3.3 Character Encoding in Entities |
| 1164 | * |
| 1165 | * Returns the canonical name for the given encoding |
| 1166 | */ |
| 1167 | |
| 1168 | const char* |
| 1169 | xmlGetCharEncodingName(xmlCharEncoding enc) { |
| 1170 | switch (enc) { |
| 1171 | case XML_CHAR_ENCODING_ERROR: |
| 1172 | return(NULL); |
| 1173 | case XML_CHAR_ENCODING_NONE: |
| 1174 | return(NULL); |
| 1175 | case XML_CHAR_ENCODING_UTF8: |
| 1176 | return("UTF-8"); |
| 1177 | case XML_CHAR_ENCODING_UTF16LE: |
| 1178 | return("UTF-16"); |
| 1179 | case XML_CHAR_ENCODING_UTF16BE: |
| 1180 | return("UTF-16"); |
| 1181 | case XML_CHAR_ENCODING_EBCDIC: |
| 1182 | return("EBCDIC"); |
| 1183 | case XML_CHAR_ENCODING_UCS4LE: |
| 1184 | return("ISO-10646-UCS-4"); |
| 1185 | case XML_CHAR_ENCODING_UCS4BE: |
| 1186 | return("ISO-10646-UCS-4"); |
| 1187 | case XML_CHAR_ENCODING_UCS4_2143: |
| 1188 | return("ISO-10646-UCS-4"); |
| 1189 | case XML_CHAR_ENCODING_UCS4_3412: |
| 1190 | return("ISO-10646-UCS-4"); |
| 1191 | case XML_CHAR_ENCODING_UCS2: |
| 1192 | return("ISO-10646-UCS-2"); |
| 1193 | case XML_CHAR_ENCODING_8859_1: |
| 1194 | return("ISO-8859-1"); |
| 1195 | case XML_CHAR_ENCODING_8859_2: |
| 1196 | return("ISO-8859-2"); |
| 1197 | case XML_CHAR_ENCODING_8859_3: |
| 1198 | return("ISO-8859-3"); |
| 1199 | case XML_CHAR_ENCODING_8859_4: |
| 1200 | return("ISO-8859-4"); |
| 1201 | case XML_CHAR_ENCODING_8859_5: |
| 1202 | return("ISO-8859-5"); |
| 1203 | case XML_CHAR_ENCODING_8859_6: |
| 1204 | return("ISO-8859-6"); |
| 1205 | case XML_CHAR_ENCODING_8859_7: |
| 1206 | return("ISO-8859-7"); |
| 1207 | case XML_CHAR_ENCODING_8859_8: |
| 1208 | return("ISO-8859-8"); |
| 1209 | case XML_CHAR_ENCODING_8859_9: |
| 1210 | return("ISO-8859-9"); |
| 1211 | case XML_CHAR_ENCODING_2022_JP: |
| 1212 | return("ISO-2022-JP"); |
| 1213 | case XML_CHAR_ENCODING_SHIFT_JIS: |
| 1214 | return("Shift-JIS"); |
| 1215 | case XML_CHAR_ENCODING_EUC_JP: |
| 1216 | return("EUC-JP"); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 1217 | case XML_CHAR_ENCODING_ASCII: |
| 1218 | return(NULL); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1219 | } |
| 1220 | return(NULL); |
| 1221 | } |
| 1222 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1223 | /**************************************************************** |
| 1224 | * * |
| 1225 | * Char encoding handlers * |
| 1226 | * * |
| 1227 | ****************************************************************/ |
| 1228 | |
| 1229 | /* the size should be growable, but it's not a big deal ... */ |
| 1230 | #define MAX_ENCODING_HANDLERS 50 |
| 1231 | static xmlCharEncodingHandlerPtr *handlers = NULL; |
| 1232 | static int nbCharEncodingHandler = 0; |
| 1233 | |
| 1234 | /* |
| 1235 | * The default is UTF-8 for XML, that's also the default used for the |
| 1236 | * parser internals, so the default encoding handler is NULL |
| 1237 | */ |
| 1238 | |
| 1239 | static xmlCharEncodingHandlerPtr xmlDefaultCharEncodingHandler = NULL; |
| 1240 | |
| 1241 | /** |
| 1242 | * xmlNewCharEncodingHandler: |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1243 | * @name: the encoding name, in UTF-8 format (ASCII actually) |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1244 | * @input: the xmlCharEncodingInputFunc to read that encoding |
| 1245 | * @output: the xmlCharEncodingOutputFunc to write that encoding |
| 1246 | * |
| 1247 | * Create and registers an xmlCharEncodingHandler. |
| 1248 | * Returns the xmlCharEncodingHandlerPtr created (or NULL in case of error). |
| 1249 | */ |
| 1250 | xmlCharEncodingHandlerPtr |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1251 | xmlNewCharEncodingHandler(const char *name, |
| 1252 | xmlCharEncodingInputFunc input, |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1253 | xmlCharEncodingOutputFunc output) { |
| 1254 | xmlCharEncodingHandlerPtr handler; |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1255 | const char *alias; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1256 | char upper[500]; |
| 1257 | int i; |
| 1258 | char *up = 0; |
| 1259 | |
| 1260 | /* |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1261 | * Do the alias resolution |
| 1262 | */ |
| 1263 | alias = xmlGetEncodingAlias(name); |
| 1264 | if (alias != NULL) |
| 1265 | name = alias; |
| 1266 | |
| 1267 | /* |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1268 | * Keep only the uppercase version of the encoding. |
| 1269 | */ |
| 1270 | if (name == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1271 | xmlGenericError(xmlGenericErrorContext, |
| 1272 | "xmlNewCharEncodingHandler : no name !\n"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1273 | return(NULL); |
| 1274 | } |
| 1275 | for (i = 0;i < 499;i++) { |
| 1276 | upper[i] = toupper(name[i]); |
| 1277 | if (upper[i] == 0) break; |
| 1278 | } |
| 1279 | upper[i] = 0; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1280 | up = xmlMemStrdup(upper); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1281 | if (up == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1282 | xmlGenericError(xmlGenericErrorContext, |
| 1283 | "xmlNewCharEncodingHandler : out of memory !\n"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1284 | return(NULL); |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * allocate and fill-up an handler block. |
| 1289 | */ |
| 1290 | handler = (xmlCharEncodingHandlerPtr) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1291 | xmlMalloc(sizeof(xmlCharEncodingHandler)); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1292 | if (handler == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1293 | xmlGenericError(xmlGenericErrorContext, |
| 1294 | "xmlNewCharEncodingHandler : out of memory !\n"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1295 | return(NULL); |
| 1296 | } |
| 1297 | handler->input = input; |
| 1298 | handler->output = output; |
| 1299 | handler->name = up; |
| 1300 | |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 1301 | #ifdef LIBXML_ICONV_ENABLED |
| 1302 | handler->iconv_in = NULL; |
| 1303 | handler->iconv_out = NULL; |
| 1304 | #endif /* LIBXML_ICONV_ENABLED */ |
| 1305 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1306 | /* |
| 1307 | * registers and returns the handler. |
| 1308 | */ |
| 1309 | xmlRegisterCharEncodingHandler(handler); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1310 | #ifdef DEBUG_ENCODING |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1311 | xmlGenericError(xmlGenericErrorContext, |
| 1312 | "Registered encoding handler for %s\n", name); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1313 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1314 | return(handler); |
| 1315 | } |
| 1316 | |
| 1317 | /** |
| 1318 | * xmlInitCharEncodingHandlers: |
| 1319 | * |
| 1320 | * Initialize the char encoding support, it registers the default |
| 1321 | * encoding supported. |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1322 | * NOTE: while public, this function usually doesn't need to be called |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1323 | * in normal processing. |
| 1324 | */ |
| 1325 | void |
| 1326 | xmlInitCharEncodingHandlers(void) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1327 | unsigned short int tst = 0x1234; |
| 1328 | unsigned char *ptr = (unsigned char *) &tst; |
| 1329 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1330 | if (handlers != NULL) return; |
| 1331 | |
| 1332 | handlers = (xmlCharEncodingHandlerPtr *) |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1333 | xmlMalloc(MAX_ENCODING_HANDLERS * sizeof(xmlCharEncodingHandlerPtr)); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1334 | |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1335 | if (*ptr == 0x12) xmlLittleEndian = 0; |
| 1336 | else if (*ptr == 0x34) xmlLittleEndian = 1; |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1337 | else xmlGenericError(xmlGenericErrorContext, |
| 1338 | "Odd problem at endianness detection\n"); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1339 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1340 | if (handlers == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1341 | xmlGenericError(xmlGenericErrorContext, |
| 1342 | "xmlInitCharEncodingHandlers : out of memory !\n"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1343 | return; |
| 1344 | } |
| 1345 | xmlNewCharEncodingHandler("UTF-8", NULL, NULL); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1346 | xmlUTF16LEHandler = |
| 1347 | xmlNewCharEncodingHandler("UTF-16LE", UTF16LEToUTF8, UTF8ToUTF16LE); |
| 1348 | xmlUTF16BEHandler = |
| 1349 | xmlNewCharEncodingHandler("UTF-16BE", UTF16BEToUTF8, UTF8ToUTF16BE); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1350 | xmlNewCharEncodingHandler("ISO-8859-1", isolat1ToUTF8, UTF8Toisolat1); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1351 | xmlNewCharEncodingHandler("ASCII", asciiToUTF8, UTF8Toascii); |
| 1352 | #ifdef LIBXML_HTML_ENABLED |
| 1353 | xmlNewCharEncodingHandler("HTML", NULL, UTF8ToHtml); |
| 1354 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | /** |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 1358 | * xmlCleanupCharEncodingHandlers: |
| 1359 | * |
| 1360 | * Cleanup the memory allocated for the char encoding support, it |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1361 | * unregisters all the encoding handlers and the aliases. |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 1362 | */ |
| 1363 | void |
| 1364 | xmlCleanupCharEncodingHandlers(void) { |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1365 | xmlCleanupEncodingAliases(); |
| 1366 | |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 1367 | if (handlers == NULL) return; |
| 1368 | |
| 1369 | for (;nbCharEncodingHandler > 0;) { |
| 1370 | nbCharEncodingHandler--; |
| 1371 | if (handlers[nbCharEncodingHandler] != NULL) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1372 | if (handlers[nbCharEncodingHandler]->name != NULL) |
| 1373 | xmlFree(handlers[nbCharEncodingHandler]->name); |
Daniel Veillard | a819dac | 1999-11-24 18:04:22 +0000 | [diff] [blame] | 1374 | xmlFree(handlers[nbCharEncodingHandler]); |
| 1375 | } |
| 1376 | } |
| 1377 | xmlFree(handlers); |
| 1378 | handlers = NULL; |
| 1379 | nbCharEncodingHandler = 0; |
| 1380 | xmlDefaultCharEncodingHandler = NULL; |
| 1381 | } |
| 1382 | |
| 1383 | /** |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1384 | * xmlRegisterCharEncodingHandler: |
| 1385 | * @handler: the xmlCharEncodingHandlerPtr handler block |
| 1386 | * |
| 1387 | * Register the char encoding handler, surprizing, isn't it ? |
| 1388 | */ |
| 1389 | void |
| 1390 | xmlRegisterCharEncodingHandler(xmlCharEncodingHandlerPtr handler) { |
| 1391 | if (handlers == NULL) xmlInitCharEncodingHandlers(); |
| 1392 | if (handler == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1393 | xmlGenericError(xmlGenericErrorContext, |
| 1394 | "xmlRegisterCharEncodingHandler: NULL handler !\n"); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1395 | return; |
| 1396 | } |
| 1397 | |
| 1398 | if (nbCharEncodingHandler >= MAX_ENCODING_HANDLERS) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1399 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1400 | "xmlRegisterCharEncodingHandler: Too many handler registered\n"); |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1401 | xmlGenericError(xmlGenericErrorContext, |
| 1402 | "\tincrease MAX_ENCODING_HANDLERS : %s\n", __FILE__); |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1403 | return; |
| 1404 | } |
| 1405 | handlers[nbCharEncodingHandler++] = handler; |
| 1406 | } |
| 1407 | |
| 1408 | /** |
| 1409 | * xmlGetCharEncodingHandler: |
| 1410 | * @enc: an xmlCharEncoding value. |
| 1411 | * |
| 1412 | * Search in the registrered set the handler able to read/write that encoding. |
| 1413 | * |
| 1414 | * Returns the handler or NULL if not found |
| 1415 | */ |
| 1416 | xmlCharEncodingHandlerPtr |
| 1417 | xmlGetCharEncodingHandler(xmlCharEncoding enc) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1418 | xmlCharEncodingHandlerPtr handler; |
| 1419 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1420 | if (handlers == NULL) xmlInitCharEncodingHandlers(); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1421 | switch (enc) { |
| 1422 | case XML_CHAR_ENCODING_ERROR: |
| 1423 | return(NULL); |
| 1424 | case XML_CHAR_ENCODING_NONE: |
| 1425 | return(NULL); |
| 1426 | case XML_CHAR_ENCODING_UTF8: |
| 1427 | return(NULL); |
| 1428 | case XML_CHAR_ENCODING_UTF16LE: |
| 1429 | return(xmlUTF16LEHandler); |
| 1430 | case XML_CHAR_ENCODING_UTF16BE: |
| 1431 | return(xmlUTF16BEHandler); |
| 1432 | case XML_CHAR_ENCODING_EBCDIC: |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1433 | handler = xmlFindCharEncodingHandler("EBCDIC"); |
| 1434 | if (handler != NULL) return(handler); |
| 1435 | handler = xmlFindCharEncodingHandler("ebcdic"); |
| 1436 | if (handler != NULL) return(handler); |
| 1437 | break; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1438 | case XML_CHAR_ENCODING_UCS4BE: |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1439 | handler = xmlFindCharEncodingHandler("ISO-10646-UCS-4"); |
| 1440 | if (handler != NULL) return(handler); |
| 1441 | handler = xmlFindCharEncodingHandler("UCS-4"); |
| 1442 | if (handler != NULL) return(handler); |
| 1443 | handler = xmlFindCharEncodingHandler("UCS4"); |
| 1444 | if (handler != NULL) return(handler); |
| 1445 | break; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1446 | case XML_CHAR_ENCODING_UCS4LE: |
| 1447 | handler = xmlFindCharEncodingHandler("ISO-10646-UCS-4"); |
| 1448 | if (handler != NULL) return(handler); |
| 1449 | handler = xmlFindCharEncodingHandler("UCS-4"); |
| 1450 | if (handler != NULL) return(handler); |
| 1451 | handler = xmlFindCharEncodingHandler("UCS4"); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1452 | if (handler != NULL) return(handler); |
| 1453 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1454 | case XML_CHAR_ENCODING_UCS4_2143: |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1455 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1456 | case XML_CHAR_ENCODING_UCS4_3412: |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1457 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1458 | case XML_CHAR_ENCODING_UCS2: |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1459 | handler = xmlFindCharEncodingHandler("ISO-10646-UCS-2"); |
| 1460 | if (handler != NULL) return(handler); |
| 1461 | handler = xmlFindCharEncodingHandler("UCS-2"); |
| 1462 | if (handler != NULL) return(handler); |
| 1463 | handler = xmlFindCharEncodingHandler("UCS2"); |
| 1464 | if (handler != NULL) return(handler); |
| 1465 | break; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1466 | |
| 1467 | /* |
| 1468 | * We used to keep ISO Latin encodings native in the |
| 1469 | * generated data. This led to so many problems that |
| 1470 | * this has been removed. One can still change this |
| 1471 | * back by registering no-ops encoders for those |
| 1472 | */ |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1473 | case XML_CHAR_ENCODING_8859_1: |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1474 | handler = xmlFindCharEncodingHandler("ISO-8859-1"); |
| 1475 | if (handler != NULL) return(handler); |
| 1476 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1477 | case XML_CHAR_ENCODING_8859_2: |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1478 | handler = xmlFindCharEncodingHandler("ISO-8859-2"); |
| 1479 | if (handler != NULL) return(handler); |
| 1480 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1481 | case XML_CHAR_ENCODING_8859_3: |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1482 | handler = xmlFindCharEncodingHandler("ISO-8859-3"); |
| 1483 | if (handler != NULL) return(handler); |
| 1484 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1485 | case XML_CHAR_ENCODING_8859_4: |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1486 | handler = xmlFindCharEncodingHandler("ISO-8859-4"); |
| 1487 | if (handler != NULL) return(handler); |
| 1488 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1489 | case XML_CHAR_ENCODING_8859_5: |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1490 | handler = xmlFindCharEncodingHandler("ISO-8859-5"); |
| 1491 | if (handler != NULL) return(handler); |
| 1492 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1493 | case XML_CHAR_ENCODING_8859_6: |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1494 | handler = xmlFindCharEncodingHandler("ISO-8859-6"); |
| 1495 | if (handler != NULL) return(handler); |
| 1496 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1497 | case XML_CHAR_ENCODING_8859_7: |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1498 | handler = xmlFindCharEncodingHandler("ISO-8859-7"); |
| 1499 | if (handler != NULL) return(handler); |
| 1500 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1501 | case XML_CHAR_ENCODING_8859_8: |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1502 | handler = xmlFindCharEncodingHandler("ISO-8859-8"); |
| 1503 | if (handler != NULL) return(handler); |
| 1504 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1505 | case XML_CHAR_ENCODING_8859_9: |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1506 | handler = xmlFindCharEncodingHandler("ISO-8859-9"); |
| 1507 | if (handler != NULL) return(handler); |
| 1508 | break; |
| 1509 | |
| 1510 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1511 | case XML_CHAR_ENCODING_2022_JP: |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1512 | handler = xmlFindCharEncodingHandler("ISO-2022-JP"); |
| 1513 | if (handler != NULL) return(handler); |
| 1514 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1515 | case XML_CHAR_ENCODING_SHIFT_JIS: |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1516 | handler = xmlFindCharEncodingHandler("SHIFT-JIS"); |
| 1517 | if (handler != NULL) return(handler); |
| 1518 | handler = xmlFindCharEncodingHandler("SHIFT_JIS"); |
| 1519 | if (handler != NULL) return(handler); |
| 1520 | handler = xmlFindCharEncodingHandler("Shift_JIS"); |
| 1521 | if (handler != NULL) return(handler); |
| 1522 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1523 | case XML_CHAR_ENCODING_EUC_JP: |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1524 | handler = xmlFindCharEncodingHandler("EUC-JP"); |
| 1525 | if (handler != NULL) return(handler); |
| 1526 | break; |
| 1527 | default: |
| 1528 | break; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1529 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1530 | |
| 1531 | #ifdef DEBUG_ENCODING |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1532 | xmlGenericError(xmlGenericErrorContext, |
| 1533 | "No handler found for encoding %d\n", enc); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1534 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1535 | return(NULL); |
| 1536 | } |
| 1537 | |
| 1538 | /** |
| 1539 | * xmlGetCharEncodingHandler: |
| 1540 | * @enc: a string describing the char encoding. |
| 1541 | * |
| 1542 | * Search in the registrered set the handler able to read/write that encoding. |
| 1543 | * |
| 1544 | * Returns the handler or NULL if not found |
| 1545 | */ |
| 1546 | xmlCharEncodingHandlerPtr |
| 1547 | xmlFindCharEncodingHandler(const char *name) { |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1548 | const char *nalias; |
| 1549 | const char *norig; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1550 | xmlCharEncoding alias; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1551 | #ifdef LIBXML_ICONV_ENABLED |
Daniel Veillard | 3f6f7f6 | 2000-06-30 17:58:25 +0000 | [diff] [blame] | 1552 | xmlCharEncodingHandlerPtr enc; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1553 | iconv_t icv_in, icv_out; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1554 | #endif /* LIBXML_ICONV_ENABLED */ |
| 1555 | char upper[100]; |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1556 | int i; |
| 1557 | |
| 1558 | if (handlers == NULL) xmlInitCharEncodingHandlers(); |
| 1559 | if (name == NULL) return(xmlDefaultCharEncodingHandler); |
| 1560 | if (name[0] == 0) return(xmlDefaultCharEncodingHandler); |
| 1561 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1562 | /* |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1563 | * Do the alias resolution |
| 1564 | */ |
| 1565 | norig = name; |
| 1566 | nalias = xmlGetEncodingAlias(name); |
| 1567 | if (nalias != NULL) |
| 1568 | name = nalias; |
| 1569 | |
| 1570 | /* |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1571 | * Check first for directly registered encoding names |
| 1572 | */ |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1573 | for (i = 0;i < 99;i++) { |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1574 | upper[i] = toupper(name[i]); |
| 1575 | if (upper[i] == 0) break; |
| 1576 | } |
| 1577 | upper[i] = 0; |
| 1578 | |
| 1579 | for (i = 0;i < nbCharEncodingHandler; i++) |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1580 | if (!strcmp(upper, handlers[i]->name)) { |
| 1581 | #ifdef DEBUG_ENCODING |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1582 | xmlGenericError(xmlGenericErrorContext, |
| 1583 | "Found registered handler for encoding %s\n", name); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1584 | #endif |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1585 | return(handlers[i]); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1586 | } |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1587 | |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1588 | #ifdef LIBXML_ICONV_ENABLED |
| 1589 | /* check whether iconv can handle this */ |
| 1590 | icv_in = iconv_open("UTF-8", name); |
| 1591 | icv_out = iconv_open(name, "UTF-8"); |
| 1592 | if ((icv_in != (iconv_t) -1) && (icv_out != (iconv_t) -1)) { |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1593 | enc = (xmlCharEncodingHandlerPtr) |
| 1594 | xmlMalloc(sizeof(xmlCharEncodingHandler)); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1595 | if (enc == NULL) { |
| 1596 | iconv_close(icv_in); |
| 1597 | iconv_close(icv_out); |
| 1598 | return(NULL); |
| 1599 | } |
Daniel Veillard | 365e13b | 2000-07-02 07:56:37 +0000 | [diff] [blame] | 1600 | enc->name = xmlMemStrdup(name); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1601 | enc->input = NULL; |
| 1602 | enc->output = NULL; |
| 1603 | enc->iconv_in = icv_in; |
| 1604 | enc->iconv_out = icv_out; |
| 1605 | #ifdef DEBUG_ENCODING |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1606 | xmlGenericError(xmlGenericErrorContext, |
| 1607 | "Found iconv handler for encoding %s\n", name); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1608 | #endif |
| 1609 | return enc; |
| 1610 | } else if ((icv_in != (iconv_t) -1) || icv_out != (iconv_t) -1) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1611 | xmlGenericError(xmlGenericErrorContext, |
| 1612 | "iconv : problems with filters for '%s'\n", name); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1613 | } |
| 1614 | #endif /* LIBXML_ICONV_ENABLED */ |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1615 | |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1616 | #ifdef DEBUG_ENCODING |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1617 | xmlGenericError(xmlGenericErrorContext, |
| 1618 | "No handler found for encoding %s\n", name); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1619 | #endif |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1620 | |
| 1621 | /* |
| 1622 | * Fallback using the canonical names |
| 1623 | */ |
Daniel Veillard | f0cc7cc | 2000-08-26 21:40:43 +0000 | [diff] [blame] | 1624 | alias = xmlParseCharEncoding(norig); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1625 | if (alias != XML_CHAR_ENCODING_ERROR) { |
| 1626 | const char* canon; |
| 1627 | canon = xmlGetCharEncodingName(alias); |
| 1628 | if ((canon != NULL) && (strcmp(name, canon))) { |
| 1629 | return(xmlFindCharEncodingHandler(canon)); |
| 1630 | } |
| 1631 | } |
| 1632 | |
Daniel Veillard | 14fff06 | 1999-06-22 21:49:07 +0000 | [diff] [blame] | 1633 | return(NULL); |
| 1634 | } |
| 1635 | |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1636 | #ifdef LIBXML_ICONV_ENABLED |
| 1637 | /** |
| 1638 | * xmlIconvWrapper: |
| 1639 | * @cd: iconv converter data structure |
| 1640 | * @out: a pointer to an array of bytes to store the result |
| 1641 | * @outlen: the length of @out |
| 1642 | * @in: a pointer to an array of ISO Latin 1 chars |
| 1643 | * @inlen: the length of @in |
| 1644 | * |
| 1645 | * Returns 0 if success, or |
| 1646 | * -1 by lack of space, or |
| 1647 | * -2 if the transcoding fails (for *in is not valid utf8 string or |
| 1648 | * the result of transformation can't fit into the encoding we want), or |
| 1649 | * -3 if there the last byte can't form a single output char. |
| 1650 | * |
| 1651 | * The value of @inlen after return is the number of octets consumed |
| 1652 | * as the return value is positive, else unpredictiable. |
| 1653 | * The value of @outlen after return is the number of ocetes consumed. |
| 1654 | */ |
| 1655 | static int |
| 1656 | xmlIconvWrapper(iconv_t cd, |
| 1657 | unsigned char *out, int *outlen, |
| 1658 | const unsigned char *in, int *inlen) { |
| 1659 | |
| 1660 | size_t icv_inlen = *inlen, icv_outlen = *outlen; |
| 1661 | const char *icv_in = (const char *) in; |
| 1662 | char *icv_out = (char *) out; |
| 1663 | int ret; |
| 1664 | |
| 1665 | ret = iconv(cd, |
| 1666 | &icv_in, &icv_inlen, |
| 1667 | &icv_out, &icv_outlen); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1668 | if (in != NULL) { |
| 1669 | *inlen -= icv_inlen; |
| 1670 | *outlen -= icv_outlen; |
| 1671 | } else { |
| 1672 | *inlen = 0; |
| 1673 | *outlen = 0; |
| 1674 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1675 | if (icv_inlen != 0 || ret == (size_t) -1) { |
| 1676 | #ifdef EILSEQ |
| 1677 | if (errno == EILSEQ) { |
| 1678 | return -2; |
| 1679 | } else |
| 1680 | #endif |
| 1681 | #ifdef E2BIG |
| 1682 | if (errno == E2BIG) { |
| 1683 | return -1; |
| 1684 | } else |
| 1685 | #endif |
| 1686 | #ifdef EINVAL |
| 1687 | if (errno == EINVAL) { |
| 1688 | return -3; |
| 1689 | } |
| 1690 | #endif |
| 1691 | else { |
| 1692 | return -3; |
| 1693 | } |
| 1694 | } |
| 1695 | return 0; |
| 1696 | } |
| 1697 | #endif /* LIBXML_ICONV_ENABLED */ |
| 1698 | |
| 1699 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1700 | * xmlCharEncFirstLine: |
| 1701 | * @handler: char enconding transformation data structure |
| 1702 | * @out: an xmlBuffer for the output. |
| 1703 | * @in: an xmlBuffer for the input |
| 1704 | * |
| 1705 | * Front-end for the encoding handler input function, but handle only |
| 1706 | * the very first line, i.e. limit itself to 45 chars. |
| 1707 | * |
| 1708 | * Returns the number of byte written if success, or |
| 1709 | * -1 general error |
| 1710 | * -2 if the transcoding fails (for *in is not valid utf8 string or |
| 1711 | * the result of transformation can't fit into the encoding we want), or |
| 1712 | */ |
| 1713 | int |
| 1714 | xmlCharEncFirstLine(xmlCharEncodingHandler *handler, xmlBufferPtr out, |
| 1715 | xmlBufferPtr in) { |
| 1716 | int ret = -2; |
| 1717 | int written; |
| 1718 | int toconv; |
| 1719 | |
| 1720 | if (handler == NULL) return(-1); |
| 1721 | if (out == NULL) return(-1); |
| 1722 | if (in == NULL) return(-1); |
| 1723 | |
| 1724 | written = out->size - out->use; |
| 1725 | toconv = in->use; |
| 1726 | if (toconv * 2 >= written) { |
| 1727 | xmlBufferGrow(out, toconv); |
| 1728 | written = out->size - out->use - 1; |
| 1729 | } |
| 1730 | |
| 1731 | /* |
| 1732 | * echo '<?xml version="1.0" encoding="UCS4"?>' | wc -c => 38 |
| 1733 | * 45 chars should be sufficient to reach the end of the encoding |
| 1734 | * decalration without going too far inside the document content. |
| 1735 | */ |
| 1736 | written = 45; |
| 1737 | |
| 1738 | if (handler->input != NULL) { |
| 1739 | ret = handler->input(&out->content[out->use], &written, |
| 1740 | in->content, &toconv); |
| 1741 | xmlBufferShrink(in, toconv); |
| 1742 | out->use += written; |
| 1743 | out->content[out->use] = 0; |
| 1744 | } |
| 1745 | #ifdef LIBXML_ICONV_ENABLED |
| 1746 | else if (handler->iconv_in != NULL) { |
| 1747 | ret = xmlIconvWrapper(handler->iconv_in, &out->content[out->use], |
| 1748 | &written, in->content, &toconv); |
| 1749 | xmlBufferShrink(in, toconv); |
| 1750 | out->use += written; |
| 1751 | out->content[out->use] = 0; |
| 1752 | if (ret == -1) ret = -3; |
| 1753 | } |
| 1754 | #endif /* LIBXML_ICONV_ENABLED */ |
| 1755 | #ifdef DEBUG_ENCODING |
| 1756 | switch (ret) { |
| 1757 | case 0: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1758 | xmlGenericError(xmlGenericErrorContext, |
| 1759 | "converted %d bytes to %d bytes of input\n", |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1760 | toconv, written); |
| 1761 | break; |
| 1762 | case -1: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1763 | xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of input, %d left\n", |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1764 | toconv, written, in->use); |
| 1765 | break; |
| 1766 | case -2: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1767 | xmlGenericError(xmlGenericErrorContext, |
| 1768 | "input conversion failed due to input error\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1769 | break; |
| 1770 | case -3: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1771 | xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of input, %d left\n", |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1772 | toconv, written, in->use); |
| 1773 | break; |
| 1774 | default: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1775 | xmlGenericError(xmlGenericErrorContext,"Unknown input conversion failed %d\n", ret); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1776 | } |
| 1777 | #endif |
| 1778 | /* |
| 1779 | * Ignore when input buffer is not on a boundary |
| 1780 | */ |
| 1781 | if (ret == -3) ret = 0; |
| 1782 | if (ret == -1) ret = 0; |
| 1783 | return(ret); |
| 1784 | } |
| 1785 | |
| 1786 | /** |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1787 | * xmlCharEncInFunc: |
| 1788 | * @handler: char enconding transformation data structure |
| 1789 | * @out: an xmlBuffer for the output. |
| 1790 | * @in: an xmlBuffer for the input |
| 1791 | * |
| 1792 | * Generic front-end for the encoding handler input function |
| 1793 | * |
| 1794 | * Returns the number of byte written if success, or |
| 1795 | * -1 general error |
| 1796 | * -2 if the transcoding fails (for *in is not valid utf8 string or |
| 1797 | * the result of transformation can't fit into the encoding we want), or |
| 1798 | */ |
| 1799 | int |
| 1800 | xmlCharEncInFunc(xmlCharEncodingHandler *handler, xmlBufferPtr out, |
| 1801 | xmlBufferPtr in) { |
| 1802 | int ret = -2; |
| 1803 | int written; |
| 1804 | int toconv; |
| 1805 | |
| 1806 | if (handler == NULL) return(-1); |
| 1807 | if (out == NULL) return(-1); |
| 1808 | if (in == NULL) return(-1); |
| 1809 | |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1810 | toconv = in->use; |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 1811 | if (toconv == 0) |
| 1812 | return(0); |
| 1813 | written = out->size - out->use; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1814 | if (toconv * 2 >= written) { |
| 1815 | xmlBufferGrow(out, toconv * 2); |
| 1816 | written = out->size - out->use - 1; |
| 1817 | } |
| 1818 | if (handler->input != NULL) { |
| 1819 | ret = handler->input(&out->content[out->use], &written, |
| 1820 | in->content, &toconv); |
| 1821 | xmlBufferShrink(in, toconv); |
| 1822 | out->use += written; |
| 1823 | out->content[out->use] = 0; |
| 1824 | } |
| 1825 | #ifdef LIBXML_ICONV_ENABLED |
| 1826 | else if (handler->iconv_in != NULL) { |
| 1827 | ret = xmlIconvWrapper(handler->iconv_in, &out->content[out->use], |
| 1828 | &written, in->content, &toconv); |
| 1829 | xmlBufferShrink(in, toconv); |
| 1830 | out->use += written; |
| 1831 | out->content[out->use] = 0; |
| 1832 | if (ret == -1) ret = -3; |
| 1833 | } |
| 1834 | #endif /* LIBXML_ICONV_ENABLED */ |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1835 | switch (ret) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1836 | #ifdef DEBUG_ENCODING |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1837 | case 0: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1838 | xmlGenericError(xmlGenericErrorContext, |
| 1839 | "converted %d bytes to %d bytes of input\n", |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1840 | toconv, written); |
| 1841 | break; |
| 1842 | case -1: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1843 | xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of input, %d left\n", |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1844 | toconv, written, in->use); |
| 1845 | break; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1846 | case -3: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1847 | xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of input, %d left\n", |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1848 | toconv, written, in->use); |
| 1849 | break; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1850 | #endif |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1851 | case -2: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1852 | xmlGenericError(xmlGenericErrorContext, |
| 1853 | "input conversion failed due to input error\n"); |
| 1854 | xmlGenericError(xmlGenericErrorContext, |
| 1855 | "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1856 | in->content[0], in->content[1], |
| 1857 | in->content[2], in->content[3]); |
| 1858 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1859 | /* |
| 1860 | * Ignore when input buffer is not on a boundary |
| 1861 | */ |
| 1862 | if (ret == -3) ret = 0; |
| 1863 | return(ret); |
| 1864 | } |
| 1865 | |
| 1866 | /** |
| 1867 | * xmlCharEncOutFunc: |
| 1868 | * @handler: char enconding transformation data structure |
| 1869 | * @out: an xmlBuffer for the output. |
| 1870 | * @in: an xmlBuffer for the input |
| 1871 | * |
| 1872 | * Generic front-end for the encoding handler output function |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1873 | * a first call with @in == NULL has to be made firs to initiate the |
| 1874 | * output in case of non-stateless encoding needing to initiate their |
| 1875 | * state or the output (like the BOM in UTF16). |
| 1876 | * In case of UTF8 sequence conversion errors for the given encoder, |
| 1877 | * the content will be automatically remapped to a CharRef sequence. |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1878 | * |
| 1879 | * Returns the number of byte written if success, or |
| 1880 | * -1 general error |
| 1881 | * -2 if the transcoding fails (for *in is not valid utf8 string or |
| 1882 | * the result of transformation can't fit into the encoding we want), or |
| 1883 | */ |
| 1884 | int |
| 1885 | xmlCharEncOutFunc(xmlCharEncodingHandler *handler, xmlBufferPtr out, |
| 1886 | xmlBufferPtr in) { |
| 1887 | int ret = -2; |
| 1888 | int written; |
| 1889 | int toconv; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1890 | int output = 0; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1891 | |
| 1892 | if (handler == NULL) return(-1); |
| 1893 | if (out == NULL) return(-1); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1894 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1895 | retry: |
| 1896 | |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1897 | written = out->size - out->use; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1898 | |
| 1899 | /* |
| 1900 | * First specific handling of in = NULL, i.e. the initialization call |
| 1901 | */ |
| 1902 | if (in == NULL) { |
| 1903 | toconv = 0; |
| 1904 | if (handler->output != NULL) { |
| 1905 | ret = handler->output(&out->content[out->use], &written, |
| 1906 | NULL, &toconv); |
| 1907 | out->use += written; |
| 1908 | out->content[out->use] = 0; |
| 1909 | } |
| 1910 | #ifdef LIBXML_ICONV_ENABLED |
| 1911 | else if (handler->iconv_out != NULL) { |
| 1912 | ret = xmlIconvWrapper(handler->iconv_out, &out->content[out->use], |
| 1913 | &written, NULL, &toconv); |
| 1914 | out->use += written; |
| 1915 | out->content[out->use] = 0; |
| 1916 | } |
| 1917 | #endif /* LIBXML_ICONV_ENABLED */ |
| 1918 | #ifdef DEBUG_ENCODING |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1919 | xmlGenericError(xmlGenericErrorContext, |
| 1920 | "initialized encoder\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1921 | #endif |
| 1922 | return(0); |
| 1923 | } |
| 1924 | |
| 1925 | /* |
| 1926 | * Convertion itself. |
| 1927 | */ |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1928 | toconv = in->use; |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 1929 | if (toconv == 0) |
| 1930 | return(0); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1931 | if (toconv * 2 >= written) { |
| 1932 | xmlBufferGrow(out, toconv * 2); |
| 1933 | written = out->size - out->use - 1; |
| 1934 | } |
| 1935 | if (handler->output != NULL) { |
| 1936 | ret = handler->output(&out->content[out->use], &written, |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1937 | in->content, &toconv); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1938 | xmlBufferShrink(in, toconv); |
| 1939 | out->use += written; |
| 1940 | out->content[out->use] = 0; |
| 1941 | } |
| 1942 | #ifdef LIBXML_ICONV_ENABLED |
| 1943 | else if (handler->iconv_out != NULL) { |
| 1944 | ret = xmlIconvWrapper(handler->iconv_out, &out->content[out->use], |
| 1945 | &written, in->content, &toconv); |
| 1946 | xmlBufferShrink(in, toconv); |
| 1947 | out->use += written; |
| 1948 | out->content[out->use] = 0; |
| 1949 | if (ret == -1) ret = -3; |
| 1950 | } |
| 1951 | #endif /* LIBXML_ICONV_ENABLED */ |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1952 | else { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1953 | xmlGenericError(xmlGenericErrorContext, |
| 1954 | "xmlCharEncOutFunc: no output function !\n"); |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1955 | return(-1); |
| 1956 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1957 | |
| 1958 | if (ret >= 0) output += ret; |
| 1959 | |
| 1960 | /* |
| 1961 | * Attempt to handle error cases |
| 1962 | */ |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1963 | switch (ret) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1964 | #ifdef DEBUG_ENCODING |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1965 | case 0: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1966 | xmlGenericError(xmlGenericErrorContext, |
| 1967 | "converted %d bytes to %d bytes of output\n", |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1968 | toconv, written); |
| 1969 | break; |
| 1970 | case -1: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1971 | xmlGenericError(xmlGenericErrorContext, |
| 1972 | "output conversion failed by lack of space\n"); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1973 | break; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1974 | case -3: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1975 | xmlGenericError(xmlGenericErrorContext,"converted %d bytes to %d bytes of output %d left\n", |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1976 | toconv, written, in->use); |
| 1977 | break; |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1978 | #endif |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1979 | case -2: { |
| 1980 | int len = in->use; |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1981 | const xmlChar *utf = (const xmlChar *) in->content; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1982 | int cur; |
| 1983 | |
| 1984 | cur = xmlGetUTF8Char(utf, &len); |
| 1985 | if (cur > 0) { |
| 1986 | xmlChar charref[20]; |
| 1987 | |
| 1988 | #ifdef DEBUG_ENCODING |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1989 | xmlGenericError(xmlGenericErrorContext, |
| 1990 | "handling output conversion error\n"); |
| 1991 | xmlGenericError(xmlGenericErrorContext, |
| 1992 | "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1993 | in->content[0], in->content[1], |
| 1994 | in->content[2], in->content[3]); |
| 1995 | #endif |
| 1996 | /* |
| 1997 | * Removes the UTF8 sequence, and replace it by a charref |
| 1998 | * and continue the transcoding phase, hoping the error |
| 1999 | * did not mangle the encoder state. |
| 2000 | */ |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 2001 | sprintf((char *) charref, "&#x%X;", cur); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 2002 | xmlBufferShrink(in, len); |
| 2003 | xmlBufferAddHead(in, charref, -1); |
| 2004 | |
| 2005 | goto retry; |
| 2006 | } else { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2007 | xmlGenericError(xmlGenericErrorContext, |
| 2008 | "output conversion failed due to conv error\n"); |
| 2009 | xmlGenericError(xmlGenericErrorContext, |
| 2010 | "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 2011 | in->content[0], in->content[1], |
| 2012 | in->content[2], in->content[3]); |
Daniel Veillard | 87b9539 | 2000-08-12 21:12:04 +0000 | [diff] [blame] | 2013 | in->content[0] = ' '; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 2014 | } |
| 2015 | break; |
| 2016 | } |
| 2017 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 2018 | return(ret); |
| 2019 | } |
| 2020 | |
| 2021 | /** |
| 2022 | * xmlCharEncCloseFunc: |
| 2023 | * @handler: char enconding transformation data structure |
| 2024 | * |
| 2025 | * Generic front-end for hencoding handler close function |
| 2026 | * |
| 2027 | * Returns 0 if success, or -1 in case of error |
| 2028 | */ |
| 2029 | int |
| 2030 | xmlCharEncCloseFunc(xmlCharEncodingHandler *handler) { |
| 2031 | int ret = 0; |
| 2032 | if (handler == NULL) return(-1); |
| 2033 | if (handler->name == NULL) return(-1); |
| 2034 | #ifdef LIBXML_ICONV_ENABLED |
| 2035 | /* |
| 2036 | * Iconv handlers can be oused only once, free the whole block. |
| 2037 | * and the associated icon resources. |
| 2038 | */ |
| 2039 | if ((handler->iconv_out != NULL) || (handler->iconv_in != NULL)) { |
| 2040 | if (handler->name != NULL) |
| 2041 | xmlFree(handler->name); |
| 2042 | handler->name = NULL; |
| 2043 | if (handler->iconv_out != NULL) { |
| 2044 | if (iconv_close(handler->iconv_out)) |
| 2045 | ret = -1; |
| 2046 | handler->iconv_out = NULL; |
| 2047 | } |
| 2048 | if (handler->iconv_in != NULL) { |
| 2049 | if (iconv_close(handler->iconv_in)) |
| 2050 | ret = -1; |
| 2051 | handler->iconv_in = NULL; |
| 2052 | } |
| 2053 | xmlFree(handler); |
| 2054 | } |
| 2055 | #endif /* LIBXML_ICONV_ENABLED */ |
| 2056 | #ifdef DEBUG_ENCODING |
| 2057 | if (ret) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2058 | xmlGenericError(xmlGenericErrorContext, |
| 2059 | "failed to close the encoding handler\n"); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 2060 | else |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 2061 | xmlGenericError(xmlGenericErrorContext, |
| 2062 | "closed the encoding handler\n"); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 2063 | |
| 2064 | #endif |
| 2065 | return(ret); |
| 2066 | } |
| 2067 | |