Annotation of parser3/src/main/pa_charset.C, revision 1.24
1.1 paf 1: /** @file
2: Parser: Charset connection implementation.
3:
1.20 paf 4: Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.4 paf 5: Author: Alexander Petrosyan<paf@design.ru>(http://paf.design.ru)
1.1 paf 6:
1.24 ! paf 7: $Id: pa_charset.C,v 1.23.8.1 2002/06/27 11:56:19 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.23 paf 156: throw Exception("parser.runtime",
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.18 paf 340: case 6: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
1.4 paf 341: curVal>>= 6;
1.18 paf 342: case 5: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
1.4 paf 343: curVal>>= 6;
1.18 paf 344: case 4: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
1.4 paf 345: curVal>>= 6;
1.18 paf 346: case 3: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
1.4 paf 347: curVal>>= 6;
1.18 paf 348: case 2: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
1.4 paf 349: curVal>>= 6;
1.18 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:
1.23 paf 407: throw Exception(0,
1.1 paf 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
1.23 paf 419: throw Exception(0,
1.1 paf 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: void Charset::transcodeFromUTF8(Pool& pool,
452: const void *source_body, size_t source_content_length,
1.11 paf 453: const void *& adest_body, size_t& dest_content_length) const {
454: dest_content_length=source_content_length/*surly enough*/;
455: XMLByte *dest_body=(XMLByte*)pool.malloc(dest_content_length);
456:
457: if(::transcodeFromUTF8(
458: (XMLByte *)source_body, source_content_length,
459: dest_body, dest_content_length,
460: tables)<0)
1.10 paf 461: throw(0, 0,
462: 0,
1.11 paf 463: "Charset::transcodeToUTF8 buffer overflow");
1.10 paf 464:
465: // return
466: adest_body=dest_body;
1.1 paf 467: }
468:
469: /// transcode using both charsets
470: void Charset::transcodeToCharset(Pool& pool,
471: const Charset& dest_charset,
472: const void *source_body, size_t source_content_length,
1.6 paf 473: const void *& adest_body, size_t& adest_content_length) const {
1.3 paf 474: if(&dest_charset==this) {
1.6 paf 475: adest_body=source_body;
476: adest_content_length=source_content_length;
477: } else {
478: size_t dest_content_length=source_content_length;
479: unsigned char *dest_body=(unsigned char *)pool.malloc(dest_content_length);
480:
1.11 paf 481: const XMLByte* srcPtr=(XMLByte *)source_body;
482: const XMLByte* srcEnd=(XMLByte *)source_body+source_content_length;
1.6 paf 483:
484: for(XMLByte* outPtr=dest_body; srcPtr<srcEnd; srcPtr++) {
1.10 paf 485: XMLCh curVal = tables.fromTable[*srcPtr];
1.6 paf 486: if(curVal)
1.10 paf 487: *outPtr++=xlatOneTo(curVal, dest_charset.tables);
1.6 paf 488: else {
489: // use the replacement character
490: *outPtr++= '?';
491: }
492: }
1.1 paf 493:
1.6 paf 494: adest_body=dest_body;
495: adest_content_length=dest_content_length;
496: }
1.1 paf 497: }
498:
499: #ifdef XML
1.10 paf 500: static int xml256CharEncodingInputFunc (
501: unsigned char *out,
502: int *outlen,
503: const unsigned char *in,
504: int *inlen,
505: void *info) {
506: return transcodeToUTF8(
1.21 paf 507: in, *(size_t*)inlen,
508: out, *(size_t*)outlen,
1.10 paf 509: *(const Charset::Tables *)info);
510: }
511:
512: static int xml256CharEncodingOutputFunc (
513: unsigned char *out,
514: int *outlen,
515: const unsigned char *in,
516: int *inlen,
517: void *info) {
518: return transcodeFromUTF8(
1.21 paf 519: in, *(size_t*)inlen,
520: out, *(size_t*)outlen,
1.10 paf 521: *(const Charset::Tables *)info);
522: }
523:
524:
525: void Charset::addEncoding(char *name_cstr) {
526: xmlCharEncodingHandler *handler=
527: (xmlCharEncodingHandler *)malloc(sizeof(xmlCharEncodingHandler));
528: handler->name=name_cstr;
529: handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables;
530: handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables;
531:
532: xmlRegisterCharEncodingHandler(handler);
533: }
534:
535: void Charset::initTranscoder(const String *source, const char *name_cstr) {
1.15 paf 536: ftranscoder=xmlFindCharEncodingHandler(name_cstr);
537: transcoder(source); // check right way
538: }
539:
540: xmlCharEncodingHandler *Charset::transcoder(const String *source) {
541: if(!ftranscoder)
1.23 paf 542: throw Exception("parser.runtime",
1.10 paf 543: source,
544: "unsupported encoding");
1.15 paf 545: return ftranscoder;
1.10 paf 546: }
547:
1.14 paf 548: const char *Charset::transcode_cstr(xmlChar *s) {
1.13 paf 549: if(!s)
1.14 paf 550: return "";
1.8 paf 551:
1.14 paf 552: int inlen=strlen((const char *)s);
1.8 paf 553: int outlen=inlen+1; // max
554: char *out=(char *)malloc(outlen*sizeof(char));
555:
1.17 paf 556: int size;
557: if(xmlCharEncodingOutputFunc output=transcoder(0)->output) {
558: size=output(
559: (unsigned char*)out, &outlen,
560: (const unsigned char*)s, &inlen,
561: transcoder(0)->outputInfo);
562: } else
563: memcpy(out, s, size=inlen);
1.8 paf 564: if(size<0)
1.23 paf 565: throw Exception(0,
1.8 paf 566: 0,
567: "transcode_cstr failed (%d)", size);
568:
569: out[size]=0;
570: return out;
1.14 paf 571: }
572: String& Charset::transcode(xmlChar *s) {
573: return *NEW String(pool(), transcode_cstr(s));
574: }
575: const char *Charset::transcode_cstr(GdomeDOMString *s) {
576: return s?transcode_cstr(BAD_CAST s->str):"";
1.1 paf 577: }
1.8 paf 578: String& Charset::transcode(GdomeDOMString *s) {
1.1 paf 579: return *NEW String(pool(), transcode_cstr(s));
580: }
581:
1.8 paf 582: /// @test less memory using -maybe- xmlParserInputBufferCreateMem
1.24 ! paf 583: xmlChar *Charset::transcode_buf2xchar(const char *buf, size_t buf_size) {
1.8 paf 584: int outlen=buf_size*6/*max*/+1;
585: unsigned char *out=(unsigned char*)malloc(outlen*sizeof(unsigned char));
1.17 paf 586:
587: int size;
588: if(xmlCharEncodingInputFunc input=transcoder(0)->input) {
589: size=input(
590: out, &outlen,
591: (const unsigned char *)buf, (int *)&buf_size,
592: transcoder(0)->inputInfo);
593: } else
594: memcpy(out, buf, size=buf_size);
595:
1.8 paf 596: if(size<0)
1.23 paf 597: throw Exception(0,
1.8 paf 598: 0,
599: "transcode_buf failed (%d)", size);
600:
601: out[size]=0;
1.24 ! paf 602: return (xmlChar *)out;
! 603: }
! 604: GdomeDOMString_auto_ptr Charset::transcode_buf2dom(const char *buf, size_t buf_size) {
! 605: return GdomeDOMString_auto_ptr((gchar*)transcode_buf2xchar(buf, buf_size));
1.1 paf 606: }
1.12 paf 607: GdomeDOMString_auto_ptr Charset::transcode(const String& s) {
1.1 paf 608: const char *cstr=s.cstr(String::UL_UNSPECIFIED);
609:
1.24 ! paf 610: return transcode_buf2dom(cstr, strlen(cstr));
1.1 paf 611: }
612: #endif
E-mail: