Annotation of parser3/src/main/pa_charset.C, revision 1.92
1.1 paf 1: /** @file
2: Parser: Charset connection implementation.
3:
1.90 moko 4: Copyright (c) 2001-2012 Art. Lebedev Studio (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:
8: #include "pa_charset.h"
1.35 paf 9: #include "pa_charsets.h"
1.1 paf 10:
1.92 ! moko 11: volatile const char * IDENT_PA_CHARSET_C="$Id: pa_charset.C,v 1.91 2012/05/28 10:33:18 moko Exp $" IDENT_PA_CHARSET_H;
1.90 moko 12:
1.1 paf 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
1.67 misha 18:
19: // reduce memory usage by pre-calculation utf-8 string length
1.60 misha 20: #define PRECALCULATE_DEST_LENGTH
1.46 paf 21:
1.38 paf 22: // globals
23:
24: Charset::UTF8CaseTable::Rec UTF8CaseToUpperRecords[]={
25: #include "utf8-to-upper.inc"
26: };
27: Charset::UTF8CaseTable UTF8CaseToUpper={
28: sizeof(UTF8CaseToUpperRecords)/sizeof(Charset::UTF8CaseTable::Rec),
29: UTF8CaseToUpperRecords};
30:
31: Charset::UTF8CaseTable::Rec UTF8CaseToLowerRecords[]={
32: #include "utf8-to-lower.inc"
33: };
34: Charset::UTF8CaseTable UTF8CaseToLower={
35: sizeof(UTF8CaseToLowerRecords)/sizeof(Charset::UTF8CaseTable::Rec),
36: UTF8CaseToLowerRecords};
37:
1.1 paf 38: // helpers
39:
40: inline void prepare_case_tables(unsigned char *tables) {
41: unsigned char *lcc_table=tables+lcc_offset;
42: unsigned char *fcc_table=tables+fcc_offset;
43: for(int i=0; i<0x100; i++)
1.53 paf 44: lcc_table[i]=fcc_table[i]=(unsigned char)i;
1.1 paf 45: }
46: inline void cstr2ctypes(unsigned char *tables, const unsigned char *cstr,
47: unsigned char bit) {
48: unsigned char *ctypes_table=tables+ctypes_offset;
49: ctypes_table[0]=bit;
50: for(; *cstr; cstr++) {
51: unsigned char c=*cstr;
52: ctypes_table[c]|=bit;
53: }
54: }
1.35 paf 55: inline unsigned int to_wchar_code(const char* cstr) {
1.1 paf 56: if(!cstr || !*cstr)
57: return 0;
58: if(cstr[1]==0)
1.4 paf 59: return(unsigned int)(unsigned char)cstr[0];
1.1 paf 60:
1.91 moko 61: return pa_atoui(cstr,0);
1.1 paf 62: }
1.35 paf 63: inline bool to_bool(const char* cstr) {
1.1 paf 64: return cstr && *cstr!=0;
65: }
66: static void element2ctypes(unsigned char c, bool belongs,
67: unsigned char *tables, unsigned char bit, int group_offset=-1) {
68: if(!belongs)
69: return;
70:
71: unsigned char *ctypes_table=tables+ctypes_offset;
72:
73: ctypes_table[c]|=bit;
74: if(group_offset>=0)
1.4 paf 75: tables[cbits_offset+group_offset+c/8] |= 1<<(c%8);
1.1 paf 76: }
77: static void element2case(unsigned char from, unsigned char to,
78: unsigned char *tables) {
79: if(!to)
80: return;
81:
82: unsigned char *lcc_table=tables+lcc_offset;
83: unsigned char *fcc_table=tables+fcc_offset;
84: lcc_table[from]=to;
85: fcc_table[from]=to; fcc_table[to]=from;
86: }
87:
88: // methods
89:
1.37 paf 90: Charset::Charset(Request_charsets* charsets, const String::Body ANAME, const String* afile_spec):
1.35 paf 91: FNAME(ANAME),
92: FNAME_CSTR(ANAME.cstrm()) {
1.7 paf 93:
1.35 paf 94: if(afile_spec) {
1.1 paf 95: fisUTF8=false;
1.35 paf 96: load_definition(*charsets, *afile_spec);
1.1 paf 97: #ifdef XML
1.35 paf 98: addEncoding(FNAME_CSTR);
1.1 paf 99: #endif
100: } else {
101: fisUTF8=true;
1.4 paf 102: // grab default onces [for UTF-8 so to be able to make a-z =>A-Z
1.65 misha 103: memcpy(pcre_tables, _pcre_default_tables, sizeof(pcre_tables));
1.1 paf 104: }
105:
106: #ifdef XML
1.35 paf 107: initTranscoder(FNAME, FNAME_CSTR);
1.1 paf 108: #endif
109: }
110:
1.35 paf 111: void Charset::load_definition(Request_charsets& charsets, const String& afile_spec) {
1.1 paf 112: // pcre_tables
113: // lowcase, flipcase, bits digit+word+whitespace, masks
114:
115: // must not move this inside of prepare_case_tables
116: // don't know the size there
117: memset(pcre_tables, 0, sizeof(pcre_tables));
118: prepare_case_tables(pcre_tables);
1.4 paf 119: cstr2ctypes(pcre_tables,(const unsigned char *)"*+?{^.$|()[", ctype_meta);
1.1 paf 120:
121: // charset
1.35 paf 122: memset(&tables, 0, sizeof(tables));
1.1 paf 123:
124: // loading text
1.35 paf 125: char *data=file_read_text(charsets, afile_spec);
1.1 paf 126:
127: // ignore header
128: getrow(&data);
129:
130: // parse cells
131: char *row;
1.42 paf 132: while((row=getrow(&data))) {
1.1 paf 133: // remove empty&comment lines
134: if(!*row || *row=='#')
135: continue;
136:
137: // char white-space digit hex-digit letter word lowercase unicode1 unicode2
1.53 paf 138: unsigned char c=0;
1.1 paf 139: char *cell;
1.42 paf 140: for(int column=0; (cell=lsplit(&row, '\t')); column++) {
1.1 paf 141: switch(column) {
1.53 paf 142: case 0: c=(unsigned char)to_wchar_code(cell); break;
1.1 paf 143: // pcre_tables
144: case 1: element2ctypes(c, to_bool(cell), pcre_tables, ctype_space, cbit_space); break;
145: case 2: element2ctypes(c, to_bool(cell), pcre_tables, ctype_digit, cbit_digit); break;
146: case 3: element2ctypes(c, to_bool(cell), pcre_tables, ctype_xdigit); break;
147: case 4: element2ctypes(c, to_bool(cell), pcre_tables, ctype_letter); break;
148: case 5: element2ctypes(c, to_bool(cell), pcre_tables, ctype_word, cbit_word); break;
1.53 paf 149: case 6: element2case(c, (unsigned char)to_wchar_code(cell), pcre_tables); break;
1.1 paf 150: case 7:
151: case 8:
152: // charset
1.10 paf 153: if(tables.toTableSize>MAX_CHARSET_UNI_CODES)
1.56 misha 154: throw Exception(PARSER_RUNTIME,
1.35 paf 155: &afile_spec,
1.1 paf 156: "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES);
157:
158: XMLCh unicode=(XMLCh)to_wchar_code(cell);
159: if(!unicode && column==7/*unicode1 column*/)
160: unicode=(XMLCh)c;
161: if(unicode) {
1.10 paf 162: if(!tables.fromTable[c])
163: tables.fromTable[c]=unicode;
164: tables.toTable[tables.toTableSize].intCh=unicode;
165: tables.toTable[tables.toTableSize].extCh=(XMLByte)c;
166: tables.toTableSize++;
1.1 paf 167: }
168: break;
169: }
170: }
171: };
172:
1.87 moko 173: // parser charset tables declare only white-space before 0x20, thus adding the missing chars
174: for(uint i=0; i<0x20; i++)
175: if(!tables.fromTable[i]){
176: tables.fromTable[i]=i;
177: tables.toTable[tables.toTableSize].intCh=i;
178: tables.toTable[tables.toTableSize].extCh=(XMLByte)i;
179: tables.toTableSize++;
180: }
181:
1.1 paf 182: // sort by the Unicode code point
183: sort_ToTable();
184: }
185:
186: static int sort_cmp_Trans_rec_intCh(const void *a, const void *b) {
187: return
1.38 paf 188: static_cast<const Charset::Tables::Rec *>(a)->intCh-
189: static_cast<const Charset::Tables::Rec *>(b)->intCh;
1.1 paf 190: }
191:
192: void Charset::sort_ToTable() {
1.92 ! moko 193: qsort(tables.toTable, tables.toTableSize, sizeof(*tables.toTable), sort_cmp_Trans_rec_intCh);
1.1 paf 194: //FILE *f=fopen("c:\\temp\\a", "wb");
1.10 paf 195: //fwrite(tables.toTable, tables.toTableSize, sizeof(*tables.toTable), f);
1.1 paf 196: //fclose(f);
197: }
198:
1.60 misha 199: // @todo: precache for spedup searching
1.10 paf 200: static XMLByte xlatOneTo(const XMLCh toXlat,
1.35 paf 201: const Charset::Tables& tables,
202: XMLByte not_found) {
1.80 misha 203: int lo = 0;
204: int hi = tables.toTableSize - 1;
1.39 paf 205: while(lo<=hi) {
1.35 paf 206: // Calc the mid point of the low and high offset.
1.39 paf 207: const unsigned int i = (lo + hi) / 2;
208:
209: XMLCh cur=tables.toTable[i].intCh;
210: if(toXlat==cur)
211: return tables.toTable[i].extCh;
212: if(toXlat>cur)
213: lo = i+1;
1.1 paf 214: else
1.39 paf 215: hi = i-1;
216: }
1.35 paf 217:
218: return not_found;
1.1 paf 219: }
220:
1.35 paf 221: String::C Charset::transcode(const String::C src,
222: const Charset& source_charset,
223: const Charset& dest_charset) {
224: if(!src.length)
225: return String::C("", 0);
1.4 paf 226:
1.1 paf 227: switch((source_charset.isUTF8()?0x10:0x00)|(dest_charset.isUTF8()?0x01:0x00)) {
228: default: // 0x00
1.35 paf 229: return source_charset.transcodeToCharset(src, dest_charset);
1.1 paf 230: case 0x01:
1.35 paf 231: return source_charset.transcodeToUTF8(src);
1.1 paf 232: case 0x10:
1.35 paf 233: return dest_charset.transcodeFromUTF8(src);
1.1 paf 234: case 0x11:
1.35 paf 235: return src;
1.1 paf 236: }
237: }
238:
239: // ---------------------------------------------------------------------------
240: // Local static data
241: //
242: // gUTFBytes
243: // A list of counts of trailing bytes for each initial byte in the input.
244: //
245: // gUTFOffsets
246: // A list of values to offset each result char type, according to how
247: // many source bytes when into making it.
248: //
249: // gFirstByteMark
250: // A list of values to mask onto the first byte of an encoded sequence,
251: // indexed by the number of bytes used to create the sequence.
252: // ---------------------------------------------------------------------------
253: static const XMLByte gUTFBytes[0x100] = {
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: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
257: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
258: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
259: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
260: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
261: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
262: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
263: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
264: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
265: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
266: , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
267: , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
268: , 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
269: , 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
270: };
271:
272: static const uint gUTFOffsets[6] = {
1.80 misha 273: 0, 0x3080, 0xE2080, 0x3C82080, 0xFA082080, 0x82082080
1.1 paf 274: };
275:
276: static const XMLByte gFirstByteMark[7] = {
1.80 misha 277: 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
1.1 paf 278: };
279:
1.71 misha 280: static int transcodeToUTF8(const XMLByte* srcData, int& srcLen,
281: XMLByte *toFill, int& toFillLen,
282: const Charset::Tables& tables) {
1.11 paf 283: const XMLByte* srcPtr=srcData;
284: const XMLByte* srcEnd=srcData+srcLen;
285: XMLByte* outPtr=toFill;
286: XMLByte* outEnd=toFill+toFillLen;
1.1 paf 287:
1.35 paf 288: while(srcPtr<srcEnd) {
289: uint curVal = tables.fromTable[*srcPtr];
1.1 paf 290: if(!curVal) {
1.35 paf 291: // use the replacement character
292: *outPtr++= '?';
293: srcPtr++;
294: continue;
295: }
1.1 paf 296:
1.35 paf 297: // Figure out how many bytes we need
298: unsigned int encodedBytes;
299: if(curVal<0x80)
300: encodedBytes = 1;
301: else if(curVal<0x800)
302: encodedBytes = 2;
303: else if(curVal<0x10000)
304: encodedBytes = 3;
305: else if(curVal<0x200000)
306: encodedBytes = 4;
307: else if(curVal<0x4000000)
308: encodedBytes = 5;
309: else if(curVal<= 0x7FFFFFFF)
310: encodedBytes = 6;
311: else {
312: // use the replacement character
313: *outPtr++= '?';
314: srcPtr++;
315: continue;
316: }
1.11 paf 317:
1.35 paf 318: // If we cannot fully get this char into the output buffer
319: if (outPtr + encodedBytes > outEnd)
320: break;
321:
322: // We can do it, so update the source index
323: srcPtr++;
324:
325: // And spit out the bytes. We spit them out in reverse order
326: // here, so bump up the output pointer and work down as we go.
327: outPtr+= encodedBytes;
328: switch(encodedBytes) {
1.60 misha 329: case 6: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
330: curVal>>= 6;
331: case 5: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
332: curVal>>= 6;
333: case 4: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
334: curVal>>= 6;
335: case 3: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
336: curVal>>= 6;
337: case 2: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
338: curVal>>= 6;
339: case 1: *--outPtr = XMLByte(curVal | gFirstByteMark[encodedBytes]);
1.35 paf 340: }
341:
342: // Add the encoded bytes back in again to indicate we've eaten them
343: outPtr+= encodedBytes;
344: }
345:
346: // Update the bytes eaten
347: srcLen = srcPtr - srcData;
348:
349: // Return the characters read
350: toFillLen = outPtr - toFill;
351:
1.29 paf 352: //return srcPtr==srcEnd?(int)toFillLen:-1;
353: /*
354: xmlCharEncodingInputFunc
355: Returns :
356: 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
357: number of octets consumed as the return value is positive, else unpredictiable. The value of outlen after return is the number
358: of ocetes consumed.
359: */
360: return 0;
1.1 paf 361: }
1.26 paf 362: /// @todo digital entites only when xml/html output [at output in html/xml mode, in html part of a letter]
1.71 misha 363: static int transcodeFromUTF8(const XMLByte* srcData, int& srcLen,
364: XMLByte* toFill, int& toFillLen,
365: const Charset::Tables& tables) {
1.11 paf 366: const XMLByte* srcPtr=srcData;
367: const XMLByte* srcEnd=srcData+srcLen;
368: XMLByte* outPtr=toFill;
369: XMLByte* outEnd=toFill+toFillLen;
1.1 paf 370:
1.35 paf 371: // We now loop until we either run out of input data, or room to store
372: while ((srcPtr < srcEnd) && (outPtr < outEnd)) {
373: // Get the next leading byte out
374: const XMLByte firstByte =* srcPtr;
375:
376: // Special-case ASCII, which is a leading byte value of<= 127
1.60 misha 377: if(firstByte<=127) {
1.35 paf 378: *outPtr++= firstByte;
379: srcPtr++;
380: continue;
381: }
382:
383: // See how many trailing src bytes this sequence is going to require
384: const unsigned int trailingBytes = gUTFBytes[firstByte];
385:
386: // If there are not enough source bytes to do this one, then we
387: // are done. Note that we done>= here because we are implicitly
388: // counting the 1 byte we get no matter what.
389: if(srcPtr+trailingBytes>= srcEnd)
390: break;
391:
392: // Looks ok, so lets build up the value
393: uint tmpVal=0;
394: switch(trailingBytes) {
395: case 5: tmpVal+=*srcPtr++; tmpVal<<=6;
396: case 4: tmpVal+=*srcPtr++; tmpVal<<=6;
397: case 3: tmpVal+=*srcPtr++; tmpVal<<=6;
398: case 2: tmpVal+=*srcPtr++; tmpVal<<=6;
399: case 1: tmpVal+=*srcPtr++; tmpVal<<=6;
400: case 0: tmpVal+=*srcPtr++;
401: break;
402:
403: default:
404: throw Exception(0,
405: 0,
1.49 paf 406: "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes); // never
1.35 paf 407: }
408: tmpVal-=gUTFOffsets[trailingBytes];
409:
410: // If it will fit into a single char, then put it in. Otherwise
411: // fail [*encode it as a surrogate pair. If its not valid, use the
412: // replacement char.*]
413: if(!(tmpVal & 0xFFFF0000)) {
1.25 paf 414: if(XMLByte xlat=xlatOneTo(tmpVal, tables, 0))
415: *outPtr++=xlat;
1.49 paf 416: else {
1.50 paf 417: outPtr+=sprintf((char *)outPtr, "&#%u;", tmpVal); // &#decimal;
1.49 paf 418: }
419: } else {
420: const XMLByte* recoverPtr=srcPtr-trailingBytes-1;
421: for(uint i=0; i<=trailingBytes; i++)
422: outPtr+=sprintf((char*)outPtr, "%%%02X", *recoverPtr++);
423: }
1.1 paf 424: }
1.35 paf 425:
426: // Update the bytes eaten
427: srcLen = srcPtr - srcData;
428:
429: // Return the characters read
430: toFillLen = outPtr - toFill;
1.11 paf 431:
1.29 paf 432: //return srcPtr==srcEnd?(int)toFillLen:-1;
433: /*
434: xmlCharEncodingOutputFunc
435: Returns :
436: 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
437: number of octets consumed as the return value is positive, else unpredictiable. The value of outlen after return is the number
438: of ocetes consumed.
439: */
440: return 0;
1.10 paf 441: }
442:
1.85 misha 443: static bool need_escape(XMLByte c){
1.60 misha 444: return
1.66 misha 445: !(
446: (c<=127)
447: && (
1.89 misha 448: pa_isalnum((unsigned char)c)
1.66 misha 449: || strchr("*@-_+./", c)!=0
450: )
451: );
1.60 misha 452: }
453:
1.70 misha 454: // read one UTF8 char and return length of this char (in bytes)
455: static unsigned int readUTF8Char(const XMLByte*& srcPtr, const XMLByte* srcEnd, XMLByte& firstByte, XMLCh& UTF8Char){
1.60 misha 456: if(!srcPtr || !*srcPtr || srcPtr>=srcEnd)
457: return 0;
458:
459: firstByte=*srcPtr;
460:
461: if(firstByte<=127){
462: UTF8Char=firstByte;
463: srcPtr++;
464: return 1;
465: }
466:
467: unsigned int trailingBytes=gUTFBytes[firstByte];
468:
469: if(srcPtr+trailingBytes>=srcEnd){
470: return 0; // not enough bytes in source string for reading
471: }
472:
473: uint tmpVal=0;
474: switch(trailingBytes){
475: case 5: tmpVal+=*srcPtr++; tmpVal<<=6;
476: case 4: tmpVal+=*srcPtr++; tmpVal<<=6;
477: case 3: tmpVal+=*srcPtr++; tmpVal<<=6;
478: case 2: tmpVal+=*srcPtr++; tmpVal<<=6;
479: case 1: tmpVal+=*srcPtr++; tmpVal<<=6;
480: case 0: tmpVal+=*srcPtr++;
481: }
482:
483: tmpVal-=gUTFOffsets[trailingBytes];
484: UTF8Char=tmpVal;
485:
486: return trailingBytes+1;
487: }
488:
1.70 misha 489: // skip UTF8 char and return length of this char (in bytes)
490: static unsigned int skipUTF8Char(const XMLByte*& srcPtr, const XMLByte* srcEnd){
1.62 misha 491: if(!srcPtr || !*srcPtr || srcPtr>=srcEnd)
492: return 0;
493:
1.63 misha 494: unsigned int trailingBytes=gUTFBytes[*srcPtr]+1;
495: srcPtr+=trailingBytes;
1.62 misha 496:
497: return trailingBytes;
1.61 misha 498: }
499:
1.85 misha 500: // read non-UTF8 char, and return number of bytes needed for storing this char in UTF8
1.61 misha 501: static unsigned int readChar(const XMLByte*& srcPtr, const XMLByte* srcEnd, XMLByte& firstByte, XMLCh& UTF8Char, const Charset::Tables& tables){
1.60 misha 502: if(!srcPtr || !*srcPtr || srcPtr>=srcEnd)
503: return 0;
504:
505: firstByte=*srcPtr++;
506: UTF8Char=tables.fromTable[firstByte];
507:
508: if(UTF8Char<0x80)
509: return 1;
510: else if(UTF8Char<0x800)
511: return 2;
512: else if(UTF8Char<0x10000)
513: return 3;
514: else if(UTF8Char<0x200000)
515: return 4;
516: else if(UTF8Char<0x4000000)
517: return 5;
518: else if(UTF8Char<= 0x7FFFFFFF)
519: return 6;
520:
521: // will use the replacement character '?'
522: firstByte=0;
523: return 1;
524: }
525:
1.85 misha 526: size_t Charset::calc_escaped_length_UTF8(XMLByte* src, size_t src_length){
527: size_t dest_length=0;
528:
529: for(UTF8_string_iterator i(src, src_length); i.has_next(); ){
530: if(i.getCharSize()==1)
531: dest_length+=!need_escape(i.getFirstByte())?1/*as-is*/:3/*%XX*/;
532: else
533: dest_length+=6; // %uXXXX
1.60 misha 534: }
535:
1.85 misha 536: return dest_length;
1.60 misha 537: }
538:
1.86 moko 539: size_t Charset::calc_escaped_length(const XMLByte* src, size_t src_length, const Charset::Tables& tables){
540: const XMLByte* src_end=src+src_length;
541: XMLByte first_byte;
542: XMLCh UTF8_char;
1.85 misha 543: size_t dest_length=0;
544:
1.86 moko 545: while(uint char_size=readChar(src, src_end, first_byte, UTF8_char, tables)){
1.85 misha 546: if(char_size==1)
547: dest_length+=(!first_byte/*replacement char '?'*/ || !need_escape(first_byte))?1:3/*'%XX'*/;
548: else
549: dest_length+=6; // %uXXXX
1.60 misha 550: }
551:
1.85 misha 552: return dest_length;
553: }
554:
555: size_t Charset::calc_escaped_length(const String::C src, const Charset& source_charset){
1.86 moko 556: if(!src.length)
1.85 misha 557: return 0;
558:
559: #ifdef PRECALCULATE_DEST_LENGTH
560: if(source_charset.isUTF8())
1.86 moko 561: return calc_escaped_length_UTF8((XMLByte *)src.str, src.length);
1.85 misha 562: else
1.86 moko 563: return calc_escaped_length((XMLByte *)src.str, src.length, source_charset.tables);
1.85 misha 564: #else
565: return src_length*6; // enough for %uXXXX but too memory-hungry
566: #endif
567: }
568:
569: #define escape_char(dest_ptr, char_size, first_byte, UTF8_char) \
570: if(char_size==1) \
571: if(first_byte){ \
572: if(need_escape(first_byte)) \
573: dest_ptr+=sprintf((char*)dest_ptr, "%%%02X", first_byte); /* %XX */ \
574: else \
575: *dest_ptr++=first_byte; /*as is*/ \
576: } else \
577: *dest_ptr++='?'; /* replacement char '?' */ \
578: else \
579: dest_ptr+=sprintf((char*)dest_ptr, "%%u%04X", UTF8_char); // %uXXXX
580:
581:
582: size_t Charset::escape_UTF8(const XMLByte* src, size_t src_length, XMLByte* dest) {
583: XMLByte* dest_ptr=dest;
584:
585: // loop until we either run out of input data
586: for(UTF8_string_iterator i((XMLByte *)src, src_length); i.has_next(); )
587: escape_char(dest_ptr, i.getCharSize(), i.getFirstByte(), i.next())
1.60 misha 588:
1.85 misha 589: return dest_ptr - dest;
1.60 misha 590: }
591:
1.85 misha 592: size_t Charset::escape(const XMLByte* src, size_t src_length, XMLByte* dest, const Charset::Tables& tables) {
593: const XMLByte* src_end=src+src_length;
594: XMLByte* dest_ptr=dest;
595:
596: XMLByte first_byte;
597: XMLCh UTF8_char;
598: uint char_size;
599:
1.86 moko 600: while(char_size=readChar(src, src_end, first_byte, UTF8_char, tables))
1.85 misha 601: escape_char(dest_ptr, char_size, first_byte, UTF8_char)
602:
603: return dest_ptr - dest;
604: }
1.60 misha 605:
606: String::C Charset::escape(const String::C src, const Charset& source_charset){
1.86 moko 607: if(!src.length)
1.60 misha 608: return String::C("", 0);
609:
1.85 misha 610: size_t dest_calculated_length=calc_escaped_length(src, source_charset);
611: XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_calculated_length+1/*terminator*/];
612:
613: size_t dest_length;
614: if(source_charset.isUTF8())
1.86 moko 615: dest_length=escape_UTF8((XMLByte *)src.str, src.length, dest_body);
1.85 misha 616: else
1.86 moko 617: dest_length=escape((XMLByte *)src.str, src.length, dest_body, source_charset.tables);
1.85 misha 618:
619: if(dest_length>dest_calculated_length)
620: throw Exception(0, 0, "Charset::escape buffer overflow");
621:
622: dest_body[dest_length]=0; // terminator
623: return String::C((char*)dest_body, dest_length);
624: }
625:
626: String::Body Charset::escape(const String::Body src, const Charset& source_charset) {
1.86 moko 627: String::C dest=Charset::escape(String::C(src.cstr(), src.length()), source_charset);
1.85 misha 628: return String::Body(dest.length ? dest.str:0);
629: }
630:
631: String& Charset::escape(const String& src, const Charset& source_charset) {
632: if(src.is_empty())
633: return *new String();
634:
635: return *new String(escape((String::Body)src, source_charset), String::L_CLEAN);
636: }
637:
638: inline bool need_json_escape(unsigned char c){
639: return strchr("\n\"\\/\t\r\b\f", c)!=0;
640: }
641:
642: size_t Charset::calc_JSON_escaped_length_UTF8(XMLByte* src, size_t src_length){
643: size_t dest_length=0;
644:
645: for(UTF8_string_iterator i(src, src_length); i.has_next(); ){
646: if(i.getCharSize()==1)
647: dest_length+=need_json_escape(i.getFirstByte()) ? 2 : 1;
648: else
649: dest_length+=6; // \uXXXX
650: }
651:
652: return dest_length;
653: }
654:
1.86 moko 655: size_t Charset::calc_JSON_escaped_length(const XMLByte* src, size_t src_length, const Charset::Tables& tables){
656: const XMLByte* src_end=src+src_length;
1.85 misha 657: XMLByte first_byte;
658: XMLCh UTF8_char;
1.60 misha 659: size_t dest_length=0;
660:
1.86 moko 661: while(uint char_size=readChar(src, src_end, first_byte, UTF8_char, tables)){
1.85 misha 662: if(char_size==1)
663: dest_length+=(!first_byte/*replacement char '?'*/ || !need_json_escape(first_byte))? 1 : 2;
664: else
665: dest_length+=6; // \uXXXX
1.60 misha 666: }
1.85 misha 667:
668: return dest_length;
669: }
670:
671: size_t Charset::calc_JSON_escaped_length(const String::C src, const Charset& source_charset){
1.86 moko 672: if(!src.length)
1.85 misha 673: return 0;
674:
675: #ifdef PRECALCULATE_DEST_LENGTH
676: if(source_charset.isUTF8())
1.86 moko 677: return calc_JSON_escaped_length_UTF8((XMLByte *)src.str, src.length);
1.85 misha 678: else
1.86 moko 679: return calc_JSON_escaped_length((XMLByte *)src.str, src.length, source_charset.tables);
1.60 misha 680: #else
1.85 misha 681: return src_length*6; // enough for \uXXXX but too memory-hungry
1.60 misha 682: #endif
1.85 misha 683: }
684:
685: #define escape_char_JSON(dest_ptr, char_size, first_byte, UTF8_char) \
686: if(char_size==1) \
687: switch(first_byte){ \
688: case '\n': *dest_ptr++='\\'; *dest_ptr++='n'; break; \
689: case '"' : *dest_ptr++='\\'; *dest_ptr++='"'; break; \
690: case '\\': *dest_ptr++='\\'; *dest_ptr++='\\'; break; \
691: case '/' : *dest_ptr++='\\'; *dest_ptr++='/'; break; \
692: case '\t': *dest_ptr++='\\'; *dest_ptr++='t'; break; \
693: case '\r': *dest_ptr++='\\'; *dest_ptr++='r'; break; \
694: case '\b': *dest_ptr++='\\'; *dest_ptr++='b'; break; \
695: case '\f': *dest_ptr++='\\'; *dest_ptr++='f'; break; \
696: case 0 : *dest_ptr++='?'; break; /*replacement char*/ \
697: default : *dest_ptr++=first_byte; \
698: } \
699: else \
700: dest_ptr+=sprintf((char*)dest_ptr, "\\u%04X", UTF8_char); // \uXXXX
701:
702:
703: size_t Charset::escape_JSON_UTF8(const XMLByte* src, size_t src_length, XMLByte* dest) {
704: XMLByte* dest_ptr=dest;
705:
706: // loop until we either run out of input data
707: for(UTF8_string_iterator i((XMLByte *)src, src_length); i.has_next(); )
708: escape_char_JSON(dest_ptr, i.getCharSize(), i.getFirstByte(), i.next())
709:
710: return dest_ptr - dest;
711: }
712:
713: size_t Charset::escape_JSON(const XMLByte* src, size_t src_length, XMLByte* dest, const Charset::Tables& tables) {
714: const XMLByte* src_end=src+src_length;
715: XMLByte* dest_ptr=dest;
716:
717: XMLByte first_byte;
718: XMLCh UTF8_char;
719: uint char_size;
720:
1.86 moko 721: while(char_size=readChar(src, src_end, first_byte, UTF8_char, tables))
1.85 misha 722: escape_char_JSON(dest_ptr, char_size, first_byte, UTF8_char)
723:
724: return dest_ptr - dest;
725: }
1.60 misha 726:
1.85 misha 727: String::C Charset::escape_JSON(const String::C src, const Charset& source_charset){
1.86 moko 728: if(!src.length)
1.85 misha 729: return String::C("", 0);
1.60 misha 730:
1.85 misha 731: size_t dest_calculated_length=calc_JSON_escaped_length(src, source_charset);
732: XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_calculated_length+1/*terminator*/];
733:
734: size_t dest_length;
735: if(source_charset.isUTF8())
1.86 moko 736: dest_length=escape_JSON_UTF8((XMLByte *)src.str, src.length, dest_body);
1.85 misha 737: else
1.86 moko 738: dest_length=escape_JSON((XMLByte *)src.str, src.length, dest_body, source_charset.tables);
1.60 misha 739:
1.85 misha 740: if(dest_length>dest_calculated_length)
741: throw Exception(0, 0, "Charset::escape_JSON buffer overflow");
1.60 misha 742:
743: dest_body[dest_length]=0; // terminator
744: return String::C((char*)dest_body, dest_length);
745: }
1.85 misha 746:
747: String::Body Charset::escape_JSON(const String::Body src, const Charset& source_charset) {
1.86 moko 748: String::C dest=Charset::escape_JSON(String::C(src.cstr(), src.length()), source_charset);
1.77 misha 749: return String::Body(dest.length ? dest.str:0);
1.64 misha 750: }
751:
1.85 misha 752: String& Charset::escape_JSON(const String& src, const Charset& source_charset) {
1.72 misha 753: if(src.is_empty())
1.73 misha 754: return *new String();
1.64 misha 755:
1.85 misha 756: return *new String(escape_JSON((String::Body)src, source_charset), String::L_CLEAN);
1.64 misha 757: }
1.60 misha 758:
1.35 paf 759: const String::C Charset::transcodeToUTF8(const String::C src) const {
1.71 misha 760: int src_length=src.length;
1.60 misha 761:
762: #ifdef PRECALCULATE_DEST_LENGTH
1.71 misha 763: int dest_length=0;
1.60 misha 764: const XMLByte* srcPtr=(XMLByte*)src.str;
765: const XMLByte* srcEnd=srcPtr+src_length;
1.69 misha 766: XMLByte firstByte;
767: XMLCh UTF8Char;
768: while(uint charSize=readChar(srcPtr, srcEnd, firstByte, UTF8Char, tables))
1.60 misha 769: dest_length+=charSize;
770: #else
1.85 misha 771: int dest_length=src_length*6; // so that surly enough (max utf8 seq len=6) but too memory-hungry
1.60 misha 772: #endif
773:
1.35 paf 774: #ifndef NDEBUG
1.71 misha 775: int saved_dest_length=dest_length;
1.35 paf 776: #endif
777: XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/];
1.11 paf 778:
779: if(::transcodeToUTF8(
1.35 paf 780: (XMLByte *)src.str, src_length,
781: dest_body, dest_length,
1.11 paf 782: tables)<0)
1.43 paf 783: throw Exception(0,
1.10 paf 784: 0,
1.11 paf 785: "Charset::transcodeToUTF8 buffer overflow");
1.10 paf 786:
1.60 misha 787: assert(dest_length<=saved_dest_length);
788: dest_body[dest_length]=0; // terminator
1.35 paf 789: return String::C((char*)dest_body, dest_length);
1.10 paf 790: }
1.38 paf 791:
792: static XMLCh change_case_UTF8(const XMLCh src, const Charset::UTF8CaseTable& table) {
1.80 misha 793: int lo = 0;
794: int hi = table.size - 1;
1.39 paf 795: while(lo<=hi) {
1.38 paf 796: // Calc the mid point of the low and high offset.
1.39 paf 797: const unsigned int i = (lo + hi) / 2;
798:
799: XMLCh cur=table.records[i].from;
800: if(src==cur)
801: return table.records[i].to;
802: if(src>cur)
803: lo = i+1;
1.38 paf 804: else
1.39 paf 805: hi = i-1;
806: }
807:
808: // not found
1.38 paf 809: return src;
810: }
811:
1.58 misha 812: static void store_UTF8(XMLCh src, XMLByte*& outPtr){
1.38 paf 813: if(!src) {
814: // use the replacement character
815: *outPtr++= '?';
816: return;
817: }
818:
819: // Figure out how many bytes we need
820: unsigned int encodedBytes;
821: if(src<0x80)
822: encodedBytes = 1;
823: else if(src<0x800)
824: encodedBytes = 2;
825: else if(src<0x10000)
826: encodedBytes = 3;
827: else if(src<0x200000)
828: encodedBytes = 4;
829: else if(src<0x4000000)
830: encodedBytes = 5;
831: else if(src<= 0x7FFFFFFF)
832: encodedBytes = 6;
833: else {
834: // use the replacement character
835: *outPtr++= '?';
836: return;
837: }
838:
839: // And spit out the bytes. We spit them out in reverse order
840: // here, so bump up the output pointer and work down as we go.
841: outPtr+= encodedBytes;
842: switch(encodedBytes) {
843: case 6: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
844: src>>= 6;
845: case 5: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
846: src>>= 6;
847: case 4: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
848: src>>= 6;
849: case 3: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
850: src>>= 6;
851: case 2: *--outPtr = XMLByte((src | 0x80UL) & 0xBFUL);
852: src>>= 6;
853: case 1: *--outPtr = XMLByte(src | gFirstByteMark[encodedBytes]);
854: }
855:
856: // Add the encoded bytes back in again to indicate we've eaten them
857: outPtr+= encodedBytes;
858: }
859:
860: static void change_case_UTF8(XMLCh src, XMLByte*& outPtr,
861: const Charset::UTF8CaseTable& table) {
862: store_UTF8(change_case_UTF8(src, table), outPtr);
863: };
1.44 paf 864: void change_case_UTF8(const XMLByte* srcData, size_t srcLen,
865: XMLByte* toFill, size_t toFillLen,
866: const Charset::UTF8CaseTable& table) {
1.38 paf 867: const XMLByte* srcPtr=srcData;
1.44 paf 868: const XMLByte* srcEnd=srcData+srcLen;
1.38 paf 869: XMLByte* outPtr=toFill;
1.44 paf 870: XMLByte* outEnd=toFill+toFillLen;
871:
872: // We now loop until we either run out of input data, or room to store
873: while ((srcPtr < srcEnd) && (outPtr < outEnd)) {
874: // Get the next leading byte out
875: const XMLByte firstByte =* srcPtr;
1.38 paf 876:
1.60 misha 877: if(firstByte<=127) {
1.38 paf 878: change_case_UTF8(firstByte, outPtr, table);
879: srcPtr++;
880: continue;
881: }
882:
883: // See how many trailing src bytes this sequence is going to require
884: const unsigned int trailingBytes = gUTFBytes[firstByte];
885:
886: // Looks ok, so lets build up the value
887: uint tmpVal=0;
888: switch(trailingBytes) {
889: case 5: tmpVal+=*srcPtr++; tmpVal<<=6;
890: case 4: tmpVal+=*srcPtr++; tmpVal<<=6;
891: case 3: tmpVal+=*srcPtr++; tmpVal<<=6;
892: case 2: tmpVal+=*srcPtr++; tmpVal<<=6;
893: case 1: tmpVal+=*srcPtr++; tmpVal<<=6;
894: case 0: tmpVal+=*srcPtr++;
895: break;
896:
897: default:
898: throw Exception(0,
899: 0,
900: "change_case_UTF8 error: wrong trailingBytes value(%d)", trailingBytes);
901: }
902: tmpVal-=gUTFOffsets[trailingBytes];
903:
904: // If it will fit into a single char, then put it in. Otherwise
905: // fail [*encode it as a surrogate pair. If its not valid, use the
906: // replacement char.*]
907: if(!(tmpVal & 0xFFFF0000))
908: change_case_UTF8(tmpVal, outPtr, table);
909: else
910: throw Exception(0,
911: 0,
912: "change_case_UTF8 error: too big tmpVal(0x%08X)", tmpVal);
913: }
914:
915: if(srcPtr!=outPtr)
916: throw Exception(0,
917: 0,
918: "change_case_UTF8 error: end pointers do not match");
919: }
920:
1.60 misha 921: static size_t getDecNumLength(XMLCh UTF8Char){
922: return
923: (UTF8Char < 100)
924: ?2
925: :(UTF8Char < 1000)
926: ?3
927: :(UTF8Char < 10000)
928: ?4
929: :5;
930: }
1.38 paf 931:
1.35 paf 932: const String::C Charset::transcodeFromUTF8(const String::C src) const {
1.82 misha 933: int src_length=src.length;
1.60 misha 934: #ifdef PRECALCULATE_DEST_LENGTH
1.71 misha 935: int dest_length=0;
1.82 misha 936: for(UTF8_string_iterator i((XMLByte *)src.str, src_length); i.has_next(); ){
1.88 misha 937: dest_length += ( i.next() & 0xFFFF0000 )
938: ? 3*i.getCharSize() // %XX for each byte
939: : ( xlatOneTo(i.next(), tables, 0) != 0 )
940: ? 1 // can convert it to a single char
941: : 3+getDecNumLength( i.next() ); // print char as &#XX;, &#XXX;, &#XXXX; or &#XXXXX;
1.60 misha 942: }
943: #else
944: // so that surly enough, "&#XXX;" has max ratio (huh? 8 bytes needed for '&#XXXXX;')
1.82 misha 945: int dest_length=src_length*6;
1.60 misha 946: #endif
947:
1.35 paf 948: #ifndef NDEBUG
1.71 misha 949: int saved_dest_length=dest_length;
1.35 paf 950: #endif
951: XMLByte *dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/];
1.11 paf 952:
953: if(::transcodeFromUTF8(
1.82 misha 954: (XMLByte *)src.str, src_length,
1.35 paf 955: dest_body, dest_length,
1.11 paf 956: tables)<0)
1.43 paf 957: throw Exception(0,
1.10 paf 958: 0,
1.35 paf 959: "Charset::transcodeFromUTF8 buffer overflow");
1.10 paf 960:
1.60 misha 961: assert(dest_length<=saved_dest_length);
962: dest_body[dest_length]=0; // terminator
1.35 paf 963: return String::C((char*)dest_body, dest_length);
1.1 paf 964: }
965:
966: /// transcode using both charsets
1.35 paf 967: const String::C Charset::transcodeToCharset(const String::C src,
1.80 misha 968: const Charset& dest_charset) const {
1.35 paf 969: if(&dest_charset==this)
970: return src;
971: else {
972: size_t dest_length=src.length;
973: XMLByte* dest_body=new(PointerFreeGC) XMLByte[dest_length+1/*for terminator*/];
974:
975: XMLByte* output=dest_body;
976: const XMLByte* input=(XMLByte *)src.str;
977: while(XMLCh c=*input++) {
978: XMLCh curVal = tables.fromTable[c];
979: *output++=curVal?
980: xlatOneTo(curVal, dest_charset.tables, '?') // OK
981: :'?'; // use the replacement character
1.6 paf 982: }
1.1 paf 983:
1.35 paf 984: dest_body[dest_length]=0; // terminator
985: return String::C((char*)dest_body, dest_length);
1.6 paf 986: }
1.1 paf 987: }
988:
1.58 misha 989: void Charset::store_Char(XMLByte*& outPtr, XMLCh src, XMLByte not_found){
1.59 misha 990: if(isUTF8())
1.58 misha 991: store_UTF8(src, outPtr);
1.59 misha 992: else if(char ch=xlatOneTo(src, tables, not_found))
1.58 misha 993: *outPtr++=ch;
1.57 misha 994: }
995:
1.1 paf 996: #ifdef XML
1.10 paf 997:
1.35 paf 998: static const Charset::Tables* tables[MAX_CHARSETS];
999:
1.46 paf 1000: #ifdef PA_PATCHED_LIBXML_BACKWARD
1001:
1002: #define declareXml256ioFuncs(i) \
1003: static int xml256CharEncodingInputFunc##i( \
1004: unsigned char *out, int *outlen, \
1005: const unsigned char *in, int *inlen, void*) { \
1006: return transcodeToUTF8( \
1.71 misha 1007: in, *inlen, \
1008: out, *outlen, \
1.46 paf 1009: *tables[i]); \
1010: } \
1011: static int xml256CharEncodingOutputFunc##i( \
1012: unsigned char *out, int *outlen, \
1013: const unsigned char *in, int *inlen, void*) { \
1014: return transcodeFromUTF8( \
1.71 misha 1015: in, *inlen, \
1016: out, *outlen, \
1.46 paf 1017: *tables[i]); \
1018: }
1019:
1020: #else
1021:
1.35 paf 1022: #define declareXml256ioFuncs(i) \
1023: static int xml256CharEncodingInputFunc##i( \
1024: unsigned char *out, int *outlen, \
1025: const unsigned char *in, int *inlen) { \
1026: return transcodeToUTF8( \
1.71 misha 1027: in, *inlen, \
1028: out, *outlen, \
1.35 paf 1029: *tables[i]); \
1030: } \
1031: static int xml256CharEncodingOutputFunc##i( \
1032: unsigned char *out, int *outlen, \
1033: const unsigned char *in, int *inlen) { \
1034: return transcodeFromUTF8( \
1.71 misha 1035: in, *inlen, \
1036: out, *outlen, \
1.35 paf 1037: *tables[i]); \
1038: }
1039:
1.46 paf 1040: #endif
1041:
1042:
1.35 paf 1043: declareXml256ioFuncs(0) declareXml256ioFuncs(1)
1044: declareXml256ioFuncs(2) declareXml256ioFuncs(3)
1045: declareXml256ioFuncs(4) declareXml256ioFuncs(5)
1046: declareXml256ioFuncs(6) declareXml256ioFuncs(7)
1047: declareXml256ioFuncs(8) declareXml256ioFuncs(9)
1048:
1049: static xmlCharEncodingInputFunc inputFuncs[MAX_CHARSETS]={
1050: xml256CharEncodingInputFunc0, xml256CharEncodingInputFunc1,
1051: xml256CharEncodingInputFunc2, xml256CharEncodingInputFunc3,
1052: xml256CharEncodingInputFunc4, xml256CharEncodingInputFunc5,
1053: xml256CharEncodingInputFunc6, xml256CharEncodingInputFunc7,
1054: xml256CharEncodingInputFunc8, xml256CharEncodingInputFunc9
1055: };
1056: static xmlCharEncodingOutputFunc outputFuncs[MAX_CHARSETS]={
1057: xml256CharEncodingOutputFunc0, xml256CharEncodingOutputFunc1,
1058: xml256CharEncodingOutputFunc2, xml256CharEncodingOutputFunc3,
1059: xml256CharEncodingOutputFunc4, xml256CharEncodingOutputFunc5,
1060: xml256CharEncodingOutputFunc6, xml256CharEncodingOutputFunc7,
1061: xml256CharEncodingOutputFunc8, xml256CharEncodingOutputFunc9
1062: };
1063: static size_t handlers_count=0;
1.10 paf 1064:
1065: void Charset::addEncoding(char *name_cstr) {
1.35 paf 1066: if(handlers_count==MAX_CHARSETS)
1067: throw Exception(0,
1068: 0,
1069: "already allocated %d handlers, no space for new encoding '%s'",
1070: MAX_CHARSETS, name_cstr);
1071:
1.45 paf 1072: xmlCharEncodingHandler* handler=new(UseGC) xmlCharEncodingHandler;
1.35 paf 1073: {
1074: handler->name=name_cstr;
1075: handler->input=inputFuncs[handlers_count];
1076: handler->output=outputFuncs[handlers_count];
1077: ::tables[handlers_count]=&tables;
1078: handlers_count++;
1079: }
1.10 paf 1080:
1081: xmlRegisterCharEncodingHandler(handler);
1.35 paf 1082:
1.10 paf 1083: }
1084:
1.37 paf 1085: void Charset::initTranscoder(const String::Body NAME, const char* name_cstr) {
1.15 paf 1086: ftranscoder=xmlFindCharEncodingHandler(name_cstr);
1.35 paf 1087: transcoder(NAME); // check right way
1.15 paf 1088: }
1089:
1.37 paf 1090: xmlCharEncodingHandler& Charset::transcoder(const String::Body NAME) {
1.15 paf 1091: if(!ftranscoder)
1.56 misha 1092: throw Exception(PARSER_RUNTIME,
1.35 paf 1093: new String(NAME, String::L_TAINTED),
1.10 paf 1094: "unsupported encoding");
1.35 paf 1095: return *ftranscoder;
1.10 paf 1096: }
1097:
1.54 paf 1098: String::C Charset::transcode_cstr(const xmlChar* s) {
1.13 paf 1099: if(!s)
1.35 paf 1100: return String::C("", 0);
1.8 paf 1101:
1.35 paf 1102: int inlen=strlen((const char*)s);
1.51 paf 1103: int outlen=inlen*6/*strlen("ÿ")*/; // max
1.35 paf 1104: #ifndef NDEBUG
1105: int saved_outlen=outlen;
1106: #endif
1107: char *out=new(PointerFreeGC) char[outlen+1];
1.8 paf 1108:
1.30 paf 1109: int error;
1.35 paf 1110: if(xmlCharEncodingOutputFunc output=transcoder(FNAME).output) {
1.30 paf 1111: error=output(
1.17 paf 1112: (unsigned char*)out, &outlen,
1.46 paf 1113: (const unsigned char*)s, &inlen
1114: #ifdef PA_PATCHED_LIBXML_BACKWARD
1115: ,0
1116: #endif
1117: );
1.30 paf 1118: } else {
1119: memcpy(out, s, outlen=inlen);
1120: error=0;
1121: }
1122: if(error<0)
1.23 paf 1123: throw Exception(0,
1.8 paf 1124: 0,
1.30 paf 1125: "transcode_cstr failed (%d)", error);
1.8 paf 1126:
1.35 paf 1127: assert(outlen<=saved_outlen); out[outlen]=0;
1128: return String::C(out, outlen);
1.14 paf 1129: }
1.54 paf 1130: const String& Charset::transcode(const xmlChar* s) {
1.35 paf 1131: String::C cstr=transcode_cstr(s);
1.75 misha 1132: return *new String(cstr.str, String::L_TAINTED);
1.1 paf 1133: }
1134:
1.8 paf 1135: /// @test less memory using -maybe- xmlParserInputBufferCreateMem
1.35 paf 1136: xmlChar* Charset::transcode_buf2xchar(const char* buf, size_t buf_size) {
1137: xmlChar* out;
1.30 paf 1138: int outlen;
1139: int error;
1.35 paf 1140: #ifndef NDEBUG
1141: int saved_outlen;
1142: #endif
1143: if(xmlCharEncodingInputFunc input=transcoder(FNAME).input) {
1.51 paf 1144: outlen=buf_size*6/*max UTF8 bytes per char*/;
1.35 paf 1145: #ifndef NDEBUG
1146: saved_outlen=outlen;
1147: #endif
1.47 paf 1148: out=(xmlChar*)xmlMalloc(outlen+1);
1.30 paf 1149: error=input(
1.17 paf 1150: out, &outlen,
1.46 paf 1151: (const unsigned char*)buf, (int*)&buf_size
1152: #ifdef PA_PATCHED_LIBXML_BACKWARD
1153: ,0
1154: #endif
1155: );
1.30 paf 1156: } else {
1157: outlen=buf_size;
1.35 paf 1158: #ifndef NDEBUG
1159: saved_outlen=outlen;
1160: #endif
1161: out=(xmlChar*)xmlMalloc(outlen+1);
1.30 paf 1162: memcpy(out, buf, outlen);
1163: error=0;
1164: }
1.17 paf 1165:
1.30 paf 1166: if(error<0)
1.23 paf 1167: throw Exception(0,
1.8 paf 1168: 0,
1.30 paf 1169: "transcode_buf failed (%d)", error);
1.8 paf 1170:
1.35 paf 1171: assert(outlen<=saved_outlen); out[outlen]=0;
1172: return out;
1.24 paf 1173: }
1.1 paf 1174:
1.79 misha 1175: xmlChar* Charset::transcode(const String& s) {
1176: String::Body sbody=s.cstr_to_string_body_untaint(String::L_AS_IS);
1177: return transcode_buf2xchar(sbody.cstr(), sbody.length());
1.1 paf 1178: }
1.35 paf 1179:
1.79 misha 1180: xmlChar* Charset::transcode(const String::Body s) {
1181: return transcode_buf2xchar(s.cstr(), s.length());
1.35 paf 1182: }
1.36 paf 1183: #endif
1.34 paf 1184:
1.37 paf 1185: String::Body Charset::transcode(const String::Body src,
1.34 paf 1186: const Charset& source_transcoder,
1.35 paf 1187: const Charset& dest_transcoder) {
1.34 paf 1188:
1.35 paf 1189: const char *src_ptr=src.cstr();
1.83 misha 1190: size_t src_size=src.length();
1.34 paf 1191:
1.77 misha 1192: String::C dest=Charset::transcode(String::C(src_ptr, src_size), source_transcoder, dest_transcoder);
1.34 paf 1193:
1.77 misha 1194: return String::Body(dest.length ? dest.str:0);
1.35 paf 1195: }
1196:
1197: String& Charset::transcode(const String& src,
1198: const Charset& source_transcoder,
1199: const Charset& dest_transcoder) {
1.72 misha 1200: if(src.is_empty())
1.73 misha 1201: return *new String();
1.34 paf 1202:
1.37 paf 1203: return *new String(transcode((String::Body)src, source_transcoder, dest_transcoder), String::L_CLEAN);
1.34 paf 1204: }
1205:
1.35 paf 1206: void Charset::transcode(ArrayString& src,
1.34 paf 1207: const Charset& source_transcoder,
1.35 paf 1208: const Charset& dest_transcoder) {
1209: for(size_t i=0; i<src.count(); i++)
1210: src.put(i, &transcode(*src[i], source_transcoder, dest_transcoder));
1.34 paf 1211: }
1212:
1213: #ifndef DOXYGEN
1214: struct Transcode_pair_info {
1215: const Charset* source_transcoder;
1216: const Charset* dest_transcoder;
1217: };
1218: #endif
1.76 misha 1219: static void transcode_pair(HashStringValue::key_type /*akey*/,
1.37 paf 1220: String::Body& avalue,
1.35 paf 1221: Transcode_pair_info* info) {
1222: avalue=Charset::transcode(avalue,
1223: *info->source_transcoder,
1224: *info->dest_transcoder);
1.34 paf 1225: }
1.61 misha 1226:
1.35 paf 1227: void Charset::transcode(HashStringString& src,
1.34 paf 1228: const Charset& source_transcoder,
1.35 paf 1229: const Charset& dest_transcoder) {
1230: Transcode_pair_info info={&source_transcoder, &dest_transcoder};
1.55 paf 1231: src.for_each_ref<Transcode_pair_info*>(transcode_pair, &info);
1.34 paf 1232: }
1.61 misha 1233:
1234: size_t getUTF8BytePos(const XMLByte* srcBegin, const XMLByte* srcEnd, size_t charPos){
1235: const XMLByte* ptr=srcBegin;
1.70 misha 1236: while(charPos-- && skipUTF8Char(ptr, srcEnd));
1.61 misha 1237:
1238: return ptr-srcBegin;
1239: }
1240:
1241: size_t getUTF8CharPos(const XMLByte* srcBegin, const XMLByte* srcEnd, size_t bytePos){
1242: size_t charPos=0;
1243: const XMLByte* ptr=srcBegin;
1244: const XMLByte* ptrEnd=srcBegin+bytePos;
1.70 misha 1245: while(skipUTF8Char(ptr, srcEnd)){
1.61 misha 1246: if(ptr>ptrEnd)
1247: return charPos;
1248: charPos++;
1249: }
1250:
1251: // scan till end but position in bytes still too low
1252: throw Exception(0,
1253: 0,
1254: "Error convertion byte pos to char pos");
1255: }
1256:
1257: size_t lengthUTF8(const XMLByte* srcBegin, const XMLByte* srcEnd){
1258: size_t size=0;
1.70 misha 1259: while(skipUTF8Char(srcBegin, srcEnd))
1.61 misha 1260: size++;
1261:
1262: return size;
1263: }
1.80 misha 1264:
1.84 misha 1265: unsigned int lengthUTF8Char(const XMLByte c){
1266: return gUTFBytes[c]+1;
1267: }
1268:
1.80 misha 1269: bool UTF8_string_iterator::has_next(){
1270: fcharSize=readUTF8Char(fsrcPtr, fsrcEnd, ffirstByte, fUTF8Char);
1271: return fcharSize!=0;
1272: }
E-mail: