Coverage Report

Created: 2026-11-09 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgres/src/common/wchar.c
Line
Count
Source
1
/*-------------------------------------------------------------------------
2
 *
3
 * wchar.c
4
 *    Functions for working with multibyte characters in various encodings.
5
 *
6
 * Portions Copyright (c) 1998-2026, PostgreSQL Global Development Group
7
 *
8
 * IDENTIFICATION
9
 *    src/common/wchar.c
10
 *
11
 *-------------------------------------------------------------------------
12
 */
13
#include "c.h"
14
15
#include <limits.h>
16
17
#include "mb/pg_wchar.h"
18
#include "utils/ascii.h"
19
20
21
/*
22
 * In today's multibyte encodings other than UTF8, this two-byte sequence
23
 * ensures pg_encoding_mblen() == 2 && pg_encoding_verifymbstr() == 0.
24
 *
25
 * For historical reasons, several verifychar implementations opt to reject
26
 * this pair specifically.  Byte pair range constraints, in encoding
27
 * originator documentation, always excluded this pair.  No core conversion
28
 * could translate it.  However, longstanding verifychar implementations
29
 * accepted any non-NUL byte.  big5_to_euc_tw even translates pairs not
30
 * valid per encoding originator documentation.  To avoid tightening core
31
 * or non-core conversions in a security patch, we sought this one pair.
32
 *
33
 * PQescapeString() historically used spaces for BYTE1; many other values
34
 * could suffice for BYTE1.
35
 */
36
0
#define NONUTF8_INVALID_BYTE0 (0x8d)
37
0
#define NONUTF8_INVALID_BYTE1 (' ')
38
39
40
/* No error-reporting facility.  Ignore incomplete trailing byte sequence. */
41
0
#define MB2CHAR_NEED_AT_LEAST(len, need) if ((len) < (need)) break
42
43
/*
44
 * SQL/ASCII
45
 */
46
static int
47
pg_ascii2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
48
0
{
49
0
  int     cnt = 0;
50
51
0
  while (len > 0 && *from)
52
0
  {
53
0
    *to++ = *from++;
54
0
    len--;
55
0
    cnt++;
56
0
  }
57
0
  *to = 0;
58
0
  return cnt;
59
0
}
60
61
static int
62
pg_ascii_mblen(const unsigned char *s)
63
54
{
64
54
  return 1;
65
54
}
66
67
static int
68
pg_ascii_dsplen(const unsigned char *s)
69
0
{
70
0
  if (*s == '\0')
71
0
    return 0;
72
0
  if (*s < 0x20 || *s == 0x7f)
73
0
    return -1;
74
75
0
  return 1;
76
0
}
77
78
/*
79
 * EUC
80
 */
81
static int
82
pg_euc2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
83
0
{
84
0
  int     cnt = 0;
85
86
0
  while (len > 0 && *from)
87
0
  {
88
0
    if (*from == SS2)   /* JIS X 0201 (so called "1 byte KANA") */
89
0
    {
90
0
      MB2CHAR_NEED_AT_LEAST(len, 2);
91
0
      from++;
92
0
      *to = (SS2 << 8) | *from++;
93
0
      len -= 2;
94
0
    }
95
0
    else if (*from == SS3) /* JIS X 0212 KANJI */
96
0
    {
97
0
      MB2CHAR_NEED_AT_LEAST(len, 3);
98
0
      from++;
99
0
      *to = (SS3 << 16) | (*from++ << 8);
100
0
      *to |= *from++;
101
0
      len -= 3;
102
0
    }
103
0
    else if (IS_HIGHBIT_SET(*from)) /* JIS X 0208 KANJI */
104
0
    {
105
0
      MB2CHAR_NEED_AT_LEAST(len, 2);
106
0
      *to = *from++ << 8;
107
0
      *to |= *from++;
108
0
      len -= 2;
109
0
    }
110
0
    else          /* must be ASCII */
111
0
    {
112
0
      *to = *from++;
113
0
      len--;
114
0
    }
115
0
    to++;
116
0
    cnt++;
117
0
  }
118
0
  *to = 0;
119
0
  return cnt;
120
0
}
121
122
static inline int
123
pg_euc_mblen(const unsigned char *s)
124
0
{
125
0
  int     len;
126
127
0
  if (*s == SS2)
128
0
    len = 2;
129
0
  else if (*s == SS3)
130
0
    len = 3;
131
0
  else if (IS_HIGHBIT_SET(*s))
132
0
    len = 2;
133
0
  else
134
0
    len = 1;
135
0
  return len;
136
0
}
137
138
static inline int
139
pg_euc_dsplen(const unsigned char *s)
140
0
{
141
0
  int     len;
142
143
0
  if (*s == SS2)
144
0
    len = 2;
145
0
  else if (*s == SS3)
146
0
    len = 2;
147
0
  else if (IS_HIGHBIT_SET(*s))
148
0
    len = 2;
149
0
  else
150
0
    len = pg_ascii_dsplen(s);
151
0
  return len;
152
0
}
153
154
/*
155
 * EUC_JP
156
 */
157
static int
158
pg_eucjp2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
159
0
{
160
0
  return pg_euc2wchar_with_len(from, to, len);
161
0
}
162
163
static int
164
pg_eucjp_mblen(const unsigned char *s)
165
0
{
166
0
  return pg_euc_mblen(s);
167
0
}
168
169
static int
170
pg_eucjp_dsplen(const unsigned char *s)
171
0
{
172
0
  int     len;
173
174
0
  if (*s == SS2)
175
0
    len = 1;
176
0
  else if (*s == SS3)
177
0
    len = 2;
178
0
  else if (IS_HIGHBIT_SET(*s))
179
0
    len = 2;
180
0
  else
181
0
    len = pg_ascii_dsplen(s);
182
0
  return len;
183
0
}
184
185
/*
186
 * EUC_KR
187
 */
188
static int
189
pg_euckr2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
190
0
{
191
0
  return pg_euc2wchar_with_len(from, to, len);
192
0
}
193
194
static int
195
pg_euckr_mblen(const unsigned char *s)
196
0
{
197
0
  return pg_euc_mblen(s);
198
0
}
199
200
static int
201
pg_euckr_dsplen(const unsigned char *s)
202
0
{
203
0
  return pg_euc_dsplen(s);
204
0
}
205
206
/*
207
 * EUC_CN
208
 *
209
 */
210
static int
211
pg_euccn2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
212
0
{
213
0
  int     cnt = 0;
214
215
0
  while (len > 0 && *from)
216
0
  {
217
0
    if (*from == SS2)   /* code set 2 (unused?) */
218
0
    {
219
0
      MB2CHAR_NEED_AT_LEAST(len, 3);
220
0
      from++;
221
0
      *to = (SS2 << 16) | (*from++ << 8);
222
0
      *to |= *from++;
223
0
      len -= 3;
224
0
    }
225
0
    else if (*from == SS3) /* code set 3 (unused ?) */
226
0
    {
227
0
      MB2CHAR_NEED_AT_LEAST(len, 3);
228
0
      from++;
229
0
      *to = (SS3 << 16) | (*from++ << 8);
230
0
      *to |= *from++;
231
0
      len -= 3;
232
0
    }
233
0
    else if (IS_HIGHBIT_SET(*from)) /* code set 1 */
234
0
    {
235
0
      MB2CHAR_NEED_AT_LEAST(len, 2);
236
0
      *to = *from++ << 8;
237
0
      *to |= *from++;
238
0
      len -= 2;
239
0
    }
240
0
    else
241
0
    {
242
0
      *to = *from++;
243
0
      len--;
244
0
    }
245
0
    to++;
246
0
    cnt++;
247
0
  }
248
0
  *to = 0;
249
0
  return cnt;
250
0
}
251
252
/*
253
 * mbverifychar does not accept SS2 or SS3 (CS2 and CS3 are not defined for
254
 * EUC_CN), but mb2wchar_with_len does.  Tell a coherent story for code that
255
 * relies on agreement between mb2wchar_with_len and mblen.  Invalid text
256
 * datums (e.g. from shared catalogs) reach this.
257
 */
258
static int
259
pg_euccn_mblen(const unsigned char *s)
260
0
{
261
0
  int     len;
262
263
0
  if (*s == SS2)
264
0
    len = 3;
265
0
  else if (*s == SS3)
266
0
    len = 3;
267
0
  else if (IS_HIGHBIT_SET(*s))
268
0
    len = 2;
269
0
  else
270
0
    len = 1;
271
0
  return len;
272
0
}
273
274
static int
275
pg_euccn_dsplen(const unsigned char *s)
276
0
{
277
0
  int     len;
278
279
0
  if (IS_HIGHBIT_SET(*s))
280
0
    len = 2;
281
0
  else
282
0
    len = pg_ascii_dsplen(s);
283
0
  return len;
284
0
}
285
286
/*
287
 * EUC_TW
288
 *
289
 */
290
static int
291
pg_euctw2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
292
0
{
293
0
  int     cnt = 0;
294
295
0
  while (len > 0 && *from)
296
0
  {
297
0
    if (*from == SS2)   /* code set 2 */
298
0
    {
299
0
      MB2CHAR_NEED_AT_LEAST(len, 4);
300
0
      from++;
301
0
      *to = (((uint32) SS2) << 24) | (*from++ << 16);
302
0
      *to |= *from++ << 8;
303
0
      *to |= *from++;
304
0
      len -= 4;
305
0
    }
306
0
    else if (*from == SS3) /* code set 3 (unused?) */
307
0
    {
308
0
      MB2CHAR_NEED_AT_LEAST(len, 3);
309
0
      from++;
310
0
      *to = (SS3 << 16) | (*from++ << 8);
311
0
      *to |= *from++;
312
0
      len -= 3;
313
0
    }
314
0
    else if (IS_HIGHBIT_SET(*from)) /* code set 2 */
315
0
    {
316
0
      MB2CHAR_NEED_AT_LEAST(len, 2);
317
0
      *to = *from++ << 8;
318
0
      *to |= *from++;
319
0
      len -= 2;
320
0
    }
321
0
    else
322
0
    {
323
0
      *to = *from++;
324
0
      len--;
325
0
    }
326
0
    to++;
327
0
    cnt++;
328
0
  }
329
0
  *to = 0;
330
0
  return cnt;
331
0
}
332
333
static int
334
pg_euctw_mblen(const unsigned char *s)
335
0
{
336
0
  int     len;
337
338
0
  if (*s == SS2)
339
0
    len = 4;
340
0
  else if (*s == SS3)
341
0
    len = 3;
342
0
  else if (IS_HIGHBIT_SET(*s))
343
0
    len = 2;
344
0
  else
345
0
    len = 1;
346
0
  return len;
347
0
}
348
349
static int
350
pg_euctw_dsplen(const unsigned char *s)
351
0
{
352
0
  int     len;
353
354
0
  if (*s == SS2)
355
0
    len = 2;
356
0
  else if (*s == SS3)
357
0
    len = 2;
358
0
  else if (IS_HIGHBIT_SET(*s))
359
0
    len = 2;
360
0
  else
361
0
    len = pg_ascii_dsplen(s);
362
0
  return len;
363
0
}
364
365
/*
366
 * Convert pg_wchar to EUC_* encoding.
367
 */
368
static int
369
pg_wchar2euc_with_len(const pg_wchar *from, unsigned char *to, int len)
370
0
{
371
0
  int     cnt = 0;
372
373
0
  while (len > 0 && *from)
374
0
  {
375
0
    unsigned char c;
376
377
0
    if ((c = (*from >> 24)))
378
0
    {
379
0
      *to++ = c;
380
0
      *to++ = (*from >> 16) & 0xff;
381
0
      *to++ = (*from >> 8) & 0xff;
382
0
      *to++ = *from & 0xff;
383
0
      cnt += 4;
384
0
    }
385
0
    else if ((c = (*from >> 16)))
386
0
    {
387
0
      *to++ = c;
388
0
      *to++ = (*from >> 8) & 0xff;
389
0
      *to++ = *from & 0xff;
390
0
      cnt += 3;
391
0
    }
392
0
    else if ((c = (*from >> 8)))
393
0
    {
394
0
      *to++ = c;
395
0
      *to++ = *from & 0xff;
396
0
      cnt += 2;
397
0
    }
398
0
    else
399
0
    {
400
0
      *to++ = *from;
401
0
      cnt++;
402
0
    }
403
0
    from++;
404
0
    len--;
405
0
  }
406
0
  *to = 0;
407
0
  return cnt;
408
0
}
409
410
411
/*
412
 * JOHAB
413
 */
414
static int
415
pg_johab_mblen(const unsigned char *s)
416
0
{
417
0
  return pg_euc_mblen(s);
418
0
}
419
420
static int
421
pg_johab_dsplen(const unsigned char *s)
422
0
{
423
0
  return pg_euc_dsplen(s);
424
0
}
425
426
/*
427
 * convert UTF8 string to pg_wchar (UCS-4)
428
 */
429
static int
430
pg_utf2wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
431
0
{
432
0
  int     cnt = 0;
433
0
  uint32    c1,
434
0
        c2,
435
0
        c3,
436
0
        c4;
437
438
0
  while (len > 0 && *from)
439
0
  {
440
0
    if ((*from & 0x80) == 0)
441
0
    {
442
0
      *to = *from++;
443
0
      len--;
444
0
    }
445
0
    else if ((*from & 0xe0) == 0xc0)
446
0
    {
447
0
      MB2CHAR_NEED_AT_LEAST(len, 2);
448
0
      c1 = *from++ & 0x1f;
449
0
      c2 = *from++ & 0x3f;
450
0
      *to = (c1 << 6) | c2;
451
0
      len -= 2;
452
0
    }
453
0
    else if ((*from & 0xf0) == 0xe0)
454
0
    {
455
0
      MB2CHAR_NEED_AT_LEAST(len, 3);
456
0
      c1 = *from++ & 0x0f;
457
0
      c2 = *from++ & 0x3f;
458
0
      c3 = *from++ & 0x3f;
459
0
      *to = (c1 << 12) | (c2 << 6) | c3;
460
0
      len -= 3;
461
0
    }
462
0
    else if ((*from & 0xf8) == 0xf0)
463
0
    {
464
0
      MB2CHAR_NEED_AT_LEAST(len, 4);
465
0
      c1 = *from++ & 0x07;
466
0
      c2 = *from++ & 0x3f;
467
0
      c3 = *from++ & 0x3f;
468
0
      c4 = *from++ & 0x3f;
469
0
      *to = (c1 << 18) | (c2 << 12) | (c3 << 6) | c4;
470
0
      len -= 4;
471
0
    }
472
0
    else
473
0
    {
474
      /* treat a bogus char as length 1; not ours to raise error */
475
0
      *to = *from++;
476
0
      len--;
477
0
    }
478
0
    to++;
479
0
    cnt++;
480
0
  }
481
0
  *to = 0;
482
0
  return cnt;
483
0
}
484
485
486
/*
487
 * Trivial conversion from pg_wchar to UTF-8.
488
 */
489
static int
490
pg_wchar2utf_with_len(const pg_wchar *from, unsigned char *to, int len)
491
0
{
492
0
  int     cnt = 0;
493
494
0
  while (len > 0 && *from)
495
0
  {
496
0
    int     char_len;
497
498
0
    unicode_to_utf8(*from, to);
499
0
    char_len = pg_utf_mblen(to);
500
0
    cnt += char_len;
501
0
    to += char_len;
502
0
    from++;
503
0
    len--;
504
0
  }
505
0
  *to = 0;
506
0
  return cnt;
507
0
}
508
509
/*
510
 * Return the byte length of a UTF8 character pointed to by s
511
 *
512
 * Note: in the current implementation we do not support UTF8 sequences
513
 * of more than 4 bytes; hence do NOT return a value larger than 4.
514
 * We return "1" for any leading byte that is either flat-out illegal or
515
 * indicates a length larger than we support.
516
 *
517
 * pg_utf2wchar_with_len(), utf8_to_unicode(), pg_utf8_islegal(), and perhaps
518
 * other places would need to be fixed to change this.
519
 */
520
int
521
pg_utf_mblen(const unsigned char *s)
522
625
{
523
625
  int     len;
524
525
625
  if ((*s & 0x80) == 0)
526
523
    len = 1;
527
102
  else if ((*s & 0xe0) == 0xc0)
528
5
    len = 2;
529
97
  else if ((*s & 0xf0) == 0xe0)
530
6
    len = 3;
531
91
  else if ((*s & 0xf8) == 0xf0)
532
58
    len = 4;
533
#ifdef NOT_USED
534
  else if ((*s & 0xfc) == 0xf8)
535
    len = 5;
536
  else if ((*s & 0xfe) == 0xfc)
537
    len = 6;
538
#endif
539
33
  else
540
33
    len = 1;
541
625
  return len;
542
625
}
543
544
/*
545
 * This is an implementation of wcwidth() and wcswidth() as defined in
546
 * "The Single UNIX Specification, Version 2, The Open Group, 1997"
547
 * <http://www.unix.org/online.html>
548
 *
549
 * Markus Kuhn -- 2001-09-08 -- public domain
550
 *
551
 * customised for PostgreSQL
552
 *
553
 * original available at : http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
554
 */
555
556
struct mbinterval
557
{
558
  unsigned int first;
559
  unsigned int last;
560
};
561
562
/* auxiliary function for binary search in interval table */
563
static int
564
mbbisearch(pg_wchar ucs, const struct mbinterval *table, int max)
565
0
{
566
0
  int     min = 0;
567
0
  int     mid;
568
569
0
  if (ucs < table[0].first || ucs > table[max].last)
570
0
    return 0;
571
0
  while (max >= min)
572
0
  {
573
0
    mid = (min + max) / 2;
574
0
    if (ucs > table[mid].last)
575
0
      min = mid + 1;
576
0
    else if (ucs < table[mid].first)
577
0
      max = mid - 1;
578
0
    else
579
0
      return 1;
580
0
  }
581
582
0
  return 0;
583
0
}
584
585
586
/*
587
 * The following functions define the column width of an ISO 10646
588
 * character as follows:
589
 *
590
 *    - The null character (U+0000) has a column width of 0.
591
 *
592
 *    - Other C0/C1 control characters and DEL will lead to a return
593
 *    value of -1.
594
 *
595
 *    - Non-spacing and enclosing combining characters (general
596
 *    category code Mn, Me or Cf in the Unicode database) have a
597
 *    column width of 0.
598
 *
599
 *    - Spacing characters in the East Asian Wide (W) or East Asian
600
 *    FullWidth (F) category as defined in Unicode Technical
601
 *    Report #11 have a column width of 2.
602
 *
603
 *    - All remaining characters (including all printable
604
 *    ISO 8859-1 and WGL4 characters, Unicode control characters,
605
 *    etc.) have a column width of 1.
606
 *
607
 * This implementation assumes that wchar_t characters are encoded
608
 * in ISO 10646.
609
 */
610
611
static int
612
ucs_wcwidth(pg_wchar ucs)
613
0
{
614
0
#include "common/unicode_nonspacing_table.h"
615
0
#include "common/unicode_east_asian_fw_table.h"
616
617
  /* test for 8-bit control characters */
618
0
  if (ucs == 0)
619
0
    return 0;
620
621
0
  if (ucs < 0x20 || (ucs >= 0x7f && ucs < 0xa0) || ucs > 0x0010ffff)
622
0
    return -1;
623
624
  /*
625
   * binary search in table of non-spacing characters
626
   *
627
   * XXX: In the official Unicode sources, it is possible for a character to
628
   * be described as both non-spacing and wide at the same time. As of
629
   * Unicode 13.0, treating the non-spacing property as the determining
630
   * factor for display width leads to the correct behavior, so do that
631
   * search first.
632
   */
633
0
  if (mbbisearch(ucs, nonspacing,
634
0
           sizeof(nonspacing) / sizeof(struct mbinterval) - 1))
635
0
    return 0;
636
637
  /* binary search in table of wide characters */
638
0
  if (mbbisearch(ucs, east_asian_fw,
639
0
           sizeof(east_asian_fw) / sizeof(struct mbinterval) - 1))
640
0
    return 2;
641
642
0
  return 1;
643
0
}
644
645
static int
646
pg_utf_dsplen(const unsigned char *s)
647
0
{
648
0
  return ucs_wcwidth(utf8_to_unicode(s));
649
0
}
650
651
/*
652
 * ISO8859-1
653
 */
654
static int
655
pg_latin12wchar_with_len(const unsigned char *from, pg_wchar *to, int len)
656
0
{
657
0
  int     cnt = 0;
658
659
0
  while (len > 0 && *from)
660
0
  {
661
0
    *to++ = *from++;
662
0
    len--;
663
0
    cnt++;
664
0
  }
665
0
  *to = 0;
666
0
  return cnt;
667
0
}
668
669
/*
670
 * Trivial conversion from pg_wchar to single byte encoding. Just ignores
671
 * high bits.
672
 */
673
static int
674
pg_wchar2single_with_len(const pg_wchar *from, unsigned char *to, int len)
675
0
{
676
0
  int     cnt = 0;
677
678
0
  while (len > 0 && *from)
679
0
  {
680
0
    *to++ = *from++;
681
0
    len--;
682
0
    cnt++;
683
0
  }
684
0
  *to = 0;
685
0
  return cnt;
686
0
}
687
688
static int
689
pg_latin1_mblen(const unsigned char *s)
690
0
{
691
0
  return 1;
692
0
}
693
694
static int
695
pg_latin1_dsplen(const unsigned char *s)
696
0
{
697
0
  return pg_ascii_dsplen(s);
698
0
}
699
700
/*
701
 * SJIS
702
 */
703
static int
704
pg_sjis_mblen(const unsigned char *s)
705
0
{
706
0
  int     len;
707
708
0
  if (*s >= 0xa1 && *s <= 0xdf)
709
0
    len = 1;       /* 1 byte kana? */
710
0
  else if (IS_HIGHBIT_SET(*s))
711
0
    len = 2;       /* kanji? */
712
0
  else
713
0
    len = 1;       /* should be ASCII */
714
0
  return len;
715
0
}
716
717
static int
718
pg_sjis_dsplen(const unsigned char *s)
719
0
{
720
0
  int     len;
721
722
0
  if (*s >= 0xa1 && *s <= 0xdf)
723
0
    len = 1;       /* 1 byte kana? */
724
0
  else if (IS_HIGHBIT_SET(*s))
725
0
    len = 2;       /* kanji? */
726
0
  else
727
0
    len = pg_ascii_dsplen(s); /* should be ASCII */
728
0
  return len;
729
0
}
730
731
/*
732
 * Big5
733
 */
734
static int
735
pg_big5_mblen(const unsigned char *s)
736
0
{
737
0
  int     len;
738
739
0
  if (IS_HIGHBIT_SET(*s))
740
0
    len = 2;       /* kanji? */
741
0
  else
742
0
    len = 1;       /* should be ASCII */
743
0
  return len;
744
0
}
745
746
static int
747
pg_big5_dsplen(const unsigned char *s)
748
0
{
749
0
  int     len;
750
751
0
  if (IS_HIGHBIT_SET(*s))
752
0
    len = 2;       /* kanji? */
753
0
  else
754
0
    len = pg_ascii_dsplen(s); /* should be ASCII */
755
0
  return len;
756
0
}
757
758
/*
759
 * GBK
760
 */
761
static int
762
pg_gbk_mblen(const unsigned char *s)
763
0
{
764
0
  int     len;
765
766
0
  if (IS_HIGHBIT_SET(*s))
767
0
    len = 2;       /* kanji? */
768
0
  else
769
0
    len = 1;       /* should be ASCII */
770
0
  return len;
771
0
}
772
773
static int
774
pg_gbk_dsplen(const unsigned char *s)
775
0
{
776
0
  int     len;
777
778
0
  if (IS_HIGHBIT_SET(*s))
779
0
    len = 2;       /* kanji? */
780
0
  else
781
0
    len = pg_ascii_dsplen(s); /* should be ASCII */
782
0
  return len;
783
0
}
784
785
/*
786
 * UHC
787
 */
788
static int
789
pg_uhc_mblen(const unsigned char *s)
790
0
{
791
0
  int     len;
792
793
0
  if (IS_HIGHBIT_SET(*s))
794
0
    len = 2;       /* 2byte? */
795
0
  else
796
0
    len = 1;       /* should be ASCII */
797
0
  return len;
798
0
}
799
800
static int
801
pg_uhc_dsplen(const unsigned char *s)
802
0
{
803
0
  int     len;
804
805
0
  if (IS_HIGHBIT_SET(*s))
806
0
    len = 2;       /* 2byte? */
807
0
  else
808
0
    len = pg_ascii_dsplen(s); /* should be ASCII */
809
0
  return len;
810
0
}
811
812
/*
813
 * GB18030
814
 *  Added by Bill Huang <bhuang@redhat.com>,<bill_huanghb@ybb.ne.jp>
815
 */
816
817
/*
818
 * Unlike all other mblen() functions, this also looks at the second byte of
819
 * the input.  However, if you only pass the first byte of a multi-byte
820
 * string, and \0 as the second byte, this still works in a predictable way:
821
 * a 4-byte character will be reported as two 2-byte characters.  That's
822
 * enough for all current uses, as a client-only encoding.  It works that
823
 * way, because in any valid 4-byte GB18030-encoded character, the third and
824
 * fourth byte look like a 2-byte encoded character, when looked at
825
 * separately.
826
 */
827
static int
828
pg_gb18030_mblen(const unsigned char *s)
829
0
{
830
0
  int     len;
831
832
0
  if (!IS_HIGHBIT_SET(*s))
833
0
    len = 1;       /* ASCII */
834
0
  else if (*(s + 1) >= 0x30 && *(s + 1) <= 0x39)
835
0
    len = 4;
836
0
  else
837
0
    len = 2;
838
0
  return len;
839
0
}
840
841
static int
842
pg_gb18030_dsplen(const unsigned char *s)
843
0
{
844
0
  int     len;
845
846
0
  if (IS_HIGHBIT_SET(*s))
847
0
    len = 2;
848
0
  else
849
0
    len = pg_ascii_dsplen(s); /* ASCII */
850
0
  return len;
851
0
}
852
853
/*
854
 *-------------------------------------------------------------------
855
 * multibyte sequence validators
856
 *-------------------------------------------------------------------
857
 */
858
859
static int
860
pg_ascii_verifychar(const unsigned char *s, int len)
861
0
{
862
0
  return 1;
863
0
}
864
865
static int
866
pg_ascii_verifystr(const unsigned char *s, int len)
867
365
{
868
365
  const unsigned char *nullpos = memchr(s, 0, len);
869
870
365
  if (nullpos == NULL)
871
311
    return len;
872
54
  else
873
54
    return nullpos - s;
874
365
}
875
876
0
#define IS_EUC_RANGE_VALID(c) ((c) >= 0xa1 && (c) <= 0xfe)
877
878
static int
879
pg_eucjp_verifychar(const unsigned char *s, int len)
880
0
{
881
0
  int     l;
882
0
  unsigned char c1,
883
0
        c2;
884
885
0
  c1 = *s++;
886
887
0
  switch (c1)
888
0
  {
889
0
    case SS2:       /* JIS X 0201 */
890
0
      l = 2;
891
0
      if (l > len)
892
0
        return -1;
893
0
      c2 = *s++;
894
0
      if (c2 < 0xa1 || c2 > 0xdf)
895
0
        return -1;
896
0
      break;
897
898
0
    case SS3:       /* JIS X 0212 */
899
0
      l = 3;
900
0
      if (l > len)
901
0
        return -1;
902
0
      c2 = *s++;
903
0
      if (!IS_EUC_RANGE_VALID(c2))
904
0
        return -1;
905
0
      c2 = *s++;
906
0
      if (!IS_EUC_RANGE_VALID(c2))
907
0
        return -1;
908
0
      break;
909
910
0
    default:
911
0
      if (IS_HIGHBIT_SET(c1)) /* JIS X 0208? */
912
0
      {
913
0
        l = 2;
914
0
        if (l > len)
915
0
          return -1;
916
0
        if (!IS_EUC_RANGE_VALID(c1))
917
0
          return -1;
918
0
        c2 = *s++;
919
0
        if (!IS_EUC_RANGE_VALID(c2))
920
0
          return -1;
921
0
      }
922
0
      else
923
        /* must be ASCII */
924
0
      {
925
0
        l = 1;
926
0
      }
927
0
      break;
928
0
  }
929
930
0
  return l;
931
0
}
932
933
static int
934
pg_eucjp_verifystr(const unsigned char *s, int len)
935
0
{
936
0
  const unsigned char *start = s;
937
938
0
  while (len > 0)
939
0
  {
940
0
    int     l;
941
942
    /* fast path for ASCII-subset characters */
943
0
    if (!IS_HIGHBIT_SET(*s))
944
0
    {
945
0
      if (*s == '\0')
946
0
        break;
947
0
      l = 1;
948
0
    }
949
0
    else
950
0
    {
951
0
      l = pg_eucjp_verifychar(s, len);
952
0
      if (l == -1)
953
0
        break;
954
0
    }
955
0
    s += l;
956
0
    len -= l;
957
0
  }
958
959
0
  return s - start;
960
0
}
961
962
static int
963
pg_euckr_verifychar(const unsigned char *s, int len)
964
0
{
965
0
  int     l;
966
0
  unsigned char c1,
967
0
        c2;
968
969
0
  c1 = *s++;
970
971
0
  if (IS_HIGHBIT_SET(c1))
972
0
  {
973
0
    l = 2;
974
0
    if (l > len)
975
0
      return -1;
976
0
    if (!IS_EUC_RANGE_VALID(c1))
977
0
      return -1;
978
0
    c2 = *s++;
979
0
    if (!IS_EUC_RANGE_VALID(c2))
980
0
      return -1;
981
0
  }
982
0
  else
983
    /* must be ASCII */
984
0
  {
985
0
    l = 1;
986
0
  }
987
988
0
  return l;
989
0
}
990
991
static int
992
pg_euckr_verifystr(const unsigned char *s, int len)
993
0
{
994
0
  const unsigned char *start = s;
995
996
0
  while (len > 0)
997
0
  {
998
0
    int     l;
999
1000
    /* fast path for ASCII-subset characters */
1001
0
    if (!IS_HIGHBIT_SET(*s))
1002
0
    {
1003
0
      if (*s == '\0')
1004
0
        break;
1005
0
      l = 1;
1006
0
    }
1007
0
    else
1008
0
    {
1009
0
      l = pg_euckr_verifychar(s, len);
1010
0
      if (l == -1)
1011
0
        break;
1012
0
    }
1013
0
    s += l;
1014
0
    len -= l;
1015
0
  }
1016
1017
0
  return s - start;
1018
0
}
1019
1020
/* EUC-CN byte sequences are exactly same as EUC-KR */
1021
#define pg_euccn_verifychar pg_euckr_verifychar
1022
#define pg_euccn_verifystr  pg_euckr_verifystr
1023
1024
static int
1025
pg_euctw_verifychar(const unsigned char *s, int len)
1026
0
{
1027
0
  int     l;
1028
0
  unsigned char c1,
1029
0
        c2;
1030
1031
0
  c1 = *s++;
1032
1033
0
  switch (c1)
1034
0
  {
1035
0
    case SS2:       /* CNS 11643 Plane 1-7 */
1036
0
      l = 4;
1037
0
      if (l > len)
1038
0
        return -1;
1039
0
      c2 = *s++;
1040
0
      if (c2 < 0xa1 || c2 > 0xa7)
1041
0
        return -1;
1042
0
      c2 = *s++;
1043
0
      if (!IS_EUC_RANGE_VALID(c2))
1044
0
        return -1;
1045
0
      c2 = *s++;
1046
0
      if (!IS_EUC_RANGE_VALID(c2))
1047
0
        return -1;
1048
0
      break;
1049
1050
0
    case SS3:       /* unused */
1051
0
      return -1;
1052
1053
0
    default:
1054
0
      if (IS_HIGHBIT_SET(c1)) /* CNS 11643 Plane 1 */
1055
0
      {
1056
0
        l = 2;
1057
0
        if (l > len)
1058
0
          return -1;
1059
        /* no further range check on c1? */
1060
0
        c2 = *s++;
1061
0
        if (!IS_EUC_RANGE_VALID(c2))
1062
0
          return -1;
1063
0
      }
1064
0
      else
1065
        /* must be ASCII */
1066
0
      {
1067
0
        l = 1;
1068
0
      }
1069
0
      break;
1070
0
  }
1071
0
  return l;
1072
0
}
1073
1074
static int
1075
pg_euctw_verifystr(const unsigned char *s, int len)
1076
0
{
1077
0
  const unsigned char *start = s;
1078
1079
0
  while (len > 0)
1080
0
  {
1081
0
    int     l;
1082
1083
    /* fast path for ASCII-subset characters */
1084
0
    if (!IS_HIGHBIT_SET(*s))
1085
0
    {
1086
0
      if (*s == '\0')
1087
0
        break;
1088
0
      l = 1;
1089
0
    }
1090
0
    else
1091
0
    {
1092
0
      l = pg_euctw_verifychar(s, len);
1093
0
      if (l == -1)
1094
0
        break;
1095
0
    }
1096
0
    s += l;
1097
0
    len -= l;
1098
0
  }
1099
1100
0
  return s - start;
1101
0
}
1102
1103
static int
1104
pg_johab_verifychar(const unsigned char *s, int len)
1105
0
{
1106
0
  int     l,
1107
0
        mbl;
1108
0
  unsigned char c;
1109
1110
0
  l = mbl = pg_johab_mblen(s);
1111
1112
0
  if (len < l)
1113
0
    return -1;
1114
1115
0
  if (!IS_HIGHBIT_SET(*s))
1116
0
    return mbl;
1117
1118
0
  while (--l > 0)
1119
0
  {
1120
0
    c = *++s;
1121
0
    if (!IS_EUC_RANGE_VALID(c))
1122
0
      return -1;
1123
0
  }
1124
0
  return mbl;
1125
0
}
1126
1127
static int
1128
pg_johab_verifystr(const unsigned char *s, int len)
1129
0
{
1130
0
  const unsigned char *start = s;
1131
1132
0
  while (len > 0)
1133
0
  {
1134
0
    int     l;
1135
1136
    /* fast path for ASCII-subset characters */
1137
0
    if (!IS_HIGHBIT_SET(*s))
1138
0
    {
1139
0
      if (*s == '\0')
1140
0
        break;
1141
0
      l = 1;
1142
0
    }
1143
0
    else
1144
0
    {
1145
0
      l = pg_johab_verifychar(s, len);
1146
0
      if (l == -1)
1147
0
        break;
1148
0
    }
1149
0
    s += l;
1150
0
    len -= l;
1151
0
  }
1152
1153
0
  return s - start;
1154
0
}
1155
1156
static int
1157
pg_latin1_verifychar(const unsigned char *s, int len)
1158
0
{
1159
0
  return 1;
1160
0
}
1161
1162
static int
1163
pg_latin1_verifystr(const unsigned char *s, int len)
1164
0
{
1165
0
  const unsigned char *nullpos = memchr(s, 0, len);
1166
1167
0
  if (nullpos == NULL)
1168
0
    return len;
1169
0
  else
1170
0
    return nullpos - s;
1171
0
}
1172
1173
static int
1174
pg_sjis_verifychar(const unsigned char *s, int len)
1175
0
{
1176
0
  int     l,
1177
0
        mbl;
1178
0
  unsigned char c1,
1179
0
        c2;
1180
1181
0
  l = mbl = pg_sjis_mblen(s);
1182
1183
0
  if (len < l)
1184
0
    return -1;
1185
1186
0
  if (l == 1)         /* pg_sjis_mblen already verified it */
1187
0
    return mbl;
1188
1189
0
  c1 = *s++;
1190
0
  c2 = *s;
1191
0
  if (!ISSJISHEAD(c1) || !ISSJISTAIL(c2))
1192
0
    return -1;
1193
0
  return mbl;
1194
0
}
1195
1196
static int
1197
pg_sjis_verifystr(const unsigned char *s, int len)
1198
0
{
1199
0
  const unsigned char *start = s;
1200
1201
0
  while (len > 0)
1202
0
  {
1203
0
    int     l;
1204
1205
    /* fast path for ASCII-subset characters */
1206
0
    if (!IS_HIGHBIT_SET(*s))
1207
0
    {
1208
0
      if (*s == '\0')
1209
0
        break;
1210
0
      l = 1;
1211
0
    }
1212
0
    else
1213
0
    {
1214
0
      l = pg_sjis_verifychar(s, len);
1215
0
      if (l == -1)
1216
0
        break;
1217
0
    }
1218
0
    s += l;
1219
0
    len -= l;
1220
0
  }
1221
1222
0
  return s - start;
1223
0
}
1224
1225
static int
1226
pg_big5_verifychar(const unsigned char *s, int len)
1227
0
{
1228
0
  int     l,
1229
0
        mbl;
1230
1231
0
  l = mbl = pg_big5_mblen(s);
1232
1233
0
  if (len < l)
1234
0
    return -1;
1235
1236
0
  if (l == 2 &&
1237
0
    s[0] == NONUTF8_INVALID_BYTE0 &&
1238
0
    s[1] == NONUTF8_INVALID_BYTE1)
1239
0
    return -1;
1240
1241
0
  while (--l > 0)
1242
0
  {
1243
0
    if (*++s == '\0')
1244
0
      return -1;
1245
0
  }
1246
1247
0
  return mbl;
1248
0
}
1249
1250
static int
1251
pg_big5_verifystr(const unsigned char *s, int len)
1252
0
{
1253
0
  const unsigned char *start = s;
1254
1255
0
  while (len > 0)
1256
0
  {
1257
0
    int     l;
1258
1259
    /* fast path for ASCII-subset characters */
1260
0
    if (!IS_HIGHBIT_SET(*s))
1261
0
    {
1262
0
      if (*s == '\0')
1263
0
        break;
1264
0
      l = 1;
1265
0
    }
1266
0
    else
1267
0
    {
1268
0
      l = pg_big5_verifychar(s, len);
1269
0
      if (l == -1)
1270
0
        break;
1271
0
    }
1272
0
    s += l;
1273
0
    len -= l;
1274
0
  }
1275
1276
0
  return s - start;
1277
0
}
1278
1279
static int
1280
pg_gbk_verifychar(const unsigned char *s, int len)
1281
0
{
1282
0
  int     l,
1283
0
        mbl;
1284
1285
0
  l = mbl = pg_gbk_mblen(s);
1286
1287
0
  if (len < l)
1288
0
    return -1;
1289
1290
0
  if (l == 2 &&
1291
0
    s[0] == NONUTF8_INVALID_BYTE0 &&
1292
0
    s[1] == NONUTF8_INVALID_BYTE1)
1293
0
    return -1;
1294
1295
0
  while (--l > 0)
1296
0
  {
1297
0
    if (*++s == '\0')
1298
0
      return -1;
1299
0
  }
1300
1301
0
  return mbl;
1302
0
}
1303
1304
static int
1305
pg_gbk_verifystr(const unsigned char *s, int len)
1306
0
{
1307
0
  const unsigned char *start = s;
1308
1309
0
  while (len > 0)
1310
0
  {
1311
0
    int     l;
1312
1313
    /* fast path for ASCII-subset characters */
1314
0
    if (!IS_HIGHBIT_SET(*s))
1315
0
    {
1316
0
      if (*s == '\0')
1317
0
        break;
1318
0
      l = 1;
1319
0
    }
1320
0
    else
1321
0
    {
1322
0
      l = pg_gbk_verifychar(s, len);
1323
0
      if (l == -1)
1324
0
        break;
1325
0
    }
1326
0
    s += l;
1327
0
    len -= l;
1328
0
  }
1329
1330
0
  return s - start;
1331
0
}
1332
1333
static int
1334
pg_uhc_verifychar(const unsigned char *s, int len)
1335
0
{
1336
0
  int     l,
1337
0
        mbl;
1338
1339
0
  l = mbl = pg_uhc_mblen(s);
1340
1341
0
  if (len < l)
1342
0
    return -1;
1343
1344
0
  if (l == 2 &&
1345
0
    s[0] == NONUTF8_INVALID_BYTE0 &&
1346
0
    s[1] == NONUTF8_INVALID_BYTE1)
1347
0
    return -1;
1348
1349
0
  while (--l > 0)
1350
0
  {
1351
0
    if (*++s == '\0')
1352
0
      return -1;
1353
0
  }
1354
1355
0
  return mbl;
1356
0
}
1357
1358
static int
1359
pg_uhc_verifystr(const unsigned char *s, int len)
1360
0
{
1361
0
  const unsigned char *start = s;
1362
1363
0
  while (len > 0)
1364
0
  {
1365
0
    int     l;
1366
1367
    /* fast path for ASCII-subset characters */
1368
0
    if (!IS_HIGHBIT_SET(*s))
1369
0
    {
1370
0
      if (*s == '\0')
1371
0
        break;
1372
0
      l = 1;
1373
0
    }
1374
0
    else
1375
0
    {
1376
0
      l = pg_uhc_verifychar(s, len);
1377
0
      if (l == -1)
1378
0
        break;
1379
0
    }
1380
0
    s += l;
1381
0
    len -= l;
1382
0
  }
1383
1384
0
  return s - start;
1385
0
}
1386
1387
static int
1388
pg_gb18030_verifychar(const unsigned char *s, int len)
1389
0
{
1390
0
  int     l;
1391
1392
0
  if (!IS_HIGHBIT_SET(*s))
1393
0
    l = 1;         /* ASCII */
1394
0
  else if (len >= 4 && *(s + 1) >= 0x30 && *(s + 1) <= 0x39)
1395
0
  {
1396
    /* Should be 4-byte, validate remaining bytes */
1397
0
    if (*s >= 0x81 && *s <= 0xfe &&
1398
0
      *(s + 2) >= 0x81 && *(s + 2) <= 0xfe &&
1399
0
      *(s + 3) >= 0x30 && *(s + 3) <= 0x39)
1400
0
      l = 4;
1401
0
    else
1402
0
      l = -1;
1403
0
  }
1404
0
  else if (len >= 2 && *s >= 0x81 && *s <= 0xfe)
1405
0
  {
1406
    /* Should be 2-byte, validate */
1407
0
    if ((*(s + 1) >= 0x40 && *(s + 1) <= 0x7e) ||
1408
0
      (*(s + 1) >= 0x80 && *(s + 1) <= 0xfe))
1409
0
      l = 2;
1410
0
    else
1411
0
      l = -1;
1412
0
  }
1413
0
  else
1414
0
    l = -1;
1415
0
  return l;
1416
0
}
1417
1418
static int
1419
pg_gb18030_verifystr(const unsigned char *s, int len)
1420
0
{
1421
0
  const unsigned char *start = s;
1422
1423
0
  while (len > 0)
1424
0
  {
1425
0
    int     l;
1426
1427
    /* fast path for ASCII-subset characters */
1428
0
    if (!IS_HIGHBIT_SET(*s))
1429
0
    {
1430
0
      if (*s == '\0')
1431
0
        break;
1432
0
      l = 1;
1433
0
    }
1434
0
    else
1435
0
    {
1436
0
      l = pg_gb18030_verifychar(s, len);
1437
0
      if (l == -1)
1438
0
        break;
1439
0
    }
1440
0
    s += l;
1441
0
    len -= l;
1442
0
  }
1443
1444
0
  return s - start;
1445
0
}
1446
1447
static int
1448
pg_utf8_verifychar(const unsigned char *s, int len)
1449
0
{
1450
0
  int     l;
1451
1452
0
  if ((*s & 0x80) == 0)
1453
0
  {
1454
0
    if (*s == '\0')
1455
0
      return -1;
1456
0
    return 1;
1457
0
  }
1458
0
  else if ((*s & 0xe0) == 0xc0)
1459
0
    l = 2;
1460
0
  else if ((*s & 0xf0) == 0xe0)
1461
0
    l = 3;
1462
0
  else if ((*s & 0xf8) == 0xf0)
1463
0
    l = 4;
1464
0
  else
1465
0
    l = 1;
1466
1467
0
  if (l > len)
1468
0
    return -1;
1469
1470
0
  if (!pg_utf8_islegal(s, l))
1471
0
    return -1;
1472
1473
0
  return l;
1474
0
}
1475
1476
/*
1477
 * The fast path of the UTF-8 verifier uses a deterministic finite automaton
1478
 * (DFA) for multibyte characters. In a traditional table-driven DFA, the
1479
 * input byte and current state are used to compute an index into an array of
1480
 * state transitions. Since the address of the next transition is dependent
1481
 * on this computation, there is latency in executing the load instruction,
1482
 * and the CPU is not kept busy.
1483
 *
1484
 * Instead, we use a "shift-based" DFA as described by Per Vognsen:
1485
 *
1486
 * https://gist.github.com/pervognsen/218ea17743e1442e59bb60d29b1aa725
1487
 *
1488
 * In a shift-based DFA, the input byte is an index into array of integers
1489
 * whose bit pattern encodes the state transitions. To compute the next
1490
 * state, we simply right-shift the integer by the current state and apply a
1491
 * mask. In this scheme, the address of the transition only depends on the
1492
 * input byte, so there is better pipelining.
1493
 *
1494
 * The naming convention for states and transitions was adopted from a UTF-8
1495
 * to UTF-16/32 transcoder, whose table is reproduced below:
1496
 *
1497
 * https://github.com/BobSteagall/utf_utils/blob/6b7a465265de2f5fa6133d653df0c9bdd73bbcf8/src/utf_utils.cpp
1498
 *
1499
 * ILL  ASC  CR1  CR2  CR3  L2A  L3A  L3B  L3C  L4A  L4B  L4C CLASS / STATE
1500
 * ==========================================================================
1501
 * err, END, err, err, err, CS1, P3A, CS2, P3B, P4A, CS3, P4B,      | BGN/END
1502
 * err, err, err, err, err, err, err, err, err, err, err, err,      | ERR
1503
 *                                                                  |
1504
 * err, err, END, END, END, err, err, err, err, err, err, err,      | CS1
1505
 * err, err, CS1, CS1, CS1, err, err, err, err, err, err, err,      | CS2
1506
 * err, err, CS2, CS2, CS2, err, err, err, err, err, err, err,      | CS3
1507
 *                                                                  |
1508
 * err, err, err, err, CS1, err, err, err, err, err, err, err,      | P3A
1509
 * err, err, CS1, CS1, err, err, err, err, err, err, err, err,      | P3B
1510
 *                                                                  |
1511
 * err, err, err, CS2, CS2, err, err, err, err, err, err, err,      | P4A
1512
 * err, err, CS2, err, err, err, err, err, err, err, err, err,      | P4B
1513
 *
1514
 * In the most straightforward implementation, a shift-based DFA for UTF-8
1515
 * requires 64-bit integers to encode the transitions, but with an SMT solver
1516
 * it's possible to find state numbers such that the transitions fit within
1517
 * 32-bit integers, as Dougall Johnson demonstrated:
1518
 *
1519
 * https://gist.github.com/dougallj/166e326de6ad4cf2c94be97a204c025f
1520
 *
1521
 * This packed representation is the reason for the seemingly odd choice of
1522
 * state values below.
1523
 */
1524
1525
/* Error */
1526
0
#define ERR  0
1527
/* Begin */
1528
0
#define BGN 11
1529
/* Continuation states, expect 1/2/3 continuation bytes */
1530
#define CS1 16
1531
#define CS2  1
1532
#define CS3  5
1533
/* Partial states, where the first continuation byte has a restricted range */
1534
#define P3A  6          /* Lead was E0, check for 3-byte overlong */
1535
#define P3B 20          /* Lead was ED, check for surrogate */
1536
#define P4A 25          /* Lead was F0, check for 4-byte overlong */
1537
#define P4B 30          /* Lead was F4, check for too-large */
1538
/* Begin and End are the same state */
1539
0
#define END BGN
1540
1541
/* the encoded state transitions for the lookup table */
1542
1543
/* ASCII */
1544
#define ASC (END << BGN)
1545
/* 2-byte lead */
1546
#define L2A (CS1 << BGN)
1547
/* 3-byte lead */
1548
#define L3A (P3A << BGN)
1549
#define L3B (CS2 << BGN)
1550
#define L3C (P3B << BGN)
1551
/* 4-byte lead */
1552
#define L4A (P4A << BGN)
1553
#define L4B (CS3 << BGN)
1554
#define L4C (P4B << BGN)
1555
/* continuation byte */
1556
#define CR1 (END << CS1) | (CS1 << CS2) | (CS2 << CS3) | (CS1 << P3B) | (CS2 << P4B)
1557
#define CR2 (END << CS1) | (CS1 << CS2) | (CS2 << CS3) | (CS1 << P3B) | (CS2 << P4A)
1558
#define CR3 (END << CS1) | (CS1 << CS2) | (CS2 << CS3) | (CS1 << P3A) | (CS2 << P4A)
1559
/* invalid byte */
1560
#define ILL ERR
1561
1562
static const uint32 Utf8Transition[256] =
1563
{
1564
  /* ASCII */
1565
1566
  ILL, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1567
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1568
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1569
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1570
1571
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1572
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1573
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1574
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1575
1576
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1577
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1578
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1579
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1580
1581
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1582
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1583
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1584
  ASC, ASC, ASC, ASC, ASC, ASC, ASC, ASC,
1585
1586
  /* continuation bytes */
1587
1588
  /* 80..8F */
1589
  CR1, CR1, CR1, CR1, CR1, CR1, CR1, CR1,
1590
  CR1, CR1, CR1, CR1, CR1, CR1, CR1, CR1,
1591
1592
  /* 90..9F */
1593
  CR2, CR2, CR2, CR2, CR2, CR2, CR2, CR2,
1594
  CR2, CR2, CR2, CR2, CR2, CR2, CR2, CR2,
1595
1596
  /* A0..BF */
1597
  CR3, CR3, CR3, CR3, CR3, CR3, CR3, CR3,
1598
  CR3, CR3, CR3, CR3, CR3, CR3, CR3, CR3,
1599
  CR3, CR3, CR3, CR3, CR3, CR3, CR3, CR3,
1600
  CR3, CR3, CR3, CR3, CR3, CR3, CR3, CR3,
1601
1602
  /* leading bytes */
1603
1604
  /* C0..DF */
1605
  ILL, ILL, L2A, L2A, L2A, L2A, L2A, L2A,
1606
  L2A, L2A, L2A, L2A, L2A, L2A, L2A, L2A,
1607
  L2A, L2A, L2A, L2A, L2A, L2A, L2A, L2A,
1608
  L2A, L2A, L2A, L2A, L2A, L2A, L2A, L2A,
1609
1610
  /* E0..EF */
1611
  L3A, L3B, L3B, L3B, L3B, L3B, L3B, L3B,
1612
  L3B, L3B, L3B, L3B, L3B, L3C, L3B, L3B,
1613
1614
  /* F0..FF */
1615
  L4A, L4B, L4B, L4B, L4C, ILL, ILL, ILL,
1616
  ILL, ILL, ILL, ILL, ILL, ILL, ILL, ILL
1617
};
1618
1619
static void
1620
utf8_advance(const unsigned char *s, uint32 *state, int len)
1621
0
{
1622
  /* Note: We deliberately don't check the state's value here. */
1623
0
  while (len > 0)
1624
0
  {
1625
    /*
1626
     * It's important that the mask value is 31: In most instruction sets,
1627
     * a shift by a 32-bit operand is understood to be a shift by its mod
1628
     * 32, so the compiler should elide the mask operation.
1629
     */
1630
0
    *state = Utf8Transition[*s++] >> (*state & 31);
1631
0
    len--;
1632
0
  }
1633
1634
0
  *state &= 31;
1635
0
}
1636
1637
static int
1638
pg_utf8_verifystr(const unsigned char *s, int len)
1639
0
{
1640
0
  const unsigned char *start = s;
1641
0
  const int orig_len = len;
1642
0
  uint32    state = BGN;
1643
1644
/*
1645
 * With a stride of two vector widths, gcc will unroll the loop. Even if
1646
 * the compiler can unroll a longer loop, it's not worth it because we
1647
 * must fall back to the byte-wise algorithm if we find any non-ASCII.
1648
 */
1649
0
#define STRIDE_LENGTH (2 * sizeof(Vector8))
1650
1651
0
  if (len >= STRIDE_LENGTH)
1652
0
  {
1653
0
    while (len >= STRIDE_LENGTH)
1654
0
    {
1655
      /*
1656
       * If the chunk is all ASCII, we can skip the full UTF-8 check,
1657
       * but we must first check for a non-END state, which means the
1658
       * previous chunk ended in the middle of a multibyte sequence.
1659
       */
1660
0
      if (state != END || !is_valid_ascii(s, STRIDE_LENGTH))
1661
0
        utf8_advance(s, &state, STRIDE_LENGTH);
1662
1663
0
      s += STRIDE_LENGTH;
1664
0
      len -= STRIDE_LENGTH;
1665
0
    }
1666
1667
    /* The error state persists, so we only need to check for it here. */
1668
0
    if (state == ERR)
1669
0
    {
1670
      /*
1671
       * Start over from the beginning with the slow path so we can
1672
       * count the valid bytes.
1673
       */
1674
0
      len = orig_len;
1675
0
      s = start;
1676
0
    }
1677
0
    else if (state != END)
1678
0
    {
1679
      /*
1680
       * The fast path exited in the middle of a multibyte sequence.
1681
       * Walk backwards to find the leading byte so that the slow path
1682
       * can resume checking from there. We must always backtrack at
1683
       * least one byte, since the current byte could be e.g. an ASCII
1684
       * byte after a 2-byte lead, which is invalid.
1685
       */
1686
0
      do
1687
0
      {
1688
0
        Assert(s > start);
1689
0
        s--;
1690
0
        len++;
1691
0
        Assert(IS_HIGHBIT_SET(*s));
1692
0
      } while (pg_utf_mblen(s) <= 1);
1693
0
    }
1694
0
  }
1695
1696
  /* check remaining bytes */
1697
0
  while (len > 0)
1698
0
  {
1699
0
    int     l;
1700
1701
    /* fast path for ASCII-subset characters */
1702
0
    if (!IS_HIGHBIT_SET(*s))
1703
0
    {
1704
0
      if (*s == '\0')
1705
0
        break;
1706
0
      l = 1;
1707
0
    }
1708
0
    else
1709
0
    {
1710
0
      l = pg_utf8_verifychar(s, len);
1711
0
      if (l == -1)
1712
0
        break;
1713
0
    }
1714
0
    s += l;
1715
0
    len -= l;
1716
0
  }
1717
1718
0
  return s - start;
1719
0
}
1720
1721
/*
1722
 * Check for validity of a single UTF-8 encoded character
1723
 *
1724
 * This directly implements the rules in RFC3629.  The bizarre-looking
1725
 * restrictions on the second byte are meant to ensure that there isn't
1726
 * more than one encoding of a given Unicode character point; that is,
1727
 * you may not use a longer-than-necessary byte sequence with high order
1728
 * zero bits to represent a character that would fit in fewer bytes.
1729
 * To do otherwise is to create security hazards (eg, create an apparent
1730
 * non-ASCII character that decodes to plain ASCII).
1731
 *
1732
 * length is assumed to have been obtained by pg_utf_mblen(), and the
1733
 * caller must have checked that that many bytes are present in the buffer.
1734
 */
1735
bool
1736
pg_utf8_islegal(const unsigned char *source, int length)
1737
0
{
1738
0
  unsigned char a;
1739
1740
0
  switch (length)
1741
0
  {
1742
0
    default:
1743
      /* reject lengths 5 and 6 for now */
1744
0
      return false;
1745
0
    case 4:
1746
0
      a = source[3];
1747
0
      if (a < 0x80 || a > 0xBF)
1748
0
        return false;
1749
0
      pg_fallthrough;
1750
0
    case 3:
1751
0
      a = source[2];
1752
0
      if (a < 0x80 || a > 0xBF)
1753
0
        return false;
1754
0
      pg_fallthrough;
1755
0
    case 2:
1756
0
      a = source[1];
1757
0
      switch (*source)
1758
0
      {
1759
0
        case 0xE0:
1760
0
          if (a < 0xA0 || a > 0xBF)
1761
0
            return false;
1762
0
          break;
1763
0
        case 0xED:
1764
0
          if (a < 0x80 || a > 0x9F)
1765
0
            return false;
1766
0
          break;
1767
0
        case 0xF0:
1768
0
          if (a < 0x90 || a > 0xBF)
1769
0
            return false;
1770
0
          break;
1771
0
        case 0xF4:
1772
0
          if (a < 0x80 || a > 0x8F)
1773
0
            return false;
1774
0
          break;
1775
0
        default:
1776
0
          if (a < 0x80 || a > 0xBF)
1777
0
            return false;
1778
0
          break;
1779
0
      }
1780
0
      pg_fallthrough;
1781
0
    case 1:
1782
0
      a = *source;
1783
0
      if (a >= 0x80 && a < 0xC2)
1784
0
        return false;
1785
0
      if (a > 0xF4)
1786
0
        return false;
1787
0
      break;
1788
0
  }
1789
0
  return true;
1790
0
}
1791
1792
1793
/*
1794
 * Fills the provided buffer with two bytes such that:
1795
 *   pg_encoding_mblen(dst) == 2 && pg_encoding_verifymbstr(dst) == 0
1796
 */
1797
void
1798
pg_encoding_set_invalid(int encoding, char *dst)
1799
0
{
1800
0
  Assert(pg_encoding_max_length(encoding) > 1);
1801
1802
0
  dst[0] = (encoding == PG_UTF8 ? 0xc0 : NONUTF8_INVALID_BYTE0);
1803
0
  dst[1] = NONUTF8_INVALID_BYTE1;
1804
0
}
1805
1806
/*
1807
 *-------------------------------------------------------------------
1808
 * encoding info table
1809
 *-------------------------------------------------------------------
1810
 */
1811
const pg_wchar_tbl pg_wchar_table[] = {
1812
  [PG_SQL_ASCII] = {pg_ascii2wchar_with_len, pg_wchar2single_with_len, pg_ascii_mblen, pg_ascii_dsplen, pg_ascii_verifychar, pg_ascii_verifystr, 1},
1813
  [PG_EUC_JP] = {pg_eucjp2wchar_with_len, pg_wchar2euc_with_len, pg_eucjp_mblen, pg_eucjp_dsplen, pg_eucjp_verifychar, pg_eucjp_verifystr, 3},
1814
  [PG_EUC_CN] = {pg_euccn2wchar_with_len, pg_wchar2euc_with_len, pg_euccn_mblen, pg_euccn_dsplen, pg_euccn_verifychar, pg_euccn_verifystr, 3},
1815
  [PG_EUC_KR] = {pg_euckr2wchar_with_len, pg_wchar2euc_with_len, pg_euckr_mblen, pg_euckr_dsplen, pg_euckr_verifychar, pg_euckr_verifystr, 3},
1816
  [PG_EUC_TW] = {pg_euctw2wchar_with_len, pg_wchar2euc_with_len, pg_euctw_mblen, pg_euctw_dsplen, pg_euctw_verifychar, pg_euctw_verifystr, 4},
1817
  [PG_EUC_JIS_2004] = {pg_eucjp2wchar_with_len, pg_wchar2euc_with_len, pg_eucjp_mblen, pg_eucjp_dsplen, pg_eucjp_verifychar, pg_eucjp_verifystr, 3},
1818
  [PG_UTF8] = {pg_utf2wchar_with_len, pg_wchar2utf_with_len, pg_utf_mblen, pg_utf_dsplen, pg_utf8_verifychar, pg_utf8_verifystr, 4},
1819
  [PG_LATIN1] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1820
  [PG_LATIN2] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1821
  [PG_LATIN3] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1822
  [PG_LATIN4] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1823
  [PG_LATIN5] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1824
  [PG_LATIN6] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1825
  [PG_LATIN7] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1826
  [PG_LATIN8] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1827
  [PG_LATIN9] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1828
  [PG_LATIN10] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1829
  [PG_WIN1256] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1830
  [PG_WIN1258] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1831
  [PG_WIN866] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1832
  [PG_WIN874] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1833
  [PG_KOI8R] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1834
  [PG_WIN1251] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1835
  [PG_WIN1252] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1836
  [PG_ISO_8859_5] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1837
  [PG_ISO_8859_6] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1838
  [PG_ISO_8859_7] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1839
  [PG_ISO_8859_8] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1840
  [PG_WIN1250] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1841
  [PG_WIN1253] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1842
  [PG_WIN1254] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1843
  [PG_WIN1255] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1844
  [PG_WIN1257] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1845
  [PG_KOI8U] = {pg_latin12wchar_with_len, pg_wchar2single_with_len, pg_latin1_mblen, pg_latin1_dsplen, pg_latin1_verifychar, pg_latin1_verifystr, 1},
1846
  [PG_SJIS] = {0, 0, pg_sjis_mblen, pg_sjis_dsplen, pg_sjis_verifychar, pg_sjis_verifystr, 2},
1847
  [PG_BIG5] = {0, 0, pg_big5_mblen, pg_big5_dsplen, pg_big5_verifychar, pg_big5_verifystr, 2},
1848
  [PG_GBK] = {0, 0, pg_gbk_mblen, pg_gbk_dsplen, pg_gbk_verifychar, pg_gbk_verifystr, 2},
1849
  [PG_UHC] = {0, 0, pg_uhc_mblen, pg_uhc_dsplen, pg_uhc_verifychar, pg_uhc_verifystr, 2},
1850
  [PG_GB18030] = {0, 0, pg_gb18030_mblen, pg_gb18030_dsplen, pg_gb18030_verifychar, pg_gb18030_verifystr, 4},
1851
  [PG_JOHAB] = {0, 0, pg_johab_mblen, pg_johab_dsplen, pg_johab_verifychar, pg_johab_verifystr, 3},
1852
  [PG_SHIFT_JIS_2004] = {0, 0, pg_sjis_mblen, pg_sjis_dsplen, pg_sjis_verifychar, pg_sjis_verifystr, 2},
1853
};
1854
1855
/*
1856
 * Returns the byte length of a multibyte character.
1857
 *
1858
 * Choose "mblen" functions based on the input string characteristics.
1859
 * pg_encoding_mblen() can be used when ANY of these conditions are met:
1860
 *
1861
 * - The input string is zero-terminated
1862
 *
1863
 * - The input string is known to be valid in the encoding (e.g., string
1864
 *   converted from database encoding)
1865
 *
1866
 * - The encoding is not GB18030 (e.g., when only database encodings are
1867
 *   passed to 'encoding' parameter)
1868
 *
1869
 * encoding==GB18030 requires examining up to two bytes to determine character
1870
 * length.  Therefore, callers satisfying none of those conditions must use
1871
 * pg_encoding_mblen_or_incomplete() instead, as access to mbstr[1] cannot be
1872
 * guaranteed to be within allocation bounds.
1873
 *
1874
 * When dealing with text that is not certainly valid in the specified
1875
 * encoding, the result may exceed the actual remaining string length.
1876
 * Callers that are not prepared to deal with that should use Min(remaining,
1877
 * pg_encoding_mblen_or_incomplete()).  For zero-terminated strings, that and
1878
 * pg_encoding_mblen_bounded() are interchangeable.
1879
 */
1880
int
1881
pg_encoding_mblen(int encoding, const char *mbstr)
1882
679
{
1883
679
  return (PG_VALID_ENCODING(encoding) ?
1884
679
      pg_wchar_table[encoding].mblen((const unsigned char *) mbstr) :
1885
679
      pg_wchar_table[PG_SQL_ASCII].mblen((const unsigned char *) mbstr));
1886
679
}
1887
1888
/*
1889
 * Returns the byte length of a multibyte character (possibly not
1890
 * zero-terminated), or INT_MAX if too few bytes remain to determine a length.
1891
 */
1892
int
1893
pg_encoding_mblen_or_incomplete(int encoding, const char *mbstr,
1894
                size_t remaining)
1895
679
{
1896
  /*
1897
   * Define zero remaining as too few, even for single-byte encodings.
1898
   * pg_gb18030_mblen() reads one or two bytes; single-byte encodings read
1899
   * zero; others read one.
1900
   */
1901
679
  if (remaining < 1 ||
1902
679
    (encoding == PG_GB18030 && IS_HIGHBIT_SET(*mbstr) && remaining < 2))
1903
0
    return INT_MAX;
1904
679
  return pg_encoding_mblen(encoding, mbstr);
1905
679
}
1906
1907
/*
1908
 * Returns the byte length of a multibyte character; but not more than the
1909
 * distance to the terminating zero byte.  For input that might lack a
1910
 * terminating zero, use Min(remaining, pg_encoding_mblen_or_incomplete()).
1911
 */
1912
int
1913
pg_encoding_mblen_bounded(int encoding, const char *mbstr)
1914
0
{
1915
0
  return strnlen(mbstr, pg_encoding_mblen(encoding, mbstr));
1916
0
}
1917
1918
/*
1919
 * Returns the display length of a multibyte character.
1920
 */
1921
int
1922
pg_encoding_dsplen(int encoding, const char *mbstr)
1923
0
{
1924
0
  return (PG_VALID_ENCODING(encoding) ?
1925
0
      pg_wchar_table[encoding].dsplen((const unsigned char *) mbstr) :
1926
0
      pg_wchar_table[PG_SQL_ASCII].dsplen((const unsigned char *) mbstr));
1927
0
}
1928
1929
/*
1930
 * Verify the first multibyte character of the given string.
1931
 *
1932
 * Return its byte length if good, -1 if bad.  (See comments at pg_wchar_tbl
1933
 * for full details of the mbverifychar API.)
1934
 */
1935
int
1936
pg_encoding_verifymbchar(int encoding, const char *mbstr, int len)
1937
0
{
1938
0
  return (PG_VALID_ENCODING(encoding) ?
1939
0
      pg_wchar_table[encoding].mbverifychar((const unsigned char *) mbstr, len) :
1940
0
      pg_wchar_table[PG_SQL_ASCII].mbverifychar((const unsigned char *) mbstr, len));
1941
0
}
1942
1943
/*
1944
 * Verify that a string is valid for the given encoding.
1945
 *
1946
 * Returns the number of input bytes (<= len) that form a valid string.  (See
1947
 * comments at pg_wchar_tbl for full details of the mbverifystr API.)
1948
 */
1949
int
1950
pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
1951
0
{
1952
0
  return (PG_VALID_ENCODING(encoding) ?
1953
0
      pg_wchar_table[encoding].mbverifystr((const unsigned char *) mbstr, len) :
1954
0
      pg_wchar_table[PG_SQL_ASCII].mbverifystr((const unsigned char *) mbstr, len));
1955
0
}
1956
1957
/*
1958
 * fetch maximum length of a given encoding
1959
 */
1960
int
1961
pg_encoding_max_length(int encoding)
1962
1.81k
{
1963
1.81k
  Assert(PG_VALID_ENCODING(encoding));
1964
1965
  /*
1966
   * Check for the encoding despite the assert, due to some mingw versions
1967
   * otherwise issuing bogus warnings.
1968
   */
1969
1.81k
  return PG_VALID_ENCODING(encoding) ?
1970
1.81k
    pg_wchar_table[encoding].maxmblen :
1971
1.81k
    pg_wchar_table[PG_SQL_ASCII].maxmblen;
1972
1.81k
}