Coverage Report

Created: 2025-10-28 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libxml2/encoding.c
Line
Count
Source
1
/*
2
 * encoding.c : implements the encoding conversion functions needed for XML
3
 *
4
 * Related specs:
5
 * rfc2044        (UTF-8 and UTF-16) F. Yergeau Alis Technologies
6
 * rfc2781        UTF-16, an encoding of ISO 10646, P. Hoffman, F. Yergeau
7
 * [ISO-10646]    UTF-8 and UTF-16 in Annexes
8
 * [ISO-8859-1]   ISO Latin-1 characters codes.
9
 * [UNICODE]      The Unicode Consortium, "The Unicode Standard --
10
 *                Worldwide Character Encoding -- Version 1.0", Addison-
11
 *                Wesley, Volume 1, 1991, Volume 2, 1992.  UTF-8 is
12
 *                described in Unicode Technical Report \#4.
13
 * [US-ASCII]     Coded Character Set--7-bit American Standard Code for
14
 *                Information Interchange, ANSI X3.4-1986.
15
 *
16
 * See Copyright for the status of this software.
17
 *
18
 * Author: Daniel Veillard
19
 *
20
 * Original code for IsoLatin1 and UTF-16 by Martin J. Duerst
21
 */
22
23
#define IN_LIBXML
24
#include "libxml.h"
25
26
#include <string.h>
27
#include <limits.h>
28
#include <ctype.h>
29
#include <stdlib.h>
30
31
#ifdef LIBXML_ICONV_ENABLED
32
#include <iconv.h>
33
#include <errno.h>
34
#endif
35
36
#include <libxml/encoding.h>
37
#include <libxml/xmlmemory.h>
38
#include <libxml/parser.h>
39
#ifdef LIBXML_HTML_ENABLED
40
#include <libxml/HTMLparser.h>
41
#endif
42
#include <libxml/xmlerror.h>
43
44
#include "private/buf.h"
45
#include "private/enc.h"
46
#include "private/error.h"
47
#include "private/memory.h"
48
49
#ifdef LIBXML_ICU_ENABLED
50
#include <unicode/ucnv.h>
51
#endif
52
53
1.55k
#define XML_HANDLER_STATIC (1 << 0)
54
48.2k
#define XML_HANDLER_LEGACY (1 << 1)
55
56
typedef struct _xmlCharEncodingAlias xmlCharEncodingAlias;
57
typedef xmlCharEncodingAlias *xmlCharEncodingAliasPtr;
58
struct _xmlCharEncodingAlias {
59
    const char *name;
60
    const char *alias;
61
};
62
63
static xmlCharEncodingAliasPtr xmlCharEncodingAliases = NULL;
64
static int xmlCharEncodingAliasesNb = 0;
65
static int xmlCharEncodingAliasesMax = 0;
66
67
static int xmlLittleEndian = 1;
68
69
typedef struct {
70
    const char *name;
71
    xmlCharEncoding enc;
72
} xmlEncTableEntry;
73
74
static const xmlEncTableEntry xmlEncTable[] = {
75
    { "ansi_x3.4-1968", XML_CHAR_ENCODING_ASCII },
76
    { "arabic", XML_CHAR_ENCODING_8859_6 },
77
    { "ascii", XML_CHAR_ENCODING_ASCII },
78
    { "asmo-708", XML_CHAR_ENCODING_8859_6 },
79
    { "cp1252", XML_CHAR_ENCODING_WINDOWS_1252 },
80
    { "cp819", XML_CHAR_ENCODING_8859_1 },
81
    { "cseucpkdfmtjapanese", XML_CHAR_ENCODING_EUC_JP },
82
    { "csiso2022jp", XML_CHAR_ENCODING_2022_JP },
83
    { "csiso88596e", XML_CHAR_ENCODING_8859_6 },
84
    { "csiso88596i", XML_CHAR_ENCODING_8859_6 },
85
    { "csiso88598e", XML_CHAR_ENCODING_8859_8 },
86
    { "csiso88598i", XML_CHAR_ENCODING_8859_8 },
87
    { "csisolatin1", XML_CHAR_ENCODING_8859_1 },
88
    { "csisolatin2", XML_CHAR_ENCODING_8859_2 },
89
    { "csisolatin3", XML_CHAR_ENCODING_8859_3 },
90
    { "csisolatin4", XML_CHAR_ENCODING_8859_4 },
91
    { "csisolatin5", XML_CHAR_ENCODING_8859_9 },
92
    { "csisolatin6", XML_CHAR_ENCODING_8859_10 },
93
    { "csisolatin9", XML_CHAR_ENCODING_8859_15 },
94
    { "csisolatinarabic", XML_CHAR_ENCODING_8859_6 },
95
    { "csisolatincyrillic", XML_CHAR_ENCODING_8859_5 },
96
    { "csisolatingreek", XML_CHAR_ENCODING_8859_7 },
97
    { "csisolatinhebrew", XML_CHAR_ENCODING_8859_8 },
98
    { "csshiftjis", XML_CHAR_ENCODING_SHIFT_JIS },
99
    { "csunicode", XML_CHAR_ENCODING_UTF16 },
100
    { "cyrillic", XML_CHAR_ENCODING_8859_5 },
101
    { "ecma-114", XML_CHAR_ENCODING_8859_6 },
102
    { "ecma-118", XML_CHAR_ENCODING_8859_7 },
103
    { "elot_928", XML_CHAR_ENCODING_8859_7 },
104
    { "euc-jp", XML_CHAR_ENCODING_EUC_JP },
105
    { "greek", XML_CHAR_ENCODING_8859_7 },
106
    { "greek8", XML_CHAR_ENCODING_8859_7 },
107
    { "html", XML_CHAR_ENCODING_HTML },
108
    { "ibm819", XML_CHAR_ENCODING_8859_1 },
109
    { "iso latin 1", XML_CHAR_ENCODING_8859_1 },
110
    { "iso latin 2", XML_CHAR_ENCODING_8859_2 },
111
    { "iso-10646-ucs-2", XML_CHAR_ENCODING_UCS2 },
112
    { "iso-10646-ucs-4", XML_CHAR_ENCODING_UCS4LE },
113
    { "iso-2022-jp", XML_CHAR_ENCODING_2022_JP },
114
    { "iso-8859-1", XML_CHAR_ENCODING_8859_1 },
115
    { "iso-8859-10", XML_CHAR_ENCODING_8859_10 },
116
    { "iso-8859-11", XML_CHAR_ENCODING_8859_11 },
117
    { "iso-8859-13", XML_CHAR_ENCODING_8859_13 },
118
    { "iso-8859-14", XML_CHAR_ENCODING_8859_14 },
119
    { "iso-8859-15", XML_CHAR_ENCODING_8859_15 },
120
    { "iso-8859-16", XML_CHAR_ENCODING_8859_16 },
121
    { "iso-8859-2", XML_CHAR_ENCODING_8859_2 },
122
    { "iso-8859-3", XML_CHAR_ENCODING_8859_3 },
123
    { "iso-8859-4", XML_CHAR_ENCODING_8859_4 },
124
    { "iso-8859-5", XML_CHAR_ENCODING_8859_5 },
125
    { "iso-8859-6", XML_CHAR_ENCODING_8859_6 },
126
    { "iso-8859-6-e", XML_CHAR_ENCODING_8859_6 },
127
    { "iso-8859-6-i", XML_CHAR_ENCODING_8859_6 },
128
    { "iso-8859-7", XML_CHAR_ENCODING_8859_7 },
129
    { "iso-8859-8", XML_CHAR_ENCODING_8859_8 },
130
    { "iso-8859-8-i", XML_CHAR_ENCODING_8859_8 },
131
    { "iso-8859-9", XML_CHAR_ENCODING_8859_9 },
132
    { "iso-ir-100", XML_CHAR_ENCODING_8859_1 },
133
    { "iso-ir-101", XML_CHAR_ENCODING_8859_2 },
134
    { "iso-ir-109", XML_CHAR_ENCODING_8859_3 },
135
    { "iso-ir-110", XML_CHAR_ENCODING_8859_4 },
136
    { "iso-ir-126", XML_CHAR_ENCODING_8859_7 },
137
    { "iso-ir-127", XML_CHAR_ENCODING_8859_6 },
138
    { "iso-ir-138", XML_CHAR_ENCODING_8859_8 },
139
    { "iso-ir-144", XML_CHAR_ENCODING_8859_5 },
140
    { "iso-ir-148", XML_CHAR_ENCODING_8859_9 },
141
    { "iso-ir-157", XML_CHAR_ENCODING_8859_10 },
142
    { "iso-latin-1", XML_CHAR_ENCODING_8859_1 },
143
    { "iso-latin-2", XML_CHAR_ENCODING_8859_2 },
144
    { "iso8859-1", XML_CHAR_ENCODING_8859_1 },
145
    { "iso8859-10", XML_CHAR_ENCODING_8859_1 },
146
    { "iso8859-13", XML_CHAR_ENCODING_8859_1 },
147
    { "iso8859-14", XML_CHAR_ENCODING_8859_1 },
148
    { "iso8859-15", XML_CHAR_ENCODING_8859_1 },
149
    { "iso8859-2", XML_CHAR_ENCODING_8859_2 },
150
    { "iso8859-3", XML_CHAR_ENCODING_8859_3 },
151
    { "iso8859-4", XML_CHAR_ENCODING_8859_4 },
152
    { "iso8859-5", XML_CHAR_ENCODING_8859_5 },
153
    { "iso8859-6", XML_CHAR_ENCODING_8859_6 },
154
    { "iso8859-7", XML_CHAR_ENCODING_8859_7 },
155
    { "iso8859-8", XML_CHAR_ENCODING_8859_8 },
156
    { "iso8859-9", XML_CHAR_ENCODING_8859_9 },
157
    { "iso88591", XML_CHAR_ENCODING_8859_1 },
158
    { "iso885910", XML_CHAR_ENCODING_8859_10 },
159
    { "iso885913", XML_CHAR_ENCODING_8859_13 },
160
    { "iso885914", XML_CHAR_ENCODING_8859_14 },
161
    { "iso885915", XML_CHAR_ENCODING_8859_15 },
162
    { "iso88592", XML_CHAR_ENCODING_8859_2 },
163
    { "iso88593", XML_CHAR_ENCODING_8859_3 },
164
    { "iso88594", XML_CHAR_ENCODING_8859_4 },
165
    { "iso88595", XML_CHAR_ENCODING_8859_5 },
166
    { "iso88596", XML_CHAR_ENCODING_8859_6 },
167
    { "iso88597", XML_CHAR_ENCODING_8859_7 },
168
    { "iso88598", XML_CHAR_ENCODING_8859_8 },
169
    { "iso88599", XML_CHAR_ENCODING_8859_9 },
170
    { "iso_8859-1", XML_CHAR_ENCODING_8859_1 },
171
    { "iso_8859-1:1987", XML_CHAR_ENCODING_8859_1 },
172
    { "iso_8859-2", XML_CHAR_ENCODING_8859_2 },
173
    { "iso_8859-2:1987", XML_CHAR_ENCODING_8859_2 },
174
    { "iso_8859-3", XML_CHAR_ENCODING_8859_3 },
175
    { "iso_8859-3:1988", XML_CHAR_ENCODING_8859_3 },
176
    { "iso_8859-4", XML_CHAR_ENCODING_8859_4 },
177
    { "iso_8859-4:1988", XML_CHAR_ENCODING_8859_4 },
178
    { "iso_8859-5", XML_CHAR_ENCODING_8859_5 },
179
    { "iso_8859-5:1988", XML_CHAR_ENCODING_8859_5 },
180
    { "iso_8859-6", XML_CHAR_ENCODING_8859_6 },
181
    { "iso_8859-6:1987", XML_CHAR_ENCODING_8859_6 },
182
    { "iso_8859-7", XML_CHAR_ENCODING_8859_7 },
183
    { "iso_8859-7:1987", XML_CHAR_ENCODING_8859_7 },
184
    { "iso_8859-8", XML_CHAR_ENCODING_8859_8 },
185
    { "iso_8859-8:1988", XML_CHAR_ENCODING_8859_8 },
186
    { "iso_8859-9", XML_CHAR_ENCODING_8859_9 },
187
    { "iso_8859-9:1989", XML_CHAR_ENCODING_8859_9 },
188
    { "l1", XML_CHAR_ENCODING_8859_1 },
189
    { "l2", XML_CHAR_ENCODING_8859_2 },
190
    { "l3", XML_CHAR_ENCODING_8859_3 },
191
    { "l4", XML_CHAR_ENCODING_8859_4 },
192
    { "l5", XML_CHAR_ENCODING_8859_9 },
193
    { "l6", XML_CHAR_ENCODING_8859_10 },
194
    { "l9", XML_CHAR_ENCODING_8859_15 },
195
    { "latin1", XML_CHAR_ENCODING_8859_1 },
196
    { "latin2", XML_CHAR_ENCODING_8859_2 },
197
    { "latin3", XML_CHAR_ENCODING_8859_3 },
198
    { "latin4", XML_CHAR_ENCODING_8859_4 },
199
    { "latin5", XML_CHAR_ENCODING_8859_9 },
200
    { "latin6", XML_CHAR_ENCODING_8859_10 },
201
    { "logical", XML_CHAR_ENCODING_8859_8 },
202
    { "ms932", XML_CHAR_ENCODING_SHIFT_JIS },
203
    { "ms_kanji", XML_CHAR_ENCODING_SHIFT_JIS },
204
    { "shift-jis", XML_CHAR_ENCODING_SHIFT_JIS },
205
    { "shift_jis", XML_CHAR_ENCODING_SHIFT_JIS },
206
    { "sjis", XML_CHAR_ENCODING_SHIFT_JIS },
207
    { "sun_eu_greek", XML_CHAR_ENCODING_8859_7 },
208
    { "ucs-2", XML_CHAR_ENCODING_UCS2 },
209
    { "ucs-4", XML_CHAR_ENCODING_UCS4LE },
210
    { "ucs2", XML_CHAR_ENCODING_UCS2 },
211
    { "ucs4", XML_CHAR_ENCODING_UCS4LE },
212
    { "unicode", XML_CHAR_ENCODING_UTF16 },
213
    { "unicode-1-1-utf-8", XML_CHAR_ENCODING_UTF8 },
214
    { "unicode11utf8", XML_CHAR_ENCODING_UTF8 },
215
    { "unicode20utf8", XML_CHAR_ENCODING_UTF8 },
216
    { "unicodefffe", XML_CHAR_ENCODING_UTF16BE },
217
    { "unicodefeff", XML_CHAR_ENCODING_UTF16LE },
218
    { "us-ascii", XML_CHAR_ENCODING_ASCII },
219
    { "utf-16", XML_CHAR_ENCODING_UTF16 },
220
    { "utf-16be", XML_CHAR_ENCODING_UTF16BE },
221
    { "utf-16le", XML_CHAR_ENCODING_UTF16LE },
222
    { "utf-8", XML_CHAR_ENCODING_UTF8 },
223
    { "utf16", XML_CHAR_ENCODING_UTF16 },
224
    { "utf8", XML_CHAR_ENCODING_UTF8 },
225
    { "visual", XML_CHAR_ENCODING_8859_8 },
226
    { "windows-1252", XML_CHAR_ENCODING_WINDOWS_1252 },
227
    { "windows-31j", XML_CHAR_ENCODING_SHIFT_JIS },
228
    { "x-cp1252", XML_CHAR_ENCODING_WINDOWS_1252 },
229
    { "x-euc-jp", XML_CHAR_ENCODING_EUC_JP },
230
    { "x-sjis", XML_CHAR_ENCODING_SHIFT_JIS },
231
    { "x-unicode20utf8", XML_CHAR_ENCODING_UTF8 }
232
};
233
234
static xmlCharEncError
235
asciiToAscii(void *vctxt, unsigned char* out, int *outlen,
236
             const unsigned char* in, int *inlen, int flush);
237
static xmlCharEncError
238
UTF8ToUTF8(void *vctxt, unsigned char* out, int *outlen,
239
           const unsigned char* inb, int *inlenb, int flush);
240
static xmlCharEncError
241
latin1ToUTF8(void *vctxt, unsigned char* out, int *outlen,
242
             const unsigned char* in, int *inlen, int flush);
243
static xmlCharEncError
244
UTF16LEToUTF8(void *vctxt, unsigned char* out, int *outlen,
245
              const unsigned char* inb, int *inlenb, int flush);
246
static xmlCharEncError
247
UTF16BEToUTF8(void *vctxt, unsigned char* out, int *outlen,
248
              const unsigned char* inb, int *inlenb, int flush);
249
250
#ifdef LIBXML_OUTPUT_ENABLED
251
252
static xmlCharEncError
253
UTF8ToLatin1(void *vctxt, unsigned char* outb, int *outlen,
254
             const unsigned char* in, int *inlen, int flush);
255
static xmlCharEncError
256
UTF8ToUTF16(void *vctxt, unsigned char* outb, int *outlen,
257
            const unsigned char* in, int *inlen, int flush);
258
static xmlCharEncError
259
UTF8ToUTF16LE(void *vctxt, unsigned char* outb, int *outlen,
260
              const unsigned char* in, int *inlen, int flush);
261
static xmlCharEncError
262
UTF8ToUTF16BE(void *vctxt, unsigned char* outb, int *outlen,
263
              const unsigned char* in, int *inlen, int flush);
264
265
#else /* LIBXML_OUTPUT_ENABLED */
266
267
#define UTF8ToLatin1 NULL
268
#define UTF8ToUTF16 NULL
269
#define UTF8ToUTF16LE NULL
270
#define UTF8ToUTF16BE NULL
271
272
#endif /* LIBXML_OUTPUT_ENABLED */
273
274
#if defined(LIBXML_OUTPUT_ENABLED) && defined(LIBXML_HTML_ENABLED)
275
static xmlCharEncError
276
UTF8ToHtmlWrapper(void *vctxt, unsigned char *out, int *outlen,
277
                  const unsigned char *in, int *inlen, int flush);
278
#else
279
#define UTF8ToHtmlWrapper NULL
280
#endif
281
282
#include "codegen/charset.inc"
283
284
static xmlCharEncError
285
EightBitToUtf8(void *vctxt, unsigned char* out, int *outlen,
286
               const unsigned char* in, int *inlen, int flush);
287
static xmlCharEncError
288
Utf8ToEightBit(void *vctxt, unsigned char *out, int *outlen,
289
               const unsigned char *in, int *inlen, int flush);
290
291
#define MAKE_8BIT_HANDLER(name, table) \
292
    { (char *) name, { EightBitToUtf8 }, { Utf8ToEightBit }, \
293
      (void *) xmlunicodetable_##table, \
294
      (void *) xmltranscodetable_##table, \
295
      NULL, XML_HANDLER_STATIC }
296
297
#if !defined(LIBXML_ICONV_ENABLED) && !defined(LIBXML_ICU_ENABLED) && \
298
    defined(LIBXML_ISO8859X_ENABLED)
299
300
#define MAKE_ISO_HANDLER(name, n) MAKE_8BIT_HANDLER(name, ISO8859_##n)
301
302
#else /* LIBXML_ISO8859X_ENABLED */
303
304
#define MAKE_ISO_HANDLER(name, n) \
305
    { (char *) name, { NULL }, { NULL }, NULL, NULL, NULL, \
306
      XML_HANDLER_STATIC }
307
308
#endif /* LIBXML_ISO8859X_ENABLED */
309
310
#define MAKE_HANDLER(name, in, out) \
311
    { (char *) name, { in }, { out }, NULL, NULL, NULL, XML_HANDLER_STATIC }
312
313
/*
314
 * The layout must match enum xmlCharEncoding.
315
 *
316
 * Names should match the IANA registry if possible:
317
 * https://www.iana.org/assignments/character-sets/character-sets.xhtml
318
 */
319
static const xmlCharEncodingHandler defaultHandlers[32] = {
320
    MAKE_HANDLER(NULL, NULL, NULL), /* NONE */
321
    MAKE_HANDLER("UTF-8", UTF8ToUTF8, UTF8ToUTF8),
322
    MAKE_HANDLER("UTF-16LE", UTF16LEToUTF8, UTF8ToUTF16LE),
323
    MAKE_HANDLER("UTF-16BE", UTF16BEToUTF8, UTF8ToUTF16BE),
324
    MAKE_HANDLER("UCS-4LE", NULL, NULL),
325
    MAKE_HANDLER("UCS-4BE", NULL, NULL),
326
    MAKE_HANDLER("IBM037", NULL, NULL),
327
    MAKE_HANDLER(NULL, NULL, NULL), /* UCS4_2143 */
328
    MAKE_HANDLER(NULL, NULL, NULL), /* UCS4_3412 */
329
    MAKE_HANDLER("UCS-2", NULL, NULL),
330
    MAKE_HANDLER("ISO-8859-1", latin1ToUTF8, UTF8ToLatin1),
331
    MAKE_ISO_HANDLER("ISO-8859-2", 2),
332
    MAKE_ISO_HANDLER("ISO-8859-3", 3),
333
    MAKE_ISO_HANDLER("ISO-8859-4", 4),
334
    MAKE_ISO_HANDLER("ISO-8859-5", 5),
335
    MAKE_ISO_HANDLER("ISO-8859-6", 6),
336
    MAKE_ISO_HANDLER("ISO-8859-7", 7),
337
    MAKE_ISO_HANDLER("ISO-8859-8", 8),
338
    MAKE_ISO_HANDLER("ISO-8859-9", 9),
339
    MAKE_HANDLER("ISO-2022-JP", NULL, NULL),
340
    MAKE_HANDLER("Shift_JIS", NULL, NULL),
341
    MAKE_HANDLER("EUC-JP", NULL, NULL),
342
    MAKE_HANDLER("US-ASCII", asciiToAscii, asciiToAscii),
343
    MAKE_HANDLER("UTF-16", UTF16LEToUTF8, UTF8ToUTF16),
344
    MAKE_HANDLER("HTML", NULL, UTF8ToHtmlWrapper),
345
    MAKE_ISO_HANDLER("ISO-8859-10", 10),
346
    MAKE_ISO_HANDLER("ISO-8859-11", 11),
347
    MAKE_ISO_HANDLER("ISO-8859-13", 13),
348
    MAKE_ISO_HANDLER("ISO-8859-14", 14),
349
    MAKE_ISO_HANDLER("ISO-8859-15", 15),
350
    MAKE_ISO_HANDLER("ISO-8859-16", 16),
351
    MAKE_8BIT_HANDLER("windows-1252", windows_1252)
352
};
353
354
#define NUM_DEFAULT_HANDLERS \
355
1.09k
    (sizeof(defaultHandlers) / sizeof(defaultHandlers[0]))
356
357
/* the size should be growable, but it's not a big deal ... */
358
0
#define MAX_ENCODING_HANDLERS 50
359
static xmlCharEncodingHandlerPtr *globalHandlers = NULL;
360
static int nbCharEncodingHandler = 0;
361
362
#ifdef LIBXML_ICONV_ENABLED
363
static xmlParserErrors
364
xmlCharEncIconv(const char *name, xmlCharEncFlags flags,
365
                xmlCharEncodingHandler **out);
366
#endif
367
368
#ifdef LIBXML_ICU_ENABLED
369
static xmlParserErrors
370
xmlCharEncUconv(const char *name, xmlCharEncFlags flags,
371
                xmlCharEncodingHandler **out);
372
#endif
373
374
/************************************************************************
375
 *                  *
376
 *    Generic encoding handling routines      *
377
 *                  *
378
 ************************************************************************/
379
380
/**
381
 * Guess the encoding of the entity using the first bytes of the entity content
382
 * according to the non-normative appendix F of the XML-1.0 recommendation.
383
 *
384
 * @param in  a pointer to the first bytes of the XML entity, must be at least
385
 *            2 bytes long (at least 4 if encoding is UTF4 variant).
386
 * @param len  pointer to the length of the buffer
387
 * @returns a xmlCharEncoding value.
388
 */
389
xmlCharEncoding
390
xmlDetectCharEncoding(const unsigned char* in, int len)
391
0
{
392
0
    if (in == NULL)
393
0
        return(XML_CHAR_ENCODING_NONE);
394
0
    if (len >= 4) {
395
0
  if ((in[0] == 0x00) && (in[1] == 0x00) &&
396
0
      (in[2] == 0x00) && (in[3] == 0x3C))
397
0
      return(XML_CHAR_ENCODING_UCS4BE);
398
0
  if ((in[0] == 0x3C) && (in[1] == 0x00) &&
399
0
      (in[2] == 0x00) && (in[3] == 0x00))
400
0
      return(XML_CHAR_ENCODING_UCS4LE);
401
0
  if ((in[0] == 0x4C) && (in[1] == 0x6F) &&
402
0
      (in[2] == 0xA7) && (in[3] == 0x94))
403
0
      return(XML_CHAR_ENCODING_EBCDIC);
404
0
  if ((in[0] == 0x3C) && (in[1] == 0x3F) &&
405
0
      (in[2] == 0x78) && (in[3] == 0x6D))
406
0
      return(XML_CHAR_ENCODING_UTF8);
407
  /*
408
   * Although not part of the recommendation, we also
409
   * attempt an "auto-recognition" of UTF-16LE and
410
   * UTF-16BE encodings.
411
   */
412
0
  if ((in[0] == 0x3C) && (in[1] == 0x00) &&
413
0
      (in[2] == 0x3F) && (in[3] == 0x00))
414
0
      return(XML_CHAR_ENCODING_UTF16LE);
415
0
  if ((in[0] == 0x00) && (in[1] == 0x3C) &&
416
0
      (in[2] == 0x00) && (in[3] == 0x3F))
417
0
      return(XML_CHAR_ENCODING_UTF16BE);
418
0
    }
419
0
    if (len >= 3) {
420
  /*
421
   * Errata on XML-1.0 June 20 2001
422
   * We now allow an UTF8 encoded BOM
423
   */
424
0
  if ((in[0] == 0xEF) && (in[1] == 0xBB) &&
425
0
      (in[2] == 0xBF))
426
0
      return(XML_CHAR_ENCODING_UTF8);
427
0
    }
428
    /* For UTF-16 we can recognize by the BOM */
429
0
    if (len >= 2) {
430
0
  if ((in[0] == 0xFE) && (in[1] == 0xFF))
431
0
      return(XML_CHAR_ENCODING_UTF16BE);
432
0
  if ((in[0] == 0xFF) && (in[1] == 0xFE))
433
0
      return(XML_CHAR_ENCODING_UTF16LE);
434
0
    }
435
0
    return(XML_CHAR_ENCODING_NONE);
436
0
}
437
438
/**
439
 * Unregisters all aliases.
440
 *
441
 * @deprecated This function modifies global state and is not
442
 * thread-safe. See #xmlCtxtSetCharEncConvImpl for an alternative.
443
 *
444
 */
445
void
446
0
xmlCleanupEncodingAliases(void) {
447
0
    int i;
448
449
0
    if (xmlCharEncodingAliases == NULL)
450
0
  return;
451
452
0
    for (i = 0;i < xmlCharEncodingAliasesNb;i++) {
453
0
  if (xmlCharEncodingAliases[i].name != NULL)
454
0
      xmlFree((char *) xmlCharEncodingAliases[i].name);
455
0
  if (xmlCharEncodingAliases[i].alias != NULL)
456
0
      xmlFree((char *) xmlCharEncodingAliases[i].alias);
457
0
    }
458
0
    xmlCharEncodingAliasesNb = 0;
459
0
    xmlCharEncodingAliasesMax = 0;
460
0
    xmlFree(xmlCharEncodingAliases);
461
0
    xmlCharEncodingAliases = NULL;
462
0
}
463
464
/**
465
 * Lookup an encoding name for the given alias.
466
 *
467
 * @deprecated This function is not thread-safe.
468
 *
469
 * @param alias  the alias name as parsed, in UTF-8 format (ASCII actually)
470
 * @returns NULL if not found, otherwise the original name.
471
 */
472
const char *
473
1.32k
xmlGetEncodingAlias(const char *alias) {
474
1.32k
    int i;
475
1.32k
    char upper[100];
476
477
1.32k
    if (alias == NULL)
478
0
  return(NULL);
479
480
1.32k
    if (xmlCharEncodingAliases == NULL)
481
1.32k
  return(NULL);
482
483
0
    for (i = 0;i < 99;i++) {
484
0
        upper[i] = (char) toupper((unsigned char) alias[i]);
485
0
  if (upper[i] == 0) break;
486
0
    }
487
0
    upper[i] = 0;
488
489
    /*
490
     * Walk down the list looking for a definition of the alias
491
     */
492
0
    for (i = 0;i < xmlCharEncodingAliasesNb;i++) {
493
0
  if (!strcmp(xmlCharEncodingAliases[i].alias, upper)) {
494
0
      return(xmlCharEncodingAliases[i].name);
495
0
  }
496
0
    }
497
0
    return(NULL);
498
0
}
499
500
/**
501
 * Registers an alias `alias` for an encoding named `name`. Existing
502
 * aliases will be overwritten.
503
 *
504
 * @deprecated This function modifies global state and is not
505
 * thread-safe. See #xmlCtxtSetCharEncConvImpl for an alternative.
506
 *
507
 * @param name  the encoding name as parsed, in UTF-8 format (ASCII actually)
508
 * @param alias  the alias name as parsed, in UTF-8 format (ASCII actually)
509
 * @returns 0 in case of success, -1 in case of error.
510
 */
511
int
512
0
xmlAddEncodingAlias(const char *name, const char *alias) {
513
0
    int i;
514
0
    char upper[100];
515
0
    char *nameCopy, *aliasCopy;
516
517
0
    if ((name == NULL) || (alias == NULL))
518
0
  return(-1);
519
520
0
    for (i = 0;i < 99;i++) {
521
0
        upper[i] = (char) toupper((unsigned char) alias[i]);
522
0
  if (upper[i] == 0) break;
523
0
    }
524
0
    upper[i] = 0;
525
526
0
    if (xmlCharEncodingAliasesNb >= xmlCharEncodingAliasesMax) {
527
0
        xmlCharEncodingAliasPtr tmp;
528
0
        int newSize;
529
530
0
        newSize = xmlGrowCapacity(xmlCharEncodingAliasesMax, sizeof(tmp[0]),
531
0
                                  20, XML_MAX_ITEMS);
532
0
        if (newSize < 0)
533
0
            return(-1);
534
0
        tmp = xmlRealloc(xmlCharEncodingAliases, newSize * sizeof(tmp[0]));
535
0
        if (tmp == NULL)
536
0
            return(-1);
537
0
        xmlCharEncodingAliases = tmp;
538
0
        xmlCharEncodingAliasesMax = newSize;
539
0
    }
540
541
    /*
542
     * Walk down the list looking for a definition of the alias
543
     */
544
0
    for (i = 0;i < xmlCharEncodingAliasesNb;i++) {
545
0
  if (!strcmp(xmlCharEncodingAliases[i].alias, upper)) {
546
      /*
547
       * Replace the definition.
548
       */
549
0
      nameCopy = xmlMemStrdup(name);
550
0
            if (nameCopy == NULL)
551
0
                return(-1);
552
0
      xmlFree((char *) xmlCharEncodingAliases[i].name);
553
0
      xmlCharEncodingAliases[i].name = nameCopy;
554
0
      return(0);
555
0
  }
556
0
    }
557
    /*
558
     * Add the definition
559
     */
560
0
    nameCopy = xmlMemStrdup(name);
561
0
    if (nameCopy == NULL)
562
0
        return(-1);
563
0
    aliasCopy = xmlMemStrdup(upper);
564
0
    if (aliasCopy == NULL) {
565
0
        xmlFree(nameCopy);
566
0
        return(-1);
567
0
    }
568
0
    xmlCharEncodingAliases[xmlCharEncodingAliasesNb].name = nameCopy;
569
0
    xmlCharEncodingAliases[xmlCharEncodingAliasesNb].alias = aliasCopy;
570
0
    xmlCharEncodingAliasesNb++;
571
0
    return(0);
572
0
}
573
574
/**
575
 * Unregisters an encoding alias.
576
 *
577
 * @deprecated This function modifies global state and is not
578
 * thread-safe. See #xmlCtxtSetCharEncConvImpl for an alternative.
579
 *
580
 * @param alias  the alias name as parsed, in UTF-8 format (ASCII actually)
581
 * @returns 0 in case of success, -1 in case of error.
582
 */
583
int
584
0
xmlDelEncodingAlias(const char *alias) {
585
0
    int i;
586
587
0
    if (alias == NULL)
588
0
  return(-1);
589
590
0
    if (xmlCharEncodingAliases == NULL)
591
0
  return(-1);
592
    /*
593
     * Walk down the list looking for a definition of the alias
594
     */
595
0
    for (i = 0;i < xmlCharEncodingAliasesNb;i++) {
596
0
  if (!strcmp(xmlCharEncodingAliases[i].alias, alias)) {
597
0
      xmlFree((char *) xmlCharEncodingAliases[i].name);
598
0
      xmlFree((char *) xmlCharEncodingAliases[i].alias);
599
0
      xmlCharEncodingAliasesNb--;
600
0
      memmove(&xmlCharEncodingAliases[i], &xmlCharEncodingAliases[i + 1],
601
0
        sizeof(xmlCharEncodingAlias) * (xmlCharEncodingAliasesNb - i));
602
0
      return(0);
603
0
  }
604
0
    }
605
0
    return(-1);
606
0
}
607
608
static int
609
8.23k
xmlCompareEncTableEntries(const void *vkey, const void *ventry) {
610
8.23k
    const char *key = vkey;
611
8.23k
    const xmlEncTableEntry *entry = ventry;
612
613
8.23k
    return(xmlStrcasecmp(BAD_CAST key, BAD_CAST entry->name));
614
8.23k
}
615
616
static xmlCharEncoding
617
xmlParseCharEncodingInternal(const char *name)
618
1.32k
{
619
1.32k
    const xmlEncTableEntry *entry;
620
621
1.32k
    if (name == NULL)
622
0
       return(XML_CHAR_ENCODING_NONE);
623
624
1.32k
    entry = bsearch(name, xmlEncTable,
625
1.32k
                    sizeof(xmlEncTable) / sizeof(xmlEncTable[0]),
626
1.32k
                    sizeof(xmlEncTable[0]), xmlCompareEncTableEntries);
627
1.32k
    if (entry != NULL)
628
772
        return(entry->enc);
629
630
548
    return(XML_CHAR_ENCODING_ERROR);
631
1.32k
}
632
633
/**
634
 * Compare the string to the encoding schemes already known. Note
635
 * that the comparison is case insensitive accordingly to the section
636
 * [XML] 4.3.3 Character Encoding in Entities.
637
 *
638
 * @param name  the encoding name as parsed, in UTF-8 format (ASCII actually)
639
 * @returns one of the xmlCharEncoding values or XML_CHAR_ENCODING_NONE
640
 * if not recognized.
641
 */
642
xmlCharEncoding
643
xmlParseCharEncoding(const char *name)
644
0
{
645
0
    xmlCharEncoding enc = xmlParseCharEncodingInternal(name);
646
647
    /* Backward compatibility */
648
0
    if (enc == XML_CHAR_ENCODING_UTF16)
649
0
        enc = XML_CHAR_ENCODING_UTF16LE;
650
651
0
    return(enc);
652
0
}
653
654
/**
655
 * The "canonical" name for XML encoding.
656
 * C.f. http://www.w3.org/TR/REC-xml#charencoding
657
 * Section 4.3.3  Character Encoding in Entities
658
 *
659
 * @param enc  the encoding
660
 * @returns the canonical name for the given encoding.
661
 */
662
const char*
663
0
xmlGetCharEncodingName(xmlCharEncoding enc) {
664
0
    switch (enc) {
665
0
        case XML_CHAR_ENCODING_UTF16LE:
666
0
      return("UTF-16");
667
0
        case XML_CHAR_ENCODING_UTF16BE:
668
0
      return("UTF-16");
669
0
        case XML_CHAR_ENCODING_UCS4LE:
670
0
            return("UCS-4");
671
0
        case XML_CHAR_ENCODING_UCS4BE:
672
0
            return("UCS-4");
673
0
        default:
674
0
            break;
675
0
    }
676
677
0
    if ((enc <= 0) || ((size_t) enc >= NUM_DEFAULT_HANDLERS))
678
0
        return(NULL);
679
680
0
    return(defaultHandlers[enc].name);
681
0
}
682
683
/************************************************************************
684
 *                  *
685
 *      Char encoding handlers        *
686
 *                  *
687
 ************************************************************************/
688
689
/**
690
 * Create and registers an xmlCharEncodingHandler.
691
 *
692
 * @deprecated This function modifies global state and is not
693
 * thread-safe. See #xmlCtxtSetCharEncConvImpl for an alternative.
694
 *
695
 * @param name  the encoding name, in UTF-8 format (ASCII actually)
696
 * @param input  the xmlCharEncodingInputFunc to read that encoding
697
 * @param output  the xmlCharEncodingOutputFunc to write that encoding
698
 * @returns the xmlCharEncodingHandler created (or NULL in case of error).
699
 */
700
xmlCharEncodingHandler *
701
xmlNewCharEncodingHandler(const char *name,
702
                          xmlCharEncodingInputFunc input,
703
0
                          xmlCharEncodingOutputFunc output) {
704
0
    xmlCharEncodingHandlerPtr handler;
705
0
    const char *alias;
706
0
    char upper[500];
707
0
    int i;
708
0
    char *up = NULL;
709
710
    /*
711
     * Do the alias resolution
712
     */
713
0
    alias = xmlGetEncodingAlias(name);
714
0
    if (alias != NULL)
715
0
  name = alias;
716
717
    /*
718
     * Keep only the uppercase version of the encoding.
719
     */
720
0
    if (name == NULL)
721
0
  return(NULL);
722
0
    for (i = 0;i < 499;i++) {
723
0
        upper[i] = (char) toupper((unsigned char) name[i]);
724
0
  if (upper[i] == 0) break;
725
0
    }
726
0
    upper[i] = 0;
727
0
    up = xmlMemStrdup(upper);
728
0
    if (up == NULL)
729
0
  return(NULL);
730
731
    /*
732
     * allocate and fill-up an handler block.
733
     */
734
0
    handler = (xmlCharEncodingHandlerPtr)
735
0
              xmlMalloc(sizeof(xmlCharEncodingHandler));
736
0
    if (handler == NULL) {
737
0
        xmlFree(up);
738
0
  return(NULL);
739
0
    }
740
0
    memset(handler, 0, sizeof(xmlCharEncodingHandler));
741
0
    handler->input.legacyFunc = input;
742
0
    handler->output.legacyFunc = output;
743
0
    handler->name = up;
744
0
    handler->flags = XML_HANDLER_STATIC | XML_HANDLER_LEGACY;
745
746
    /*
747
     * registers and returns the handler.
748
     */
749
0
    xmlRegisterCharEncodingHandler(handler);
750
0
    return(handler);
751
0
}
752
753
/**
754
 * Create a custom xmlCharEncodingHandler.
755
 *
756
 * @param name  the encoding name
757
 * @param input  input callback which converts to UTF-8
758
 * @param output  output callback which converts from UTF-8
759
 * @param ctxtDtor  context destructor
760
 * @param inputCtxt  context for input callback
761
 * @param outputCtxt  context for output callback
762
 * @param out  pointer to resulting handler
763
 * @returns an xmlParserErrors code.
764
 */
765
xmlParserErrors
766
xmlCharEncNewCustomHandler(const char *name,
767
                           xmlCharEncConvFunc input, xmlCharEncConvFunc output,
768
                           xmlCharEncConvCtxtDtor ctxtDtor,
769
                           void *inputCtxt, void *outputCtxt,
770
1.06k
                           xmlCharEncodingHandler **out) {
771
1.06k
    xmlCharEncodingHandler *handler;
772
773
1.06k
    if (out == NULL)
774
0
        return(XML_ERR_ARGUMENT);
775
776
1.06k
    handler = xmlMalloc(sizeof(*handler));
777
1.06k
    if (handler == NULL)
778
0
        goto error;
779
1.06k
    memset(handler, 0, sizeof(*handler));
780
781
1.06k
    if (name != NULL) {
782
1.06k
        handler->name = xmlMemStrdup(name);
783
1.06k
        if (handler->name == NULL)
784
0
            goto error;
785
1.06k
    }
786
787
1.06k
    handler->input.func = input;
788
1.06k
    handler->output.func = output;
789
1.06k
    handler->ctxtDtor = ctxtDtor;
790
1.06k
    handler->inputCtxt = inputCtxt;
791
1.06k
    handler->outputCtxt = outputCtxt;
792
793
1.06k
    *out = handler;
794
1.06k
    return(XML_ERR_OK);
795
796
0
error:
797
0
    xmlFree(handler);
798
799
0
    if (ctxtDtor != NULL) {
800
0
        if (inputCtxt != NULL)
801
0
            ctxtDtor(inputCtxt);
802
0
        if (outputCtxt != NULL)
803
0
            ctxtDtor(outputCtxt);
804
0
    }
805
806
0
    return(XML_ERR_NO_MEMORY);
807
1.06k
}
808
809
/**
810
 * @deprecated Alias for #xmlInitParser.
811
 */
812
void
813
0
xmlInitCharEncodingHandlers(void) {
814
0
    xmlInitParser();
815
0
}
816
817
/**
818
 * Initialize the char encoding support.
819
 */
820
void
821
1
xmlInitEncodingInternal(void) {
822
1
    unsigned short int tst = 0x1234;
823
1
    unsigned char *ptr = (unsigned char *) &tst;
824
825
1
    if (*ptr == 0x12) xmlLittleEndian = 0;
826
1
    else xmlLittleEndian = 1;
827
1
}
828
829
/**
830
 * Cleanup the memory allocated for the char encoding support, it
831
 * unregisters all the encoding handlers and the aliases.
832
 *
833
 * @deprecated This function will be made private. Call #xmlCleanupParser
834
 * to free global state but see the warnings there. #xmlCleanupParser
835
 * should be only called once at program exit. In most cases, you don't
836
 * have call cleanup functions at all.
837
 *
838
 */
839
void
840
0
xmlCleanupCharEncodingHandlers(void) {
841
0
    xmlCleanupEncodingAliases();
842
843
0
    if (globalHandlers == NULL) return;
844
845
0
    for (;nbCharEncodingHandler > 0;) {
846
0
        xmlCharEncodingHandler *handler;
847
848
0
        nbCharEncodingHandler--;
849
0
        handler = globalHandlers[nbCharEncodingHandler];
850
0
  if (handler != NULL) {
851
0
      if (handler->name != NULL)
852
0
    xmlFree(handler->name);
853
0
      xmlFree(handler);
854
0
  }
855
0
    }
856
0
    xmlFree(globalHandlers);
857
0
    globalHandlers = NULL;
858
0
    nbCharEncodingHandler = 0;
859
0
}
860
861
/**
862
 * Register the char encoding handler.
863
 *
864
 * @deprecated This function modifies global state and is not
865
 * thread-safe. See #xmlCtxtSetCharEncConvImpl for an alternative.
866
 *
867
 * @param handler  the xmlCharEncodingHandler handler block
868
 */
869
void
870
0
xmlRegisterCharEncodingHandler(xmlCharEncodingHandler *handler) {
871
0
    if (handler == NULL)
872
0
        return;
873
0
    if (globalHandlers == NULL) {
874
0
        globalHandlers = xmlMalloc(
875
0
                MAX_ENCODING_HANDLERS * sizeof(globalHandlers[0]));
876
0
        if (globalHandlers == NULL)
877
0
            goto free_handler;
878
0
    }
879
880
0
    if (nbCharEncodingHandler >= MAX_ENCODING_HANDLERS)
881
0
        goto free_handler;
882
0
    globalHandlers[nbCharEncodingHandler++] = handler;
883
0
    return;
884
885
0
free_handler:
886
0
    if (handler != NULL) {
887
0
        if (handler->name != NULL) {
888
0
            xmlFree(handler->name);
889
0
        }
890
0
        xmlFree(handler);
891
0
    }
892
0
}
893
894
/**
895
 * Search the non-default handlers for an exact match.
896
 *
897
 * @param norig  name of the char encoding
898
 * @param name  potentially aliased name of the encoding
899
 * @param flags  bit mask of flags
900
 * @param impl  a conversion implementation (optional)
901
 * @param implCtxt  user data for conversion implementation (optional)
902
 * @param out  pointer to resulting handler
903
 * @returns an xmlParserErrors error code.
904
 */
905
static xmlParserErrors
906
xmlFindExtraHandler(const char *norig, const char *name, xmlCharEncFlags flags,
907
                    xmlCharEncConvImpl impl, void *implCtxt,
908
1.12k
                    xmlCharEncodingHandler **out) {
909
    /*
910
     * Try custom implementation before deprecated global handlers.
911
     *
912
     * Note that we pass the original name without deprecated
913
     * alias resolution.
914
     */
915
1.12k
    if (impl != NULL)
916
0
        return(impl(implCtxt, norig, flags, out));
917
918
    /*
919
     * Deprecated
920
     */
921
1.12k
    if (globalHandlers != NULL) {
922
0
        int i;
923
924
0
        for (i = 0; i < nbCharEncodingHandler; i++) {
925
0
            xmlCharEncodingHandler *h = globalHandlers[i];
926
927
0
            if (!xmlStrcasecmp((const xmlChar *) name,
928
0
                               (const xmlChar *) h->name)) {
929
0
                if ((((flags & XML_ENC_INPUT) == 0) || (h->input.func)) &&
930
0
                    (((flags & XML_ENC_OUTPUT) == 0) || (h->output.func))) {
931
0
                    *out = h;
932
0
                    return(XML_ERR_OK);
933
0
                }
934
0
            }
935
0
        }
936
0
    }
937
938
1.12k
#ifdef LIBXML_ICONV_ENABLED
939
1.12k
    {
940
1.12k
        int ret = xmlCharEncIconv(name, flags, out);
941
942
1.12k
        if (ret == XML_ERR_OK)
943
1.06k
            return(XML_ERR_OK);
944
60
        if (ret != XML_ERR_UNSUPPORTED_ENCODING)
945
0
            return(ret);
946
60
    }
947
60
#endif /* LIBXML_ICONV_ENABLED */
948
949
#ifdef LIBXML_ICU_ENABLED
950
    {
951
        int ret = xmlCharEncUconv(name, flags, out);
952
953
        if (ret == XML_ERR_OK)
954
            return(XML_ERR_OK);
955
        if (ret != XML_ERR_UNSUPPORTED_ENCODING)
956
            return(ret);
957
    }
958
#endif /* LIBXML_ICU_ENABLED */
959
960
60
    return(XML_ERR_UNSUPPORTED_ENCODING);
961
60
}
962
963
/**
964
 * Find or create a handler matching the encoding. The following
965
 * converters are looked up in order:
966
 *
967
 * - Built-in handler (UTF-8, UTF-16, ISO-8859-1, ASCII)
968
 * - User-registered global handler (deprecated)
969
 * - iconv if enabled
970
 * - ICU if enabled
971
 *
972
 * The handler must be closed with #xmlCharEncCloseFunc.
973
 *
974
 * If the encoding is UTF-8, a NULL handler and no error code will
975
 * be returned.
976
 *
977
 * @since 2.13.0
978
 *
979
 * @param enc  an xmlCharEncoding value.
980
 * @param out  pointer to result
981
 * @returns XML_ERR_OK, XML_ERR_UNSUPPORTED_ENCODING or another
982
 * xmlParserErrors error code.
983
 */
984
xmlParserErrors
985
xmlLookupCharEncodingHandler(xmlCharEncoding enc,
986
320
                             xmlCharEncodingHandler **out) {
987
320
    const xmlCharEncodingHandler *handler;
988
989
320
    if (out == NULL)
990
0
        return(XML_ERR_ARGUMENT);
991
320
    *out = NULL;
992
993
320
    if ((enc <= 0) || ((size_t) enc >= NUM_DEFAULT_HANDLERS))
994
0
        return(XML_ERR_UNSUPPORTED_ENCODING);
995
996
    /* Return NULL handler for UTF-8 */
997
320
    if ((enc == XML_CHAR_ENCODING_UTF8) ||
998
296
        (enc == XML_CHAR_ENCODING_NONE))
999
24
        return(XML_ERR_OK);
1000
1001
296
    handler = &defaultHandlers[enc];
1002
296
    if ((handler->input.func != NULL) || (handler->output.func != NULL)) {
1003
277
        *out = (xmlCharEncodingHandler *) handler;
1004
277
        return(XML_ERR_OK);
1005
277
    }
1006
1007
19
    if (handler->name != NULL) {
1008
19
        xmlCharEncFlags flags = XML_ENC_INPUT;
1009
1010
19
#ifdef LIBXML_OUTPUT_ENABLED
1011
19
        flags |= XML_ENC_OUTPUT;
1012
19
#endif
1013
19
        return(xmlFindExtraHandler(handler->name, handler->name, flags,
1014
19
                                   NULL, NULL, out));
1015
19
    }
1016
1017
0
    return(XML_ERR_UNSUPPORTED_ENCODING);
1018
19
}
1019
1020
/**
1021
 * @deprecated Use #xmlLookupCharEncodingHandler which has better error
1022
 * reporting.
1023
 *
1024
 * @param enc  an xmlCharEncoding value.
1025
 * @returns the handler or NULL if no handler was found or an error
1026
 * occurred.
1027
 */
1028
xmlCharEncodingHandler *
1029
0
xmlGetCharEncodingHandler(xmlCharEncoding enc) {
1030
0
    xmlCharEncodingHandler *ret;
1031
1032
0
    xmlLookupCharEncodingHandler(enc, &ret);
1033
0
    return(ret);
1034
0
}
1035
1036
/**
1037
 * Find or create a handler matching the encoding. The following
1038
 * converters are looked up in order:
1039
 *
1040
 * - Built-in handler (UTF-8, UTF-16, ISO-8859-1, ASCII)
1041
 * - Custom implementation if provided
1042
 * - User-registered global handler (deprecated)
1043
 * - iconv if enabled
1044
 * - ICU if enabled
1045
 *
1046
 * The handler must be closed with #xmlCharEncCloseFunc.
1047
 *
1048
 * If the encoding is UTF-8, a NULL handler and no error code will
1049
 * be returned.
1050
 *
1051
 * `flags` can contain XML_ENC_INPUT, XML_ENC_OUTPUT or both.
1052
 *
1053
 * @since 2.14.0
1054
 *
1055
 * @param name  a string describing the char encoding.
1056
 * @param flags  bit mask of flags
1057
 * @param impl  a conversion implementation (optional)
1058
 * @param implCtxt  user data for conversion implementation (optional)
1059
 * @param out  pointer to result
1060
 * @returns XML_ERR_OK, XML_ERR_UNSUPPORTED_ENCODING or another
1061
 * xmlParserErrors error code.
1062
 */
1063
xmlParserErrors
1064
xmlCreateCharEncodingHandler(const char *name, xmlCharEncFlags flags,
1065
                             xmlCharEncConvImpl impl, void *implCtxt,
1066
1.32k
                             xmlCharEncodingHandler **out) {
1067
1.32k
    const xmlCharEncodingHandler *handler;
1068
1.32k
    const char *norig, *nalias;
1069
1.32k
    xmlCharEncoding enc;
1070
1071
1.32k
    if (out == NULL)
1072
0
        return(XML_ERR_ARGUMENT);
1073
1.32k
    *out = NULL;
1074
1075
1.32k
    if ((name == NULL) || (flags == 0))
1076
0
        return(XML_ERR_ARGUMENT);
1077
1078
1.32k
    norig = name;
1079
1.32k
    nalias = xmlGetEncodingAlias(name);
1080
1.32k
    if (nalias != NULL)
1081
0
  name = nalias;
1082
1083
1.32k
    enc = xmlParseCharEncodingInternal(name);
1084
1085
    /* Return NULL handler for UTF-8 */
1086
1.32k
    if (enc == XML_CHAR_ENCODING_UTF8)
1087
1
        return(XML_ERR_OK);
1088
1089
1.31k
    if ((enc > 0) && ((size_t) enc < NUM_DEFAULT_HANDLERS)) {
1090
771
        if (flags & XML_ENC_HTML) {
1091
            /*
1092
             * TODO: HTML5 only allows a fixed set of charset
1093
             * labels. We should add an option to enable or
1094
             * disable this restriction.
1095
             *
1096
             * TODO: Map ISO-8859-9 to windows-1254.
1097
             */
1098
0
            switch (enc) {
1099
0
                case XML_CHAR_ENCODING_ASCII:
1100
0
                case XML_CHAR_ENCODING_8859_1:
1101
0
                    enc = XML_CHAR_ENCODING_WINDOWS_1252;
1102
0
                    break;
1103
0
                case XML_CHAR_ENCODING_UCS2:
1104
0
                case XML_CHAR_ENCODING_UTF16:
1105
0
                    enc = XML_CHAR_ENCODING_UTF16LE;
1106
0
                    break;
1107
0
                default:
1108
0
                    break;
1109
0
            }
1110
0
        }
1111
1112
771
        handler = &defaultHandlers[enc];
1113
771
        if ((((flags & XML_ENC_INPUT) == 0) || (handler->input.func)) &&
1114
215
            (((flags & XML_ENC_OUTPUT) == 0) || (handler->output.func))) {
1115
215
            xmlCharEncodingHandler *ret;
1116
1117
            /*
1118
             * Return a copy of the handler with the original name.
1119
             */
1120
1121
215
            ret = xmlMalloc(sizeof(*ret));
1122
215
            if (ret == NULL)
1123
0
                return(XML_ERR_NO_MEMORY);
1124
215
            memset(ret, 0, sizeof(*ret));
1125
1126
215
            ret->name = xmlMemStrdup(norig);
1127
215
            if (ret->name == NULL) {
1128
0
                xmlFree(ret);
1129
0
                return(XML_ERR_NO_MEMORY);
1130
0
            }
1131
215
            ret->input = handler->input;
1132
215
            ret->output = handler->output;
1133
215
            ret->inputCtxt = handler->inputCtxt;
1134
215
            ret->outputCtxt = handler->outputCtxt;
1135
215
            ret->ctxtDtor = handler->ctxtDtor;
1136
1137
215
            *out = ret;
1138
215
            return(XML_ERR_OK);
1139
215
        }
1140
771
    }
1141
1142
1.10k
    return(xmlFindExtraHandler(norig, name, flags, impl, implCtxt, out));
1143
1.31k
}
1144
1145
/**
1146
 * Find or create a handler matching the encoding. The following
1147
 * converters are looked up in order:
1148
 *
1149
 * - Built-in handler (UTF-8, UTF-16, ISO-8859-1, ASCII)
1150
 * - User-registered global handler (deprecated)
1151
 * - iconv if enabled
1152
 * - ICU if enabled
1153
 *
1154
 * The handler must be closed with #xmlCharEncCloseFunc.
1155
 *
1156
 * If the encoding is UTF-8, a NULL handler and no error code will
1157
 * be returned.
1158
 *
1159
 * @since 2.13.0
1160
 *
1161
 * @param name  a string describing the char encoding.
1162
 * @param output  boolean, use handler for output
1163
 * @param out  pointer to result
1164
 * @returns XML_ERR_OK, XML_ERR_UNSUPPORTED_ENCODING or another
1165
 * xmlParserErrors error code.
1166
 */
1167
xmlParserErrors
1168
xmlOpenCharEncodingHandler(const char *name, int output,
1169
0
                           xmlCharEncodingHandler **out) {
1170
0
    xmlCharEncFlags flags = output ? XML_ENC_OUTPUT : XML_ENC_INPUT;
1171
1172
0
    return(xmlCreateCharEncodingHandler(name, flags, NULL, NULL, out));
1173
0
}
1174
1175
/**
1176
 * If the encoding is UTF-8, this will return a no-op handler that
1177
 * shouldn't be used.
1178
 *
1179
 * @deprecated Use #xmlOpenCharEncodingHandler which has better error
1180
 * reporting.
1181
 *
1182
 * @param name  a string describing the char encoding.
1183
 * @returns the handler or NULL if no handler was found or an error
1184
 * occurred.
1185
 */
1186
xmlCharEncodingHandler *
1187
0
xmlFindCharEncodingHandler(const char *name) {
1188
0
    xmlCharEncodingHandler *ret;
1189
0
    xmlCharEncFlags flags;
1190
1191
    /*
1192
     * This handler shouldn't be used, but we must return a non-NULL
1193
     * handler.
1194
     */
1195
0
    if ((xmlStrcasecmp(BAD_CAST name, BAD_CAST "UTF-8") == 0) ||
1196
0
        (xmlStrcasecmp(BAD_CAST name, BAD_CAST "UTF8") == 0))
1197
0
        return((xmlCharEncodingHandlerPtr)
1198
0
                &defaultHandlers[XML_CHAR_ENCODING_UTF8]);
1199
1200
0
    flags = XML_ENC_INPUT;
1201
0
#ifdef LIBXML_OUTPUT_ENABLED
1202
0
    flags |= XML_ENC_OUTPUT;
1203
0
#endif
1204
0
    xmlCreateCharEncodingHandler(name, flags, NULL, NULL, &ret);
1205
0
    return(ret);
1206
0
}
1207
1208
/************************************************************************
1209
 *                  *
1210
 *    ICONV based generic conversion functions    *
1211
 *                  *
1212
 ************************************************************************/
1213
1214
#ifdef LIBXML_ICONV_ENABLED
1215
typedef struct {
1216
    iconv_t cd;
1217
} xmlIconvCtxt;
1218
1219
/**
1220
 * The value of `inlen` after return is the number of bytes consumed.
1221
 * The value of `outlen` after return is the number of bytes produced.
1222
 *
1223
 * @param vctxt  conversion context
1224
 * @param out  a pointer to an array of bytes to store the result
1225
 * @param outlen  the length of `out`
1226
 * @param in  a pointer to an array of input bytes
1227
 * @param inlen  the length of `in`
1228
 * @param flush  end of input
1229
 * @returns an xmlCharEncError code.
1230
 */
1231
static xmlCharEncError
1232
xmlIconvConvert(void *vctxt, unsigned char *out, int *outlen,
1233
                const unsigned char *in, int *inlen,
1234
32.3k
                int flush ATTRIBUTE_UNUSED) {
1235
32.3k
    xmlIconvCtxt *ctxt = vctxt;
1236
32.3k
    size_t icv_inlen, icv_outlen;
1237
32.3k
    const char *icv_in = (const char *) in;
1238
32.3k
    char *icv_out = (char *) out;
1239
32.3k
    size_t ret;
1240
1241
32.3k
    if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL)) {
1242
0
        if (outlen != NULL) *outlen = 0;
1243
0
        return(XML_ENC_ERR_INTERNAL);
1244
0
    }
1245
32.3k
    icv_inlen = *inlen;
1246
32.3k
    icv_outlen = *outlen;
1247
    /*
1248
     * Some versions take const, other versions take non-const input.
1249
     */
1250
32.3k
    ret = iconv(ctxt->cd, (void *) &icv_in, &icv_inlen, &icv_out, &icv_outlen);
1251
32.3k
    *inlen -= icv_inlen;
1252
32.3k
    *outlen -= icv_outlen;
1253
32.3k
    if (ret == (size_t) -1) {
1254
31.4k
        if (errno == EILSEQ)
1255
57
            return(XML_ENC_ERR_INPUT);
1256
31.3k
        if (errno == E2BIG)
1257
29.9k
            return(XML_ENC_ERR_SPACE);
1258
        /*
1259
         * EINVAL means a truncated multi-byte sequence at the end
1260
         * of the input buffer. We treat this as success.
1261
         */
1262
1.44k
        if (errno == EINVAL)
1263
1.44k
            return(XML_ENC_ERR_SUCCESS);
1264
#ifdef __APPLE__
1265
        /*
1266
         * Apple's new libiconv can return EOPNOTSUPP under
1267
         * unknown circumstances (detected when fuzzing).
1268
         */
1269
        if (errno == EOPNOTSUPP)
1270
            return(XML_ENC_ERR_INPUT);
1271
#endif
1272
0
        return(XML_ENC_ERR_INTERNAL);
1273
1.44k
    }
1274
959
    return(XML_ENC_ERR_SUCCESS);
1275
32.3k
}
1276
1277
static void
1278
2.14k
xmlIconvFree(void *vctxt) {
1279
2.14k
    xmlIconvCtxt *ctxt = vctxt;
1280
1281
2.14k
    if (ctxt == NULL)
1282
1.04k
        return;
1283
1284
1.10k
    if (ctxt->cd != (iconv_t) -1)
1285
1.08k
        iconv_close(ctxt->cd);
1286
1287
1.10k
    xmlFree(ctxt);
1288
1.10k
}
1289
1290
#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && \
1291
    defined(__GLIBC__)
1292
#include <libxml/parserInternals.h>
1293
1294
static int
1295
2.20k
xmlEncodingMatch(const char *name1, const char *name2) {
1296
    /*
1297
     * Fuzzy match for encoding names
1298
     */
1299
2.41k
    while (1) {
1300
2.85k
        while ((*name1 != 0) && (!IS_ASCII_LETTER(*name1)))
1301
439
            name1 += 1;
1302
2.41k
        while ((*name2 != 0) && (!IS_ASCII_LETTER(*name2)))
1303
4
            name2 += 1;
1304
2.41k
        if ((*name1 == 0) || (*name2 == 0))
1305
61
            break;
1306
2.35k
        if ((*name1 | 0x20) != (*name2 | 0x20))
1307
2.14k
            return(0);
1308
206
        name1 += 1;
1309
206
        name2 += 1;
1310
206
    }
1311
1312
    /* Only check whether name1 starts with name2 */
1313
61
    return(*name2 == 0);
1314
2.20k
}
1315
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
1316
1317
static xmlParserErrors
1318
xmlCharEncIconv(const char *name, xmlCharEncFlags flags,
1319
1.12k
                xmlCharEncodingHandler **out) {
1320
1.12k
    xmlCharEncConvFunc inFunc = NULL, outFunc = NULL;
1321
1.12k
    xmlIconvCtxt *inputCtxt = NULL, *outputCtxt = NULL;
1322
1.12k
    iconv_t icv_in;
1323
1.12k
    iconv_t icv_out;
1324
1.12k
    xmlParserErrors ret;
1325
1326
    /*
1327
     * POSIX allows "indicator suffixes" like "//IGNORE" to be
1328
     * passed to iconv_open. This can change the behavior in
1329
     * unexpected ways.
1330
     *
1331
     * Many iconv implementations also support non-standard
1332
     * codesets like "wchar_t", "char" or the empty string "".
1333
     * It would make sense to disallow them, but codeset names
1334
     * are matched fuzzily, so a string like "w-C.hA_rt" could
1335
     * be interpreted as "wchar_t".
1336
     *
1337
     * When escaping characters that aren't supported in the
1338
     * target encoding, we also rely on GNU libiconv behavior to
1339
     * stop conversion without trying any kind of fallback.
1340
     * This violates the POSIX spec which says:
1341
     *
1342
     * > If iconv() encounters a character in the input buffer
1343
     * > that is valid, but for which an identical character does
1344
     * > not exist in the output codeset [...] iconv() shall
1345
     * > perform an implementation-defined conversion on the
1346
     * > character.
1347
     *
1348
     * See: https://sourceware.org/bugzilla/show_bug.cgi?id=29913
1349
     *
1350
     * Unfortunately, strict POSIX compliance makes it impossible
1351
     * to detect untranslatable characters.
1352
     */
1353
1.12k
    if (strstr(name, "//") != NULL) {
1354
0
        ret = XML_ERR_UNSUPPORTED_ENCODING;
1355
0
        goto error;
1356
0
    }
1357
1358
1.12k
#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && \
1359
1.12k
    defined(__GLIBC__)
1360
    /*
1361
     * This glibc bug can lead to unpredictable results with the
1362
     * push parser.
1363
     *
1364
     * https://sourceware.org/bugzilla/show_bug.cgi?id=32633
1365
     */
1366
1.12k
    if ((xmlEncodingMatch(name, "TSCII")) ||
1367
1.08k
        (xmlEncodingMatch(name, "BIG5-HKSCS"))) {
1368
37
        ret = XML_ERR_UNSUPPORTED_ENCODING;
1369
37
        goto error;
1370
37
    }
1371
1.08k
#endif
1372
1373
1.08k
    if (flags & XML_ENC_INPUT) {
1374
1.08k
        inputCtxt = xmlMalloc(sizeof(xmlIconvCtxt));
1375
1.08k
        if (inputCtxt == NULL) {
1376
0
            ret = XML_ERR_NO_MEMORY;
1377
0
            goto error;
1378
0
        }
1379
1.08k
        inputCtxt->cd = (iconv_t) -1;
1380
1381
1.08k
        icv_in = iconv_open("UTF-8", name);
1382
1.08k
        if (icv_in == (iconv_t) -1) {
1383
23
            if (errno == EINVAL)
1384
23
                ret = XML_ERR_UNSUPPORTED_ENCODING;
1385
0
            else if (errno == ENOMEM)
1386
0
                ret = XML_ERR_NO_MEMORY;
1387
0
            else
1388
0
                ret = XML_ERR_SYSTEM;
1389
23
            goto error;
1390
23
        }
1391
1.06k
        inputCtxt->cd = icv_in;
1392
1393
1.06k
        inFunc = xmlIconvConvert;
1394
1.06k
    }
1395
1396
1.06k
    if (flags & XML_ENC_OUTPUT) {
1397
19
        outputCtxt = xmlMalloc(sizeof(xmlIconvCtxt));
1398
19
        if (outputCtxt == NULL) {
1399
0
            ret = XML_ERR_NO_MEMORY;
1400
0
            goto error;
1401
0
        }
1402
19
        outputCtxt->cd = (iconv_t) -1;
1403
1404
19
        icv_out = iconv_open(name, "UTF-8");
1405
19
        if (icv_out == (iconv_t) -1) {
1406
0
            if (errno == EINVAL)
1407
0
                ret = XML_ERR_UNSUPPORTED_ENCODING;
1408
0
            else if (errno == ENOMEM)
1409
0
                ret = XML_ERR_NO_MEMORY;
1410
0
            else
1411
0
                ret = XML_ERR_SYSTEM;
1412
0
            goto error;
1413
0
        }
1414
19
        outputCtxt->cd = icv_out;
1415
1416
19
        outFunc = xmlIconvConvert;
1417
19
    }
1418
1419
1.06k
    return(xmlCharEncNewCustomHandler(name, inFunc, outFunc, xmlIconvFree,
1420
1.06k
                                      inputCtxt, outputCtxt, out));
1421
1422
60
error:
1423
60
    if (inputCtxt != NULL)
1424
23
        xmlIconvFree(inputCtxt);
1425
60
    if (outputCtxt != NULL)
1426
0
        xmlIconvFree(outputCtxt);
1427
60
    return(ret);
1428
1.06k
}
1429
#endif /* LIBXML_ICONV_ENABLED */
1430
1431
/************************************************************************
1432
 *                  *
1433
 *    ICU based generic conversion functions    *
1434
 *                  *
1435
 ************************************************************************/
1436
1437
#ifdef LIBXML_ICU_ENABLED
1438
/* Size of pivot buffer, same as icu/source/common/ucnv.cpp CHUNK_SIZE */
1439
#define ICU_PIVOT_BUF_SIZE 1024
1440
1441
typedef struct _uconv_t xmlUconvCtxt;
1442
struct _uconv_t {
1443
  UConverter *uconv; /* for conversion between an encoding and UTF-16 */
1444
  UConverter *utf8; /* for conversion between UTF-8 and UTF-16 */
1445
  UChar      *pivot_source;
1446
  UChar      *pivot_target;
1447
  int        isInput;
1448
  UChar      pivot_buf[ICU_PIVOT_BUF_SIZE];
1449
};
1450
1451
/**
1452
 * The value of `inlen` after return is the number of bytes consumed.
1453
 * The value of `outlen` after return is the number of bytes produced.
1454
 *
1455
 * @param vctxt  conversion context
1456
 * @param out  a pointer to an array of bytes to store the result
1457
 * @param outlen  the length of `out`
1458
 * @param in  a pointer to an array of input bytes
1459
 * @param inlen  the length of `in`
1460
 * @param flush  end of input
1461
 * @returns an xmlCharEncError code.
1462
 */
1463
static xmlCharEncError
1464
xmlUconvConvert(void *vctxt, unsigned char *out, int *outlen,
1465
                const unsigned char *in, int *inlen, int flush) {
1466
    xmlUconvCtxt *cd = vctxt;
1467
    const char *ucv_in = (const char *) in;
1468
    char *ucv_out = (char *) out;
1469
    UConverter *target, *source;
1470
    UErrorCode err = U_ZERO_ERROR;
1471
    int ret;
1472
1473
    if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL)) {
1474
        if (outlen != NULL)
1475
            *outlen = 0;
1476
        return(XML_ENC_ERR_INTERNAL);
1477
    }
1478
1479
    /*
1480
     * The ICU API can consume input, including partial sequences,
1481
     * even if the output buffer would overflow. The remaining input
1482
     * must be processed by calling ucnv_convertEx with a possibly
1483
     * empty input buffer.
1484
     */
1485
    if (cd->isInput) {
1486
        source = cd->uconv;
1487
        target = cd->utf8;
1488
    } else {
1489
        source = cd->utf8;
1490
        target = cd->uconv;
1491
    }
1492
1493
    ucnv_convertEx(target, source, &ucv_out, ucv_out + *outlen,
1494
                   &ucv_in, ucv_in + *inlen, cd->pivot_buf,
1495
                   &cd->pivot_source, &cd->pivot_target,
1496
                   cd->pivot_buf + ICU_PIVOT_BUF_SIZE,
1497
                   /* reset */ 0, flush, &err);
1498
1499
    *inlen = ucv_in - (const char*) in;
1500
    *outlen = ucv_out - (char *) out;
1501
1502
    if (U_SUCCESS(err)) {
1503
        ret = XML_ENC_ERR_SUCCESS;
1504
    } else {
1505
        switch (err) {
1506
            case U_TRUNCATED_CHAR_FOUND:
1507
                /* Should only happen with flush */
1508
                ret = XML_ENC_ERR_INPUT;
1509
                break;
1510
1511
            case U_BUFFER_OVERFLOW_ERROR:
1512
                ret = XML_ENC_ERR_SPACE;
1513
                break;
1514
1515
            case U_INVALID_CHAR_FOUND:
1516
            case U_ILLEGAL_CHAR_FOUND:
1517
            case U_ILLEGAL_ESCAPE_SEQUENCE:
1518
            case U_UNSUPPORTED_ESCAPE_SEQUENCE:
1519
                ret = XML_ENC_ERR_INPUT;
1520
                break;
1521
1522
            case U_MEMORY_ALLOCATION_ERROR:
1523
                ret = XML_ENC_ERR_MEMORY;
1524
                break;
1525
1526
            default:
1527
                ret = XML_ENC_ERR_INTERNAL;
1528
                break;
1529
        }
1530
    }
1531
1532
    return(ret);
1533
}
1534
1535
static xmlParserErrors
1536
openIcuConverter(const char* name, int isInput, xmlUconvCtxt **out)
1537
{
1538
    UErrorCode status;
1539
    xmlUconvCtxt *conv;
1540
1541
    *out = NULL;
1542
1543
    conv = (xmlUconvCtxt *) xmlMalloc(sizeof(xmlUconvCtxt));
1544
    if (conv == NULL)
1545
        return(XML_ERR_NO_MEMORY);
1546
1547
    conv->isInput = isInput;
1548
    conv->pivot_source = conv->pivot_buf;
1549
    conv->pivot_target = conv->pivot_buf;
1550
1551
    status = U_ZERO_ERROR;
1552
    conv->uconv = ucnv_open(name, &status);
1553
    if (U_FAILURE(status))
1554
        goto error;
1555
1556
    status = U_ZERO_ERROR;
1557
    if (isInput) {
1558
        ucnv_setToUCallBack(conv->uconv, UCNV_TO_U_CALLBACK_STOP,
1559
                                                NULL, NULL, NULL, &status);
1560
    }
1561
    else {
1562
        ucnv_setFromUCallBack(conv->uconv, UCNV_FROM_U_CALLBACK_STOP,
1563
                                                NULL, NULL, NULL, &status);
1564
    }
1565
    if (U_FAILURE(status))
1566
        goto error;
1567
1568
    status = U_ZERO_ERROR;
1569
    conv->utf8 = ucnv_open("UTF-8", &status);
1570
    if (U_FAILURE(status))
1571
        goto error;
1572
1573
    *out = conv;
1574
    return(XML_ERR_OK);
1575
1576
error:
1577
    if (conv->uconv)
1578
        ucnv_close(conv->uconv);
1579
    xmlFree(conv);
1580
1581
    if (status == U_FILE_ACCESS_ERROR)
1582
        return(XML_ERR_UNSUPPORTED_ENCODING);
1583
    if (status == U_MEMORY_ALLOCATION_ERROR)
1584
        return(XML_ERR_NO_MEMORY);
1585
    return(XML_ERR_SYSTEM);
1586
}
1587
1588
static void
1589
closeIcuConverter(xmlUconvCtxt *conv)
1590
{
1591
    if (conv == NULL)
1592
        return;
1593
    ucnv_close(conv->uconv);
1594
    ucnv_close(conv->utf8);
1595
    xmlFree(conv);
1596
}
1597
1598
static void
1599
xmlUconvFree(void *vctxt) {
1600
    closeIcuConverter(vctxt);
1601
}
1602
1603
static xmlParserErrors
1604
xmlCharEncUconv(const char *name, xmlCharEncFlags flags,
1605
                xmlCharEncodingHandler **out) {
1606
    xmlCharEncConvFunc inFunc = NULL, outFunc = NULL;
1607
    xmlUconvCtxt *ucv_in = NULL;
1608
    xmlUconvCtxt *ucv_out = NULL;
1609
    int ret;
1610
1611
    if (flags & XML_ENC_INPUT) {
1612
        ret = openIcuConverter(name, 1, &ucv_in);
1613
        if (ret != 0)
1614
            goto error;
1615
        inFunc = xmlUconvConvert;
1616
    }
1617
1618
    if (flags & XML_ENC_OUTPUT) {
1619
        ret = openIcuConverter(name, 0, &ucv_out);
1620
        if (ret != 0)
1621
            goto error;
1622
        outFunc = xmlUconvConvert;
1623
    }
1624
1625
    return(xmlCharEncNewCustomHandler(name, inFunc, outFunc, xmlUconvFree,
1626
                                      ucv_in, ucv_out, out));
1627
1628
error:
1629
    if (ucv_in != NULL)
1630
        closeIcuConverter(ucv_in);
1631
    if (ucv_out != NULL)
1632
        closeIcuConverter(ucv_out);
1633
    return(ret);
1634
}
1635
#endif /* LIBXML_ICU_ENABLED */
1636
1637
/************************************************************************
1638
 *                  *
1639
 *    The real API used by libxml for on-the-fly conversion *
1640
 *                  *
1641
 ************************************************************************/
1642
1643
/**
1644
 * Convert xmlCharEncError to xmlParserErrors code.
1645
 *
1646
 * @param code  xmlCharEncError code
1647
 */
1648
static xmlParserErrors
1649
93
xmlEncConvertError(xmlCharEncError code) {
1650
93
    xmlParserErrors ret;
1651
1652
93
    switch (code) {
1653
0
        case XML_ENC_ERR_SUCCESS:
1654
0
            ret = XML_ERR_OK;
1655
0
            break;
1656
93
        case XML_ENC_ERR_INPUT:
1657
93
            ret = XML_ERR_INVALID_ENCODING;
1658
93
            break;
1659
0
        case XML_ENC_ERR_MEMORY:
1660
0
            ret = XML_ERR_NO_MEMORY;
1661
0
            break;
1662
0
        default:
1663
0
            ret = XML_ERR_INTERNAL_ERROR;
1664
0
            break;
1665
93
    }
1666
1667
93
    return(ret);
1668
93
}
1669
1670
/**
1671
 * The value of `inlen` after return is the number of bytes consumed.
1672
 * The value of `outlen` after return is the number of bytes produced.
1673
 *
1674
 * @param handler  encoding handler
1675
 * @param out  a pointer to an array of bytes to store the result
1676
 * @param outlen  the length of `out`
1677
 * @param in  a pointer to an array of input bytes
1678
 * @param inlen  the length of `in`
1679
 * @param flush  end of input
1680
 * @returns an xmlCharEncError code.
1681
 */
1682
xmlCharEncError
1683
xmlEncInputChunk(xmlCharEncodingHandler *handler, unsigned char *out,
1684
                 int *outlen, const unsigned char *in, int *inlen,
1685
48.2k
                 int flush) {
1686
48.2k
    xmlCharEncError ret;
1687
1688
48.2k
    if (handler->flags & XML_HANDLER_LEGACY) {
1689
0
        xmlCharEncodingInputFunc func = handler->input.legacyFunc;
1690
1691
0
        if (func == NULL) {
1692
0
            *outlen = 0;
1693
0
            *inlen = 0;
1694
0
            return(XML_ENC_ERR_INTERNAL);
1695
0
        }
1696
1697
0
        ret = func(out, outlen, in, inlen);
1698
48.2k
    } else {
1699
48.2k
        xmlCharEncConvFunc func = handler->input.func;
1700
48.2k
        int oldInlen;
1701
1702
48.2k
        if (func == NULL) {
1703
0
            *outlen = 0;
1704
0
            *inlen = 0;
1705
0
            return(XML_ENC_ERR_INTERNAL);
1706
0
        }
1707
1708
48.2k
        oldInlen = *inlen;
1709
48.2k
        ret = func(handler->inputCtxt, out, outlen, in, inlen, flush);
1710
1711
        /*
1712
         * Check for truncated multi-byte sequence.
1713
         */
1714
48.2k
        if ((flush) && (ret == XML_ENC_ERR_SUCCESS) && (*inlen != oldInlen))
1715
1
            ret = XML_ENC_ERR_INPUT;
1716
48.2k
    }
1717
1718
48.2k
    if (ret > 0)
1719
449
        ret = XML_ENC_ERR_SUCCESS;
1720
1721
48.2k
    return(ret);
1722
48.2k
}
1723
1724
/**
1725
 * The value of `inlen` after return is the number of bytes consumed.
1726
 * The value of `outlen` after return is the number of bytes produced.
1727
 *
1728
 * @param handler  encoding handler
1729
 * @param out  a pointer to an array of bytes to store the result
1730
 * @param outlen  the length of `out`
1731
 * @param in  a pointer to an array of input bytes
1732
 * @param inlen  the length of `in`
1733
 * @returns an xmlCharEncError code.
1734
 */
1735
static xmlCharEncError
1736
xmlEncOutputChunk(xmlCharEncodingHandler *handler, unsigned char *out,
1737
0
                  int *outlen, const unsigned char *in, int *inlen) {
1738
0
    xmlCharEncError ret;
1739
1740
0
    if (handler->flags & XML_HANDLER_LEGACY) {
1741
0
        xmlCharEncodingOutputFunc func = handler->output.legacyFunc;
1742
1743
0
        if (func == NULL) {
1744
0
            *outlen = 0;
1745
0
            *inlen = 0;
1746
0
            return(XML_ENC_ERR_INTERNAL);
1747
0
        }
1748
1749
0
        ret = func(out, outlen, in, inlen);
1750
0
    } else {
1751
0
        xmlCharEncConvFunc func = handler->output.func;
1752
1753
0
        if (func == NULL) {
1754
0
            *outlen = 0;
1755
0
            *inlen = 0;
1756
0
            return(XML_ENC_ERR_INTERNAL);
1757
0
        }
1758
1759
0
        ret = func(handler->outputCtxt, out, outlen, in, inlen, /* flush */ 0);
1760
0
    }
1761
1762
0
    if (ret > 0)
1763
0
        ret = XML_ENC_ERR_SUCCESS;
1764
1765
0
    return(ret);
1766
0
}
1767
1768
/**
1769
 * DEPERECATED: Don't use.
1770
 *
1771
 * @param handler  encoding handler
1772
 * @param out  an xmlBuffer for the output.
1773
 * @param in  an xmlBuffer for the input
1774
 * @returns the number of bytes written or an xmlCharEncError code.
1775
 */
1776
int
1777
xmlCharEncFirstLine(xmlCharEncodingHandler *handler, struct _xmlBuffer *out,
1778
0
                    struct _xmlBuffer *in) {
1779
0
    return(xmlCharEncInFunc(handler, out, in));
1780
0
}
1781
1782
/**
1783
 * Generic front-end for input encoding conversion.
1784
 *
1785
 * `sizeOut` should be set to the maximum output size (or SIZE_MAX).
1786
 * After return, it is set to the number of bytes written.
1787
 *
1788
 * @param input  a parser input buffer
1789
 * @param sizeOut  pointer to output size
1790
 * @param flush  end of input
1791
 * @returns an xmlCharEncError code.
1792
 */
1793
xmlCharEncError
1794
xmlCharEncInput(xmlParserInputBuffer *input, size_t *sizeOut, int flush)
1795
119k
{
1796
119k
    xmlBufPtr out, in;
1797
119k
    const xmlChar *dataIn;
1798
119k
    size_t availIn;
1799
119k
    size_t maxOut;
1800
119k
    size_t totalIn, totalOut;
1801
119k
    xmlCharEncError ret;
1802
1803
119k
    out = input->buffer;
1804
119k
    in = input->raw;
1805
1806
119k
    maxOut = *sizeOut;
1807
119k
    totalOut = 0;
1808
1809
119k
    *sizeOut = 0;
1810
1811
119k
    availIn = xmlBufUse(in);
1812
119k
    if ((availIn == 0) && (!flush))
1813
75.1k
        return(0);
1814
43.8k
    dataIn = xmlBufContent(in);
1815
43.8k
    totalIn = 0;
1816
1817
47.9k
    while (1) {
1818
47.9k
        size_t availOut;
1819
47.9k
        int completeOut, completeIn;
1820
47.9k
        int c_out, c_in;
1821
1822
47.9k
        availOut = xmlBufAvail(out);
1823
47.9k
        if (availOut > INT_MAX / 2)
1824
0
            availOut = INT_MAX / 2;
1825
1826
47.9k
        if (availOut < maxOut) {
1827
5.68k
            c_out = availOut;
1828
5.68k
            completeOut = 0;
1829
42.2k
        } else {
1830
42.2k
            c_out = maxOut;
1831
42.2k
            completeOut = 1;
1832
42.2k
        }
1833
1834
47.9k
        if (availIn > INT_MAX / 2) {
1835
0
            c_in = INT_MAX / 2;
1836
0
            completeIn = 0;
1837
47.9k
        } else {
1838
47.9k
            c_in = availIn;
1839
47.9k
            completeIn = 1;
1840
47.9k
        }
1841
1842
47.9k
        ret = xmlEncInputChunk(input->encoder, xmlBufEnd(out), &c_out,
1843
47.9k
                               dataIn, &c_in, flush && completeIn);
1844
1845
47.9k
        totalIn += c_in;
1846
47.9k
        dataIn += c_in;
1847
47.9k
        availIn -= c_in;
1848
1849
47.9k
        totalOut += c_out;
1850
47.9k
        maxOut -= c_out;
1851
47.9k
        xmlBufAddLen(out, c_out);
1852
1853
47.9k
        if ((ret != XML_ENC_ERR_SUCCESS) && (ret != XML_ENC_ERR_SPACE)) {
1854
93
            input->error = xmlEncConvertError(ret);
1855
93
            return(ret);
1856
93
        }
1857
1858
47.8k
        if ((completeOut) && (completeIn))
1859
42.1k
            break;
1860
5.67k
        if ((completeOut) && (ret == XML_ENC_ERR_SPACE))
1861
0
            break;
1862
5.67k
        if ((completeIn) && (ret == XML_ENC_ERR_SUCCESS))
1863
1.58k
            break;
1864
1865
4.09k
        if (ret == XML_ENC_ERR_SPACE) {
1866
4.09k
            if (xmlBufGrow(out, 4096) < 0) {
1867
0
                input->error = XML_ERR_NO_MEMORY;
1868
0
                return(XML_ENC_ERR_MEMORY);
1869
0
            }
1870
4.09k
        }
1871
4.09k
    }
1872
1873
43.7k
    xmlBufShrink(in, totalIn);
1874
1875
43.7k
    if (input->rawconsumed > ULONG_MAX - (unsigned long) totalIn)
1876
0
        input->rawconsumed = ULONG_MAX;
1877
43.7k
    else
1878
43.7k
        input->rawconsumed += totalIn;
1879
1880
43.7k
    *sizeOut = totalOut;
1881
43.7k
    return(XML_ENC_ERR_SUCCESS);
1882
43.8k
}
1883
1884
/**
1885
 * Generic front-end for input encoding conversion.
1886
 *
1887
 * @param handler  encoding handler
1888
 * @param out  an xmlBuffer for the output.
1889
 * @param in  an xmlBuffer for the input
1890
 * @returns the number of bytes written or an xmlCharEncError code.
1891
 */
1892
int
1893
xmlCharEncInFunc(xmlCharEncodingHandler * handler, struct _xmlBuffer *out,
1894
                 struct _xmlBuffer *in)
1895
0
{
1896
0
    int ret;
1897
0
    int written;
1898
0
    int toconv;
1899
1900
0
    if (handler == NULL)
1901
0
        return(XML_ENC_ERR_INTERNAL);
1902
0
    if (out == NULL)
1903
0
        return(XML_ENC_ERR_INTERNAL);
1904
0
    if (in == NULL)
1905
0
        return(XML_ENC_ERR_INTERNAL);
1906
1907
0
    toconv = in->use;
1908
0
    if (toconv == 0)
1909
0
        return (0);
1910
0
    written = out->size - out->use -1; /* count '\0' */
1911
0
    if (toconv * 2 >= written) {
1912
0
        xmlBufferGrow(out, out->size + toconv * 2);
1913
0
        written = out->size - out->use - 1;
1914
0
    }
1915
0
    ret = xmlEncInputChunk(handler, &out->content[out->use], &written,
1916
0
                           in->content, &toconv, /* flush */ 0);
1917
0
    xmlBufferShrink(in, toconv);
1918
0
    out->use += written;
1919
0
    out->content[out->use] = 0;
1920
1921
0
    return (written? written : ret);
1922
0
}
1923
1924
/*
1925
 * @param buf  a char buffer
1926
 * @param val  a codepoint
1927
 *
1928
 * Serializes a decimal char ref like `&#38;`.
1929
 *
1930
 * Writes at most 10 bytes. Does not include a terminating zero byte.
1931
 *
1932
 * @returns the number of bytes written.
1933
 */
1934
static int
1935
0
xmlSerializeDecCharRef(char *buf, int val) {
1936
0
    char *out = buf;
1937
0
    int len, i;
1938
1939
0
    *out++ = '&';
1940
0
    *out++ = '#';
1941
1942
0
    if (val < 100) {
1943
0
        len = (val < 10) ? 1 : 2;
1944
0
    } else if (val < 10000) {
1945
0
        len = (val < 1000) ? 3 : 4;
1946
0
    } else if (val < 1000000) {
1947
0
        len = (val < 100000) ? 5 : 6;
1948
0
    } else {
1949
0
        len = 7;
1950
0
    }
1951
1952
0
    for (i = len - 1; i >= 0; i--) {
1953
0
        out[i] = '0' + val % 10;
1954
0
        val /= 10;
1955
0
    }
1956
1957
0
    out[len] = ';';
1958
1959
0
    return(len + 3);
1960
0
}
1961
1962
#ifdef LIBXML_OUTPUT_ENABLED
1963
/**
1964
 * Generic front-end for output encoding conversion.
1965
 *
1966
 * A first call with `init` set to 1 has to be made to write a BOM.
1967
 *
1968
 * When using GNU libiconv, unsupported characters in the output
1969
 * encoding will be automatically replaced with a numeric character
1970
 * reference.
1971
 *
1972
 * @param output  a parser output buffer
1973
 * @param init  is this an initialization call without data
1974
 * @returns the number of bytes written or an xmlCharEncError code.
1975
 */
1976
int
1977
xmlCharEncOutput(xmlOutputBuffer *output, int init)
1978
0
{
1979
0
    int ret;
1980
0
    size_t written;
1981
0
    int writtentot = 0;
1982
0
    size_t toconv;
1983
0
    int c_in;
1984
0
    int c_out;
1985
0
    xmlBufPtr in;
1986
0
    xmlBufPtr out;
1987
1988
0
    if ((output == NULL) || (output->encoder == NULL) ||
1989
0
        (output->buffer == NULL) || (output->conv == NULL))
1990
0
        return(XML_ENC_ERR_INTERNAL);
1991
0
    out = output->conv;
1992
0
    in = output->buffer;
1993
1994
0
retry:
1995
1996
0
    written = xmlBufAvail(out);
1997
1998
    /*
1999
     * First specific handling of the initialization call
2000
     */
2001
0
    if (init) {
2002
0
        c_in = 0;
2003
0
        c_out = written;
2004
        /* TODO: Check return value. */
2005
0
        xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out,
2006
0
                          NULL, &c_in);
2007
0
        xmlBufAddLen(out, c_out);
2008
0
        return(c_out);
2009
0
    }
2010
2011
    /*
2012
     * Conversion itself.
2013
     */
2014
0
    toconv = xmlBufUse(in);
2015
0
    if (toconv > 64 * 1024)
2016
0
        toconv = 64 * 1024;
2017
0
    if (toconv * 4 >= written) {
2018
0
        if (xmlBufGrow(out, toconv * 4) < 0) {
2019
0
            ret = XML_ENC_ERR_MEMORY;
2020
0
            goto error;
2021
0
        }
2022
0
        written = xmlBufAvail(out);
2023
0
    }
2024
0
    if (written > 256 * 1024)
2025
0
        written = 256 * 1024;
2026
2027
0
    c_in = toconv;
2028
0
    c_out = written;
2029
0
    ret = xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out,
2030
0
                            xmlBufContent(in), &c_in);
2031
0
    xmlBufShrink(in, c_in);
2032
0
    xmlBufAddLen(out, c_out);
2033
0
    writtentot += c_out;
2034
2035
0
    if (ret == XML_ENC_ERR_SPACE)
2036
0
        goto retry;
2037
2038
    /*
2039
     * Attempt to handle error cases
2040
     */
2041
0
    if (ret == XML_ENC_ERR_INPUT) {
2042
0
        xmlChar charref[20];
2043
0
        int len = xmlBufUse(in);
2044
0
        xmlChar *content = xmlBufContent(in);
2045
0
        int cur, charrefLen;
2046
2047
0
        cur = xmlGetUTF8Char(content, &len);
2048
0
        if (cur <= 0)
2049
0
            goto error;
2050
2051
        /*
2052
         * Removes the UTF8 sequence, and replace it by a charref
2053
         * and continue the transcoding phase, hoping the error
2054
         * did not mangle the encoder state.
2055
         */
2056
0
        charrefLen = xmlSerializeDecCharRef((char *) charref, cur);
2057
0
        xmlBufGrow(out, charrefLen * 4);
2058
0
        c_out = xmlBufAvail(out);
2059
0
        c_in = charrefLen;
2060
0
        ret = xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out,
2061
0
                                charref, &c_in);
2062
0
        if ((ret < 0) || (c_in != charrefLen)) {
2063
0
            ret = XML_ENC_ERR_INTERNAL;
2064
0
            goto error;
2065
0
        }
2066
2067
0
        xmlBufShrink(in, len);
2068
0
        xmlBufAddLen(out, c_out);
2069
0
        writtentot += c_out;
2070
0
        goto retry;
2071
0
    }
2072
2073
0
error:
2074
0
    if (((writtentot <= 0) && (ret != 0)) ||
2075
0
        (ret == XML_ENC_ERR_MEMORY)) {
2076
0
        if (output->error == 0)
2077
0
            output->error = xmlEncConvertError(ret);
2078
0
        return(ret);
2079
0
    }
2080
2081
0
    return(writtentot);
2082
0
}
2083
#endif
2084
2085
/**
2086
 * Generic front-end for output encoding conversion.
2087
 *
2088
 * A first call with `in` set to NULL has to be made to write a BOM.
2089
 *
2090
 * When using GNU libiconv, unsupported characters in the output
2091
 * encoding will be automatically replaced with a numeric character
2092
 * reference.
2093
 *
2094
 * @param handler  encoding handler
2095
 * @param out  an xmlBuffer for the output.
2096
 * @param in  an xmlBuffer for the input
2097
 * @returns the number of bytes written or an xmlCharEncError code.
2098
 */
2099
int
2100
xmlCharEncOutFunc(xmlCharEncodingHandler *handler, struct _xmlBuffer *out,
2101
0
                  struct _xmlBuffer *in) {
2102
0
    int ret;
2103
0
    int written;
2104
0
    int writtentot = 0;
2105
0
    int toconv;
2106
2107
0
    if (handler == NULL) return(XML_ENC_ERR_INTERNAL);
2108
0
    if (out == NULL) return(XML_ENC_ERR_INTERNAL);
2109
2110
0
retry:
2111
2112
0
    written = out->size - out->use;
2113
2114
0
    if (written > 0)
2115
0
  written--; /* Gennady: count '/0' */
2116
2117
    /*
2118
     * First specific handling of in = NULL, i.e. the initialization call
2119
     */
2120
0
    if (in == NULL) {
2121
0
        toconv = 0;
2122
        /* TODO: Check return value. */
2123
0
        xmlEncOutputChunk(handler, &out->content[out->use], &written,
2124
0
                          NULL, &toconv);
2125
0
        out->use += written;
2126
0
        out->content[out->use] = 0;
2127
0
        return(0);
2128
0
    }
2129
2130
    /*
2131
     * Conversion itself.
2132
     */
2133
0
    toconv = in->use;
2134
0
    if (toconv * 4 >= written) {
2135
0
        xmlBufferGrow(out, toconv * 4);
2136
0
  written = out->size - out->use - 1;
2137
0
    }
2138
0
    ret = xmlEncOutputChunk(handler, &out->content[out->use], &written,
2139
0
                            in->content, &toconv);
2140
0
    xmlBufferShrink(in, toconv);
2141
0
    out->use += written;
2142
0
    writtentot += written;
2143
0
    out->content[out->use] = 0;
2144
2145
0
    if (ret == XML_ENC_ERR_SPACE)
2146
0
        goto retry;
2147
2148
    /*
2149
     * Attempt to handle error cases
2150
     */
2151
0
    if (ret == XML_ENC_ERR_INPUT) {
2152
0
        xmlChar charref[20];
2153
0
        int len = in->use;
2154
0
        const xmlChar *utf = (const xmlChar *) in->content;
2155
0
        int cur, charrefLen;
2156
2157
0
        cur = xmlGetUTF8Char(utf, &len);
2158
0
        if (cur <= 0)
2159
0
            return(ret);
2160
2161
        /*
2162
         * Removes the UTF8 sequence, and replace it by a charref
2163
         * and continue the transcoding phase, hoping the error
2164
         * did not mangle the encoder state.
2165
         */
2166
0
        charrefLen = xmlSerializeDecCharRef((char *) charref, cur);
2167
0
        xmlBufferShrink(in, len);
2168
0
        xmlBufferGrow(out, charrefLen * 4);
2169
0
        written = out->size - out->use - 1;
2170
0
        toconv = charrefLen;
2171
0
        ret = xmlEncOutputChunk(handler, &out->content[out->use], &written,
2172
0
                                charref, &toconv);
2173
0
        if ((ret < 0) || (toconv != charrefLen))
2174
0
            return(XML_ENC_ERR_INTERNAL);
2175
2176
0
        out->use += written;
2177
0
        writtentot += written;
2178
0
        out->content[out->use] = 0;
2179
0
        goto retry;
2180
0
    }
2181
0
    return(writtentot ? writtentot : ret);
2182
0
}
2183
2184
/**
2185
 * Releases an xmlCharEncodingHandler. Must be called after
2186
 * a handler is no longer in use.
2187
 *
2188
 * @param handler  encoding handler
2189
 * @returns 0.
2190
 */
2191
int
2192
1.55k
xmlCharEncCloseFunc(xmlCharEncodingHandler *handler) {
2193
1.55k
    if (handler == NULL)
2194
0
        return(0);
2195
2196
1.55k
    if (handler->flags & XML_HANDLER_STATIC)
2197
277
        return(0);
2198
2199
1.27k
    xmlFree(handler->name);
2200
1.27k
    if (handler->ctxtDtor != NULL) {
2201
1.06k
        handler->ctxtDtor(handler->inputCtxt);
2202
1.06k
        handler->ctxtDtor(handler->outputCtxt);
2203
1.06k
    }
2204
1.27k
    xmlFree(handler);
2205
1.27k
    return(0);
2206
1.55k
}
2207
2208
/**
2209
 * This function provides the current index of the parser relative
2210
 * to the start of the current entity. This function is computed in
2211
 * bytes from the beginning starting at zero and finishing at the
2212
 * size in byte of the file if parsing a file. The function is
2213
 * of constant cost if the input is UTF-8 but can be costly if run
2214
 * on non-UTF-8 input.
2215
 *
2216
 * @deprecated Don't use.
2217
 *
2218
 * @param ctxt  an XML parser context
2219
 * @returns the index in bytes from the beginning of the entity or -1
2220
 *         in case the index could not be computed.
2221
 */
2222
long
2223
0
xmlByteConsumed(xmlParserCtxt *ctxt) {
2224
0
    xmlParserInputPtr in;
2225
2226
0
    if (ctxt == NULL)
2227
0
        return(-1);
2228
0
    in = ctxt->input;
2229
0
    if (in == NULL)
2230
0
        return(-1);
2231
2232
0
    if ((in->buf != NULL) && (in->buf->encoder != NULL)) {
2233
0
        int unused = 0;
2234
0
  xmlCharEncodingHandler * handler = in->buf->encoder;
2235
2236
        /*
2237
   * Encoding conversion, compute the number of unused original
2238
   * bytes from the input not consumed and subtract that from
2239
   * the raw consumed value, this is not a cheap operation
2240
   */
2241
0
        if (in->end - in->cur > 0) {
2242
0
      unsigned char *convbuf;
2243
0
      const unsigned char *cur = (const unsigned char *)in->cur;
2244
0
      int toconv, ret;
2245
2246
0
            convbuf = xmlMalloc(32000);
2247
0
            if (convbuf == NULL)
2248
0
                return(-1);
2249
2250
0
            toconv = in->end - cur;
2251
0
            unused = 32000;
2252
0
            ret = xmlEncOutputChunk(handler, convbuf, &unused, cur, &toconv);
2253
2254
0
            xmlFree(convbuf);
2255
2256
0
            if (ret != XML_ENC_ERR_SUCCESS)
2257
0
                return(-1);
2258
0
  }
2259
2260
0
  if (in->buf->rawconsumed < (unsigned long) unused)
2261
0
      return(-1);
2262
0
  return(in->buf->rawconsumed - unused);
2263
0
    }
2264
2265
0
    return(in->consumed + (in->cur - in->base));
2266
0
}
2267
2268
/************************************************************************
2269
 *                  *
2270
 *    Conversions To/From UTF8 encoding     *
2271
 *                  *
2272
 ************************************************************************/
2273
2274
static xmlCharEncError
2275
asciiToAscii(void *vctxt ATTRIBUTE_UNUSED,
2276
             unsigned char* out, int *poutlen,
2277
             const unsigned char* in, int *pinlen,
2278
421
             int flush ATTRIBUTE_UNUSED) {
2279
421
    const unsigned char *inend;
2280
421
    const unsigned char *instart = in;
2281
421
    int inlen, outlen, ret;
2282
2283
421
    if (in == NULL) {
2284
0
        *pinlen = 0;
2285
0
        *poutlen = 0;
2286
0
        return(XML_ENC_ERR_SUCCESS);
2287
0
    }
2288
2289
421
    inlen = *pinlen;
2290
421
    outlen = *poutlen;
2291
2292
421
    if (outlen < inlen) {
2293
406
        inlen = outlen;
2294
406
        ret = XML_ENC_ERR_SPACE;
2295
406
    } else {
2296
15
        ret = inlen;
2297
15
    }
2298
2299
421
    inend = in + inlen;
2300
421
    *poutlen = inlen;
2301
421
    *pinlen = inlen;
2302
2303
1.00M
    while (in < inend) {
2304
1.00M
  unsigned c = *in;
2305
2306
1.00M
        if (c >= 0x80) {
2307
10
      *poutlen = in - instart;
2308
10
      *pinlen = in - instart;
2309
10
      return(XML_ENC_ERR_INPUT);
2310
10
  }
2311
2312
1.00M
        in++;
2313
1.00M
  *out++ = c;
2314
1.00M
    }
2315
2316
411
    return(ret);
2317
421
}
2318
2319
static xmlCharEncError
2320
latin1ToUTF8(void *vctxt ATTRIBUTE_UNUSED,
2321
             unsigned char* out, int *outlen,
2322
             const unsigned char* in, int *inlen,
2323
7.78k
             int flush ATTRIBUTE_UNUSED) {
2324
7.78k
    unsigned char* outstart = out;
2325
7.78k
    const unsigned char* instart = in;
2326
7.78k
    unsigned char* outend;
2327
7.78k
    const unsigned char* inend;
2328
7.78k
    int ret = XML_ENC_ERR_SPACE;
2329
2330
7.78k
    if ((out == NULL) || (in == NULL) || (outlen == NULL) || (inlen == NULL))
2331
0
  return(XML_ENC_ERR_INTERNAL);
2332
2333
7.78k
    outend = out + *outlen;
2334
7.78k
    inend = in + *inlen;
2335
2336
17.1M
    while (in < inend) {
2337
17.1M
        unsigned c = *in;
2338
2339
17.1M
  if (c < 0x80) {
2340
9.11M
            if (out >= outend)
2341
3.14k
                goto done;
2342
9.10M
            *out++ = c;
2343
9.10M
  } else {
2344
8.04M
            if (outend - out < 2)
2345
4.44k
                goto done;
2346
8.04M
      *out++ = (c >> 6) | 0xC0;
2347
8.04M
            *out++ = (c & 0x3F) | 0x80;
2348
8.04M
        }
2349
2350
17.1M
        in++;
2351
17.1M
    }
2352
2353
192
    ret = out - outstart;
2354
2355
7.78k
done:
2356
7.78k
    *outlen = out - outstart;
2357
7.78k
    *inlen = in - instart;
2358
7.78k
    return(ret);
2359
192
}
2360
2361
/**
2362
 * Take a block of ISO Latin 1 chars in and try to convert it to an UTF-8
2363
 * block of chars out.
2364
 *
2365
 * The value of `inlen` after return is the number of bytes consumed.
2366
 * The value of `outlen` after return is the number of bytes produced.
2367
 *
2368
 * @param out  a pointer to an array of bytes to store the result
2369
 * @param outlen  the length of `out`
2370
 * @param in  a pointer to an array of ISO Latin 1 chars
2371
 * @param inlen  the length of `in`
2372
 * @returns the number of bytes written or an xmlCharEncError code.
2373
 */
2374
int
2375
xmlIsolat1ToUTF8(unsigned char* out, int *outlen,
2376
0
                 const unsigned char* in, int *inlen) {
2377
0
    return(latin1ToUTF8(/* ctxt */ NULL, out, outlen, in, inlen,
2378
0
                        /* flush */ 0));
2379
0
}
2380
2381
static xmlCharEncError
2382
UTF8ToUTF8(void *vctxt ATTRIBUTE_UNUSED,
2383
           unsigned char* out, int *outlen,
2384
           const unsigned char* in, int *inlen,
2385
0
           int flush ATTRIBUTE_UNUSED) {
2386
0
    int len;
2387
0
    int ret;
2388
2389
0
    if (in == NULL) {
2390
0
        *inlen = 0;
2391
0
        *outlen = 0;
2392
0
        return(XML_ENC_ERR_SUCCESS);
2393
0
    }
2394
2395
0
    if (*outlen < *inlen) {
2396
0
  len = *outlen;
2397
0
        ret = XML_ENC_ERR_SPACE;
2398
0
    } else {
2399
0
  len = *inlen;
2400
0
        ret = len;
2401
0
    }
2402
2403
0
    memcpy(out, in, len);
2404
2405
0
    *outlen = len;
2406
0
    *inlen = len;
2407
0
    return(ret);
2408
0
}
2409
2410
2411
#ifdef LIBXML_OUTPUT_ENABLED
2412
static xmlCharEncError
2413
UTF8ToLatin1(void *vctxt ATTRIBUTE_UNUSED,
2414
             unsigned char* out, int *outlen,
2415
             const unsigned char* in, int *inlen,
2416
0
             int flush ATTRIBUTE_UNUSED) {
2417
0
    const unsigned char* outend;
2418
0
    const unsigned char* outstart = out;
2419
0
    const unsigned char* instart = in;
2420
0
    const unsigned char* inend;
2421
0
    unsigned c;
2422
0
    int ret = XML_ENC_ERR_SPACE;
2423
2424
0
    if ((out == NULL) || (outlen == NULL) || (inlen == NULL))
2425
0
        return(XML_ENC_ERR_INTERNAL);
2426
2427
0
    if (in == NULL) {
2428
0
        *inlen = 0;
2429
0
        *outlen = 0;
2430
0
        return(XML_ENC_ERR_SUCCESS);
2431
0
    }
2432
2433
0
    inend = in + *inlen;
2434
0
    outend = out + *outlen;
2435
0
    while (in < inend) {
2436
0
        if (out >= outend)
2437
0
            goto done;
2438
2439
0
  c = *in;
2440
2441
0
        if (c < 0x80) {
2442
0
            *out++ = c;
2443
0
        } else if ((c >= 0xC2) && (c <= 0xC3)) {
2444
0
            if (inend - in < 2)
2445
0
                break;
2446
0
            in++;
2447
0
            *out++ = (unsigned char) ((c << 6) | (*in & 0x3F));
2448
0
        } else {
2449
0
            ret = XML_ENC_ERR_INPUT;
2450
0
            goto done;
2451
0
  }
2452
2453
0
        in++;
2454
0
    }
2455
2456
0
    ret = out - outstart;
2457
2458
0
done:
2459
0
    *outlen = out - outstart;
2460
0
    *inlen = in - instart;
2461
0
    return(ret);
2462
0
}
2463
2464
/**
2465
 * Take a block of UTF-8 chars in and try to convert it to an ISO Latin 1
2466
 * block of chars out.
2467
 *
2468
 * The value of `inlen` after return is the number of bytes consumed.
2469
 * The value of `outlen` after return is the number of bytes produced.
2470
 *
2471
 * @param out  a pointer to an array of bytes to store the result
2472
 * @param outlen  the length of `out`
2473
 * @param in  a pointer to an array of UTF-8 chars
2474
 * @param inlen  the length of `in`
2475
 * @returns the number of bytes written or an xmlCharEncError code.
2476
 */
2477
int
2478
xmlUTF8ToIsolat1(unsigned char* out, int *outlen,
2479
0
              const unsigned char* in, int *inlen) {
2480
0
    if ((out == NULL) || (outlen == NULL) || (in == NULL) || (inlen == NULL))
2481
0
        return(XML_ENC_ERR_INTERNAL);
2482
2483
0
    return(UTF8ToLatin1(/* ctxt */ NULL, out, outlen, in, inlen,
2484
0
                        /* flush */ 0));
2485
0
}
2486
#endif /* LIBXML_OUTPUT_ENABLED */
2487
2488
static xmlCharEncError
2489
UTF16LEToUTF8(void *vctxt ATTRIBUTE_UNUSED,
2490
              unsigned char *out, int *outlen,
2491
              const unsigned char *in, int *inlen,
2492
3.88k
              int flush ATTRIBUTE_UNUSED) {
2493
3.88k
    const unsigned char *instart = in;
2494
3.88k
    const unsigned char *inend = in + (*inlen & ~1);
2495
3.88k
    unsigned char *outstart = out;
2496
3.88k
    unsigned char *outend = out + *outlen;
2497
3.88k
    unsigned c, d;
2498
3.88k
    int ret = XML_ENC_ERR_SPACE;
2499
2500
1.63M
    while (in < inend) {
2501
1.62M
        c = in[0] | (in[1] << 8);
2502
2503
1.62M
        if (c < 0x80) {
2504
95.6k
            if (out >= outend)
2505
63
                goto done;
2506
95.5k
            out[0] = c;
2507
95.5k
            in += 2;
2508
95.5k
            out += 1;
2509
1.53M
        } else if (c < 0x800) {
2510
191k
            if (outend - out < 2)
2511
164
                goto done;
2512
190k
            out[0] = (c >> 6)   | 0xC0;
2513
190k
            out[1] = (c & 0x3F) | 0x80;
2514
190k
            in += 2;
2515
190k
            out += 2;
2516
1.34M
        } else if ((c & 0xF800) != 0xD800) {
2517
1.33M
            if (outend - out < 3)
2518
1.03k
                goto done;
2519
1.33M
            out[0] =  (c >> 12)         | 0xE0;
2520
1.33M
            out[1] = ((c >>  6) & 0x3F) | 0x80;
2521
1.33M
            out[2] =  (c        & 0x3F) | 0x80;
2522
1.33M
            in += 2;
2523
1.33M
            out += 3;
2524
1.33M
        } else {
2525
            /* Surrogate pair */
2526
7.31k
            if ((c & 0xFC00) != 0xD800) {
2527
4
                ret = XML_ENC_ERR_INPUT;
2528
4
                goto done;
2529
4
            }
2530
7.30k
      if (inend - in < 4)
2531
706
    break;
2532
6.60k
            d = in[2] | (in[3] << 8);
2533
6.60k
            if ((d & 0xFC00) != 0xDC00) {
2534
6
                ret = XML_ENC_ERR_INPUT;
2535
6
                goto done;
2536
6
            }
2537
6.59k
      if (outend - out < 4)
2538
32
    goto done;
2539
6.56k
            c = (c << 10) + d - ((0xD800 << 10) + 0xDC00 - 0x10000);
2540
6.56k
            out[0] =  (c >> 18)         | 0xF0;
2541
6.56k
            out[1] = ((c >> 12) & 0x3F) | 0x80;
2542
6.56k
            out[2] = ((c >>  6) & 0x3F) | 0x80;
2543
6.56k
            out[3] =  (c        & 0x3F) | 0x80;
2544
6.56k
            in += 4;
2545
6.56k
            out += 4;
2546
6.56k
        }
2547
1.62M
    }
2548
2549
2.57k
    ret = out - outstart;
2550
2551
3.88k
done:
2552
3.88k
    *outlen = out - outstart;
2553
3.88k
    *inlen = in - instart;
2554
3.88k
    return(ret);
2555
2.57k
}
2556
2557
#ifdef LIBXML_OUTPUT_ENABLED
2558
static xmlCharEncError
2559
UTF8ToUTF16LE(void *vctxt ATTRIBUTE_UNUSED,
2560
              unsigned char *out, int *outlen,
2561
              const unsigned char *in, int *inlen,
2562
0
              int flush ATTRIBUTE_UNUSED) {
2563
0
    const unsigned char *instart = in;
2564
0
    const unsigned char *inend;
2565
0
    unsigned char *outstart = out;
2566
0
    unsigned char *outend;
2567
0
    unsigned c, d;
2568
0
    int ret = XML_ENC_ERR_SPACE;
2569
2570
    /* UTF16LE encoding has no BOM */
2571
0
    if ((out == NULL) || (outlen == NULL) || (inlen == NULL))
2572
0
        return(XML_ENC_ERR_INTERNAL);
2573
0
    if (in == NULL) {
2574
0
  *outlen = 0;
2575
0
  *inlen = 0;
2576
0
  return(0);
2577
0
    }
2578
0
    inend = in + *inlen;
2579
0
    outend = out + (*outlen & ~1);
2580
0
    while (in < inend) {
2581
0
        c = in[0];
2582
2583
0
        if (c < 0x80) {
2584
0
            if (out >= outend)
2585
0
                goto done;
2586
0
            out[0] = c;
2587
0
            out[1] = 0;
2588
0
            in += 1;
2589
0
            out += 2;
2590
0
        } else {
2591
0
            int i, len;
2592
0
            unsigned min;
2593
2594
0
            if (c < 0xE0) {
2595
0
                if (c < 0xC2) {
2596
0
                    ret = XML_ENC_ERR_INPUT;
2597
0
                    goto done;
2598
0
                }
2599
0
                c &= 0x1F;
2600
0
                len = 2;
2601
0
                min = 0x80;
2602
0
            } else if (c < 0xF0) {
2603
0
                c &= 0x0F;
2604
0
                len = 3;
2605
0
                min = 0x800;
2606
0
            } else {
2607
0
                c &= 0x0F;
2608
0
                len = 4;
2609
0
                min = 0x10000;
2610
0
            }
2611
2612
0
            if (inend - in < len)
2613
0
                break;
2614
2615
0
            for (i = 1; i < len; i++) {
2616
0
                if ((in[i] & 0xC0) != 0x80) {
2617
0
                    ret = XML_ENC_ERR_INPUT;
2618
0
                    goto done;
2619
0
                }
2620
0
                c = (c << 6) | (in[i] & 0x3F);
2621
0
            }
2622
2623
0
            if ((c < min) ||
2624
0
                ((c >= 0xD800) && (c <= 0xDFFF)) ||
2625
0
                (c > 0x10FFFF)) {
2626
0
                ret = XML_ENC_ERR_INPUT;
2627
0
                goto done;
2628
0
            }
2629
2630
0
            if (c < 0x10000) {
2631
0
                if (out >= outend)
2632
0
                    goto done;
2633
0
                out[0] = c & 0xFF;
2634
0
                out[1] = c >> 8;
2635
0
                out += 2;
2636
0
            } else {
2637
0
                if (outend - out < 4)
2638
0
                    goto done;
2639
0
                c -= 0x10000;
2640
0
                d = (c & 0x03FF) | 0xDC00;
2641
0
                c = (c >> 10)    | 0xD800;
2642
0
                out[0] = c & 0xFF;
2643
0
                out[1] = c >> 8;
2644
0
                out[2] = d & 0xFF;
2645
0
                out[3] = d >> 8;
2646
0
                out += 4;
2647
0
            }
2648
2649
0
            in += len;
2650
0
        }
2651
0
    }
2652
2653
0
    ret = out - outstart;
2654
2655
0
done:
2656
0
    *outlen = out - outstart;
2657
0
    *inlen = in - instart;
2658
0
    return(ret);
2659
0
}
2660
2661
static xmlCharEncError
2662
UTF8ToUTF16(void *vctxt,
2663
            unsigned char* outb, int *outlen,
2664
            const unsigned char* in, int *inlen,
2665
0
            int flush) {
2666
0
    if (in == NULL) {
2667
  /*
2668
   * initialization, add the Byte Order Mark for UTF-16LE
2669
   */
2670
0
        if (*outlen >= 2) {
2671
0
      outb[0] = 0xFF;
2672
0
      outb[1] = 0xFE;
2673
0
      *outlen = 2;
2674
0
      *inlen = 0;
2675
0
      return(2);
2676
0
  }
2677
0
  *outlen = 0;
2678
0
  *inlen = 0;
2679
0
  return(0);
2680
0
    }
2681
0
    return (UTF8ToUTF16LE(vctxt, outb, outlen, in, inlen, flush));
2682
0
}
2683
#endif /* LIBXML_OUTPUT_ENABLED */
2684
2685
static xmlCharEncError
2686
UTF16BEToUTF8(void *vctxt ATTRIBUTE_UNUSED,
2687
              unsigned char *out, int *outlen,
2688
              const unsigned char *in, int *inlen,
2689
3.75k
              int flush ATTRIBUTE_UNUSED) {
2690
3.75k
    const unsigned char *instart = in;
2691
3.75k
    const unsigned char *inend = in + (*inlen & ~1);
2692
3.75k
    unsigned char *outstart = out;
2693
3.75k
    unsigned char *outend = out + *outlen;
2694
3.75k
    unsigned c, d;
2695
3.75k
    int ret = XML_ENC_ERR_SPACE;
2696
2697
670k
    while (in < inend) {
2698
667k
        c = (in[0] << 8) | in[1];
2699
2700
667k
        if (c < 0x80) {
2701
13.8k
            if (out >= outend)
2702
8
                goto done;
2703
13.8k
            out[0] = c;
2704
13.8k
            in += 2;
2705
13.8k
            out += 1;
2706
653k
        } else if (c < 0x800) {
2707
307k
            if (outend - out < 2)
2708
265
                goto done;
2709
306k
            out[0] = (c >> 6)   | 0xC0;
2710
306k
            out[1] = (c & 0x3F) | 0x80;
2711
306k
            in += 2;
2712
306k
            out += 2;
2713
346k
        } else if ((c & 0xF800) != 0xD800) {
2714
341k
            if (outend - out < 3)
2715
363
                goto done;
2716
341k
            out[0] =  (c >> 12)         | 0xE0;
2717
341k
            out[1] = ((c >>  6) & 0x3F) | 0x80;
2718
341k
            out[2] =  (c        & 0x3F) | 0x80;
2719
341k
            in += 2;
2720
341k
            out += 3;
2721
341k
        } else {
2722
            /* Surrogate pair */
2723
4.63k
            if ((c & 0xFC00) != 0xD800) {
2724
8
                ret = XML_ENC_ERR_INPUT;
2725
8
                goto done;
2726
8
            }
2727
4.62k
      if (inend - in < 4)
2728
211
    break;
2729
4.41k
            d = (in[2] << 8) | in[3];
2730
4.41k
            if ((d & 0xFC00) != 0xDC00) {
2731
7
                ret = XML_ENC_ERR_INPUT;
2732
7
                goto done;
2733
7
            }
2734
4.40k
      if (outend - out < 4)
2735
20
    goto done;
2736
4.38k
            c = (c << 10) + d - ((0xD800 << 10) + 0xDC00 - 0x10000);
2737
4.38k
            out[0] =  (c >> 18)         | 0xF0;
2738
4.38k
            out[1] = ((c >> 12) & 0x3F) | 0x80;
2739
4.38k
            out[2] = ((c >>  6) & 0x3F) | 0x80;
2740
4.38k
            out[3] =  (c        & 0x3F) | 0x80;
2741
4.38k
            in += 4;
2742
4.38k
            out += 4;
2743
4.38k
        }
2744
667k
    }
2745
2746
3.08k
    ret = out - outstart;
2747
2748
3.75k
done:
2749
3.75k
    *outlen = out - outstart;
2750
3.75k
    *inlen = in - instart;
2751
3.75k
    return(ret);
2752
3.08k
}
2753
2754
#ifdef LIBXML_OUTPUT_ENABLED
2755
static xmlCharEncError
2756
UTF8ToUTF16BE(void *vctxt ATTRIBUTE_UNUSED,
2757
              unsigned char *out, int *outlen,
2758
              const unsigned char *in, int *inlen,
2759
0
              int flush ATTRIBUTE_UNUSED) {
2760
0
    const unsigned char *instart = in;
2761
0
    const unsigned char *inend;
2762
0
    unsigned char *outstart = out;
2763
0
    unsigned char *outend;
2764
0
    unsigned c, d;
2765
0
    int ret = XML_ENC_ERR_SPACE;
2766
2767
    /* UTF-16BE has no BOM */
2768
0
    if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) return(-1);
2769
0
    if (in == NULL) {
2770
0
  *outlen = 0;
2771
0
  *inlen = 0;
2772
0
  return(0);
2773
0
    }
2774
0
    inend = in + *inlen;
2775
0
    outend = out + (*outlen & ~1);
2776
0
    while (in < inend) {
2777
0
        c = in[0];
2778
2779
0
        if (c < 0x80) {
2780
0
            if (out >= outend)
2781
0
                goto done;
2782
0
            out[0] = 0;
2783
0
            out[1] = c;
2784
0
            in += 1;
2785
0
            out += 2;
2786
0
        } else {
2787
0
            int i, len;
2788
0
            unsigned min;
2789
2790
0
            if (c < 0xE0) {
2791
0
                if (c < 0xC2) {
2792
0
                    ret = XML_ENC_ERR_INPUT;
2793
0
                    goto done;
2794
0
                }
2795
0
                c &= 0x1F;
2796
0
                len = 2;
2797
0
                min = 0x80;
2798
0
            } else if (c < 0xF0) {
2799
0
                c &= 0x0F;
2800
0
                len = 3;
2801
0
                min = 0x800;
2802
0
            } else {
2803
0
                c &= 0x0F;
2804
0
                len = 4;
2805
0
                min = 0x10000;
2806
0
            }
2807
2808
0
            if (inend - in < len)
2809
0
                break;
2810
2811
0
            for (i = 1; i < len; i++) {
2812
0
                if ((in[i] & 0xC0) != 0x80) {
2813
0
                    ret = XML_ENC_ERR_INPUT;
2814
0
                    goto done;
2815
0
                }
2816
0
                c = (c << 6) | (in[i] & 0x3F);
2817
0
            }
2818
2819
0
            if ((c < min) ||
2820
0
                ((c >= 0xD800) && (c <= 0xDFFF)) ||
2821
0
                (c > 0x10FFFF)) {
2822
0
                ret = XML_ENC_ERR_INPUT;
2823
0
                goto done;
2824
0
            }
2825
2826
0
            if (c < 0x10000) {
2827
0
                if (out >= outend)
2828
0
                    goto done;
2829
0
                out[0] = c >> 8;
2830
0
                out[1] = c & 0xFF;
2831
0
                out += 2;
2832
0
            } else {
2833
0
                if (outend - out < 4)
2834
0
                    goto done;
2835
0
                c -= 0x10000;
2836
0
                d = (c & 0x03FF) | 0xDC00;
2837
0
                c = (c >> 10)    | 0xD800;
2838
0
                out[0] = c >> 8;
2839
0
                out[1] = c & 0xFF;
2840
0
                out[2] = d >> 8;
2841
0
                out[3] = d & 0xFF;
2842
0
                out += 4;
2843
0
            }
2844
2845
0
            in += len;
2846
0
        }
2847
0
    }
2848
2849
0
    ret = out - outstart;
2850
2851
0
done:
2852
0
    *outlen = out - outstart;
2853
0
    *inlen = in - instart;
2854
0
    return(ret);
2855
0
}
2856
#endif /* LIBXML_OUTPUT_ENABLED */
2857
2858
#if defined(LIBXML_OUTPUT_ENABLED) && defined(LIBXML_HTML_ENABLED)
2859
static xmlCharEncError
2860
UTF8ToHtmlWrapper(void *vctxt ATTRIBUTE_UNUSED,
2861
                  unsigned char *out, int *outlen,
2862
                  const unsigned char *in, int *inlen,
2863
0
                  int flush ATTRIBUTE_UNUSED) {
2864
0
    return(htmlUTF8ToHtml(out, outlen, in, inlen));
2865
0
}
2866
#endif
2867
2868
static xmlCharEncError
2869
Utf8ToEightBit(void *vctxt,
2870
               unsigned char *out, int *outlen,
2871
               const unsigned char *in, int *inlen,
2872
0
               int flush ATTRIBUTE_UNUSED) {
2873
0
    const unsigned char *xlattable = vctxt;
2874
0
    const unsigned char *instart = in;
2875
0
    const unsigned char *inend;
2876
0
    unsigned char *outstart = out;
2877
0
    unsigned char *outend;
2878
0
    int ret = XML_ENC_ERR_SPACE;
2879
2880
0
    if (in == NULL) {
2881
        /*
2882
        * initialization nothing to do
2883
        */
2884
0
        *outlen = 0;
2885
0
        *inlen = 0;
2886
0
        return(XML_ENC_ERR_SUCCESS);
2887
0
    }
2888
2889
0
    inend = in + *inlen;
2890
0
    outend = out + *outlen;
2891
0
    while (in < inend) {
2892
0
        unsigned d = *in;
2893
2894
0
        if  (d < 0x80)  {
2895
0
            if (out >= outend)
2896
0
                goto done;
2897
0
            in += 1;
2898
0
        } else if (d < 0xE0) {
2899
0
            unsigned c;
2900
2901
0
            if (inend - in < 2)
2902
0
                break;
2903
0
            c = in[1] & 0x3F;
2904
0
            d = d & 0x1F;
2905
0
            d = xlattable [48 + c + xlattable [d] * 64];
2906
0
            if (d == 0) {
2907
                /* not in character set */
2908
0
                ret = XML_ENC_ERR_INPUT;
2909
0
                goto done;
2910
0
            }
2911
0
            if (out >= outend)
2912
0
                goto done;
2913
0
            in += 2;
2914
0
        } else if (d < 0xF0) {
2915
0
            unsigned c1;
2916
0
            unsigned c2;
2917
2918
0
            if (inend - in < 3)
2919
0
                break;
2920
0
            c1 = in[1] & 0x3F;
2921
0
            c2 = in[2] & 0x3F;
2922
0
      d = d & 0x0F;
2923
0
      d = xlattable [48 + c2 + xlattable [48 + c1 +
2924
0
      xlattable [32 + d] * 64] * 64];
2925
0
            if (d == 0) {
2926
                /* not in character set */
2927
0
                ret = XML_ENC_ERR_INPUT;
2928
0
                goto done;
2929
0
            }
2930
0
            if (out >= outend)
2931
0
                goto done;
2932
0
            in += 3;
2933
0
        } else {
2934
            /* cannot transcode >= U+010000 */
2935
0
                ret = XML_ENC_ERR_INPUT;
2936
0
                goto done;
2937
0
        }
2938
2939
0
        *out++ = d;
2940
0
    }
2941
2942
0
    ret = out - outstart;
2943
2944
0
done:
2945
0
    *outlen = out - outstart;
2946
0
    *inlen = in - instart;
2947
0
    return(ret);
2948
0
}
2949
2950
static xmlCharEncError
2951
EightBitToUtf8(void *vctxt,
2952
               unsigned char* out, int *outlen,
2953
               const unsigned char* in, int *inlen,
2954
0
               int flush ATTRIBUTE_UNUSED) {
2955
0
    unsigned short const *unicodetable = vctxt;
2956
0
    const unsigned char* instart = in;
2957
0
    const unsigned char* inend;
2958
0
    unsigned char* outstart = out;
2959
0
    unsigned char* outend;
2960
0
    int ret = XML_ENC_ERR_SPACE;
2961
2962
0
    outend = out + *outlen;
2963
0
    inend = in + *inlen;
2964
2965
0
    while (in < inend) {
2966
0
        unsigned c = *in;
2967
2968
0
        if (c < 0x80) {
2969
0
            if (out >= outend)
2970
0
                goto done;
2971
0
            *out++ = c;
2972
0
        } else {
2973
0
            c = unicodetable[c - 0x80];
2974
0
            if (c == 0) {
2975
                /* undefined code point */
2976
0
                ret = XML_ENC_ERR_INPUT;
2977
0
                goto done;
2978
0
            }
2979
0
            if (c < 0x800) {
2980
0
                if (outend - out < 2)
2981
0
                    goto done;
2982
0
                *out++ = ((c >>  6) & 0x1F) | 0xC0;
2983
0
                *out++ = (c & 0x3F) | 0x80;
2984
0
            } else {
2985
0
                if (outend - out < 3)
2986
0
                    goto done;
2987
0
                *out++ = ((c >>  12) & 0x0F) | 0xE0;
2988
0
                *out++ = ((c >>  6) & 0x3F) | 0x80;
2989
0
                *out++ = (c & 0x3F) | 0x80;
2990
0
            }
2991
0
        }
2992
2993
0
        in += 1;
2994
0
    }
2995
2996
0
    ret = out - outstart;
2997
2998
0
done:
2999
0
    *outlen = out - outstart;
3000
0
    *inlen = in - instart;
3001
0
    return(ret);
3002
0
}
3003