aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libpqxx/src/encodings.cxx
blob: 7102c891c4b0f2ad625db64d9124898de6de0fff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/** Implementation of string encodings support
 *
 * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
 *
 * See COPYING for copyright license.  If you did not receive a file called
 * COPYING with this source code, please notify the distributor of this mistake,
 * or contact the author.
 */
#include "pqxx/compiler-internal.hxx"

#include "pqxx/except.hxx"
#include "pqxx/internal/encodings.hxx"

#include <cstring>
#include <iomanip>
#include <map>
#include <sstream>

using namespace pqxx::internal;

extern "C"
{
#include "libpq-fe.h"
}


// Internal helper functions
namespace
{
/// Extract byte from buffer, return as unsigned char.
unsigned char get_byte(const char buffer[], std::string::size_type offset)
{
  return static_cast<unsigned char>(buffer[offset]);
}


[[noreturn]] void throw_for_encoding_error(
  const char* encoding_name,
  const char buffer[],
  std::string::size_type start,
  std::string::size_type count
)
{
  std::stringstream s;
  s
    << "Invalid byte sequence for encoding "
    << encoding_name
    << " at byte "
    << start
    << ": "
    << std::hex
    << std::setw(2)
    << std::setfill('0')
  ;
  for (std::string::size_type i{0}; i < count; ++i)
  {
    s << "0x" << static_cast<unsigned int>(get_byte(buffer, start + i));
    if (i + 1 < count) s << " ";
  }
  throw pqxx::argument_error{s.str()};
}


/// Does value lie between bottom and top, inclusive?
constexpr bool between_inc(unsigned char value, unsigned bottom, unsigned top)
{
  return value >= bottom and value <= top;
}


/*
EUC-JP and EUC-JIS-2004 represent slightly different code points but iterate
the same:
 * https://en.wikipedia.org/wiki/Extended_Unix_Code#EUC-JP
 * http://x0213.org/codetable/index.en.html
*/
std::string::size_type next_seq_for_euc_jplike(
	const char buffer[],
	std::string::size_type buffer_len,
	std::string::size_type start,
	const char encoding_name[])
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80) return start + 1;

  if (start + 2 > buffer_len)
    throw_for_encoding_error(encoding_name, buffer, start, 1);

  const auto byte2 = get_byte(buffer, start + 1);
  if (byte1 == 0x8e)
  {
    if (not between_inc(byte2, 0xa1, 0xfe))
      throw_for_encoding_error(encoding_name, buffer, start, 2);

    return start + 2;
  }

  if (between_inc(byte1, 0xa1, 0xfe))
  {
    if (not between_inc(byte2, 0xa1, 0xfe))
      throw_for_encoding_error(encoding_name, buffer, start, 2);

    return start + 2;
  }

  if (byte1 == 0x8f and start + 3 <= buffer_len)
  {
    const auto byte3 = get_byte(buffer, start + 2);
    if (
	not between_inc(byte2, 0xa1, 0xfe) or
        not between_inc(byte3, 0xa1, 0xfe)
      )
      throw_for_encoding_error(encoding_name, buffer, start, 3);

    return start + 3;
  }

  throw_for_encoding_error(encoding_name, buffer, start, 1);
}

/*
As far as I can tell, for the purposes of iterating the only difference between
SJIS and SJIS-2004 is increased range in the first byte of two-byte sequences
(0xEF increased to 0xFC).  Officially, that is; apparently the version of SJIS
used by Postgres has the same range as SJIS-2004.  They both have increased
range over the documented versions, not having the even/odd restriction for the
first byte in 2-byte sequences.
*/
// https://en.wikipedia.org/wiki/Shift_JIS#Shift_JIS_byte_map
// http://x0213.org/codetable/index.en.html
std::string::size_type next_seq_for_sjislike(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start,
  const char* encoding_name
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80 or between_inc(byte1, 0xa1, 0xdf)) return start + 1;

  if (
	not between_inc(byte1, 0x81, 0x9f) and
	not between_inc(byte1, 0xe0, 0xfc)
  )
    throw_for_encoding_error(encoding_name, buffer, start, 1);

  if (start + 2 > buffer_len)
    throw_for_encoding_error(
	encoding_name,
	buffer,
	start,
	buffer_len - start);

  const auto byte2 = get_byte(buffer, start + 1);
  if (byte2 == 0x7f) throw_for_encoding_error(encoding_name, buffer, start, 2);

  if (between_inc(byte2, 0x40, 0x9e) or between_inc(byte2, 0x9f, 0xfc))
    return start + 2;

  throw_for_encoding_error(encoding_name, buffer, start, 2);
}
} // namespace


// Implement template specializations first
namespace pqxx
{
namespace internal
{
template<encoding_group> struct glyph_scanner
{
  static std::string::size_type call(
	const char buffer[],
	std::string::size_type buffer_len,
	std::string::size_type start);
};

template<>
std::string::size_type glyph_scanner<encoding_group::MONOBYTE>::call(
  const char /* buffer */[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;
  else return start + 1;
}

// https://en.wikipedia.org/wiki/Big5#Organization
template<> std::string::size_type glyph_scanner<encoding_group::BIG5>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80) return start + 1;

  if (not between_inc(byte1, 0x81, 0xfe) or (start + 2 > buffer_len))
    throw_for_encoding_error("BIG5", buffer, start, 1);

  const auto byte2 = get_byte(buffer, start + 1);
  if (
	not between_inc(byte2, 0x40, 0x7e) and
	not between_inc(byte2, 0xa1, 0xfe))
    throw_for_encoding_error("BIG5", buffer, start, 2);

  return start + 2;
}

/*
The PostgreSQL documentation claims that the EUC_* encodings are 1-3 bytes each,
but other documents explain that the EUC sets can contain 1-(2,3,4) bytes
depending on the specific extension:
    EUC_CN      : 1-2
    EUC_JP      : 1-3
    EUC_JIS_2004: 1-2
    EUC_KR      : 1-2
    EUC_TW      : 1-4
*/

// https://en.wikipedia.org/wiki/GB_2312#EUC-CN
template<> std::string::size_type glyph_scanner<encoding_group::EUC_CN>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80) return start + 1;

  if (not between_inc(byte1, 0xa1, 0xf7) or start + 2 > buffer_len)
    throw_for_encoding_error("EUC_CN", buffer, start, 1);

  const auto byte2 = get_byte(buffer, start + 1);
  if (not between_inc(byte2, 0xa1, 0xfe))
    throw_for_encoding_error("EUC_CN", buffer, start, 2);

  return start + 2;
}

template<> std::string::size_type glyph_scanner<encoding_group::EUC_JP>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  return next_seq_for_euc_jplike(buffer, buffer_len, start, "EUC_JP");
}

template<>
std::string::size_type glyph_scanner<encoding_group::EUC_JIS_2004>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  return next_seq_for_euc_jplike(buffer, buffer_len, start, "EUC_JIS_2004");
}

// https://en.wikipedia.org/wiki/Extended_Unix_Code#EUC-KR
template<> std::string::size_type glyph_scanner<encoding_group::EUC_KR>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80) return start + 1;

  if (not between_inc(byte1, 0xa1, 0xfe) or start + 2 > buffer_len)
    throw_for_encoding_error("EUC_KR", buffer, start, 1);

  const auto byte2 = get_byte(buffer, start + 1);
  if (not between_inc(byte2, 0xa1, 0xfe))
    throw_for_encoding_error("EUC_KR", buffer, start, 1);

  return start + 2;
}

// https://en.wikipedia.org/wiki/Extended_Unix_Code#EUC-TW
template<> std::string::size_type glyph_scanner<encoding_group::EUC_TW>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80) return start + 1;

  if (start + 2 > buffer_len)
    throw_for_encoding_error("EUC_KR", buffer, start, 1);

  const auto byte2 = get_byte(buffer, start + 1);
  if (between_inc(byte1, 0xa1, 0xfe))
  {
    if (not between_inc(byte2, 0xa1, 0xfe))
      throw_for_encoding_error("EUC_KR", buffer, start, 2);

    return start + 2;
  }

  if (byte1 != 0x8e or start + 4 > buffer_len)
    throw_for_encoding_error("EUC_KR", buffer, start, 1);

  if (
        between_inc(byte2, 0xa1, 0xb0) and
        between_inc(get_byte(buffer, start + 2), 0xa1, 0xfe) and
        between_inc(get_byte(buffer, start + 3), 0xa1, 0xfe)
  )
    return start + 4;

  throw_for_encoding_error("EUC_KR", buffer, start, 4);
}

// https://en.wikipedia.org/wiki/GB_18030#Mapping
template<> std::string::size_type glyph_scanner<encoding_group::GB18030>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (between_inc(byte1, 0x80, 0xff)) return start + 1;

  if (start + 2 > buffer_len)
    throw_for_encoding_error("GB18030", buffer, start, buffer_len - start);

  const auto byte2 = get_byte(buffer, start + 1);
  if (between_inc(byte2, 0x40, 0xfe))
  {
    if (byte2 == 0x7f)
      throw_for_encoding_error("GB18030", buffer, start, 2);

    return start + 2;
  }

  if (start + 4 > buffer_len)
    throw_for_encoding_error("GB18030", buffer, start, buffer_len - start);

  if (
	between_inc(byte2, 0x30, 0x39) and
	between_inc(get_byte(buffer, start + 2), 0x81, 0xfe) and
	between_inc(get_byte(buffer, start + 3), 0x30, 0x39)
  )
    return start + 4;

  throw_for_encoding_error("GB18030", buffer, start, 4);
}

// https://en.wikipedia.org/wiki/GBK_(character_encoding)#Encoding
template<> std::string::size_type glyph_scanner<encoding_group::GBK>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80) return start + 1;

  if (start + 2 > buffer_len)
    throw_for_encoding_error("GBK", buffer, start, 1);

  const auto byte2 = get_byte(buffer, start + 1);
  if (
    (between_inc(byte1, 0xa1, 0xa9) and between_inc(byte2, 0xa1, 0xfe))
    or
    (between_inc(byte1, 0xb0, 0xf7) and between_inc(byte2, 0xa1, 0xfe))
    or
    (
      between_inc(byte1, 0x81, 0xa0) and
      between_inc(byte2, 0x40, 0xfe) and
      byte2 != 0x7f
    )
    or
    (
      between_inc(byte1, 0xaa, 0xfe) and
      between_inc(byte2, 0x40, 0xa0) and
      byte2 != 0x7f
    )
    or
    (
      between_inc(byte1, 0xa8, 0xa9) and
      between_inc(byte2, 0x40, 0xa0) and
      byte2 != 0x7f
    )
    or
    (between_inc(byte1, 0xaa, 0xaf) and between_inc(byte2, 0xa1, 0xfe))
    or
    (between_inc(byte1, 0xf8, 0xfe) and between_inc(byte2, 0xa1, 0xfe))
    or
    (
      between_inc(byte1, 0xa1, 0xa7) and
      between_inc(byte2, 0x40, 0xa0) and
      byte2 != 0x7f
    )
  )
    return start + 2;

  throw_for_encoding_error("GBK", buffer, start, 2);
}

/*
The PostgreSQL documentation claims that the JOHAB encoding is 1-3 bytes, but
"CJKV Information Processing" describes it (actually just the Hangul portion)
as "three five-bit segments" that reside inside 16 bits (2 bytes).

CJKV Information Processing by Ken Lunde, pg. 269:

  https://bit.ly/2BEOu5V
*/
template<> std::string::size_type glyph_scanner<encoding_group::JOHAB>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80) return start + 1;

  if (start + 2 > buffer_len)
    throw_for_encoding_error("JOHAB", buffer, start, 1);

  const auto byte2 = get_byte(buffer, start);
  if (
    (
      between_inc(byte1, 0x84, 0xd3) and
      (between_inc(byte2, 0x41, 0x7e) or between_inc(byte2, 0x81, 0xfe))
    )
    or
    (
      (between_inc(byte1, 0xd8, 0xde) or between_inc(byte1, 0xe0, 0xf9)) and
      (between_inc(byte2, 0x31, 0x7e) or between_inc(byte2, 0x91, 0xfe))
    )
  )
    return start + 2;

  throw_for_encoding_error("JOHAB", buffer, start, 2);
}

/*
PostgreSQL's MULE_INTERNAL is the emacs rather than Xemacs implementation;
see the server/mb/pg_wchar.h PostgreSQL header file.
This is implemented according to the description in said header file, but I was
unable to get it to successfully iterate a MULE-encoded test CSV generated using
PostgreSQL 9.2.23.  Use this at your own risk.
*/
template<>
std::string::size_type glyph_scanner<encoding_group::MULE_INTERNAL>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80) return start + 1;

  if (start + 2 > buffer_len)
    throw_for_encoding_error("MULE_INTERNAL", buffer, start, 1);

  const auto byte2 = get_byte(buffer, start + 1);
  if (between_inc(byte1, 0x81, 0x8d) and byte2 >= 0xA0)
    return start + 2;

  if (start + 3 > buffer_len)
    throw_for_encoding_error("MULE_INTERNAL", buffer, start, 2);

  if (
    (
      (byte1 == 0x9A and between_inc(byte2, 0xa0, 0xdf)) or
      (byte1 == 0x9B and between_inc(byte2, 0xe0, 0xef)) or
      (between_inc(byte1, 0x90, 0x99) and byte2 >= 0xa0)
    )
    and
    (
      byte2 >= 0xA0
    )
  )
    return start + 3;

  if (start + 4 > buffer_len)
    throw_for_encoding_error("MULE_INTERNAL", buffer, start, 3);

  if (
    (
      (byte1 == 0x9C and between_inc(byte2, 0xf0, 0xf4)) or
      (byte1 == 0x9D and between_inc(byte2, 0xf5, 0xfe))
    )
    and
    get_byte(buffer, start + 2) >= 0xa0 and
    get_byte(buffer, start + 4) >= 0xa0
  )
    return start + 4;

  throw_for_encoding_error("MULE_INTERNAL", buffer, start, 4);
}

template<> std::string::size_type glyph_scanner<encoding_group::SJIS>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  return next_seq_for_sjislike(buffer, buffer_len, start, "SJIS");
}

template<>
std::string::size_type glyph_scanner<encoding_group::SHIFT_JIS_2004>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  return next_seq_for_sjislike(buffer, buffer_len, start, "SHIFT_JIS_2004");
}

// https://en.wikipedia.org/wiki/Unified_Hangul_Code
template<> std::string::size_type glyph_scanner<encoding_group::UHC>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80) return start + 1;

  if (start + 2 > buffer_len)
    throw_for_encoding_error("UHC", buffer, start, buffer_len - start);

  const auto byte2 = get_byte(buffer, start + 1);
  if (between_inc(byte1, 0x80, 0xc6))
  {
    if (
      between_inc(byte2, 0x41, 0x5a) or
      between_inc(byte2, 0x61, 0x7a) or
      between_inc(byte2, 0x80, 0xfe)
    )
      return start + 2;

    throw_for_encoding_error("UHC", buffer, start, 2);
  }

  if (between_inc(byte1, 0xa1, 0xfe))
  {
    if (not between_inc(byte2, 0xa1, 0xfe))
      throw_for_encoding_error("UHC", buffer, start, 2);

   return start + 2;
  }

  throw_for_encoding_error("UHC", buffer, start, 1);
}

// https://en.wikipedia.org/wiki/UTF-8#Description
template<> std::string::size_type glyph_scanner<encoding_group::UTF8>::call(
  const char buffer[],
  std::string::size_type buffer_len,
  std::string::size_type start
)
{
  if (start >= buffer_len) return std::string::npos;

  const auto byte1 = get_byte(buffer, start);
  if (byte1 < 0x80) return start + 1;

  if (start + 2 > buffer_len)
      throw_for_encoding_error("UTF8", buffer, start, buffer_len - start);

  const auto byte2 = get_byte(buffer, start + 1);
  if (between_inc(byte1, 0xc0, 0xdf))
  {
    if (not between_inc(byte2, 0x80, 0xbf))
      throw_for_encoding_error("UTF8", buffer, start, 2);

    return start + 2;
  }

  if (start + 3 > buffer_len)
      throw_for_encoding_error("UTF8", buffer, start, buffer_len - start);

  const auto byte3 = get_byte(buffer, start + 2);
  if (between_inc(byte1, 0xe0, 0xef))
  {
    if (between_inc(byte2, 0x80, 0xbf) and between_inc(byte3, 0x80, 0xbf))
      return start + 3;

    throw_for_encoding_error("UTF8", buffer, start, 3);
  }

  if (start + 4 > buffer_len)
      throw_for_encoding_error("UTF8", buffer, start, buffer_len - start);

  if (between_inc(byte1, 0xf0, 0xf7))
  {
    if (
      between_inc(byte2, 0x80, 0xbf) and
      between_inc(byte3, 0x80, 0xbf) and
      between_inc(get_byte(buffer, start + 3), 0x80, 0xbf)
    )
      return start + 4;

    throw_for_encoding_error("UTF8", buffer, start, 4);
  }

  throw_for_encoding_error("UTF8", buffer, start, 1);
}


const char *name_encoding(int encoding_id)
{
  return pg_encoding_to_char(encoding_id);
}


encoding_group enc_group(int libpq_enc_id)
{
  return enc_group(name_encoding(libpq_enc_id));
}


encoding_group enc_group(const std::string& encoding_name)
{
  static const std::map<std::string, encoding_group> encoding_map{
    {"BIG5", encoding_group::BIG5},
    {"EUC_CN", encoding_group::EUC_CN},
    {"EUC_JP", encoding_group::EUC_JP},
    {"EUC_JIS_2004", encoding_group::EUC_JIS_2004},
    {"EUC_KR", encoding_group::EUC_KR},
    {"EUC_TW", encoding_group::EUC_TW},
    {"GB18030", encoding_group::GB18030},
    {"GBK", encoding_group::GBK},
    {"ISO_8859_5", encoding_group::MONOBYTE},
    {"ISO_8859_6", encoding_group::MONOBYTE},
    {"ISO_8859_7", encoding_group::MONOBYTE},
    {"ISO_8859_8", encoding_group::MONOBYTE},
    {"JOHAB", encoding_group::JOHAB},
    {"KOI8R", encoding_group::MONOBYTE},
    {"KOI8U", encoding_group::MONOBYTE},
    {"LATIN1", encoding_group::MONOBYTE},
    {"LATIN2", encoding_group::MONOBYTE},
    {"LATIN3", encoding_group::MONOBYTE},
    {"LATIN4", encoding_group::MONOBYTE},
    {"LATIN5", encoding_group::MONOBYTE},
    {"LATIN6", encoding_group::MONOBYTE},
    {"LATIN7", encoding_group::MONOBYTE},
    {"LATIN8", encoding_group::MONOBYTE},
    {"LATIN9", encoding_group::MONOBYTE},
    {"LATIN10", encoding_group::MONOBYTE},
    {"MULE_INTERNAL", encoding_group::MULE_INTERNAL},
    {"SJIS", encoding_group::SJIS},
    {"SHIFT_JIS_2004", encoding_group::SHIFT_JIS_2004},
    {"SQL_ASCII", encoding_group::MONOBYTE},
    {"UHC", encoding_group::UHC},
    {"UTF8", encoding_group::UTF8},
    {"WIN866", encoding_group::MONOBYTE},
    {"WIN874", encoding_group::MONOBYTE},
    {"WIN1250", encoding_group::MONOBYTE},
    {"WIN1251", encoding_group::MONOBYTE},
    {"WIN1252", encoding_group::MONOBYTE},
    {"WIN1253", encoding_group::MONOBYTE},
    {"WIN1254", encoding_group::MONOBYTE},
    {"WIN1255", encoding_group::MONOBYTE},
    {"WIN1256", encoding_group::MONOBYTE},
    {"WIN1257", encoding_group::MONOBYTE},
    {"WIN1258", encoding_group::MONOBYTE},
  };
  
  const auto found_encoding_group = encoding_map.find(encoding_name);
  if (found_encoding_group == encoding_map.end())
    throw std::invalid_argument{
      "unrecognized encoding '" + encoding_name + "'"
    };
  return found_encoding_group->second;
}


/// Look up instantiation @c T<enc>::call at runtime.
/** Here, "T" is a struct template with a static member function "call", whose
 * type is "F".
 *
 * The return value is a pointer to the "call" member function for the
 * instantiation of T for encoding group enc.
 */
template<template<encoding_group> class T, typename F>
inline F *for_encoding(encoding_group enc)
{

#define CASE_GROUP(ENC) \
	case encoding_group::ENC: return T<encoding_group::ENC>::call

  switch (enc)
  {
  CASE_GROUP(MONOBYTE);
  CASE_GROUP(BIG5);
  CASE_GROUP(EUC_CN);
  CASE_GROUP(EUC_JP);
  CASE_GROUP(EUC_JIS_2004);
  CASE_GROUP(EUC_KR);
  CASE_GROUP(EUC_TW);
  CASE_GROUP(GB18030);
  CASE_GROUP(GBK);
  CASE_GROUP(JOHAB);
  CASE_GROUP(MULE_INTERNAL);
  CASE_GROUP(SJIS);
  CASE_GROUP(SHIFT_JIS_2004);
  CASE_GROUP(UHC);
  CASE_GROUP(UTF8);
  }
  throw pqxx::usage_error{
	"Unsupported encoding group code " + to_string(int(enc)) + "."};

#undef CASE_GROUP
}


glyph_scanner_func *get_glyph_scanner(encoding_group enc)
{
  return for_encoding<glyph_scanner, glyph_scanner_func>(enc);
}


template<encoding_group E> struct char_finder
{
  static std::string::size_type call(
	const std::string &haystack,
	char needle,
	std::string::size_type start)
  {
    const auto buffer = haystack.c_str();
    const auto size = haystack.size();
    for (
	auto here = start;
	here + 1 <= size;
	here = glyph_scanner<E>::call(buffer, size, here)
    )
    {
      if (haystack[here] == needle) return here;
    }
    return std::string::npos;
  }
};


template<encoding_group E> struct string_finder
{
  static std::string::size_type call(
	const std::string &haystack,
	const std::string &needle,
	std::string::size_type start)
  {
    const auto buffer = haystack.c_str();
    const auto size = haystack.size();
    const auto needle_size = needle.size();
    for (
	auto here = start;
	here + needle_size <= size;
	here = glyph_scanner<E>::call(buffer, size, here)
    )
    {
      if (std::memcmp(buffer + here, needle.c_str(), needle_size) == 0)
        return here;
    }
    return std::string::npos;
  }
};


std::string::size_type find_with_encoding(
	encoding_group enc,
	const std::string& haystack,
	char needle,
	std::string::size_type start
)
{
  using finder_func =
    std::string::size_type(
	const std::string &haystack,
	char needle,
	std::string::size_type start);
  const auto finder = for_encoding<char_finder, finder_func>(enc);
  return finder(haystack, needle, start);
}


std::string::size_type find_with_encoding(
	encoding_group enc,
	const std::string& haystack,
	const std::string& needle,
	std::string::size_type start
)
{
  using finder_func =
    std::string::size_type(
	const std::string &haystack,
	const std::string &needle,
	std::string::size_type start);
  const auto finder = for_encoding<string_finder, finder_func>(enc);
  return finder(haystack, needle, start);
}

#undef DISPATCH_ENCODING_OPERATION

} // namespace pqxx::internal
} // namespace pqxx