* xmlCharEncOutput: * @output: a parser output buffer * @init: is this an initialization call without data * * Generic front-end for the encoding handler on parser output * a first call with @init == 1 has to be made first to initiate the * output in case of non-stateless encoding needing to initiate their * state or the output (like the BOM in UTF16). * In case of UTF8 sequence conversion
| 2430 | * Returns the number of bytes written or an XML_ENC_ERR code. |
| 2431 | */ |
| 2432 | int |
| 2433 | xmlCharEncOutput(xmlOutputBufferPtr output, int init) |
| 2434 | { |
| 2435 | int ret; |
| 2436 | size_t written; |
| 2437 | int writtentot = 0; |
| 2438 | size_t toconv; |
| 2439 | int c_in; |
| 2440 | int c_out; |
| 2441 | xmlBufPtr in; |
| 2442 | xmlBufPtr out; |
| 2443 | |
| 2444 | if ((output == NULL) || (output->encoder == NULL) || |
| 2445 | (output->buffer == NULL) || (output->conv == NULL)) |
| 2446 | return(XML_ENC_ERR_INTERNAL); |
| 2447 | out = output->conv; |
| 2448 | in = output->buffer; |
| 2449 | |
| 2450 | retry: |
| 2451 | |
| 2452 | written = xmlBufAvail(out); |
| 2453 | |
| 2454 | /* |
| 2455 | * First specific handling of the initialization call |
| 2456 | */ |
| 2457 | if (init) { |
| 2458 | c_in = 0; |
| 2459 | c_out = written; |
| 2460 | /* TODO: Check return value. */ |
| 2461 | xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out, |
| 2462 | NULL, &c_in); |
| 2463 | xmlBufAddLen(out, c_out); |
| 2464 | return(c_out); |
| 2465 | } |
| 2466 | |
| 2467 | /* |
| 2468 | * Conversion itself. |
| 2469 | */ |
| 2470 | toconv = xmlBufUse(in); |
| 2471 | if (toconv > 64 * 1024) |
| 2472 | toconv = 64 * 1024; |
| 2473 | if (toconv * 4 >= written) { |
| 2474 | if (xmlBufGrow(out, toconv * 4) < 0) { |
| 2475 | ret = XML_ENC_ERR_MEMORY; |
| 2476 | goto error; |
| 2477 | } |
| 2478 | written = xmlBufAvail(out); |
| 2479 | } |
| 2480 | if (written > 256 * 1024) |
| 2481 | written = 256 * 1024; |
| 2482 | |
| 2483 | c_in = toconv; |
| 2484 | c_out = written; |
| 2485 | ret = xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out, |
| 2486 | xmlBufContent(in), &c_in); |
| 2487 | xmlBufShrink(in, c_in); |
| 2488 | xmlBufAddLen(out, c_out); |
| 2489 | writtentot += c_out; |
no test coverage detected