Annotation of parser3/src/main/pa_charset.C, revision 1.57
1.1 paf 1: /** @file
2: Parser: Charset connection implementation.
3:
1.52 paf 4: Copyright(c) 2001-2005 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.57 ! misha 8: static const char * const IDENT_CHARSET_C="$Date: 2007/04/23 10:30:31 $";
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++)
1.53 paf 41: lcc_table[i]=fcc_table[i]=(unsigned char)i;
1.1 paf 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
1.53 paf 137: unsigned char c=0;
1.1 paf 138: char *cell;
1.42 paf 139: for(int column=0; (cell=lsplit(&row, '\t')); column++) {
1.1 paf 140: switch(column) {
1.53 paf 141: case 0: c=(unsigned char)to_wchar_code(cell); break;
1.1 paf 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;
1.53 paf 148: case 6: element2case(c, (unsigned char)to_wchar_code(cell), pcre_tables); break;
1.1 paf 149: case 7:
150: case 8:
151: // charset
1.10 paf 152: if(tables.toTableSize>MAX_CHARSET_UNI_CODES)
1.56 misha 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,
1.49 paf 396: "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes); // never
1.35 paf 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;
1.49 paf 406: else {
1.50 paf 407: outPtr+=sprintf((char *)outPtr, "&#%u;", tmpVal); // &#decimal;
1.49 paf 408: }
409: } else {
410: const XMLByte* recoverPtr=srcPtr-trailingBytes-1;
411: for(uint i=0; i<=trailingBytes; i++)
412: outPtr+=sprintf((char*)outPtr, "%%%02X", *recoverPtr++);
413: }
1.1 paf 414: }
1.35 paf 415:
416: // Update the bytes eaten
417: srcLen = srcPtr - srcData;
418:
419: // Return the characters read
420: toFillLen = outPtr - toFill;
1.11 paf 421:
1.29 paf 422: //return srcPtr==srcEnd?(int)toFillLen:-1;
423: /*
424: xmlCharEncodingOutputFunc
425: Returns :
426: 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
427: number of octets consumed as the return value is positive, else unpredictiable. The value of outlen after return is the number
428: of ocetes consumed.
429: */
430: return 0;
1.10 paf 431: }
432:
433: /// @todo not so memory-hungry with prescan
1.35 paf 434: const String::C Charset::transcodeToUTF8(const String::C src) const {
435: size_t src_length=src.length;
436: size_t dest_length=src.length*6/*so that surly enough, max utf8 seq len=6*/;
437: #ifndef NDEBUG
438: size_t saved_dest_length=dest_length;
439: #endif
440: XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/];
1.11 paf 441:
442: if(::transcodeToUTF8(
1.35 paf 443: (XMLByte *)src.str, src_length,
444: dest_body, dest_length,
1.11 paf 445: tables)<0)
1.43 paf 446: throw Exception(0,
1.10 paf 447: 0,
1.11 paf 448: "Charset::transcodeToUTF8 buffer overflow");
1.10 paf 449:
1.35 paf 450: assert(dest_length<=saved_dest_length); dest_body[dest_length]=0; // terminator
451: return String::C((char*)dest_body, dest_length);
1.10 paf 452: }
1.38 paf 453:
454: static XMLCh change_case_UTF8(const XMLCh src, const Charset::UTF8CaseTable& table) {
1.39 paf 455: int lo = 0;
456: int hi = table.size - 1;
457: while(lo<=hi) {
1.38 paf 458: // Calc the mid point of the low and high offset.
1.39 paf 459: const unsigned int i = (lo + hi) / 2;
460:
461: XMLCh cur=table.records[i].from;
462: if(src==cur)
463: return table.records[i].to;
464: if(src>cur)
465: lo = i+1;
1.38 paf 466: else
1.39 paf 467: hi = i-1;
468: }
469:
470: // not found
1.38 paf 471: return src;
472: }
473:
474: static void store_UTF8(XMLCh src, XMLByte*& outPtr ) {
475: if(!src) {
476: // use the replacement character
477: *outPtr++= '?';
478: return;
479: }
480:
481: // Figure out how many bytes we need
482: unsigned int encodedBytes;
483: if(src<0x80)
484: encodedBytes = 1;
485: else if(src<0x800)
486: encodedBytes = 2;
487: else if(src<0x10000)
488: encodedBytes = 3;
489: else if(src<0x200000)
490: encodedBytes = 4;
491: else if(src<0x4000000)
492: encodedBytes = 5;
493: else if(src<= 0x7FFFFFFF)
494: encodedBytes = 6;
495: else {
496: // use the replacement character
497: *outPtr++= '?';
498: return;
499: }
500:
501: // And spit out the bytes. We spit them out in reverse order
502: // here, so bump up the output pointer and work down as we go.
503: outPtr+= encodedBytes;
504: switch(encodedBytes) {
505: case 6: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
506: src>>= 6;
507: case 5: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
508: src>>= 6;
509: case 4: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
510: src>>= 6;
511: case 3: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
512: src>>= 6;
513: case 2: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
514: src>>= 6;
515: case 1: *--outPtr = XMLByte(src | gFirstByteMark[encodedBytes]);
516: }
517:
518: // Add the encoded bytes back in again to indicate we've eaten them
519: outPtr+= encodedBytes;
520: }
521:
522: static void change_case_UTF8(XMLCh src, XMLByte*& outPtr,
523: const Charset::UTF8CaseTable& table) {
524: store_UTF8(change_case_UTF8(src, table), outPtr);
525: };
1.44 paf 526: void change_case_UTF8(const XMLByte* srcData, size_t srcLen,
527: XMLByte* toFill, size_t toFillLen,
528: const Charset::UTF8CaseTable& table) {
1.38 paf 529: const XMLByte* srcPtr=srcData;
1.44 paf 530: const XMLByte* srcEnd=srcData+srcLen;
1.38 paf 531: XMLByte* outPtr=toFill;
1.44 paf 532: XMLByte* outEnd=toFill+toFillLen;
533:
534: // We now loop until we either run out of input data, or room to store
535: while ((srcPtr < srcEnd) && (outPtr < outEnd)) {
536: // Get the next leading byte out
537: const XMLByte firstByte =* srcPtr;
1.38 paf 538:
539: if(firstByte<= 127) {
540: change_case_UTF8(firstByte, outPtr, table);
541: srcPtr++;
542: continue;
543: }
544:
545: // See how many trailing src bytes this sequence is going to require
546: const unsigned int trailingBytes = gUTFBytes[firstByte];
547:
548: // Looks ok, so lets build up the value
549: uint tmpVal=0;
550: switch(trailingBytes) {
551: case 5: tmpVal+=*srcPtr++; tmpVal<<=6;
552: case 4: tmpVal+=*srcPtr++; tmpVal<<=6;
553: case 3: tmpVal+=*srcPtr++; tmpVal<<=6;
554: case 2: tmpVal+=*srcPtr++; tmpVal<<=6;
555: case 1: tmpVal+=*srcPtr++; tmpVal<<=6;
556: case 0: tmpVal+=*srcPtr++;
557: break;
558:
559: default:
560: throw Exception(0,
561: 0,
562: "change_case_UTF8 error: wrong trailingBytes value(%d)", trailingBytes);
563: }
564: tmpVal-=gUTFOffsets[trailingBytes];
565:
566: // If it will fit into a single char, then put it in. Otherwise
567: // fail [*encode it as a surrogate pair. If its not valid, use the
568: // replacement char.*]
569: if(!(tmpVal & 0xFFFF0000))
570: change_case_UTF8(tmpVal, outPtr, table);
571: else
572: throw Exception(0,
573: 0,
574: "change_case_UTF8 error: too big tmpVal(0x%08X)", tmpVal);
575: }
576:
577: if(srcPtr!=outPtr)
578: throw Exception(0,
579: 0,
580: "change_case_UTF8 error: end pointers do not match");
581: }
582:
583:
1.35 paf 584: const String::C Charset::transcodeFromUTF8(const String::C src) const {
585: size_t src_length=src.length;
586: size_t dest_length=src.length*6/*so that surly enough, "ÿ" has max ratio */;
587: #ifndef NDEBUG
588: size_t saved_dest_length=dest_length;
589: #endif
590: XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/];
1.11 paf 591:
592: if(::transcodeFromUTF8(
1.35 paf 593: (XMLByte *)src.str, src_length,
594: dest_body, dest_length,
1.11 paf 595: tables)<0)
1.43 paf 596: throw Exception(0,
1.10 paf 597: 0,
1.35 paf 598: "Charset::transcodeFromUTF8 buffer overflow");
1.10 paf 599:
1.35 paf 600: assert(dest_length<=saved_dest_length); dest_body[dest_length]=0; // terminator
601: return String::C((char*)dest_body, dest_length);
1.1 paf 602: }
603:
604: /// transcode using both charsets
1.35 paf 605: const String::C Charset::transcodeToCharset(const String::C src,
606: const Charset& dest_charset) const {
607: if(&dest_charset==this)
608: return src;
609: else {
610: size_t dest_length=src.length;
611: XMLByte* dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/];
612:
613: XMLByte* output=dest_body;
614: const XMLByte* input=(XMLByte *)src.str;
615: while(XMLCh c=*input++) {
616: XMLCh curVal = tables.fromTable[c];
617: *output++=curVal?
618: xlatOneTo(curVal, dest_charset.tables, '?') // OK
619: :'?'; // use the replacement character
1.6 paf 620: }
1.1 paf 621:
1.35 paf 622: dest_body[dest_length]=0; // terminator
623: return String::C((char*)dest_body, dest_length);
1.6 paf 624: }
1.1 paf 625: }
626:
1.57 ! misha 627: XMLByte Charset::transcodeCharFromUTF8(XMLCh utf8code, XMLByte not_found){
! 628: return xlatOneTo(utf8code, tables, not_found);
! 629: }
! 630:
! 631:
1.1 paf 632: #ifdef XML
1.10 paf 633:
1.35 paf 634: static const Charset::Tables* tables[MAX_CHARSETS];
635:
1.46 paf 636: #ifdef PA_PATCHED_LIBXML_BACKWARD
637:
638: #define declareXml256ioFuncs(i) \
639: static int xml256CharEncodingInputFunc##i( \
640: unsigned char *out, int *outlen, \
641: const unsigned char *in, int *inlen, void*) { \
642: return transcodeToUTF8( \
643: in, *(size_t*)inlen, \
644: out, *(size_t*)outlen, \
645: *tables[i]); \
646: } \
647: static int xml256CharEncodingOutputFunc##i( \
648: unsigned char *out, int *outlen, \
649: const unsigned char *in, int *inlen, void*) { \
650: return transcodeFromUTF8( \
651: in, *(size_t*)inlen, \
652: out, *(size_t*)outlen, \
653: *tables[i]); \
654: }
655:
656: #else
657:
1.35 paf 658: #define declareXml256ioFuncs(i) \
659: static int xml256CharEncodingInputFunc##i( \
660: unsigned char *out, int *outlen, \
661: const unsigned char *in, int *inlen) { \
662: return transcodeToUTF8( \
663: in, *(size_t*)inlen, \
664: out, *(size_t*)outlen, \
665: *tables[i]); \
666: } \
667: static int xml256CharEncodingOutputFunc##i( \
668: unsigned char *out, int *outlen, \
669: const unsigned char *in, int *inlen) { \
670: return transcodeFromUTF8( \
671: in, *(size_t*)inlen, \
672: out, *(size_t*)outlen, \
673: *tables[i]); \
674: }
675:
1.46 paf 676: #endif
677:
678:
1.35 paf 679: declareXml256ioFuncs(0) declareXml256ioFuncs(1)
680: declareXml256ioFuncs(2) declareXml256ioFuncs(3)
681: declareXml256ioFuncs(4) declareXml256ioFuncs(5)
682: declareXml256ioFuncs(6) declareXml256ioFuncs(7)
683: declareXml256ioFuncs(8) declareXml256ioFuncs(9)
684:
685: static xmlCharEncodingInputFunc inputFuncs[MAX_CHARSETS]={
686: xml256CharEncodingInputFunc0, xml256CharEncodingInputFunc1,
687: xml256CharEncodingInputFunc2, xml256CharEncodingInputFunc3,
688: xml256CharEncodingInputFunc4, xml256CharEncodingInputFunc5,
689: xml256CharEncodingInputFunc6, xml256CharEncodingInputFunc7,
690: xml256CharEncodingInputFunc8, xml256CharEncodingInputFunc9
691: };
692: static xmlCharEncodingOutputFunc outputFuncs[MAX_CHARSETS]={
693: xml256CharEncodingOutputFunc0, xml256CharEncodingOutputFunc1,
694: xml256CharEncodingOutputFunc2, xml256CharEncodingOutputFunc3,
695: xml256CharEncodingOutputFunc4, xml256CharEncodingOutputFunc5,
696: xml256CharEncodingOutputFunc6, xml256CharEncodingOutputFunc7,
697: xml256CharEncodingOutputFunc8, xml256CharEncodingOutputFunc9
698: };
699: static size_t handlers_count=0;
1.10 paf 700:
701: void Charset::addEncoding(char *name_cstr) {
1.35 paf 702: if(handlers_count==MAX_CHARSETS)
703: throw Exception(0,
704: 0,
705: "already allocated %d handlers, no space for new encoding '%s'",
706: MAX_CHARSETS, name_cstr);
707:
1.45 paf 708: xmlCharEncodingHandler* handler=new(UseGC) xmlCharEncodingHandler;
1.35 paf 709: {
710: handler->name=name_cstr;
711: handler->input=inputFuncs[handlers_count];
712: handler->output=outputFuncs[handlers_count];
713: ::tables[handlers_count]=&tables;
714: handlers_count++;
715: }
1.10 paf 716:
717: xmlRegisterCharEncodingHandler(handler);
1.35 paf 718:
1.10 paf 719: }
720:
1.37 paf 721: void Charset::initTranscoder(const String::Body NAME, const char* name_cstr) {
1.15 paf 722: ftranscoder=xmlFindCharEncodingHandler(name_cstr);
1.35 paf 723: transcoder(NAME); // check right way
1.15 paf 724: }
725:
1.37 paf 726: xmlCharEncodingHandler& Charset::transcoder(const String::Body NAME) {
1.15 paf 727: if(!ftranscoder)
1.56 misha 728: throw Exception(PARSER_RUNTIME,
1.35 paf 729: new String(NAME, String::L_TAINTED),
1.10 paf 730: "unsupported encoding");
1.35 paf 731: return *ftranscoder;
1.10 paf 732: }
733:
1.54 paf 734: String::C Charset::transcode_cstr(const xmlChar* s) {
1.13 paf 735: if(!s)
1.35 paf 736: return String::C("", 0);
1.8 paf 737:
1.35 paf 738: int inlen=strlen((const char*)s);
1.51 paf 739: int outlen=inlen*6/*strlen("ÿ")*/; // max
1.35 paf 740: #ifndef NDEBUG
741: int saved_outlen=outlen;
742: #endif
743: char *out=new(PointerFreeGC) char[outlen+1];
1.8 paf 744:
1.30 paf 745: int error;
1.35 paf 746: if(xmlCharEncodingOutputFunc output=transcoder(FNAME).output) {
1.30 paf 747: error=output(
1.17 paf 748: (unsigned char*)out, &outlen,
1.46 paf 749: (const unsigned char*)s, &inlen
750: #ifdef PA_PATCHED_LIBXML_BACKWARD
751: ,0
752: #endif
753: );
1.30 paf 754: } else {
755: memcpy(out, s, outlen=inlen);
756: error=0;
757: }
758: if(error<0)
1.23 paf 759: throw Exception(0,
1.8 paf 760: 0,
1.30 paf 761: "transcode_cstr failed (%d)", error);
1.8 paf 762:
1.35 paf 763: assert(outlen<=saved_outlen); out[outlen]=0;
764: return String::C(out, outlen);
1.14 paf 765: }
1.54 paf 766: const String& Charset::transcode(const xmlChar* s) {
1.35 paf 767: String::C cstr=transcode_cstr(s);
768: return *new String(cstr.str, cstr.length, true);
1.1 paf 769: }
770:
1.8 paf 771: /// @test less memory using -maybe- xmlParserInputBufferCreateMem
1.35 paf 772: xmlChar* Charset::transcode_buf2xchar(const char* buf, size_t buf_size) {
773: xmlChar* out;
1.30 paf 774: int outlen;
775: int error;
1.35 paf 776: #ifndef NDEBUG
777: int saved_outlen;
778: #endif
779: if(xmlCharEncodingInputFunc input=transcoder(FNAME).input) {
1.51 paf 780: outlen=buf_size*6/*max UTF8 bytes per char*/;
1.35 paf 781: #ifndef NDEBUG
782: saved_outlen=outlen;
783: #endif
1.47 paf 784: out=(xmlChar*)xmlMalloc(outlen+1);
1.30 paf 785: error=input(
1.17 paf 786: out, &outlen,
1.46 paf 787: (const unsigned char*)buf, (int*)&buf_size
788: #ifdef PA_PATCHED_LIBXML_BACKWARD
789: ,0
790: #endif
791: );
1.30 paf 792: } else {
793: outlen=buf_size;
1.35 paf 794: #ifndef NDEBUG
795: saved_outlen=outlen;
796: #endif
797: out=(xmlChar*)xmlMalloc(outlen+1);
1.30 paf 798: memcpy(out, buf, outlen);
799: error=0;
800: }
1.17 paf 801:
1.30 paf 802: if(error<0)
1.23 paf 803: throw Exception(0,
1.8 paf 804: 0,
1.30 paf 805: "transcode_buf failed (%d)", error);
1.8 paf 806:
1.35 paf 807: assert(outlen<=saved_outlen); out[outlen]=0;
808: return out;
1.24 paf 809: }
1.54 paf 810: xmlChar* Charset::transcode(const String& s) {
1.35 paf 811: const char* cstr=s.cstr(String::L_UNSPECIFIED);
1.1 paf 812:
1.54 paf 813: return transcode_buf2xchar(cstr, strlen(cstr));
1.1 paf 814: }
1.54 paf 815: xmlChar* Charset::transcode(const String::Body s) {
1.35 paf 816: const char* cstr=s.cstr();
817:
1.54 paf 818: return transcode_buf2xchar(cstr, s.length());
1.35 paf 819: }
1.36 paf 820: #endif
1.34 paf 821:
1.37 paf 822: String::Body Charset::transcode(const String::Body src,
1.34 paf 823: const Charset& source_transcoder,
1.35 paf 824: const Charset& dest_transcoder) {
1.34 paf 825:
1.35 paf 826: const char *src_ptr=src.cstr();
1.34 paf 827: size_t src_size=strlen(src_ptr);
828:
1.35 paf 829: String::C dest=Charset::transcode(String::C(src_ptr, src_size),
830: source_transcoder,
831: dest_transcoder);
1.34 paf 832:
1.37 paf 833: return String::Body(dest.str, dest.length);
1.35 paf 834: }
835:
836: String& Charset::transcode(const String& src,
837: const Charset& source_transcoder,
838: const Charset& dest_transcoder) {
839: if(!src.length())
840: return *new String("", 0, false);
1.34 paf 841:
1.37 paf 842: return *new String(transcode((String::Body)src, source_transcoder, dest_transcoder), String::L_CLEAN);
1.34 paf 843: }
844:
1.35 paf 845: void Charset::transcode(ArrayString& src,
1.34 paf 846: const Charset& source_transcoder,
1.35 paf 847: const Charset& dest_transcoder) {
848: for(size_t i=0; i<src.count(); i++)
849: src.put(i, &transcode(*src[i], source_transcoder, dest_transcoder));
1.34 paf 850: }
851:
852: #ifndef DOXYGEN
853: struct Transcode_pair_info {
854: const Charset* source_transcoder;
855: const Charset* dest_transcoder;
856: };
857: #endif
1.40 paf 858: static void transcode_pair(const String::Body /*akey*/,
1.37 paf 859: String::Body& avalue,
1.35 paf 860: Transcode_pair_info* info) {
861: avalue=Charset::transcode(avalue,
862: *info->source_transcoder,
863: *info->dest_transcoder);
1.34 paf 864: }
1.35 paf 865: void Charset::transcode(HashStringString& src,
1.34 paf 866: const Charset& source_transcoder,
1.35 paf 867: const Charset& dest_transcoder) {
868: Transcode_pair_info info={&source_transcoder, &dest_transcoder};
1.55 paf 869: src.for_each_ref<Transcode_pair_info*>(transcode_pair, &info);
1.34 paf 870: }
E-mail: