Coverage Report

Created: 2026-07-16 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tidy-html5/src/utf8.c
Line
Count
Source
1
/* utf8.c -- convert characters to/from UTF-8
2
3
  (c) 1998-2007 (W3C) MIT, ERCIM, Keio University
4
  See tidy.h for the copyright notice.
5
6
  Uses public interfaces to abstract input source and output
7
  sink, which may be user supplied or either FILE* or memory
8
  based Tidy implementations.  Encoding support is uniform
9
  regardless of I/O mechanism.
10
11
  Note, UTF-8 encoding, by itself, does not affect the actual
12
  "codepoints" of the underlying character encoding.  In the
13
  cases of ASCII, Latin1, Unicode (16-bit, BMP), these all 
14
  refer to ISO-10646 "codepoints".  For anything else, they
15
  refer to some other "codepoint" set.
16
17
  Put another way, UTF-8 is a variable length method to 
18
  represent any non-negative integer value.  The glyph 
19
  that a integer value represents is unchanged and defined
20
  externally (e.g. by ISO-10646, Big5, Win1252, MacRoman,
21
  Latin2-9, and so on).
22
23
  Put still another way, UTF-8 is more of a _transfer_ encoding
24
  than a _character_ encoding, per se.
25
*/
26
27
#include "tidy.h"
28
#include "forward.h"
29
#include "utf8.h"
30
31
/* 
32
UTF-8 encoding/decoding functions
33
Return # of bytes in UTF-8 sequence; result < 0 if illegal sequence
34
35
Also see below for UTF-16 encoding/decoding functions
36
37
References :
38
39
1) UCS Transformation Format 8 (UTF-8):
40
ISO/IEC 10646-1:1996 Amendment 2 or ISO/IEC 10646-1:2000 Annex D
41
<http://anubis.dkuug.dk/JTC1/SC2/WG2/docs/n1335>
42
<http://www.cl.cam.ac.uk/~mgk25/ucs/ISO-10646-UTF-8.html>
43
44
Table 4 - Mapping from UCS-4 to UTF-8
45
46
2) Unicode standards:
47
<https://www.unicode.org/standard/standard.html>
48
49
3) Legal UTF-8 byte sequences:
50
<https://www.unicode.org/versions/corrigendum1.html>
51
52
Code point          1st byte    2nd byte    3rd byte    4th byte
53
----------          --------    --------    --------    --------
54
U+0000..U+007F      00..7F
55
U+0080..U+07FF      C2..DF      80..BF
56
U+0800..U+0FFF      E0          A0..BF      80..BF
57
U+1000..U+FFFF      E1..EF      80..BF      80..BF
58
U+10000..U+3FFFF    F0          90..BF      80..BF      80..BF
59
U+40000..U+FFFFF    F1..F3      80..BF      80..BF      80..BF
60
U+100000..U+10FFFF  F4          80..8F      80..BF      80..BF
61
62
The definition of UTF-8 in Annex D of ISO/IEC 10646-1:2000 also
63
allows for the use of five- and six-byte sequences to encode
64
characters that are outside the range of the Unicode character
65
set; those five- and six-byte sequences are illegal for the use
66
of UTF-8 as a transformation of Unicode characters. ISO/IEC 10646
67
does not allow mapping of unpaired surrogates, nor U+FFFE and U+FFFF
68
(but it does allow other noncharacters).
69
70
4) RFC 2279: UTF-8, a transformation format of ISO 10646:
71
<http://www.ietf.org/rfc/rfc2279.txt>
72
73
5) UTF-8 and Unicode FAQ:
74
<http://www.cl.cam.ac.uk/~mgk25/unicode.html>
75
76
6) Markus Kuhn's UTF-8 decoder stress test file:
77
<http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt>
78
79
7) UTF-8 Demo:
80
<http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-demo.txt>
81
82
8) UTF-8 Sampler:
83
<http://www.columbia.edu/kermit/utf8.html>
84
85
9) Transformation Format for 16 Planes of Group 00 (UTF-16):
86
ISO/IEC 10646-1:1996 Amendment 1 or ISO/IEC 10646-1:2000 Annex C
87
<http://anubis.dkuug.dk/JTC1/SC2/WG2/docs/n2005/n2005.pdf>
88
<http://www.cl.cam.ac.uk/~mgk25/ucs/ISO-10646-UTF-16.html>
89
90
10) RFC 2781: UTF-16, an encoding of ISO 10646:
91
<http://www.ietf.org/rfc/rfc2781.txt>
92
93
11) UTF-16 invalid surrogate pairs:
94
<https://www.unicode.org/faq/utf_bom.html#16>
95
96
UTF-16       UTF-8          UCS-4
97
D83F DFF*    F0 9F BF B*    0001FFF*
98
D87F DFF*    F0 AF BF B*    0002FFF*
99
D8BF DFF*    F0 BF BF B*    0003FFF*
100
D8FF DFF*    F1 8F BF B*    0004FFF*
101
D93F DFF*    F1 9F BF B*    0005FFF*
102
D97F DFF*    F1 AF BF B*    0006FFF*
103
                ...
104
DBBF DFF*    F3 BF BF B*    000FFFF*
105
DBFF DFF*    F4 8F BF B*    0010FFF*
106
107
* = E or F
108
                                   
109
1010  A
110
1011  B
111
1100  C
112
1101  D
113
1110  E
114
1111  F
115
116
*/
117
118
#define kNumUTF8Sequences        7
119
#define kMaxUTF8Bytes            4
120
121
523M
#define kUTF8ByteSwapNotAChar    0xFFFE
122
380M
#define kUTF8NotAChar            0xFFFF
123
124
236M
#define kMaxUTF8FromUCS4         0x10FFFF
125
126
1.98k
#define kUTF16SurrogatesBegin    0x10000
127
8.15k
#define kMaxUTF16FromUCS4        0x10FFFF
128
129
/* UTF-16 surrogate pair areas */
130
68.1k
#define kUTF16LowSurrogateBegin  0xD800
131
9.98k
#define kUTF16LowSurrogateEnd    0xDBFF
132
47.2k
#define kUTF16HighSurrogateBegin 0xDC00
133
3.86k
#define kUTF16HighSurrogateEnd   0xDFFF
134
135
136
/* offsets into validUTF8 table below */
137
static const int offsetUTF8Sequences[kMaxUTF8Bytes + 1] =
138
{
139
    0, /* 1 byte */
140
    1, /* 2 bytes */
141
    2, /* 3 bytes */
142
    4, /* 4 bytes */
143
    kNumUTF8Sequences /* must be last */
144
};
145
146
static const struct validUTF8Sequence
147
{
148
     uint lowChar;
149
     uint highChar;
150
     int  numBytes;
151
     byte validBytes[8];
152
} validUTF8[kNumUTF8Sequences] =
153
{
154
/*   low       high   #bytes  byte 1      byte 2      byte 3      byte 4 */
155
    {0x0000,   0x007F,   1, {0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
156
    {0x0080,   0x07FF,   2, {0xC2, 0xDF, 0x80, 0xBF, 0x00, 0x00, 0x00, 0x00}},
157
    {0x0800,   0x0FFF,   3, {0xE0, 0xE0, 0xA0, 0xBF, 0x80, 0xBF, 0x00, 0x00}},
158
    {0x1000,   0xFFFF,   3, {0xE1, 0xEF, 0x80, 0xBF, 0x80, 0xBF, 0x00, 0x00}},
159
    {0x10000,  0x3FFFF,  4, {0xF0, 0xF0, 0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}},
160
    {0x40000,  0xFFFFF,  4, {0xF1, 0xF3, 0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}},
161
    {0x100000, 0x10FFFF, 4, {0xF4, 0xF4, 0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}} 
162
};
163
164
int TY_(DecodeUTF8BytesToChar)( uint* c, uint firstByte, ctmbstr successorBytes,
165
                                TidyInputSource* inp, int* count )
166
239M
{
167
239M
    byte tempbuf[10];
168
239M
    byte *buf = &tempbuf[0];
169
239M
    uint ch = 0, n = 0;
170
239M
    int i, bytes = 0;
171
239M
    Bool hasError = no;
172
    
173
239M
    if ( successorBytes )
174
170M
        buf = (byte*) successorBytes;
175
        
176
    /* special check if we have been passed an EOF char */
177
239M
    if ( firstByte == EndOfStream )
178
0
    {
179
        /* at present */
180
0
        *c = firstByte;
181
0
        *count = 1;
182
0
        return 0;
183
0
    }
184
185
239M
    ch = firstByte; /* first byte is passed in separately */
186
    
187
239M
    if (ch <= 0x7F) /* 0XXX XXXX one byte */
188
93.8M
    {
189
93.8M
        n = ch;
190
93.8M
        bytes = 1;
191
93.8M
    }
192
145M
    else if ((ch & 0xE0) == 0xC0)  /* 110X XXXX  two bytes */
193
545k
    {
194
545k
        n = ch & 31;
195
545k
        bytes = 2;
196
545k
    }
197
145M
    else if ((ch & 0xF0) == 0xE0)  /* 1110 XXXX  three bytes */
198
143M
    {
199
143M
        n = ch & 15;
200
143M
        bytes = 3;
201
143M
    }
202
1.68M
    else if ((ch & 0xF8) == 0xF0)  /* 1111 0XXX  four bytes */
203
184k
    {
204
184k
        n = ch & 7;
205
184k
        bytes = 4;
206
184k
    }
207
1.50M
    else if ((ch & 0xFC) == 0xF8)  /* 1111 10XX  five bytes */
208
34.2k
    {
209
34.2k
        n = ch & 3;
210
34.2k
        bytes = 5;
211
34.2k
        hasError = yes;
212
34.2k
    }
213
1.46M
    else if ((ch & 0xFE) == 0xFC)  /* 1111 110X  six bytes */
214
11.8k
    {
215
11.8k
        n = ch & 1;
216
11.8k
        bytes = 6;
217
11.8k
        hasError = yes;
218
11.8k
    }
219
1.45M
    else
220
1.45M
    {
221
        /* not a valid first byte of a UTF-8 sequence */
222
1.45M
        n = ch;
223
1.45M
        bytes = 1;
224
1.45M
        hasError = yes;
225
1.45M
    }
226
227
    /* successor bytes should have the form 10XX XXXX */
228
229
    /* If caller supplied buffer, use it.  Else see if caller
230
    ** supplied an input source, use that.
231
    */
232
239M
    if ( successorBytes )
233
170M
    {
234
455M
        for ( i=0; i < bytes-1; ++i )
235
286M
        {
236
286M
            if ( !buf[i] || (buf[i] & 0xC0) != 0x80 )
237
497k
            {
238
497k
                hasError = yes;
239
497k
                bytes = i+1;
240
497k
                break;
241
497k
            }
242
285M
            n = (n << 6) | (buf[i] & 0x3F);
243
285M
        }
244
170M
    }
245
69.1M
    else if ( inp )
246
69.1M
    {
247
69.4M
        for ( i=0; i < bytes-1 && !inp->eof(inp->sourceData); ++i )
248
996k
        {
249
996k
            int b = inp->getByte( inp->sourceData );
250
996k
            buf[i] = (tmbchar) b;
251
252
            /* End of data or illegal successor byte value */
253
996k
            if ( b == EOF || (buf[i] & 0xC0) != 0x80 )
254
670k
            {
255
670k
                hasError = yes;
256
670k
                bytes = i+1;
257
670k
                if ( b != EOF )
258
670k
                    inp->ungetByte( inp->sourceData, buf[i] );
259
670k
                break;
260
670k
            }
261
326k
            n = (n << 6) | (buf[i] & 0x3F);
262
326k
        }
263
69.1M
    }
264
0
    else if ( bytes > 1 )
265
0
    {
266
0
        hasError = yes;
267
0
        bytes = 1;
268
0
    }
269
    
270
239M
    if (!hasError && ((n == kUTF8ByteSwapNotAChar) || (n == kUTF8NotAChar)))
271
329
        hasError = yes;
272
        
273
239M
    if (!hasError && (n > kMaxUTF8FromUCS4))
274
400
        hasError = yes;
275
276
239M
    if (!hasError)
277
236M
    {
278
236M
        int lo, hi;
279
        
280
236M
        lo = offsetUTF8Sequences[bytes - 1];
281
236M
        hi = offsetUTF8Sequences[bytes] - 1;
282
        
283
        /* check for overlong sequences */
284
236M
        if ((n < validUTF8[lo].lowChar) || (n > validUTF8[hi].highChar))
285
10.7k
            hasError = yes;
286
236M
        else
287
236M
        {
288
236M
            hasError = yes; /* assume error until proven otherwise */
289
        
290
616M
            for (i = lo; i <= hi; i++)
291
379M
            {
292
379M
                int tempCount;
293
379M
                byte theByte;
294
                
295
980M
                for (tempCount = 0; tempCount < bytes; tempCount++)
296
717M
                {
297
717M
                    if (!tempCount)
298
379M
                        theByte = (tmbchar) firstByte;
299
337M
                    else
300
337M
                        theByte = buf[tempCount - 1];
301
                        
302
717M
                    if ( theByte >= validUTF8[i].validBytes[(tempCount * 2)] &&
303
691M
                         theByte <= validUTF8[i].validBytes[(tempCount * 2) + 1] )
304
574M
                        hasError = no;
305
717M
                    if (hasError)
306
116M
                        break;
307
717M
                }
308
379M
            }
309
236M
        }
310
236M
    }
311
312
#if 1 && defined(_DEBUG)
313
    if ( hasError )
314
    {
315
       /* debug */
316
       fprintf( stderr, "UTF-8 decoding error of %d bytes : ", bytes );
317
       fprintf( stderr, "0x%02x ", firstByte );
318
       for (i = 1; i < bytes; i++)
319
           fprintf( stderr, "0x%02x ", buf[i - 1] );
320
       fprintf( stderr, " = U+%04X\n", n );
321
    }
322
#endif
323
324
239M
    *count = bytes;
325
239M
    *c = n;
326
239M
    if ( hasError )
327
2.63M
        return -1;
328
236M
    return 0;
329
239M
}
330
331
int TY_(EncodeCharToUTF8Bytes)( uint c, tmbstr encodebuf,
332
                                TidyOutputSink* outp, int* count )
333
913M
{
334
913M
    byte tempbuf[10] = {0};
335
913M
    byte* buf = &tempbuf[0];
336
913M
    int bytes = 0;
337
913M
    Bool hasError = no;
338
    
339
913M
    if ( encodebuf )
340
552M
        buf = (byte*) encodebuf;
341
        
342
913M
    if (c <= 0x7F)  /* 0XXX XXXX one byte */
343
769M
    {
344
769M
        buf[0] = (tmbchar) c;
345
769M
        bytes = 1;
346
769M
    }
347
143M
    else if (c <= 0x7FF)  /* 110X XXXX  two bytes */
348
115k
    {
349
115k
        buf[0] = (tmbchar) ( 0xC0 | (c >> 6) );
350
115k
        buf[1] = (tmbchar) ( 0x80 | (c & 0x3F) );
351
115k
        bytes = 2;
352
115k
    }
353
143M
    else if (c <= 0xFFFF)  /* 1110 XXXX  three bytes */
354
143M
    {
355
143M
        buf[0] = (tmbchar) (0xE0 | (c >> 12));
356
143M
        buf[1] = (tmbchar) (0x80 | ((c >> 6) & 0x3F));
357
143M
        buf[2] = (tmbchar) (0x80 | (c & 0x3F));
358
143M
        bytes = 3;
359
143M
        if ( c == kUTF8ByteSwapNotAChar || c == kUTF8NotAChar )
360
299
            hasError = yes;
361
143M
    }
362
159k
    else if (c <= 0x1FFFFF)  /* 1111 0XXX  four bytes */
363
39.1k
    {
364
39.1k
        buf[0] = (tmbchar) (0xF0 | (c >> 18));
365
39.1k
        buf[1] = (tmbchar) (0x80 | ((c >> 12) & 0x3F));
366
39.1k
        buf[2] = (tmbchar) (0x80 | ((c >> 6) & 0x3F));
367
39.1k
        buf[3] = (tmbchar) (0x80 | (c & 0x3F));
368
39.1k
        bytes = 4;
369
39.1k
        if (c > kMaxUTF8FromUCS4)
370
160
            hasError = yes;
371
39.1k
    }
372
120k
    else if (c <= 0x3FFFFFF)  /* 1111 10XX  five bytes */
373
201
    {
374
201
        buf[0] = (tmbchar) (0xF8 | (c >> 24));
375
201
        buf[1] = (tmbchar) (0x80 | (c >> 18));
376
201
        buf[2] = (tmbchar) (0x80 | ((c >> 12) & 0x3F));
377
201
        buf[3] = (tmbchar) (0x80 | ((c >> 6) & 0x3F));
378
201
        buf[4] = (tmbchar) (0x80 | (c & 0x3F));
379
201
        bytes = 5;
380
201
        hasError = yes;
381
201
    }
382
120k
    else if (c <= 0x7FFFFFFF)  /* 1111 110X  six bytes */
383
138
    {
384
138
        buf[0] = (tmbchar) (0xFC | (c >> 30));
385
138
        buf[1] = (tmbchar) (0x80 | ((c >> 24) & 0x3F));
386
138
        buf[2] = (tmbchar) (0x80 | ((c >> 18) & 0x3F));
387
138
        buf[3] = (tmbchar) (0x80 | ((c >> 12) & 0x3F));
388
138
        buf[4] = (tmbchar) (0x80 | ((c >> 6) & 0x3F));
389
138
        buf[5] = (tmbchar) (0x80 | (c & 0x3F));
390
138
        bytes = 6;
391
138
        hasError = yes;
392
138
    }
393
120k
    else
394
120k
        hasError = yes;
395
        
396
    /* don't output invalid UTF-8 byte sequence to a stream */
397
913M
    if ( !hasError && outp != NULL )
398
360M
    {
399
360M
        int ix;
400
797M
        for ( ix=0; ix < bytes; ++ix )
401
437M
          outp->putByte( outp->sinkData, buf[ix] );
402
360M
    }
403
404
#if 1 && defined(_DEBUG)
405
    if ( hasError )
406
    {
407
        int i;
408
        fprintf( stderr, "UTF-8 encoding error for U+%x : ", c );
409
        for (i = 0; i < bytes; i++)
410
            fprintf( stderr, "0x%02x ", buf[i] );
411
        fprintf( stderr, "\n" );
412
    }
413
#endif
414
    
415
913M
    *count = bytes;
416
913M
    if (hasError)
417
121k
        return -1;
418
913M
    return 0;
419
913M
}
420
421
422
/* return one less than the number of bytes used by the UTF-8 byte sequence */
423
/* str points to the UTF-8 byte sequence */
424
/* the Unicode char is returned in *ch */
425
uint TY_(GetUTF8)( ctmbstr str, uint *ch )
426
170M
{
427
170M
    uint n;
428
170M
    int bytes;
429
430
170M
    int err;
431
    
432
170M
    bytes = 0;
433
    
434
    /* first byte "str[0]" is passed in separately from the */
435
    /* rest of the UTF-8 byte sequence starting at "str[1]" */
436
170M
    err = TY_(DecodeUTF8BytesToChar)( &n, str[0], str+1, NULL, &bytes );
437
170M
    if (err)
438
869k
    {
439
#if 1 && defined(_DEBUG)
440
        fprintf(stderr, "pprint UTF-8 decoding error for U+%x : ", n);
441
#endif
442
869k
        n = 0xFFFD; /* replacement char */
443
869k
    }
444
445
170M
    *ch = n;
446
170M
    return bytes - 1;
447
170M
}
448
449
/* store char c as UTF-8 encoded byte stream */
450
tmbstr TY_(PutUTF8)( tmbstr buf, uint c )
451
221M
{
452
221M
    int err, count = 0;
453
        
454
221M
    err = TY_(EncodeCharToUTF8Bytes)( c, buf, NULL, &count );
455
221M
    if (err)
456
0
    {
457
#if 1 && defined(_DEBUG)
458
        fprintf(stderr, "pprint UTF-8 encoding error for U+%x : ", c);
459
#endif
460
        /* replacement char 0xFFFD encoded as UTF-8 */
461
0
        buf[0] = (byte) 0xEF;
462
0
        buf[1] = (byte) 0xBF;
463
0
        buf[2] = (byte) 0xBD;
464
0
        count = 3;
465
0
    }
466
    
467
221M
    buf += count;
468
221M
    return buf;
469
221M
}
470
471
Bool    TY_(IsValidUTF16FromUCS4)( tchar ucs4 )
472
8.15k
{
473
8.15k
  return ( ucs4 <= kMaxUTF16FromUCS4 );
474
8.15k
}
475
476
Bool    TY_(IsHighSurrogate)( tchar ch )
477
23.1k
{
478
23.1k
    return ( ch >= kUTF16HighSurrogateBegin && ch <= kUTF16HighSurrogateEnd );
479
23.1k
}
480
Bool    TY_(IsLowSurrogate)( tchar ch )
481
33.5k
{
482
33.5k
    return ( ch >= kUTF16LowSurrogateBegin && ch <= kUTF16LowSurrogateEnd );
483
33.5k
}
484
485
tchar   TY_(CombineSurrogatePair)( tchar high, tchar low )
486
993
{
487
993
    assert( TY_(IsHighSurrogate)(high) && TY_(IsLowSurrogate)(low) );
488
993
    return ( ((low - kUTF16LowSurrogateBegin) * 0x400) + 
489
993
             high - kUTF16HighSurrogateBegin + 0x10000 );
490
993
}
491
492
Bool   TY_(SplitSurrogatePair)( tchar utf16, tchar* low, tchar* high )
493
0
{
494
0
    Bool status = ( TY_(IsValidCombinedChar)( utf16 ) && high && low );
495
0
    if ( status )
496
0
    {
497
0
        *low  = (utf16 - kUTF16SurrogatesBegin) / 0x400 + kUTF16LowSurrogateBegin;
498
0
        *high = (utf16 - kUTF16SurrogatesBegin) % 0x400 + kUTF16HighSurrogateBegin;
499
0
    }
500
0
    return status;
501
0
}
502
503
Bool    TY_(IsValidCombinedChar)( tchar ch )
504
993
{
505
993
    return ( ch >= kUTF16SurrogatesBegin &&
506
993
             (ch & 0x0000FFFE) != 0x0000FFFE &&
507
213
             (ch & 0x0000FFFF) != 0x0000FFFF );
508
993
}
509
510
Bool    TY_(IsCombinedChar)( tchar ch )
511
0
{
512
0
    return ( ch >= kUTF16SurrogatesBegin );
513
0
}
514
515
/*
516
 * local variables:
517
 * mode: c
518
 * indent-tabs-mode: nil
519
 * c-basic-offset: 4
520
 * eval: (c-set-offset 'substatement-open 0)
521
 * end:
522
 */