Annotation of parser3/src/main/pa_charset.C, revision 1.10
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.10 ! paf 7: $Id: pa_charset.C,v 1.9 2001/12/28 14:06:51 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.10 ! paf 290: static size_t transcodeToUTF8(
! 291: const void *source_body, size_t source_content_length,
! 292: XMLByte *dest_body, size_t dest_buf_length,
! 293: const Charset::Tables& tables) {
1.1 paf 294: const XMLByte* srcPtr=(const XMLByte*)source_body;
295: const XMLByte* srcEnd=(const XMLByte*)source_body+source_content_length;
296: XMLByte* outPtr=dest_body;
1.10 ! paf 297: XMLByte* outEnd=dest_body+dest_buf_length;
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.4 paf 340: case 6: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
341: curVal>>= 6;
342: case 5: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
343: curVal>>= 6;
344: case 4: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
345: curVal>>= 6;
346: case 3: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
347: curVal>>= 6;
348: case 2: *--outPtr = XMLByte((curVal | 0x80UL) & 0xBFUL);
349: curVal>>= 6;
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.10 ! paf 357: // eaten
! 358: return outPtr-dest_body;
1.1 paf 359: }
1.10 ! paf 360: static size_t transcodeFromUTF8(
1.1 paf 361: const void *source_body, size_t source_content_length,
1.10 ! paf 362: XMLByte* dest_body, size_t dest_buf_length,
! 363: const Charset::Tables& tables) {
1.1 paf 364: const XMLByte* srcPtr=(const XMLByte*)source_body;
365: const XMLByte* srcEnd=(const XMLByte*)source_body+source_content_length;
366: XMLByte* outPtr=dest_body;
1.10 ! paf 367: XMLByte* outEnd=dest_body+dest_buf_length;
1.1 paf 368:
1.10 ! paf 369: // We now loop until we either run out of input data, or room to store
! 370: while ((srcPtr < srcEnd) && (outPtr < outEnd)) {
1.1 paf 371: // Get the next leading byte out
372: const XMLByte firstByte = *srcPtr;
373:
1.4 paf 374: // Special-case ASCII, which is a leading byte value of<= 127
375: if(firstByte<= 127) {
376: *outPtr++= firstByte;
1.1 paf 377: srcPtr++;
378: continue;
379: }
380:
381: // See how many trailing src bytes this sequence is going to require
382: const unsigned int trailingBytes = gUTFBytes[firstByte];
383:
384: // If there are not enough source bytes to do this one, then we
1.4 paf 385: // are done. Note that we done>= here because we are implicitly
1.1 paf 386: // counting the 1 byte we get no matter what.
1.4 paf 387: if(srcPtr+trailingBytes>= srcEnd)
1.1 paf 388: break;
389:
390: // Looks ok, so lets build up the value
391: uint tmpVal=0;
392: switch(trailingBytes) {
393: case 5: tmpVal+=*srcPtr++; tmpVal<<=6;
394: case 4: tmpVal+=*srcPtr++; tmpVal<<=6;
395: case 3: tmpVal+=*srcPtr++; tmpVal<<=6;
396: case 2: tmpVal+=*srcPtr++; tmpVal<<=6;
397: case 1: tmpVal+=*srcPtr++; tmpVal<<=6;
398: case 0: tmpVal+=*srcPtr++;
399: break;
400:
401: default:
402: throw Exception(0, 0,
403: 0,
1.4 paf 404: "transcodeFromUTF8 error: wrong trailingBytes value(%d)", trailingBytes);
1.1 paf 405: }
406: tmpVal-=gUTFOffsets[trailingBytes];
407:
408: // If it will fit into a single char, then put it in. Otherwise
409: // fail [*encode it as a surrogate pair. If its not valid, use the
410: // replacement char.*]
1.4 paf 411: if(!(tmpVal & 0xFFFF0000))
1.10 ! paf 412: *outPtr++= xlatOneTo(tmpVal, tables);
1.1 paf 413: else
414: throw Exception(0, 0,
415: 0,
1.4 paf 416: "transcodeFromUTF8 error: too big tmpVal(0x%08X)", tmpVal);
1.1 paf 417: }
418:
1.10 ! paf 419: // eaten
! 420: return outPtr-dest_body;
! 421: }
! 422:
! 423: /// @todo not so memory-hungry with prescan
! 424: /// @test buf overflow
! 425: void Charset::transcodeToUTF8(Pool& pool,
! 426: const void *source_body, size_t source_content_length,
! 427: const void *& adest_body, size_t& adest_content_length) const {
! 428: size_t dest_buf_length=source_content_length*6/*so that surly enough*/;
! 429: XMLByte *dest_body=(XMLByte*)pool.malloc(dest_buf_length);
! 430:
! 431: size_t dest_content_length=::transcodeToUTF8(
! 432: source_body, source_content_length,
! 433: dest_body, dest_buf_length,
! 434: tables);
! 435: /*if(dest_content_length<dest_buf_length)
! 436: throw(0, 0,
! 437: 0,
! 438: "Charset::transcodeToUTF8 error");*/
! 439:
1.1 paf 440: // return
441: adest_body=dest_body;
1.10 ! paf 442: adest_content_length=dest_content_length;
! 443: }
! 444: /// @test buf overflow
! 445: void Charset::transcodeFromUTF8(Pool& pool,
! 446: const void *source_body, size_t source_content_length,
! 447: const void *& adest_body, size_t& adest_content_length) const {
! 448: size_t dest_buf_length=source_content_length/*surly enough*/;
! 449: XMLByte *dest_body=(XMLByte*)pool.malloc(dest_buf_length);
! 450:
! 451: size_t dest_content_length=::transcodeFromUTF8(
! 452: source_body, source_content_length,
! 453: dest_body, dest_buf_length,
! 454: tables);
! 455: /* if(dest_content_length!=dest_buf_length)
! 456: throw(0, 0,
! 457: 0,
! 458: "Charset::transcodeToUTF8 error");*/
! 459:
! 460:
! 461: // return
! 462: adest_body=dest_body;
! 463: adest_content_length=dest_content_length;
1.1 paf 464: }
465:
466: /// transcode using both charsets
467: void Charset::transcodeToCharset(Pool& pool,
468: const Charset& dest_charset,
469: const void *source_body, size_t source_content_length,
1.6 paf 470: const void *& adest_body, size_t& adest_content_length) const {
1.3 paf 471: if(&dest_charset==this) {
1.6 paf 472: adest_body=source_body;
473: adest_content_length=source_content_length;
474: } else {
475: size_t dest_content_length=source_content_length;
476: unsigned char *dest_body=(unsigned char *)pool.malloc(dest_content_length);
477:
478: const XMLByte* srcPtr=(const XMLByte*)source_body;
479: const XMLByte* srcEnd=(const XMLByte*)source_body+source_content_length;
480:
481: for(XMLByte* outPtr=dest_body; srcPtr<srcEnd; srcPtr++) {
1.10 ! paf 482: XMLCh curVal = tables.fromTable[*srcPtr];
1.6 paf 483: if(curVal)
1.10 ! paf 484: *outPtr++=xlatOneTo(curVal, dest_charset.tables);
1.6 paf 485: else {
486: // use the replacement character
487: *outPtr++= '?';
488: }
489: }
1.1 paf 490:
1.6 paf 491: adest_body=dest_body;
492: adest_content_length=dest_content_length;
493: }
1.1 paf 494: }
495:
496: #ifdef XML
1.10 ! paf 497: static int xml256CharEncodingInputFunc (
! 498: unsigned char *out,
! 499: int *outlen,
! 500: const unsigned char *in,
! 501: int *inlen,
! 502: void *info) {
! 503: return transcodeToUTF8(
! 504: in, *inlen,
! 505: out, *(unsigned int*)outlen,
! 506: *(const Charset::Tables *)info);
! 507: }
! 508:
! 509: static int xml256CharEncodingOutputFunc (
! 510: unsigned char *out,
! 511: int *outlen,
! 512: const unsigned char *in,
! 513: int *inlen,
! 514: void *info) {
! 515: return transcodeFromUTF8(
! 516: in, *inlen,
! 517: out, *outlen,
! 518: *(const Charset::Tables *)info);
! 519: }
! 520:
! 521:
! 522: void Charset::addEncoding(char *name_cstr) {
! 523: xmlCharEncodingHandler *handler=
! 524: (xmlCharEncodingHandler *)malloc(sizeof(xmlCharEncodingHandler));
! 525: handler->name=name_cstr;
! 526: handler->input=xml256CharEncodingInputFunc; handler->inputInfo=&tables;
! 527: handler->output=xml256CharEncodingOutputFunc; handler->outputInfo=&tables;
! 528:
! 529: xmlRegisterCharEncodingHandler(handler);
! 530: }
! 531:
! 532: void Charset::initTranscoder(const String *source, const char *name_cstr) {
! 533: transcoder=xmlFindCharEncodingHandler(name_cstr);
! 534: if(!transcoder)
! 535: throw Exception(0, 0,
! 536: source,
! 537: "unsupported encoding");
! 538: }
! 539:
1.8 paf 540: const char *Charset::transcode_cstr(GdomeDOMString *s) {
541: if(!transcoder)
542: throw Exception(0, 0,
543: 0,
544: "transcode_cstr no transcoder");
545:
546: int inlen=gdome_str_length(s);
547: int outlen=inlen+1; // max
548: char *out=(char *)malloc(outlen*sizeof(char));
549:
550: int size=transcoder->output(
551: (unsigned char*)out, &outlen,
1.10 ! paf 552: (const unsigned char*)s->str, &inlen,
! 553: transcoder->outputInfo);
1.8 paf 554: if(size<0)
555: throw Exception(0, 0,
556: 0,
557: "transcode_cstr failed (%d)", size);
558:
559: out[size]=0;
560: return out;
1.1 paf 561: }
1.8 paf 562: String& Charset::transcode(GdomeDOMString *s) {
1.1 paf 563: return *NEW String(pool(), transcode_cstr(s));
564: }
565:
1.8 paf 566: /// @test less memory using -maybe- xmlParserInputBufferCreateMem
567: GdomeDOMString *Charset::transcode_buf(const char *buf, size_t buf_size) {
568: if(!transcoder)
569: throw Exception(0, 0,
570: 0,
571: "transcode_buf no transcoder");
572:
573: int outlen=buf_size*6/*max*/+1;
574: unsigned char *out=(unsigned char*)malloc(outlen*sizeof(unsigned char));
575: int size=transcoder->input(
576: out, &outlen,
1.10 ! paf 577: (const unsigned char *)buf, (int *)&buf_size,
! 578: transcoder->inputInfo);
1.8 paf 579: if(size<0)
580: throw Exception(0, 0,
581: 0,
582: "transcode_buf failed (%d)", size);
583:
584: out[size]=0;
585: return gdome_str_mkref_own((gchar*)out);
1.1 paf 586: }
1.8 paf 587: GdomeDOMString *Charset::transcode(const String& s) {
1.1 paf 588: const char *cstr=s.cstr(String::UL_UNSPECIFIED);
589:
590: return transcode_buf(cstr, strlen(cstr));
591: }
592: #endif
E-mail: