Annotation of parser3/src/main/pa_charset.C, revision 1.16
1.1 paf 1: /** @file
2: Parser: Charset connection implementation.
3:
1.4 paf 4: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
5: Author: Alexander Petrosyan<paf@design.ru>(http://paf.design.ru)
1.1 paf 6:
1.16 ! paf 7: $Id: pa_charset.C,v 1.15 2002/01/15 13:18:43 paf Exp $
1.1 paf 8: */
9:
10: #include "pa_charset.h"
11:
12: #ifdef XML
1.8 paf 13: #include "libxml/encoding.h"
1.1 paf 14: #endif
15:
16: // globals
17:
18:
19: // consts
20:
21: #define MAX_CHARSET_UNI_CODES 500
22:
23: // helpers
24:
25: inline void prepare_case_tables(unsigned char *tables) {
26: unsigned char *lcc_table=tables+lcc_offset;
27: unsigned char *fcc_table=tables+fcc_offset;
28: for(int i=0; i<0x100; i++)
29: lcc_table[i]=fcc_table[i]=i;
30: }
31: inline void cstr2ctypes(unsigned char *tables, const unsigned char *cstr,
32: unsigned char bit) {
33: unsigned char *ctypes_table=tables+ctypes_offset;
34: ctypes_table[0]=bit;
35: for(; *cstr; cstr++) {
36: unsigned char c=*cstr;
37: ctypes_table[c]|=bit;
38: }
39: }
40: inline unsigned int to_wchar_code(const char *cstr) {
41: if(!cstr || !*cstr)
42: return 0;
43: if(cstr[1]==0)
1.4 paf 44: return(unsigned int)(unsigned char)cstr[0];
1.1 paf 45:
46: char *error_pos;
1.4 paf 47: return(unsigned int)strtol(cstr, &error_pos, 0);
1.1 paf 48: }
49: inline bool to_bool(const char *cstr) {
50: return cstr && *cstr!=0;
51: }
52: static void element2ctypes(unsigned char c, bool belongs,
53: unsigned char *tables, unsigned char bit, int group_offset=-1) {
54: if(!belongs)
55: return;
56:
57: unsigned char *ctypes_table=tables+ctypes_offset;
58:
59: ctypes_table[c]|=bit;
60: if(group_offset>=0)
1.4 paf 61: tables[cbits_offset+group_offset+c/8] |= 1<<(c%8);
1.1 paf 62: }
63: static void element2case(unsigned char from, unsigned char to,
64: unsigned char *tables) {
65: if(!to)
66: return;
67:
68: unsigned char *lcc_table=tables+lcc_offset;
69: unsigned char *fcc_table=tables+fcc_offset;
70: lcc_table[from]=to;
71: fcc_table[from]=to; fcc_table[to]=from;
72: }
73:
74: // methods
75:
76: extern "C" unsigned char pcre_default_tables[]; // pcre/chartables.c
1.7 paf 77: Charset::Charset(Pool& apool, const String& aname, const String *request_file_spec): Pooled(apool),
78: fname(aname) {
1.1 paf 79:
1.10 paf 80: char *name_cstr=fname.cstr();
81: for(char *c=name_cstr; *c; c++)
82: *c = toupper(*c);
1.7 paf 83:
84: if(request_file_spec) {
1.1 paf 85: fisUTF8=false;
1.7 paf 86: loadDefinition(*request_file_spec);
1.1 paf 87: #ifdef XML
88: addEncoding(name_cstr);
89: #endif
90: } else {
91: fisUTF8=true;
1.4 paf 92: // grab default onces [for UTF-8 so to be able to make a-z =>A-Z
1.1 paf 93: memcpy(pcre_tables, pcre_default_tables, sizeof(pcre_tables));
94: }
95:
96: #ifdef XML
97: initTranscoder(&aname, name_cstr);
98: #endif
99: }
100:
101: Charset::~Charset() {
102: #ifdef XML
1.9 paf 103: // not deleting transcoder, that's not our business
1.1 paf 104: #endif
105: }
106:
1.7 paf 107: void Charset::loadDefinition(const String& request_file_spec) {
1.1 paf 108: // pcre_tables
109: // lowcase, flipcase, bits digit+word+whitespace, masks
110:
111: // must not move this inside of prepare_case_tables
112: // don't know the size there
113: memset(pcre_tables, 0, sizeof(pcre_tables));
114: prepare_case_tables(pcre_tables);
1.4 paf 115: cstr2ctypes(pcre_tables,(const unsigned char *)"*+?{^.$|()[", ctype_meta);
1.1 paf 116:
117: // charset
1.10 paf 118: memset(tables.fromTable, 0, sizeof(tables.fromTable));
119: tables.toTable=(Charset_TransRec *)calloc(sizeof(Charset_TransRec)*MAX_CHARSET_UNI_CODES);
120: tables.toTableSize=0;
1.1 paf 121: // strangly vital
1.10 paf 122: tables.toTable[tables.toTableSize].intCh=0;
123: tables.toTable[tables.toTableSize].extCh=(XMLByte)0;
124: tables.toTableSize++;
1.1 paf 125:
126: // loading text
1.7 paf 127: char *data=file_read_text(pool(), request_file_spec);
1.1 paf 128:
129: // ignore header
130: getrow(&data);
131:
132: // parse cells
133: char *row;
134: while(row=getrow(&data)) {
135: // remove empty&comment lines
136: if(!*row || *row=='#')
137: continue;
138:
139: // char white-space digit hex-digit letter word lowercase unicode1 unicode2
140: unsigned int c=0;
141: char *cell;
142: for(int column=0; cell=lsplit(&row, '\t'); column++) {
143: switch(column) {
144: case 0: c=to_wchar_code(cell); break;
145: // pcre_tables
146: case 1: element2ctypes(c, to_bool(cell), pcre_tables, ctype_space, cbit_space); break;
147: case 2: element2ctypes(c, to_bool(cell), pcre_tables, ctype_digit, cbit_digit); break;
148: case 3: element2ctypes(c, to_bool(cell), pcre_tables, ctype_xdigit); break;
149: case 4: element2ctypes(c, to_bool(cell), pcre_tables, ctype_letter); break;
150: case 5: element2ctypes(c, to_bool(cell), pcre_tables, ctype_word, cbit_word); break;
151: case 6: element2case(c, to_wchar_code(cell), pcre_tables); break;
152: case 7:
153: case 8:
154: // charset
1.10 paf 155: if(tables.toTableSize>MAX_CHARSET_UNI_CODES)
1.1 paf 156: throw Exception(0, 0,
1.7 paf 157: &request_file_spec,
1.1 paf 158: "charset must contain not more then %d unicode values", MAX_CHARSET_UNI_CODES);
159:
160: XMLCh unicode=(XMLCh)to_wchar_code(cell);
161: if(!unicode && column==7/*unicode1 column*/)
162: unicode=(XMLCh)c;
163: if(unicode) {
1.10 paf 164: if(!tables.fromTable[c])
165: tables.fromTable[c]=unicode;
166: tables.toTable[tables.toTableSize].intCh=unicode;
167: tables.toTable[tables.toTableSize].extCh=(XMLByte)c;
168: tables.toTableSize++;
1.1 paf 169: }
170: break;
171: }
172: }
173: };
174:
175: // sort by the Unicode code point
176: sort_ToTable();
177: }
178:
179: static int sort_cmp_Trans_rec_intCh(const void *a, const void *b) {
180: return
181: static_cast<const Charset_TransRec *>(a)->intCh-
182: static_cast<const Charset_TransRec *>(b)->intCh;
183: }
184:
185: void Charset::sort_ToTable() {
1.10 paf 186: _qsort(tables.toTable, tables.toTableSize, sizeof(*tables.toTable),
1.1 paf 187: sort_cmp_Trans_rec_intCh);
188: //FILE *f=fopen("c:\\temp\\a", "wb");
1.10 paf 189: //fwrite(tables.toTable, tables.toTableSize, sizeof(*tables.toTable), f);
1.1 paf 190: //fclose(f);
191: }
192:
1.10 paf 193: static XMLByte xlatOneTo(const XMLCh toXlat,
194: const Charset::Tables& tables) {
1.1 paf 195: unsigned int lowOfs = 0;
1.10 paf 196: unsigned int hiOfs = tables.toTableSize - 1;
1.1 paf 197: XMLByte curByte = 0;
198: do {
199: // Calc the mid point of the low and high offset.
1.4 paf 200: const unsigned int midOfs =((hiOfs - lowOfs) / 2)+lowOfs;
1.1 paf 201:
202: // If our test char is greater than the mid point char, then
203: // we move up to the upper half. Else we move to the lower
204: // half. If its equal, then its our guy.
1.10 paf 205: if(toXlat>tables.toTable[midOfs].intCh)
1.1 paf 206: lowOfs = midOfs;
1.10 paf 207: else if(toXlat<tables.toTable[midOfs].intCh)
1.1 paf 208: hiOfs = midOfs;
209: else
1.10 paf 210: return tables.toTable[midOfs].extCh;
1.4 paf 211: } while(lowOfs+1<hiOfs);
1.1 paf 212:
213: return '?';
214: }
215:
216: void Charset::transcode(Pool& pool,
217: const Charset& source_charset, const void *source_body, size_t source_content_length,
218: const Charset& dest_charset, const void *& dest_body, size_t& dest_content_length
219: ) {
1.4 paf 220: if(!source_content_length) {
221: dest_body=0;
222: dest_content_length=0;
223: return;
224: }
225:
1.1 paf 226: switch((source_charset.isUTF8()?0x10:0x00)|(dest_charset.isUTF8()?0x01:0x00)) {
227: default: // 0x00
228: source_charset.transcodeToCharset(pool, dest_charset,
229: source_body, source_content_length,
230: dest_body, dest_content_length);
231: break;
232: case 0x01:
233: source_charset.transcodeToUTF8(pool,
234: source_body, source_content_length,
235: dest_body, dest_content_length);
236: break;
237: case 0x10:
238: dest_charset.transcodeFromUTF8(pool,
239: source_body, source_content_length,
240: dest_body, dest_content_length);
241: break;
242: case 0x11:
243: dest_body=source_body;
244: dest_content_length=source_content_length;
245: break;
246: }
247: }
248:
249: // ---------------------------------------------------------------------------
250: // Local static data
251: //
252: // gUTFBytes
253: // A list of counts of trailing bytes for each initial byte in the input.
254: //
255: // gUTFOffsets
256: // A list of values to offset each result char type, according to how
257: // many source bytes when into making it.
258: //
259: // gFirstByteMark
260: // A list of values to mask onto the first byte of an encoded sequence,
261: // indexed by the number of bytes used to create the sequence.
262: // ---------------------------------------------------------------------------
263: static const XMLByte gUTFBytes[0x100] = {
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: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
267: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
268: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
269: , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
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: , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
277: , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
278: , 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
279: , 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5
280: };
281:
282: static const uint gUTFOffsets[6] = {
283: 0, 0x3080, 0xE2080, 0x3C82080, 0xFA082080, 0x82082080
284: };
285:
286: static const XMLByte gFirstByteMark[7] = {
287: 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
288: };
289:
1.11 paf 290: static int transcodeToUTF8(
291: const XMLByte* srcData, size_t& srcLen,
292: XMLByte *toFill, size_t& toFillLen,
1.10 paf 293: const Charset::Tables& tables) {
1.11 paf 294: const XMLByte* srcPtr=srcData;
295: const XMLByte* srcEnd=srcData+srcLen;
296: XMLByte* outPtr=toFill;
297: XMLByte* outEnd=toFill+toFillLen;
1.1 paf 298:
1.4 paf 299: while(srcPtr<srcEnd) {
1.10 paf 300: uint curVal = tables.fromTable[*srcPtr];
1.1 paf 301: if(!curVal) {
302: // use the replacement character
1.4 paf 303: *outPtr++= '?';
304: srcPtr++;
1.1 paf 305: continue;
306: }
307:
308: // Figure out how many bytes we need
309: unsigned int encodedBytes;
1.4 paf 310: if(curVal<0x80)
1.1 paf 311: encodedBytes = 1;
1.4 paf 312: else if(curVal<0x800)
1.1 paf 313: encodedBytes = 2;
1.4 paf 314: else if(curVal<0x10000)
1.1 paf 315: encodedBytes = 3;
1.4 paf 316: else if(curVal<0x200000)
1.1 paf 317: encodedBytes = 4;
1.4 paf 318: else if(curVal<0x4000000)
1.1 paf 319: encodedBytes = 5;
1.4 paf 320: else if(curVal<= 0x7FFFFFFF)
1.1 paf 321: encodedBytes = 6;
322: else {
323: // use the replacement character
1.4 paf 324: *outPtr++= '?';
325: srcPtr++;
1.1 paf 326: continue;
327: }
328:
1.10 paf 329: // If we cannot fully get this char into the output buffer
330: if (outPtr + encodedBytes > outEnd)
331: break;
1.1 paf 332:
333: // We can do it, so update the source index
334: srcPtr++;
335:
336: // And spit out the bytes. We spit them out in reverse order
337: // here, so bump up the output pointer and work down as we go.
1.4 paf 338: outPtr+= encodedBytes;
1.1 paf 339: switch(encodedBytes) {
1.16 ! paf 340: case 6: *--outPtr = (XMLByte)((curVal | 0x80UL) & 0xBFUL);
1.4 paf 341: curVal>>= 6;
1.16 ! paf 342: case 5: *--outPtr = (XMLByte)((curVal | 0x80UL) & 0xBFUL);
1.4 paf 343: curVal>>= 6;
1.16 ! paf 344: case 4: *--outPtr = (XMLByte)((curVal | 0x80UL) & 0xBFUL);
1.4 paf 345: curVal>>= 6;
1.16 ! paf 346: case 3: *--outPtr = (XMLByte)((curVal | 0x80UL) & 0xBFUL);
1.4 paf 347: curVal>>= 6;
1.16 ! paf 348: case 2: *--outPtr = (XMLByte)((curVal | 0x80UL) & 0xBFUL);
1.4 paf 349: curVal>>= 6;
1.16 ! paf 350: case 1: *--outPtr = (XMLByte)(curVal | gFirstByteMark[encodedBytes]);
1.1 paf 351: }
352:
353: // Add the encoded bytes back in again to indicate we've eaten them
1.4 paf 354: outPtr+= encodedBytes;
1.1 paf 355: }
356:
1.11 paf 357: // Update the bytes eaten
358: srcLen = srcPtr - srcData;
359:
360: // Return the characters read
361: toFillLen = outPtr - toFill;
362:
1.16 ! paf 363: return srcPtr==srcEnd?(int)toFillLen:-1;
1.1 paf 364: }
1.10 paf 365: static size_t transcodeFromUTF8(
1.11 paf 366: const XMLByte *srcData, size_t& srcLen,
367: XMLByte* toFill, size_t& toFillLen,
368: const Charset::Tables& tables) {
369: const XMLByte* srcPtr=srcData;
370: const XMLByte* srcEnd=srcData+srcLen;
371: XMLByte* outPtr=toFill;
372: XMLByte* outEnd=toFill+toFillLen;
1.1 paf 373:
1.10 paf 374: // We now loop until we either run out of input data, or room to store
375: while ((srcPtr < srcEnd) && (outPtr < outEnd)) {
1.1 paf 376: // Get the next leading byte out
377: const XMLByte firstByte = *srcPtr;
378:
1.4 paf 379: // Special-case ASCII, which is a leading byte value of<= 127
380: if(firstByte<= 127) {
381: *outPtr++= firstByte;
1.1 paf 382: srcPtr++;
383: continue;
384: }
385:
386: // See how many trailing src bytes this sequence is going to require
387: const unsigned int trailingBytes = gUTFBytes[firstByte];
388:
389: // If there are not enough source bytes to do this one, then we
1.4 paf 390: // are done. Note that we done>= here because we are implicitly
1.1 paf 391: // counting the 1 byte we get no matter what.
1.4 paf 392: if(srcPtr+trailingBytes>= srcEnd)
1.1 paf 393: break;
394:
395: // Looks ok, so lets build up the value
396: uint tmpVal=0;
397: switch(trailingBytes) {
398: case 5: tmpVal+=*srcPtr++; tmpVal<<=6;
399: case 4: tmpVal+=*srcPtr++; tmpVal<<=6;
400: case 3: tmpVal+=*srcPtr++; tmpVal<<=6;
401: case 2: tmpVal+=*srcPtr++; tmpVal<<=6;
402: case 1: tmpVal+=*srcPtr++; tmpVal<<=6;
403: case 0: tmpVal+=*srcPtr++;
404: break;
405:
406: default:
407: throw Exception(0, 0,
408: 0,
1.4 paf 409: "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes);
1.1 paf 410: }
411: tmpVal-=gUTFOffsets[trailingBytes];
412:
413: // If it will fit into a single char, then put it in. Otherwise
414: // fail [*encode it as a surrogate pair. If its not valid, use the
415: // replacement char.*]
1.4 paf 416: if(!(tmpVal & 0xFFFF0000))
1.10 paf 417: *outPtr++= xlatOneTo(tmpVal, tables);
1.1 paf 418: else
419: throw Exception(0, 0,
420: 0,
1.4 paf 421: "transcodeFromUTF8 error: too big tmpVal(0x%08X)", tmpVal);
1.1 paf 422: }
423:
1.11 paf 424: // Update the bytes eaten
425: srcLen = srcPtr - srcData;
426:
427: // Return the characters read
428: toFillLen = outPtr - toFill;
429:
1.16 ! paf 430: return srcPtr==srcEnd?(int)toFillLen:-1;
1.10 paf 431: }
432:
433: /// @todo not so memory-hungry with prescan
434: void Charset::transcodeToUTF8(Pool& pool,
435: const void *source_body, size_t source_content_length,
1.11 paf 436: const void *& adest_body, size_t& dest_content_length) const {
437: dest_content_length=source_content_length*6/*so that surly enough*/;
438: XMLByte *dest_body=(XMLByte*)pool.malloc(dest_content_length);
439:
440: if(::transcodeToUTF8(
441: (XMLByte *)source_body, source_content_length,
442: dest_body, dest_content_length,
443: tables)<0)
1.10 paf 444: throw(0, 0,
445: 0,
1.11 paf 446: "Charset::transcodeToUTF8 buffer overflow");
1.10 paf 447:
1.1 paf 448: // return
449: adest_body=dest_body;
1.10 paf 450: }
451: /// @test buf overflow
452: void Charset::transcodeFromUTF8(Pool& pool,
453: const void *source_body, size_t source_content_length,
1.11 paf 454: const void *& adest_body, size_t& dest_content_length) const {
455: dest_content_length=source_content_length/*surly enough*/;
456: XMLByte *dest_body=(XMLByte*)pool.malloc(dest_content_length);
457:
458: if(::transcodeFromUTF8(
459: (XMLByte *)source_body, source_content_length,
460: dest_body, dest_content_length,
461: tables)<0)
1.10 paf 462: throw(0, 0,
463: 0,
1.11 paf 464: "Charset::transcodeToUTF8 buffer overflow");
1.10 paf 465:
466: // return
467: adest_body=dest_body;
1.1 paf 468: }
469:
470: /// transcode using both charsets
471: void Charset::transcodeToCharset(Pool& pool,
472: const Charset& dest_charset,
473: const void *source_body, size_t source_content_length,
1.6 paf 474: const void *& adest_body, size_t& adest_content_length) const {
1.3 paf 475: if(&dest_charset==this) {
1.6 paf 476: adest_body=source_body;
477: adest_content_length=source_content_length;
478: } else {
479: size_t dest_content_length=source_content_length;
480: unsigned char *dest_body=(unsigned char *)pool.malloc(dest_content_length);
481:
1.11 paf 482: const XMLByte* srcPtr=(XMLByte *)source_body;
483: const XMLByte* srcEnd=(XMLByte *)source_body+source_content_length;
1.6 paf 484:
485: for(XMLByte* outPtr=dest_body; srcPtr<srcEnd; srcPtr++) {
1.10 paf 486: XMLCh curVal = tables.fromTable[*srcPtr];
1.6 paf 487: if(curVal)
1.10 paf 488: *outPtr++=xlatOneTo(curVal, dest_charset.tables);
1.6 paf 489: else {
490: // use the replacement character
491: *outPtr++= '?';
492: }
493: }
1.1 paf 494:
1.6 paf 495: adest_body=dest_body;
496: adest_content_length=dest_content_length;
497: }
1.1 paf 498: }
499:
500: #ifdef XML
1.10 paf 501: static int xml256CharEncodingInputFunc (
502: unsigned char *out,
503: int *outlen,
504: const unsigned char *in,
505: int *inlen,
506: void *info) {
507: return transcodeToUTF8(
1.11 paf 508: in, *(unsigned int*)inlen,
1.10 paf 509: out, *(unsigned int*)outlen,
510: *(const Charset::Tables *)info);
511: }
512:
513: static int xml256CharEncodingOutputFunc (
514: unsigned char *out,
515: int *outlen,
516: const unsigned char *in,
517: int *inlen,
518: void *info) {
519: return transcodeFromUTF8(
1.11 paf 520: in, *(unsigned int*)inlen,
521: out, *(unsigned int*)outlen,
1.10 paf 522: *(const Charset::Tables *)info);
523: }
524:
525:
526: void Charset::addEncoding(char *name_cstr) {
527: xmlCharEncodingHandler *handler=
528: (xmlCharEncodingHandler *)malloc(sizeof(xmlCharEncodingHandler));
529: handler->name=name_cstr;
530: handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables;
531: handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables;
532:
533: xmlRegisterCharEncodingHandler(handler);
534: }
535:
536: void Charset::initTranscoder(const String *source, const char *name_cstr) {
1.15 paf 537: ftranscoder=xmlFindCharEncodingHandler(name_cstr);
538: transcoder(source); // check right way
539: }
540:
541: xmlCharEncodingHandler *Charset::transcoder(const String *source) {
542: if(!ftranscoder)
1.10 paf 543: throw Exception(0, 0,
544: source,
545: "unsupported encoding");
1.15 paf 546: return ftranscoder;
1.10 paf 547: }
548:
1.14 paf 549: const char *Charset::transcode_cstr(xmlChar *s) {
1.13 paf 550: if(!s)
1.14 paf 551: return "";
1.8 paf 552:
1.14 paf 553: int inlen=strlen((const char *)s);
1.8 paf 554: int outlen=inlen+1; // max
555: char *out=(char *)malloc(outlen*sizeof(char));
556:
1.15 paf 557: int size=transcoder(0)->output(
1.8 paf 558: (unsigned char*)out, &outlen,
1.14 paf 559: (const unsigned char*)s, &inlen,
1.15 paf 560: transcoder(0)->outputInfo);
1.8 paf 561: if(size<0)
562: throw Exception(0, 0,
563: 0,
564: "transcode_cstr failed (%d)", size);
565:
566: out[size]=0;
567: return out;
1.14 paf 568: }
569: String& Charset::transcode(xmlChar *s) {
570: return *NEW String(pool(), transcode_cstr(s));
571: }
572: const char *Charset::transcode_cstr(GdomeDOMString *s) {
573: return s?transcode_cstr(BAD_CAST s->str):"";
1.1 paf 574: }
1.8 paf 575: String& Charset::transcode(GdomeDOMString *s) {
1.1 paf 576: return *NEW String(pool(), transcode_cstr(s));
577: }
578:
1.8 paf 579: /// @test less memory using -maybe- xmlParserInputBufferCreateMem
1.12 paf 580: GdomeDOMString_auto_ptr Charset::transcode_buf(const char *buf, size_t buf_size) {
1.8 paf 581: int outlen=buf_size*6/*max*/+1;
582: unsigned char *out=(unsigned char*)malloc(outlen*sizeof(unsigned char));
1.15 paf 583: int size=transcoder(0)->input(
1.8 paf 584: out, &outlen,
1.10 paf 585: (const unsigned char *)buf, (int *)&buf_size,
1.15 paf 586: transcoder(0)->inputInfo);
1.8 paf 587: if(size<0)
588: throw Exception(0, 0,
589: 0,
590: "transcode_buf failed (%d)", size);
591:
592: out[size]=0;
1.16 ! paf 593: return GdomeDOMString_auto_ptr((gchar*)out);
1.1 paf 594: }
1.12 paf 595: GdomeDOMString_auto_ptr Charset::transcode(const String& s) {
1.1 paf 596: const char *cstr=s.cstr(String::UL_UNSPECIFIED);
597:
598: return transcode_buf(cstr, strlen(cstr));
599: }
600: #endif
E-mail: