Coverage Report

Created: 2026-12-01 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgres/src/backend/utils/mb/conv.c
Line
Count
Source
1
/*-------------------------------------------------------------------------
2
 *
3
 *    Utility functions for conversion procs.
4
 *
5
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
6
 * Portions Copyright (c) 1994, Regents of the University of California
7
 *
8
 * IDENTIFICATION
9
 *    src/backend/utils/mb/conv.c
10
 *
11
 *-------------------------------------------------------------------------
12
 */
13
#include "postgres.h"
14
#include "mb/pg_wchar.h"
15
16
17
/*
18
 * local2local: a generic single byte charset encoding
19
 * conversion between two ASCII-superset encodings.
20
 *
21
 * l points to the source string of length len
22
 * p is the output area (must be large enough!)
23
 * src_encoding is the PG identifier for the source encoding
24
 * dest_encoding is the PG identifier for the target encoding
25
 * tab holds conversion entries for the source charset
26
 * starting from 128 (0x80). each entry in the table holds the corresponding
27
 * code point for the target charset, or 0 if there is no equivalent code.
28
 *
29
 * Returns the number of input bytes consumed.  If noError is true, this can
30
 * be less than 'len'.
31
 */
32
int
33
local2local(const unsigned char *l,
34
      unsigned char *p,
35
      int len,
36
      int src_encoding,
37
      int dest_encoding,
38
      const unsigned char *tab,
39
      bool noError)
40
0
{
41
0
  const unsigned char *start = l;
42
0
  unsigned char c1,
43
0
        c2;
44
45
0
  while (len > 0)
46
0
  {
47
0
    c1 = *l;
48
0
    if (c1 == 0)
49
0
    {
50
0
      if (noError)
51
0
        break;
52
0
      report_invalid_encoding(src_encoding, (const char *) l, len);
53
0
    }
54
0
    if (!IS_HIGHBIT_SET(c1))
55
0
      *p++ = c1;
56
0
    else
57
0
    {
58
0
      c2 = tab[c1 - HIGHBIT];
59
0
      if (c2)
60
0
        *p++ = c2;
61
0
      else
62
0
      {
63
0
        if (noError)
64
0
          break;
65
0
        report_untranslatable_char(src_encoding, dest_encoding,
66
0
                       (const char *) l, len);
67
0
      }
68
0
    }
69
0
    l++;
70
0
    len--;
71
0
  }
72
0
  *p = '\0';
73
74
0
  return l - start;
75
0
}
76
77
/*
78
 * comparison routine for bsearch()
79
 * this routine is intended for combined UTF8 -> local code
80
 */
81
static int
82
compare3(const void *p1, const void *p2)
83
0
{
84
0
  uint32    s1,
85
0
        s2,
86
0
        d1,
87
0
        d2;
88
89
0
  s1 = *(const uint32 *) p1;
90
0
  s2 = *((const uint32 *) p1 + 1);
91
0
  d1 = ((const pg_utf_to_local_combined *) p2)->utf1;
92
0
  d2 = ((const pg_utf_to_local_combined *) p2)->utf2;
93
0
  return (s1 > d1 || (s1 == d1 && s2 > d2)) ? 1 : ((s1 == d1 && s2 == d2) ? 0 : -1);
94
0
}
95
96
/*
97
 * comparison routine for bsearch()
98
 * this routine is intended for local code -> combined UTF8
99
 */
100
static int
101
compare4(const void *p1, const void *p2)
102
0
{
103
0
  uint32    v1,
104
0
        v2;
105
106
0
  v1 = *(const uint32 *) p1;
107
0
  v2 = ((const pg_local_to_utf_combined *) p2)->code;
108
0
  return (v1 > v2) ? 1 : ((v1 == v2) ? 0 : -1);
109
0
}
110
111
/*
112
 * store 32bit character representation into multibyte stream
113
 */
114
static inline unsigned char *
115
store_coded_char(unsigned char *dest, uint32 code)
116
0
{
117
0
  if (code & 0xff000000)
118
0
    *dest++ = code >> 24;
119
0
  if (code & 0x00ff0000)
120
0
    *dest++ = code >> 16;
121
0
  if (code & 0x0000ff00)
122
0
    *dest++ = code >> 8;
123
0
  if (code & 0x000000ff)
124
0
    *dest++ = code;
125
0
  return dest;
126
0
}
127
128
/*
129
 * Convert a character using a conversion radix tree.
130
 *
131
 * 'l' is the length of the input character in bytes, and b1-b4 are
132
 * the input character's bytes.
133
 */
134
static inline uint32
135
pg_mb_radix_conv(const pg_mb_radix_tree *rt,
136
         int l,
137
         unsigned char b1,
138
         unsigned char b2,
139
         unsigned char b3,
140
         unsigned char b4)
141
0
{
142
0
  if (l == 4)
143
0
  {
144
    /* 4-byte code */
145
146
    /* check code validity */
147
0
    if (b1 < rt->b4_1_lower || b1 > rt->b4_1_upper ||
148
0
      b2 < rt->b4_2_lower || b2 > rt->b4_2_upper ||
149
0
      b3 < rt->b4_3_lower || b3 > rt->b4_3_upper ||
150
0
      b4 < rt->b4_4_lower || b4 > rt->b4_4_upper)
151
0
      return 0;
152
153
    /* perform lookup */
154
0
    if (rt->chars32)
155
0
    {
156
0
      uint32    idx = rt->b4root;
157
158
0
      idx = rt->chars32[b1 + idx - rt->b4_1_lower];
159
0
      idx = rt->chars32[b2 + idx - rt->b4_2_lower];
160
0
      idx = rt->chars32[b3 + idx - rt->b4_3_lower];
161
0
      return rt->chars32[b4 + idx - rt->b4_4_lower];
162
0
    }
163
0
    else
164
0
    {
165
0
      uint16    idx = rt->b4root;
166
167
0
      idx = rt->chars16[b1 + idx - rt->b4_1_lower];
168
0
      idx = rt->chars16[b2 + idx - rt->b4_2_lower];
169
0
      idx = rt->chars16[b3 + idx - rt->b4_3_lower];
170
0
      return rt->chars16[b4 + idx - rt->b4_4_lower];
171
0
    }
172
0
  }
173
0
  else if (l == 3)
174
0
  {
175
    /* 3-byte code */
176
177
    /* check code validity */
178
0
    if (b2 < rt->b3_1_lower || b2 > rt->b3_1_upper ||
179
0
      b3 < rt->b3_2_lower || b3 > rt->b3_2_upper ||
180
0
      b4 < rt->b3_3_lower || b4 > rt->b3_3_upper)
181
0
      return 0;
182
183
    /* perform lookup */
184
0
    if (rt->chars32)
185
0
    {
186
0
      uint32    idx = rt->b3root;
187
188
0
      idx = rt->chars32[b2 + idx - rt->b3_1_lower];
189
0
      idx = rt->chars32[b3 + idx - rt->b3_2_lower];
190
0
      return rt->chars32[b4 + idx - rt->b3_3_lower];
191
0
    }
192
0
    else
193
0
    {
194
0
      uint16    idx = rt->b3root;
195
196
0
      idx = rt->chars16[b2 + idx - rt->b3_1_lower];
197
0
      idx = rt->chars16[b3 + idx - rt->b3_2_lower];
198
0
      return rt->chars16[b4 + idx - rt->b3_3_lower];
199
0
    }
200
0
  }
201
0
  else if (l == 2)
202
0
  {
203
    /* 2-byte code */
204
205
    /* check code validity - first byte */
206
0
    if (b3 < rt->b2_1_lower || b3 > rt->b2_1_upper ||
207
0
      b4 < rt->b2_2_lower || b4 > rt->b2_2_upper)
208
0
      return 0;
209
210
    /* perform lookup */
211
0
    if (rt->chars32)
212
0
    {
213
0
      uint32    idx = rt->b2root;
214
215
0
      idx = rt->chars32[b3 + idx - rt->b2_1_lower];
216
0
      return rt->chars32[b4 + idx - rt->b2_2_lower];
217
0
    }
218
0
    else
219
0
    {
220
0
      uint16    idx = rt->b2root;
221
222
0
      idx = rt->chars16[b3 + idx - rt->b2_1_lower];
223
0
      return rt->chars16[b4 + idx - rt->b2_2_lower];
224
0
    }
225
0
  }
226
0
  else if (l == 1)
227
0
  {
228
    /* 1-byte code */
229
230
    /* check code validity - first byte */
231
0
    if (b4 < rt->b1_lower || b4 > rt->b1_upper)
232
0
      return 0;
233
234
    /* perform lookup */
235
0
    if (rt->chars32)
236
0
      return rt->chars32[b4 + rt->b1root - rt->b1_lower];
237
0
    else
238
0
      return rt->chars16[b4 + rt->b1root - rt->b1_lower];
239
0
  }
240
0
  return 0;         /* shouldn't happen */
241
0
}
242
243
/*
244
 * UTF8 ---> local code
245
 *
246
 * utf: input string in UTF8 encoding (need not be null-terminated)
247
 * len: length of input string (in bytes)
248
 * iso: pointer to the output area (must be large enough!)
249
 *      (output string will be null-terminated)
250
 * map: conversion map for single characters
251
 * cmap: conversion map for combined characters
252
 *      (optional, pass NULL if none)
253
 * cmapsize: number of entries in the conversion map for combined characters
254
 *      (optional, pass 0 if none)
255
 * conv_func: algorithmic encoding conversion function
256
 *      (optional, pass NULL if none)
257
 * encoding: PG identifier for the local encoding
258
 *
259
 * For each character, the cmap (if provided) is consulted first; if no match,
260
 * the map is consulted next; if still no match, the conv_func (if provided)
261
 * is applied.  An error is raised if no match is found.
262
 *
263
 * See pg_wchar.h for more details about the data structures used here.
264
 *
265
 * Returns the number of input bytes consumed.  If noError is true, this can
266
 * be less than 'len'.
267
 */
268
int
269
UtfToLocal(const unsigned char *utf, int len,
270
       unsigned char *iso,
271
       const pg_mb_radix_tree *map,
272
       const pg_utf_to_local_combined *cmap, int cmapsize,
273
       utf_local_conversion_func conv_func,
274
       int encoding, bool noError)
275
0
{
276
0
  uint32    iutf;
277
0
  int     l;
278
0
  const pg_utf_to_local_combined *cp;
279
0
  const unsigned char *start = utf;
280
281
0
  if (!PG_VALID_ENCODING(encoding))
282
0
    ereport(ERROR,
283
0
        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
284
0
         errmsg("invalid encoding number: %d", encoding)));
285
286
0
  for (; len > 0; len -= l)
287
0
  {
288
0
    unsigned char b1 = 0;
289
0
    unsigned char b2 = 0;
290
0
    unsigned char b3 = 0;
291
0
    unsigned char b4 = 0;
292
293
    /* "break" cases all represent errors */
294
0
    if (*utf == '\0')
295
0
      break;
296
297
0
    l = pg_utf_mblen(utf);
298
0
    if (len < l)
299
0
      break;
300
301
0
    if (!pg_utf8_islegal(utf, l))
302
0
      break;
303
304
0
    if (l == 1)
305
0
    {
306
      /* ASCII case is easy, assume it's one-to-one conversion */
307
0
      *iso++ = *utf++;
308
0
      continue;
309
0
    }
310
311
    /* collect coded char of length l */
312
0
    if (l == 2)
313
0
    {
314
0
      b3 = *utf++;
315
0
      b4 = *utf++;
316
0
    }
317
0
    else if (l == 3)
318
0
    {
319
0
      b2 = *utf++;
320
0
      b3 = *utf++;
321
0
      b4 = *utf++;
322
0
    }
323
0
    else if (l == 4)
324
0
    {
325
0
      b1 = *utf++;
326
0
      b2 = *utf++;
327
0
      b3 = *utf++;
328
0
      b4 = *utf++;
329
0
    }
330
0
    else
331
0
    {
332
0
      elog(ERROR, "unsupported character length %d", l);
333
0
      iutf = 0;     /* keep compiler quiet */
334
0
    }
335
0
    iutf = (b1 << 24 | b2 << 16 | b3 << 8 | b4);
336
337
    /* First, try with combined map if possible */
338
0
    if (cmap && len > l)
339
0
    {
340
0
      const unsigned char *utf_save = utf;
341
0
      int     len_save = len;
342
0
      int     l_save = l;
343
344
      /* collect next character, same as above */
345
0
      len -= l;
346
347
0
      l = pg_utf_mblen(utf);
348
0
      if (len < l)
349
0
      {
350
        /* need more data to decide if this is a combined char */
351
0
        utf -= l_save;
352
0
        break;
353
0
      }
354
355
0
      if (!pg_utf8_islegal(utf, l))
356
0
      {
357
0
        if (!noError)
358
0
          report_invalid_encoding(PG_UTF8, (const char *) utf, len);
359
0
        utf -= l_save;
360
0
        break;
361
0
      }
362
363
      /* We assume ASCII character cannot be in combined map */
364
0
      if (l > 1)
365
0
      {
366
0
        uint32    iutf2;
367
0
        uint32    cutf[2];
368
369
0
        if (l == 2)
370
0
        {
371
0
          iutf2 = *utf++ << 8;
372
0
          iutf2 |= *utf++;
373
0
        }
374
0
        else if (l == 3)
375
0
        {
376
0
          iutf2 = *utf++ << 16;
377
0
          iutf2 |= *utf++ << 8;
378
0
          iutf2 |= *utf++;
379
0
        }
380
0
        else if (l == 4)
381
0
        {
382
0
          iutf2 = *utf++ << 24;
383
0
          iutf2 |= *utf++ << 16;
384
0
          iutf2 |= *utf++ << 8;
385
0
          iutf2 |= *utf++;
386
0
        }
387
0
        else
388
0
        {
389
0
          elog(ERROR, "unsupported character length %d", l);
390
0
          iutf2 = 0;  /* keep compiler quiet */
391
0
        }
392
393
0
        cutf[0] = iutf;
394
0
        cutf[1] = iutf2;
395
396
0
        cp = bsearch(cutf, cmap, cmapsize,
397
0
               sizeof(pg_utf_to_local_combined), compare3);
398
399
0
        if (cp)
400
0
        {
401
0
          iso = store_coded_char(iso, cp->code);
402
0
          continue;
403
0
        }
404
0
      }
405
406
      /* fail, so back up to reprocess second character next time */
407
0
      utf = utf_save;
408
0
      len = len_save;
409
0
      l = l_save;
410
0
    }
411
412
    /* Now check ordinary map */
413
0
    if (map)
414
0
    {
415
0
      uint32    converted = pg_mb_radix_conv(map, l, b1, b2, b3, b4);
416
417
0
      if (converted)
418
0
      {
419
0
        iso = store_coded_char(iso, converted);
420
0
        continue;
421
0
      }
422
0
    }
423
424
    /* if there's a conversion function, try that */
425
0
    if (conv_func)
426
0
    {
427
0
      uint32    converted = (*conv_func) (iutf);
428
429
0
      if (converted)
430
0
      {
431
0
        iso = store_coded_char(iso, converted);
432
0
        continue;
433
0
      }
434
0
    }
435
436
    /* failed to translate this character */
437
0
    utf -= l;
438
0
    if (noError)
439
0
      break;
440
0
    report_untranslatable_char(PG_UTF8, encoding,
441
0
                   (const char *) utf, len);
442
0
  }
443
444
  /* if we broke out of loop early, must be invalid input */
445
0
  if (len > 0 && !noError)
446
0
    report_invalid_encoding(PG_UTF8, (const char *) utf, len);
447
448
0
  *iso = '\0';
449
450
0
  return utf - start;
451
0
}
452
453
/*
454
 * local code ---> UTF8
455
 *
456
 * iso: input string in local encoding (need not be null-terminated)
457
 * len: length of input string (in bytes)
458
 * utf: pointer to the output area (must be large enough!)
459
 *      (output string will be null-terminated)
460
 * map: conversion map for single characters
461
 * cmap: conversion map for combined characters
462
 *      (optional, pass NULL if none)
463
 * cmapsize: number of entries in the conversion map for combined characters
464
 *      (optional, pass 0 if none)
465
 * conv_func: algorithmic encoding conversion function
466
 *      (optional, pass NULL if none)
467
 * encoding: PG identifier for the local encoding
468
 *
469
 * For each character, the map is consulted first; if no match, the cmap
470
 * (if provided) is consulted next; if still no match, the conv_func
471
 * (if provided) is applied.  An error is raised if no match is found.
472
 *
473
 * See pg_wchar.h for more details about the data structures used here.
474
 *
475
 * Returns the number of input bytes consumed.  If noError is true, this can
476
 * be less than 'len'.
477
 */
478
int
479
LocalToUtf(const unsigned char *iso, int len,
480
       unsigned char *utf,
481
       const pg_mb_radix_tree *map,
482
       const pg_local_to_utf_combined *cmap, int cmapsize,
483
       utf_local_conversion_func conv_func,
484
       int encoding,
485
       bool noError)
486
0
{
487
0
  uint32    iiso;
488
0
  int     l;
489
0
  const pg_local_to_utf_combined *cp;
490
0
  const unsigned char *start = iso;
491
492
0
  if (!PG_VALID_ENCODING(encoding))
493
0
    ereport(ERROR,
494
0
        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
495
0
         errmsg("invalid encoding number: %d", encoding)));
496
497
0
  for (; len > 0; len -= l)
498
0
  {
499
0
    unsigned char b1 = 0;
500
0
    unsigned char b2 = 0;
501
0
    unsigned char b3 = 0;
502
0
    unsigned char b4 = 0;
503
504
    /* "break" cases all represent errors */
505
0
    if (*iso == '\0')
506
0
      break;
507
508
0
    if (!IS_HIGHBIT_SET(*iso))
509
0
    {
510
      /* ASCII case is easy, assume it's one-to-one conversion */
511
0
      *utf++ = *iso++;
512
0
      l = 1;
513
0
      continue;
514
0
    }
515
516
0
    l = pg_encoding_verifymbchar(encoding, (const char *) iso, len);
517
0
    if (l < 0)
518
0
      break;
519
520
    /* collect coded char of length l */
521
0
    if (l == 1)
522
0
      b4 = *iso++;
523
0
    else if (l == 2)
524
0
    {
525
0
      b3 = *iso++;
526
0
      b4 = *iso++;
527
0
    }
528
0
    else if (l == 3)
529
0
    {
530
0
      b2 = *iso++;
531
0
      b3 = *iso++;
532
0
      b4 = *iso++;
533
0
    }
534
0
    else if (l == 4)
535
0
    {
536
0
      b1 = *iso++;
537
0
      b2 = *iso++;
538
0
      b3 = *iso++;
539
0
      b4 = *iso++;
540
0
    }
541
0
    else
542
0
    {
543
0
      elog(ERROR, "unsupported character length %d", l);
544
0
      iiso = 0;     /* keep compiler quiet */
545
0
    }
546
0
    iiso = (b1 << 24 | b2 << 16 | b3 << 8 | b4);
547
548
0
    if (map)
549
0
    {
550
0
      uint32    converted = pg_mb_radix_conv(map, l, b1, b2, b3, b4);
551
552
0
      if (converted)
553
0
      {
554
0
        utf = store_coded_char(utf, converted);
555
0
        continue;
556
0
      }
557
558
      /* If there's a combined character map, try that */
559
0
      if (cmap)
560
0
      {
561
0
        cp = bsearch(&iiso, cmap, cmapsize,
562
0
               sizeof(pg_local_to_utf_combined), compare4);
563
564
0
        if (cp)
565
0
        {
566
0
          utf = store_coded_char(utf, cp->utf1);
567
0
          utf = store_coded_char(utf, cp->utf2);
568
0
          continue;
569
0
        }
570
0
      }
571
0
    }
572
573
    /* if there's a conversion function, try that */
574
0
    if (conv_func)
575
0
    {
576
0
      uint32    converted = (*conv_func) (iiso);
577
578
0
      if (converted)
579
0
      {
580
0
        utf = store_coded_char(utf, converted);
581
0
        continue;
582
0
      }
583
0
    }
584
585
    /* failed to translate this character */
586
0
    iso -= l;
587
0
    if (noError)
588
0
      break;
589
0
    report_untranslatable_char(encoding, PG_UTF8,
590
0
                   (const char *) iso, len);
591
0
  }
592
593
  /* if we broke out of loop early, must be invalid input */
594
0
  if (len > 0 && !noError)
595
0
    report_invalid_encoding(encoding, (const char *) iso, len);
596
597
0
  *utf = '\0';
598
599
0
  return iso - start;
600
0
}