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