Annotation of parser3/src/main/pa_charset.C, revision 1.47
1.1 paf 1: /** @file
2: Parser: Charset connection implementation.
3:
1.35 paf 4: Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.4 paf 5: Author: Alexander Petrosyan<paf@design.ru>(http://paf.design.ru)
1.27 paf 6: */
1.1 paf 7:
1.47 ! paf 8: static const char * const IDENT_CHARSET_C="$Date: 2004/02/03 13:09:52 $";
1.1 paf 9:
10: #include "pa_charset.h"
1.35 paf 11: #include "pa_charsets.h"
1.1 paf 12:
13: #ifdef XML
1.8 paf 14: #include "libxml/encoding.h"
1.1 paf 15: #endif
16:
1.46 paf 17: //#define PA_PATCHED_LIBXML_BACKWARD
18:
1.38 paf 19: // globals
20:
21: Charset::UTF8CaseTable::Rec UTF8CaseToUpperRecords[]={
22: #include "utf8-to-upper.inc"
23: };
24: Charset::UTF8CaseTable UTF8CaseToUpper={
25: sizeof(UTF8CaseToUpperRecords)/sizeof(Charset::UTF8CaseTable::Rec),
26: UTF8CaseToUpperRecords};
27:
28: Charset::UTF8CaseTable::Rec UTF8CaseToLowerRecords[]={
29: #include "utf8-to-lower.inc"
30: };
31: Charset::UTF8CaseTable UTF8CaseToLower={
32: sizeof(UTF8CaseToLowerRecords)/sizeof(Charset::UTF8CaseTable::Rec),
33: UTF8CaseToLowerRecords};
34:
1.1 paf 35: // helpers
36:
37: inline void prepare_case_tables(unsigned char *tables) {
38: unsigned char *lcc_table=tables+lcc_offset;
39: unsigned char *fcc_table=tables+fcc_offset;
40: for(int i=0; i<0x100; i++)
41: lcc_table[i]=fcc_table[i]=i;
42: }
43: inline void cstr2ctypes(unsigned char *tables, const unsigned char *cstr,
44: unsigned char bit) {
45: unsigned char *ctypes_table=tables+ctypes_offset;
46: ctypes_table[0]=bit;
47: for(; *cstr; cstr++) {
48: unsigned char c=*cstr;
49: ctypes_table[c]|=bit;
50: }
51: }
1.35 paf 52: inline unsigned int to_wchar_code(const char* cstr) {
1.1 paf 53: if(!cstr || !*cstr)
54: return 0;
55: if(cstr[1]==0)
1.4 paf 56: return(unsigned int)(unsigned char)cstr[0];
1.1 paf 57:
58: char *error_pos;
1.4 paf 59: return(unsigned int)strtol(cstr, &error_pos, 0);
1.1 paf 60: }
1.35 paf 61: inline bool to_bool(const char* cstr) {
1.1 paf 62: return cstr && *cstr!=0;
63: }
64: static void element2ctypes(unsigned char c, bool belongs,
65: unsigned char *tables, unsigned char bit, int group_offset=-1) {
66: if(!belongs)
67: return;
68:
69: unsigned char *ctypes_table=tables+ctypes_offset;
70:
71: ctypes_table[c]|=bit;
72: if(group_offset>=0)
1.4 paf 73: tables[cbits_offset+group_offset+c/8] |= 1<<(c%8);
1.1 paf 74: }
75: static void element2case(unsigned char from, unsigned char to,
76: unsigned char *tables) {
77: if(!to)
78: return;
79:
80: unsigned char *lcc_table=tables+lcc_offset;
81: unsigned char *fcc_table=tables+fcc_offset;
82: lcc_table[from]=to;
83: fcc_table[from]=to; fcc_table[to]=from;
84: }
85:
86: // methods
87:
88: extern "C" unsigned char pcre_default_tables[]; // pcre/chartables.c
1.37 paf 89: Charset::Charset(Request_charsets* charsets, const String::Body ANAME, const String* afile_spec):
1.35 paf 90: FNAME(ANAME),
91: FNAME_CSTR(ANAME.cstrm()) {
1.7 paf 92:
1.35 paf 93: if(afile_spec) {
1.1 paf 94: fisUTF8=false;
1.35 paf 95: load_definition(*charsets, *afile_spec);
1.1 paf 96: #ifdef XML
1.35 paf 97: addEncoding(FNAME_CSTR);
1.1 paf 98: #endif
99: } else {
100: fisUTF8=true;
1.4 paf 101: // grab default onces [for UTF-8 so to be able to make a-z =>A-Z
1.1 paf 102: memcpy(pcre_tables, pcre_default_tables, sizeof(pcre_tables));
103: }
104:
105: #ifdef XML
1.35 paf 106: initTranscoder(FNAME, FNAME_CSTR);
1.1 paf 107: #endif
108: }
109:
1.35 paf 110: void Charset::load_definition(Request_charsets& charsets, const String& afile_spec) {
1.1 paf 111: // pcre_tables
112: // lowcase, flipcase, bits digit+word+whitespace, masks
113:
114: // must not move this inside of prepare_case_tables
115: // don't know the size there
116: memset(pcre_tables, 0, sizeof(pcre_tables));
117: prepare_case_tables(pcre_tables);
1.4 paf 118: cstr2ctypes(pcre_tables,(const unsigned char *)"*+?{^.$|()[", ctype_meta);
1.1 paf 119:
120: // charset
1.35 paf 121: memset(&tables, 0, sizeof(tables));
1.1 paf 122:
123: // loading text
1.35 paf 124: char *data=file_read_text(charsets, afile_spec);
1.1 paf 125:
126: // ignore header
127: getrow(&data);
128:
129: // parse cells
130: char *row;
1.42 paf 131: while((row=getrow(&data))) {
1.1 paf 132: // remove empty&comment lines
133: if(!*row || *row=='#')
134: continue;
135:
136: // char white-space digit hex-digit letter word lowercase unicode1 unicode2
137: unsigned int c=0;
138: char *cell;
1.42 paf 139: for(int column=0; (cell=lsplit(&row, '\t')); column++) {
1.1 paf 140: switch(column) {
141: case 0: c=to_wchar_code(cell); break;
142: // pcre_tables
143: case 1: element2ctypes(c, to_bool(cell), pcre_tables, ctype_space, cbit_space); break;
144: case 2: element2ctypes(c, to_bool(cell), pcre_tables, ctype_digit, cbit_digit); break;
145: case 3: element2ctypes(c, to_bool(cell), pcre_tables, ctype_xdigit); break;
146: case 4: element2ctypes(c, to_bool(cell), pcre_tables, ctype_letter); break;
147: case 5: element2ctypes(c, to_bool(cell), pcre_tables, ctype_word, cbit_word); break;
148: case 6: element2case(c, to_wchar_code(cell), pcre_tables); break;
149: case 7:
150: case 8:
151: // charset
1.10 paf 152: if(tables.toTableSize>MAX_CHARSET_UNI_CODES)
1.23 paf 153: throw Exception("parser.runtime",
1.35 paf 154: &afile_spec,
1.1 paf 155: "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES);
156:
157: XMLCh unicode=(XMLCh)to_wchar_code(cell);
158: if(!unicode && column==7/*unicode1 column*/)
159: unicode=(XMLCh)c;
160: if(unicode) {
1.10 paf 161: if(!tables.fromTable[c])
162: tables.fromTable[c]=unicode;
163: tables.toTable[tables.toTableSize].intCh=unicode;
164: tables.toTable[tables.toTableSize].extCh=(XMLByte)c;
165: tables.toTableSize++;
1.1 paf 166: }
167: break;
168: }
169: }
170: };
171:
172: // sort by the Unicode code point
173: sort_ToTable();
174: }
175:
176: static int sort_cmp_Trans_rec_intCh(const void *a, const void *b) {
177: return
1.38 paf 178: static_cast<const Charset::Tables::Rec *>(a)->intCh-
179: static_cast<const Charset::Tables::Rec *>(b)->intCh;
1.1 paf 180: }
181:
182: void Charset::sort_ToTable() {
1.10 paf 183: _qsort(tables.toTable, tables.toTableSize, sizeof(*tables.toTable),
1.1 paf 184: sort_cmp_Trans_rec_intCh);
185: //FILE *f=fopen("c:\\temp\\a", "wb");
1.10 paf 186: //fwrite(tables.toTable, tables.toTableSize, sizeof(*tables.toTable), f);
1.1 paf 187: //fclose(f);
188: }
189:
1.10 paf 190: static XMLByte xlatOneTo(const XMLCh toXlat,
1.35 paf 191: const Charset::Tables& tables,
192: XMLByte not_found) {
1.39 paf 193: int lo = 0;
194: int hi = tables.toTableSize - 1;
195: while(lo<=hi) {
1.35 paf 196: // Calc the mid point of the low and high offset.
1.39 paf 197: const unsigned int i = (lo + hi) / 2;
198:
199: XMLCh cur=tables.toTable[i].intCh;
200: if(toXlat==cur)
201: return tables.toTable[i].extCh;
202: if(toXlat>cur)
203: lo = i+1;
1.1 paf 204: else
1.39 paf 205: hi = i-1;
206: }
1.35 paf 207:
208: return not_found;
1.1 paf 209: }
210:
1.35 paf 211: String::C Charset::transcode(const String::C src,
212: const Charset& source_charset,
213: const Charset& dest_charset) {
214: if(!src.length)
215: return String::C("", 0);
1.4 paf 216:
1.1 paf 217: switch((source_charset.isUTF8()?0x10:0x00)|(dest_charset.isUTF8()?0x01:0x00)) {
218: default: // 0x00
1.35 paf 219: return source_charset.transcodeToCharset(src, dest_charset);
1.1 paf 220: case 0x01:
1.35 paf 221: return source_charset.transcodeToUTF8(src);
1.1 paf 222: case 0x10:
1.35 paf 223: return dest_charset.transcodeFromUTF8(src);
1.1 paf 224: case 0x11:
1.35 paf 225: return src;
1.1 paf 226: }
227: }
228:
229: // ---------------------------------------------------------------------------
230: // Local static data
231: //
232: // gUTFBytes
233: // A list of counts of trailing bytes for each initial byte in the input.
234: //
235: // gUTFOffsets
236: // A list of values to offset each result char type, according to how
237: // many source bytes when into making it.
238: //
239: // gFirstByteMark
240: // A list of values to mask onto the first byte of an encoded sequence,
241: // indexed by the number of bytes used to create the sequence.
242: // ---------------------------------------------------------------------------
243: static const XMLByte gUTFBytes[0x100] = {
244: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
245: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
246: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
247: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
248: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
249: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
250: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
251: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
252: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
253: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
254: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
255: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
256: , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
257: , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
258: , 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
259: , 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
260: };
261:
262: static const uint gUTFOffsets[6] = {
263: 0, 0x3080, 0xE2080, 0x3C82080, 0xFA082080, 0x82082080
264: };
265:
266: static const XMLByte gFirstByteMark[7] = {
267: 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
268: };
269:
1.35 paf 270: static int transcodeToUTF8(const XMLByte* srcData, size_t& srcLen,
271: XMLByte *toFill, size_t& toFillLen,
272: const Charset::Tables& tables) {
1.11 paf 273: const XMLByte* srcPtr=srcData;
274: const XMLByte* srcEnd=srcData+srcLen;
275: XMLByte* outPtr=toFill;
276: XMLByte* outEnd=toFill+toFillLen;
1.1 paf 277:
1.35 paf 278: while(srcPtr<srcEnd) {
279: uint curVal = tables.fromTable[*srcPtr];
1.1 paf 280: if(!curVal) {
1.35 paf 281: // use the replacement character
282: *outPtr++= '?';
283: srcPtr++;
284: continue;
285: }
1.1 paf 286:
1.35 paf 287: // Figure out how many bytes we need
288: unsigned int encodedBytes;
289: if(curVal<0x80)
290: encodedBytes = 1;
291: else if(curVal<0x800)
292: encodedBytes = 2;
293: else if(curVal<0x10000)
294: encodedBytes = 3;
295: else if(curVal<0x200000)
296: encodedBytes = 4;
297: else if(curVal<0x4000000)
298: encodedBytes = 5;
299: else if(curVal<= 0x7FFFFFFF)
300: encodedBytes = 6;
301: else {
302: // use the replacement character
303: *outPtr++= '?';
304: srcPtr++;
305: continue;
306: }
1.11 paf 307:
1.35 paf 308: // If we cannot fully get this char into the output buffer
309: if (outPtr + encodedBytes > outEnd)
310: break;
311:
312: // We can do it, so update the source index
313: srcPtr++;
314:
315: // And spit out the bytes. We spit them out in reverse order
316: // here, so bump up the output pointer and work down as we go.
317: outPtr+= encodedBytes;
318: switch(encodedBytes) {
319: case 6: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
320: curVal>>= 6;
321: case 5: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
322: curVal>>= 6;
323: case 4: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
324: curVal>>= 6;
325: case 3: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
326: curVal>>= 6;
327: case 2: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
328: curVal>>= 6;
329: case 1: *--outPtr = XMLByte(curVal | gFirstByteMark[encodedBytes]);
330: }
331:
332: // Add the encoded bytes back in again to indicate we've eaten them
333: outPtr+= encodedBytes;
334: }
335:
336: // Update the bytes eaten
337: srcLen = srcPtr - srcData;
338:
339: // Return the characters read
340: toFillLen = outPtr - toFill;
341:
1.29 paf 342: //return srcPtr==srcEnd?(int)toFillLen:-1;
343: /*
344: xmlCharEncodingInputFunc
345: Returns :
346: the number of byte written, or -1 by lack of space, or -2 if the transcoding failed. The value of inlen after return is the
347: number of octets consumed as the return value is positive, else unpredictiable. The value of outlen after return is the number
348: of ocetes consumed.
349: */
350: return 0;
1.1 paf 351: }
1.26 paf 352: /// @todo digital entites only when xml/html output [at output in html/xml mode, in html part of a letter]
1.35 paf 353: static int transcodeFromUTF8(const XMLByte* srcData, size_t& srcLen,
354: XMLByte* toFill, size_t& toFillLen,
355: const Charset::Tables& tables) {
1.11 paf 356: const XMLByte* srcPtr=srcData;
357: const XMLByte* srcEnd=srcData+srcLen;
358: XMLByte* outPtr=toFill;
359: XMLByte* outEnd=toFill+toFillLen;
1.1 paf 360:
1.35 paf 361: // We now loop until we either run out of input data, or room to store
362: while ((srcPtr < srcEnd) && (outPtr < outEnd)) {
363: // Get the next leading byte out
364: const XMLByte firstByte =* srcPtr;
365:
366: // Special-case ASCII, which is a leading byte value of<= 127
367: if(firstByte<= 127) {
368: *outPtr++= firstByte;
369: srcPtr++;
370: continue;
371: }
372:
373: // See how many trailing src bytes this sequence is going to require
374: const unsigned int trailingBytes = gUTFBytes[firstByte];
375:
376: // If there are not enough source bytes to do this one, then we
377: // are done. Note that we done>= here because we are implicitly
378: // counting the 1 byte we get no matter what.
379: if(srcPtr+trailingBytes>= srcEnd)
380: break;
381:
382: // Looks ok, so lets build up the value
383: uint tmpVal=0;
384: switch(trailingBytes) {
385: case 5: tmpVal+=*srcPtr++; tmpVal<<=6;
386: case 4: tmpVal+=*srcPtr++; tmpVal<<=6;
387: case 3: tmpVal+=*srcPtr++; tmpVal<<=6;
388: case 2: tmpVal+=*srcPtr++; tmpVal<<=6;
389: case 1: tmpVal+=*srcPtr++; tmpVal<<=6;
390: case 0: tmpVal+=*srcPtr++;
391: break;
392:
393: default:
394: throw Exception(0,
395: 0,
396: "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes);
397: }
398: tmpVal-=gUTFOffsets[trailingBytes];
399:
400: // If it will fit into a single char, then put it in. Otherwise
401: // fail [*encode it as a surrogate pair. If its not valid, use the
402: // replacement char.*]
403: if(!(tmpVal & 0xFFFF0000)) {
1.25 paf 404: if(XMLByte xlat=xlatOneTo(tmpVal, tables, 0))
405: *outPtr++=xlat;
406: else
407: outPtr+=sprintf((char *)outPtr, "&#%d;", tmpVal); // &#decimal;
408: } else
1.23 paf 409: throw Exception(0,
1.35 paf 410: 0,
411: "transcodeFromUTF8 error: too big tmpVal(0x%08X)", tmpVal);
1.1 paf 412: }
1.35 paf 413:
414: // Update the bytes eaten
415: srcLen = srcPtr - srcData;
416:
417: // Return the characters read
418: toFillLen = outPtr - toFill;
1.11 paf 419:
1.29 paf 420: //return srcPtr==srcEnd?(int)toFillLen:-1;
421: /*
422: xmlCharEncodingOutputFunc
423: Returns :
424: the number of byte written, or -1 by lack of space, or -2 if the transcoding failed. The value of inlen after return is the
425: number of octets consumed as the return value is positive, else unpredictiable. The value of outlen after return is the number
426: of ocetes consumed.
427: */
428: return 0;
1.10 paf 429: }
430:
431: /// @todo not so memory-hungry with prescan
1.35 paf 432: const String::C Charset::transcodeToUTF8(const String::C src) const {
433: size_t src_length=src.length;
434: size_t dest_length=src.length*6/*so that surly enough, max utf8 seq len=6*/;
435: #ifndef NDEBUG
436: size_t saved_dest_length=dest_length;
437: #endif
438: XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/];
1.11 paf 439:
440: if(::transcodeToUTF8(
1.35 paf 441: (XMLByte *)src.str, src_length,
442: dest_body, dest_length,
1.11 paf 443: tables)<0)
1.43 paf 444: throw Exception(0,
1.10 paf 445: 0,
1.11 paf 446: "Charset::transcodeToUTF8 buffer overflow");
1.10 paf 447:
1.35 paf 448: assert(dest_length<=saved_dest_length); dest_body[dest_length]=0; // terminator
449: return String::C((char*)dest_body, dest_length);
1.10 paf 450: }
1.38 paf 451:
452: static XMLCh change_case_UTF8(const XMLCh src, const Charset::UTF8CaseTable& table) {
1.39 paf 453: int lo = 0;
454: int hi = table.size - 1;
455: while(lo<=hi) {
1.38 paf 456: // Calc the mid point of the low and high offset.
1.39 paf 457: const unsigned int i = (lo + hi) / 2;
458:
459: XMLCh cur=table.records[i].from;
460: if(src==cur)
461: return table.records[i].to;
462: if(src>cur)
463: lo = i+1;
1.38 paf 464: else
1.39 paf 465: hi = i-1;
466: }
467:
468: // not found
1.38 paf 469: return src;
470: }
471:
472: static void store_UTF8(XMLCh src, XMLByte*& outPtr ) {
473: if(!src) {
474: // use the replacement character
475: *outPtr++= '?';
476: return;
477: }
478:
479: // Figure out how many bytes we need
480: unsigned int encodedBytes;
481: if(src<0x80)
482: encodedBytes = 1;
483: else if(src<0x800)
484: encodedBytes = 2;
485: else if(src<0x10000)
486: encodedBytes = 3;
487: else if(src<0x200000)
488: encodedBytes = 4;
489: else if(src<0x4000000)
490: encodedBytes = 5;
491: else if(src<= 0x7FFFFFFF)
492: encodedBytes = 6;
493: else {
494: // use the replacement character
495: *outPtr++= '?';
496: return;
497: }
498:
499: // And spit out the bytes. We spit them out in reverse order
500: // here, so bump up the output pointer and work down as we go.
501: outPtr+= encodedBytes;
502: switch(encodedBytes) {
503: case 6: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
504: src>>= 6;
505: case 5: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
506: src>>= 6;
507: case 4: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
508: src>>= 6;
509: case 3: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
510: src>>= 6;
511: case 2: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
512: src>>= 6;
513: case 1: *--outPtr = XMLByte(src | gFirstByteMark[encodedBytes]);
514: }
515:
516: // Add the encoded bytes back in again to indicate we've eaten them
517: outPtr+= encodedBytes;
518: }
519:
520: static void change_case_UTF8(XMLCh src, XMLByte*& outPtr,
521: const Charset::UTF8CaseTable& table) {
522: store_UTF8(change_case_UTF8(src, table), outPtr);
523: };
1.44 paf 524: void change_case_UTF8(const XMLByte* srcData, size_t srcLen,
525: XMLByte* toFill, size_t toFillLen,
526: const Charset::UTF8CaseTable& table) {
1.38 paf 527: const XMLByte* srcPtr=srcData;
1.44 paf 528: const XMLByte* srcEnd=srcData+srcLen;
1.38 paf 529: XMLByte* outPtr=toFill;
1.44 paf 530: XMLByte* outEnd=toFill+toFillLen;
531:
532: // We now loop until we either run out of input data, or room to store
533: while ((srcPtr < srcEnd) && (outPtr < outEnd)) {
534: // Get the next leading byte out
535: const XMLByte firstByte =* srcPtr;
1.38 paf 536:
537: if(firstByte<= 127) {
538: change_case_UTF8(firstByte, outPtr, table);
539: srcPtr++;
540: continue;
541: }
542:
543: // See how many trailing src bytes this sequence is going to require
544: const unsigned int trailingBytes = gUTFBytes[firstByte];
545:
546: // Looks ok, so lets build up the value
547: uint tmpVal=0;
548: switch(trailingBytes) {
549: case 5: tmpVal+=*srcPtr++; tmpVal<<=6;
550: case 4: tmpVal+=*srcPtr++; tmpVal<<=6;
551: case 3: tmpVal+=*srcPtr++; tmpVal<<=6;
552: case 2: tmpVal+=*srcPtr++; tmpVal<<=6;
553: case 1: tmpVal+=*srcPtr++; tmpVal<<=6;
554: case 0: tmpVal+=*srcPtr++;
555: break;
556:
557: default:
558: throw Exception(0,
559: 0,
560: "change_case_UTF8 error: wrong trailingBytes value(%d)", trailingBytes);
561: }
562: tmpVal-=gUTFOffsets[trailingBytes];
563:
564: // If it will fit into a single char, then put it in. Otherwise
565: // fail [*encode it as a surrogate pair. If its not valid, use the
566: // replacement char.*]
567: if(!(tmpVal & 0xFFFF0000))
568: change_case_UTF8(tmpVal, outPtr, table);
569: else
570: throw Exception(0,
571: 0,
572: "change_case_UTF8 error: too big tmpVal(0x%08X)", tmpVal);
573: }
574:
575: if(srcPtr!=outPtr)
576: throw Exception(0,
577: 0,
578: "change_case_UTF8 error: end pointers do not match");
579: }
580:
581:
1.35 paf 582: const String::C Charset::transcodeFromUTF8(const String::C src) const {
583: size_t src_length=src.length;
584: size_t dest_length=src.length*6/*so that surly enough, "ÿ" has max ratio */;
585: #ifndef NDEBUG
586: size_t saved_dest_length=dest_length;
587: #endif
588: XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/];
1.11 paf 589:
590: if(::transcodeFromUTF8(
1.35 paf 591: (XMLByte *)src.str, src_length,
592: dest_body, dest_length,
1.11 paf 593: tables)<0)
1.43 paf 594: throw Exception(0,
1.10 paf 595: 0,
1.35 paf 596: "Charset::transcodeFromUTF8 buffer overflow");
1.10 paf 597:
1.35 paf 598: assert(dest_length<=saved_dest_length); dest_body[dest_length]=0; // terminator
599: return String::C((char*)dest_body, dest_length);
1.1 paf 600: }
601:
602: /// transcode using both charsets
1.35 paf 603: const String::C Charset::transcodeToCharset(const String::C src,
604: const Charset& dest_charset) const {
605: if(&dest_charset==this)
606: return src;
607: else {
608: size_t dest_length=src.length;
609: XMLByte* dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/];
610:
611: XMLByte* output=dest_body;
612: const XMLByte* input=(XMLByte *)src.str;
613: while(XMLCh c=*input++) {
614: XMLCh curVal = tables.fromTable[c];
615: *output++=curVal?
616: xlatOneTo(curVal, dest_charset.tables, '?') // OK
617: :'?'; // use the replacement character
1.6 paf 618: }
1.1 paf 619:
1.35 paf 620: dest_body[dest_length]=0; // terminator
621: return String::C((char*)dest_body, dest_length);
1.6 paf 622: }
1.1 paf 623: }
624:
625: #ifdef XML
1.10 paf 626:
1.35 paf 627: static const Charset::Tables* tables[MAX_CHARSETS];
628:
1.46 paf 629: #ifdef PA_PATCHED_LIBXML_BACKWARD
630:
631: #define declareXml256ioFuncs(i) \
632: static int xml256CharEncodingInputFunc##i( \
633: unsigned char *out, int *outlen, \
634: const unsigned char *in, int *inlen, void*) { \
635: return transcodeToUTF8( \
636: in, *(size_t*)inlen, \
637: out, *(size_t*)outlen, \
638: *tables[i]); \
639: } \
640: static int xml256CharEncodingOutputFunc##i( \
641: unsigned char *out, int *outlen, \
642: const unsigned char *in, int *inlen, void*) { \
643: return transcodeFromUTF8( \
644: in, *(size_t*)inlen, \
645: out, *(size_t*)outlen, \
646: *tables[i]); \
647: }
648:
649: #else
650:
1.35 paf 651: #define declareXml256ioFuncs(i) \
652: static int xml256CharEncodingInputFunc##i( \
653: unsigned char *out, int *outlen, \
654: const unsigned char *in, int *inlen) { \
655: return transcodeToUTF8( \
656: in, *(size_t*)inlen, \
657: out, *(size_t*)outlen, \
658: *tables[i]); \
659: } \
660: static int xml256CharEncodingOutputFunc##i( \
661: unsigned char *out, int *outlen, \
662: const unsigned char *in, int *inlen) { \
663: return transcodeFromUTF8( \
664: in, *(size_t*)inlen, \
665: out, *(size_t*)outlen, \
666: *tables[i]); \
667: }
668:
1.46 paf 669: #endif
670:
671:
1.35 paf 672: declareXml256ioFuncs(0) declareXml256ioFuncs(1)
673: declareXml256ioFuncs(2) declareXml256ioFuncs(3)
674: declareXml256ioFuncs(4) declareXml256ioFuncs(5)
675: declareXml256ioFuncs(6) declareXml256ioFuncs(7)
676: declareXml256ioFuncs(8) declareXml256ioFuncs(9)
677:
678: static xmlCharEncodingInputFunc inputFuncs[MAX_CHARSETS]={
679: xml256CharEncodingInputFunc0, xml256CharEncodingInputFunc1,
680: xml256CharEncodingInputFunc2, xml256CharEncodingInputFunc3,
681: xml256CharEncodingInputFunc4, xml256CharEncodingInputFunc5,
682: xml256CharEncodingInputFunc6, xml256CharEncodingInputFunc7,
683: xml256CharEncodingInputFunc8, xml256CharEncodingInputFunc9
684: };
685: static xmlCharEncodingOutputFunc outputFuncs[MAX_CHARSETS]={
686: xml256CharEncodingOutputFunc0, xml256CharEncodingOutputFunc1,
687: xml256CharEncodingOutputFunc2, xml256CharEncodingOutputFunc3,
688: xml256CharEncodingOutputFunc4, xml256CharEncodingOutputFunc5,
689: xml256CharEncodingOutputFunc6, xml256CharEncodingOutputFunc7,
690: xml256CharEncodingOutputFunc8, xml256CharEncodingOutputFunc9
691: };
692: static size_t handlers_count=0;
1.10 paf 693:
694: void Charset::addEncoding(char *name_cstr) {
1.35 paf 695: if(handlers_count==MAX_CHARSETS)
696: throw Exception(0,
697: 0,
698: "already allocated %d handlers, no space for new encoding '%s'",
699: MAX_CHARSETS, name_cstr);
700:
1.45 paf 701: xmlCharEncodingHandler* handler=new(UseGC) xmlCharEncodingHandler;
1.35 paf 702: {
703: handler->name=name_cstr;
704: handler->input=inputFuncs[handlers_count];
705: handler->output=outputFuncs[handlers_count];
706: ::tables[handlers_count]=&tables;
707: handlers_count++;
708: }
1.10 paf 709:
710: xmlRegisterCharEncodingHandler(handler);
1.35 paf 711:
1.10 paf 712: }
713:
1.37 paf 714: void Charset::initTranscoder(const String::Body NAME, const char* name_cstr) {
1.15 paf 715: ftranscoder=xmlFindCharEncodingHandler(name_cstr);
1.35 paf 716: transcoder(NAME); // check right way
1.15 paf 717: }
718:
1.37 paf 719: xmlCharEncodingHandler& Charset::transcoder(const String::Body NAME) {
1.15 paf 720: if(!ftranscoder)
1.23 paf 721: throw Exception("parser.runtime",
1.35 paf 722: new String(NAME, String::L_TAINTED),
1.10 paf 723: "unsupported encoding");
1.35 paf 724: return *ftranscoder;
1.10 paf 725: }
726:
1.35 paf 727: String::C Charset::transcode_cstr(xmlChar* s) {
1.13 paf 728: if(!s)
1.35 paf 729: return String::C("", 0);
1.8 paf 730:
1.35 paf 731: int inlen=strlen((const char*)s);
732: int outlen=inlen; // max
733: #ifndef NDEBUG
734: int saved_outlen=outlen;
735: #endif
736: char *out=new(PointerFreeGC) char[outlen+1];
1.8 paf 737:
1.30 paf 738: int error;
1.35 paf 739: if(xmlCharEncodingOutputFunc output=transcoder(FNAME).output) {
1.30 paf 740: error=output(
1.17 paf 741: (unsigned char*)out, &outlen,
1.46 paf 742: (const unsigned char*)s, &inlen
743: #ifdef PA_PATCHED_LIBXML_BACKWARD
744: ,0
745: #endif
746: );
1.30 paf 747: } else {
748: memcpy(out, s, outlen=inlen);
749: error=0;
750: }
751: if(error<0)
1.23 paf 752: throw Exception(0,
1.8 paf 753: 0,
1.30 paf 754: "transcode_cstr failed (%d)", error);
1.8 paf 755:
1.35 paf 756: assert(outlen<=saved_outlen); out[outlen]=0;
757: return String::C(out, outlen);
1.14 paf 758: }
1.35 paf 759: const String& Charset::transcode(xmlChar* s) {
760: String::C cstr=transcode_cstr(s);
761: return *new String(cstr.str, cstr.length, true);
762: }
763: String::C Charset::transcode_cstr(GdomeDOMString* s) {
764: return s?transcode_cstr(BAD_CAST s->str)
765: :String::C("", 0);
766: }
767: const String& Charset::transcode(GdomeDOMString* s) {
768: String::C cstr=transcode_cstr(s);
769: return *new String(cstr.str, cstr.length, true);
1.1 paf 770: }
771:
1.8 paf 772: /// @test less memory using -maybe- xmlParserInputBufferCreateMem
1.35 paf 773: xmlChar* Charset::transcode_buf2xchar(const char* buf, size_t buf_size) {
774: xmlChar* out;
1.30 paf 775: int outlen;
776: int error;
1.35 paf 777: #ifndef NDEBUG
778: int saved_outlen;
779: #endif
780: if(xmlCharEncodingInputFunc input=transcoder(FNAME).input) {
1.32 paf 781: outlen=buf_size*6/*max*/;
1.35 paf 782: #ifndef NDEBUG
783: saved_outlen=outlen;
784: #endif
1.47 ! paf 785: out=(xmlChar*)xmlMalloc(outlen+1);
1.30 paf 786: error=input(
1.17 paf 787: out, &outlen,
1.46 paf 788: (const unsigned char*)buf, (int*)&buf_size
789: #ifdef PA_PATCHED_LIBXML_BACKWARD
790: ,0
791: #endif
792: );
1.30 paf 793: } else {
794: outlen=buf_size;
1.35 paf 795: #ifndef NDEBUG
796: saved_outlen=outlen;
797: #endif
798: out=(xmlChar*)xmlMalloc(outlen+1);
1.30 paf 799: memcpy(out, buf, outlen);
800: error=0;
801: }
1.17 paf 802:
1.30 paf 803: if(error<0)
1.23 paf 804: throw Exception(0,
1.8 paf 805: 0,
1.30 paf 806: "transcode_buf failed (%d)", error);
1.8 paf 807:
1.35 paf 808: assert(outlen<=saved_outlen); out[outlen]=0;
809: return out;
1.24 paf 810: }
1.35 paf 811: GdomeDOMString_auto_ptr Charset::transcode_buf2dom(const char* buf, size_t buf_size) {
812: return GdomeDOMString_auto_ptr(transcode_buf2xchar(buf, buf_size));
1.1 paf 813: }
1.12 paf 814: GdomeDOMString_auto_ptr Charset::transcode(const String& s) {
1.35 paf 815: const char* cstr=s.cstr(String::L_UNSPECIFIED);
1.1 paf 816:
1.24 paf 817: return transcode_buf2dom(cstr, strlen(cstr));
1.1 paf 818: }
1.37 paf 819: GdomeDOMString_auto_ptr Charset::transcode(const String::Body s) {
1.35 paf 820: const char* cstr=s.cstr();
821:
822: return transcode_buf2dom(cstr, s.length());
823: }
1.36 paf 824: #endif
1.34 paf 825:
1.37 paf 826: String::Body Charset::transcode(const String::Body src,
1.34 paf 827: const Charset& source_transcoder,
1.35 paf 828: const Charset& dest_transcoder) {
1.34 paf 829:
1.35 paf 830: const char *src_ptr=src.cstr();
1.34 paf 831: size_t src_size=strlen(src_ptr);
832:
1.35 paf 833: String::C dest=Charset::transcode(String::C(src_ptr, src_size),
834: source_transcoder,
835: dest_transcoder);
1.34 paf 836:
1.37 paf 837: return String::Body(dest.str, dest.length);
1.35 paf 838: }
839:
840: String& Charset::transcode(const String& src,
841: const Charset& source_transcoder,
842: const Charset& dest_transcoder) {
843: if(!src.length())
844: return *new String("", 0, false);
1.34 paf 845:
1.37 paf 846: return *new String(transcode((String::Body)src, source_transcoder, dest_transcoder), String::L_CLEAN);
1.34 paf 847: }
848:
1.35 paf 849: void Charset::transcode(ArrayString& src,
1.34 paf 850: const Charset& source_transcoder,
1.35 paf 851: const Charset& dest_transcoder) {
852: for(size_t i=0; i<src.count(); i++)
853: src.put(i, &transcode(*src[i], source_transcoder, dest_transcoder));
1.34 paf 854: }
855:
856: #ifndef DOXYGEN
857: struct Transcode_pair_info {
858: const Charset* source_transcoder;
859: const Charset* dest_transcoder;
860: };
861: #endif
1.40 paf 862: static void transcode_pair(const String::Body /*akey*/,
1.37 paf 863: String::Body& avalue,
1.35 paf 864: Transcode_pair_info* info) {
865: avalue=Charset::transcode(avalue,
866: *info->source_transcoder,
867: *info->dest_transcoder);
1.34 paf 868: }
1.35 paf 869: void Charset::transcode(HashStringString& src,
1.34 paf 870: const Charset& source_transcoder,
1.35 paf 871: const Charset& dest_transcoder) {
872: Transcode_pair_info info={&source_transcoder, &dest_transcoder};
873: src.for_each_ref(transcode_pair, &info);
1.34 paf 874: }
E-mail: