Menu

[e18b04]: / backend / eci.c  Maximize  Restore  History

Download this file

1187 lines (1064 with data), 39.8 kB

   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
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
/* eci.c - Extended Channel Interpretations */
/*
libzint - the open source barcode library
Copyright (C) 2009-2025 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/* SPDX-License-Identifier: BSD-3-Clause */
#include <assert.h>
#include "common.h"
#include "eci.h"
#include "eci_sb.h"
#include "big5.h"
#include "gb18030.h"
#include "gb2312.h"
#include "gbk.h"
#include "ksx1001.h"
#include "sjis.h"
/* Single-byte stuff */
/* ECI 2 (bottom half ASCII, top half CP437), included for libzueci compatibility - assumes valid Unicode */
static int u_cp437(const unsigned int u, unsigned char *dest) {
int s, e;
if (u < 0x80) {
*dest = (unsigned char) u;
return 1;
}
s = 0;
e = ARRAY_SIZE(cp437_u) - 1;
while (s <= e) {
const int m = (s + e) >> 1;
if (cp437_u[m] < u) {
s = m + 1;
} else if (cp437_u[m] > u) {
e = m - 1;
} else {
*dest = cp437_sb[m];
return 1;
}
}
return 0;
}
/* Base ISO/IEC 8859 routine to convert Unicode codepoint `u` */
static int u_iso8859(const unsigned int u, const unsigned short *tab_s, const unsigned short *tab_u,
const unsigned char *tab_sb, int e, unsigned char *dest) {
int s;
if (u < 0xA0) {
if (u >= 0x80) { /* U+0080-9F fail */
return 0;
}
*dest = (unsigned char) u;
return 1;
}
if (u <= 0xFF) {
const unsigned int u2 = u - 0xA0;
if (tab_s[u2 >> 4] & ((unsigned short) 1 << (u2 & 0xF))) {
*dest = (unsigned char) u; /* Straight-thru */
return 1;
}
}
s = 0;
while (s <= e) {
const int m = (s + e) >> 1;
if (tab_u[m] < u) {
s = m + 1;
} else if (tab_u[m] > u) {
e = m - 1;
} else {
*dest = tab_sb[m];
return 1;
}
}
return 0;
}
/* Base Windows-125x routine to convert Unicode codepoint `u` */
static int u_cp125x(const unsigned int u, const unsigned short *tab_s, const unsigned short *tab_u,
const unsigned char *tab_sb, int e, unsigned char *dest) {
int s;
if (u < 0x80) {
*dest = (unsigned char) u;
return 1;
}
if (u <= 0xFF && u >= 0xA0) {
const unsigned int u2 = u - 0xA0;
if (tab_s[u2 >> 4] & ((unsigned short) 1 << (u2 & 0xF))) {
*dest = (unsigned char) u; /* Straight-thru */
return 1;
}
}
s = 0;
while (s <= e) {
const int m = (s + e) >> 1;
if (tab_u[m] < u) {
s = m + 1;
} else if (tab_u[m] > u) {
e = m - 1;
} else {
*dest = tab_sb[m];
return 1;
}
}
return 0;
}
/* ECI 27 ASCII (ISO/IEC 646:1991 IRV (US)) */
static int u_ascii(const unsigned int u, unsigned char *dest) {
if (u < 0x80) {
*dest = (unsigned char) u;
return 1;
}
return 0;
}
/* ECI 170 ASCII subset (ISO/IEC 646:1991 Invariant), excludes 12 chars that historically had national variants,
namely "#$@[\]^`{|}~" */
static int u_ascii_inv(const unsigned int u, unsigned char *dest) {
if (u == 0x7F || (u <= 'z' && u != '#' && u != '$' && u != '@' && (u <= 'Z' || u == '_' || u >= 'a'))) {
*dest = (unsigned char) u;
return 1;
}
return 0;
}
/* ECI 25 UTF-16 Big Endian (ISO/IEC 10646) - assumes valid Unicode */
static int u_utf16be(const unsigned int u, unsigned char *dest) {
unsigned int u2, v;
if (u < 0x10000) {
dest[0] = (unsigned char) (u >> 8);
dest[1] = (unsigned char) u;
return 2;
}
u2 = u - 0x10000;
v = u2 >> 10;
dest[0] = (unsigned char) (0xD8 + (v >> 8));
dest[1] = (unsigned char) v;
v = u2 & 0x3FF;
dest[2] = (unsigned char) (0xDC + (v >> 8));
dest[3] = (unsigned char) v;
return 4;
}
/* ECI 33 UTF-16 Little Endian (ISO/IEC 10646) - assumes valid Unicode */
static int u_utf16le(const unsigned int u, unsigned char *dest) {
unsigned int u2, v;
if (u < 0x10000) {
dest[0] = (unsigned char) u;
dest[1] = (unsigned char) (u >> 8);
return 2;
}
u2 = u - 0x10000;
v = u2 >> 10;
dest[0] = (unsigned char) v;
dest[1] = (unsigned char) (0xD8 + (v >> 8));
v = u2 & 0x3FF;
dest[2] = (unsigned char) v;
dest[3] = (unsigned char) (0xDC + (v >> 8));
return 4;
}
/* ECI 34 UTF-32 Big Endian (ISO/IEC 10646) - assumes valid Unicode */
static int u_utf32be(const unsigned int u, unsigned char *dest) {
dest[0] = 0;
dest[1] = (unsigned char) (u >> 16);
dest[2] = (unsigned char) (u >> 8);
dest[3] = (unsigned char) u;
return 4;
}
/* ECI 35 UTF-32 Little Endian (ISO/IEC 10646) - assumes valid Unicode */
static int u_utf32le(const unsigned int u, unsigned char *dest) {
dest[0] = (unsigned char) u;
dest[1] = (unsigned char) (u >> 8);
dest[2] = (unsigned char) (u >> 16);
dest[3] = 0;
return 4;
}
/* ECI 899 Binary, included for libzueci compatibility - assumes valid Unicode */
static int u_binary(const unsigned int u, unsigned char *dest) {
if (u <= 0xFF) {
*dest = (unsigned char) u;
return 1;
}
return 0;
}
/* Multibyte stuff */
/* Acknowledgements to Bruno Haible <bruno@clisp.org> for a no. of techniques used here */
/* Helper to lookup Unicode codepoint `u` in the URO (Unified Repertoire and Ordering) block (U+4E00-9FFF) */
static int eci_u_lookup_uro_int(const unsigned int u, const unsigned short *tab_u, const unsigned short *tab_mb_ind,
const unsigned short *tab_mb, unsigned int *d) {
unsigned int u2 = (u - 0x4E00) >> 4; /* Blocks of 16 */
unsigned int v = (unsigned int) 1 << (u & 0xF);
if ((tab_u[u2] & v) == 0) {
return 0;
}
v = tab_u[u2] & (v - 1); /* Mask to bits prior to this one */
/* Count bits set (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel) */
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
v = (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
*d = tab_mb[tab_mb_ind[u2] + v];
return 2;
}
/* Version of `eci_u_lookup_uro_int()` taking unsigned char destination */
static int eci_u_lookup_uro(const unsigned int u, const unsigned short *tab_u, const unsigned short *tab_mb_ind,
const unsigned short *tab_mb, unsigned char *dest) {
unsigned int d;
int ret = eci_u_lookup_uro_int(u, tab_u, tab_mb_ind, tab_mb, &d);
if (ret) {
dest[0] = (unsigned char) (d >> 8);
dest[1] = (unsigned char) d;
}
return ret;
}
/* ECI 20 Shift JIS */
static int u_sjis_int(const unsigned int u, unsigned int *d) {
unsigned int u2, dv, md;
int s, e;
if (u < 0x80 && u != 0x5C && u != 0x7E) { /* Backslash & tilde re-mapped according to JIS X 0201 Roman */
*d = u;
return 1;
}
/* Special case URO block sequential mappings (considerably lessens size of `sjis_u[]` array) */
if (u >= 0x4E00 && u <= 0xDFFF) { /* 0xE000 next used value >= 0x4E00 */
if (u >= 0x9FB0) {
return 0;
}
return eci_u_lookup_uro_int(u, sjis_uro_u, sjis_uro_mb_ind, sjis_mb, d);
}
/* PUA to user-defined (Table 4-86, Lunde, 2nd ed.) */
if (u >= 0xE000 && u <= 0xE757) {
u2 = u - 0xE000;
dv = u2 / 188;
md = u2 - dv * 188;
*d = ((dv + 0xF0) << 8) | (md + 0x40 + (md >= 0x3F));
return 2;
}
if (u >= sjis_u[0] && u <= sjis_u[ARRAY_SIZE(sjis_u) - 1]) {
s = 0;
e = ARRAY_SIZE(sjis_u) - 1;
while (s <= e) {
const int m = (s + e) >> 1;
if (sjis_u[m] < u) {
s = m + 1;
} else if (sjis_u[m] > u) {
e = m - 1;
} else {
*d = sjis_mb[u >= 0x4E00 ? m + 6356 : m]; /* Adjust for URO block */
return 1 + (*d > 0xFF);
}
}
}
return 0;
}
#ifdef ZINT_TEST /* Wrapper for direct testing */
INTERNAL int u_sjis_int_test(const unsigned int u, unsigned int *d) {
return u_sjis_int(u, d);
}
#endif
/* Version of `u_sjis_int()` taking unsigned char destination, for use by `utf8_to_eci()` */
static int u_sjis(const unsigned int u, unsigned char *dest) {
unsigned int d;
int ret = u_sjis_int(u, &d);
if (ret) {
if (ret == 1) {
dest[0] = (unsigned char) d;
} else {
dest[0] = (unsigned char) (d >> 8);
dest[1] = (unsigned char) d;
}
}
return ret;
}
/* ECI 28 Big5 Chinese (Taiwan) */
static int u_big5(const unsigned int u, unsigned char *dest) {
int s, e;
if (u < 0x80) {
*dest = (unsigned char) u;
return 1;
}
/* Special case URO block sequential mappings (considerably lessens size of `big5_u[]` array) */
if (u >= 0x4E00 && u <= 0xFA0B) { /* 0xFA0C next used value >= 0x4E00 */
if (u >= 0x9FB0) {
return 0;
}
return eci_u_lookup_uro(u, big5_uro_u, big5_uro_mb_ind, big5_mb, dest);
}
if (u >= big5_u[0] && u <= big5_u[ARRAY_SIZE(big5_u) - 1]) {
s = 0;
e = ARRAY_SIZE(big5_u) - 1;
while (s <= e) {
const int m = (s + e) >> 1;
if (big5_u[m] < u) {
s = m + 1;
} else if (big5_u[m] > u) {
e = m - 1;
} else {
const unsigned short mb = big5_mb[u >= 0x4E00 ? m + 13061 : m]; /* Adjust for URO block */
dest[0] = (unsigned char) (mb >> 8);
dest[1] = (unsigned char) mb;
return 2;
}
}
}
return 0;
}
#ifdef ZINT_TEST /* Wrapper for direct testing */
INTERNAL int u_big5_test(const unsigned int u, unsigned char *dest) {
return u_big5(u, dest);
}
#endif
/* ECI 30 EUC-KR (KS X 1001, formerly KS C 5601) Korean */
static int u_ksx1001(const unsigned int u, unsigned char *dest) {
int s, e;
if (u < 0x80) {
*dest = (unsigned char) u;
return 1;
}
/* Special case URO block sequential mappings (considerably lessens size of `ksx1001_u[]` array) */
if (u >= 0x4E00 && u <= 0xABFF) { /* 0xAC00 next used value >= 0x4E00 */
if (u >= 0x9FA0) {
return 0;
}
return eci_u_lookup_uro(u, ksx1001_uro_u, ksx1001_uro_mb_ind, ksx1001_mb, dest);
}
if (u >= ksx1001_u[0] && u <= ksx1001_u[ARRAY_SIZE(ksx1001_u) - 1]) {
s = ksx1001_u_ind[(u - ksx1001_u[0]) >> 8];
e = s + 0x100 > ARRAY_SIZE(ksx1001_u) ? ARRAY_SIZE(ksx1001_u) - 1 : s + 0x100 - 1;
while (s <= e) {
const int m = (s + e) >> 1;
if (ksx1001_u[m] < u) {
s = m + 1;
} else if (ksx1001_u[m] > u) {
e = m - 1;
} else {
const unsigned short mb = ksx1001_mb[u >= 0x4E00 ? m + 4620 : m]; /* Adjust for URO block */
dest[0] = (unsigned char) (mb >> 8);
dest[1] = (unsigned char) mb;
return 2;
}
}
}
return 0;
}
#ifdef ZINT_TEST /* Wrapper for direct testing */
INTERNAL int u_ksx1001_test(const unsigned int u, unsigned char *dest) {
return u_ksx1001(u, dest);
}
#endif
/* ECI 29 GB 2312 Chinese (PRC) */
static int u_gb2312_int(const unsigned int u, unsigned int *d) {
int s, e;
if (u < 0x80) {
*d = u;
return 1;
}
/* Special case URO block sequential mappings (considerably lessens size of `gb2312_u[]` array) */
if (u >= 0x4E00 && u <= 0x9E1E) { /* 0x9E1F next used value >= 0x4E00 */
if (u >= 0x9CF0) {
return 0;
}
return eci_u_lookup_uro_int(u, gb2312_uro_u, gb2312_uro_mb_ind, gb2312_mb, d);
}
if (u >= gb2312_u[0] && u <= gb2312_u[ARRAY_SIZE(gb2312_u) - 1]) {
s = gb2312_u_ind[(u - gb2312_u[0]) >> 8];
e = s + 0x100 > ARRAY_SIZE(gb2312_u) ? ARRAY_SIZE(gb2312_u) - 1 : s + 0x100 - 1;
while (s <= e) {
const int m = (s + e) >> 1;
if (gb2312_u[m] < u) {
s = m + 1;
} else if (gb2312_u[m] > u) {
e = m - 1;
} else {
*d = gb2312_mb[u > 0x4E00 ? m + 6627 : m]; /* Adjust for URO block */
return 2;
}
}
}
return 0;
}
#ifdef ZINT_TEST /* Wrapper for direct testing */
INTERNAL int u_gb2312_int_test(const unsigned int u, unsigned int *d) {
return u_gb2312_int(u, d);
}
#endif
/* Version of `u_gb2312_int()` taking unsigned char destination, for use by `utf8_to_eci()` */
static int u_gb2312(const unsigned int u, unsigned char *dest) {
unsigned int d;
int ret = u_gb2312_int(u, &d);
if (ret) {
if (ret == 1) {
dest[0] = (unsigned char) d;
} else {
dest[0] = (unsigned char) (d >> 8);
dest[1] = (unsigned char) d;
}
}
return ret;
}
/* ECI 31 GBK Chinese */
static int u_gbk_int(const unsigned int u, unsigned int *d) {
int s, e;
if (u < 0x80) {
*d = u;
return 1;
}
/* Check GB 2312 first */
if (u == 0x30FB) {
/* KATAKANA MIDDLE DOT, mapped by GB 2312 but not by GBK (U+00B7 MIDDLE DOT mapped to 0xA1A4 instead) */
return 0;
}
if (u == 0x2015) {
/* HORIZONTAL BAR, mapped to 0xA844 by GBK rather than 0xA1AA (U+2014 EM DASH mapped there instead) */
*d = 0xA844;
return 2;
}
if (u_gb2312_int(u, d)) { /* Includes the 2 GB 6345.1-86 corrections given in Table 3-22, Lunde, 2nd ed. */
return 2;
}
/* Special case URO block sequential mappings (considerably lessens size of `gbk_u[]` array) */
if (u >= 0x4E00 && u <= 0xF92B) { /* 0xF92C next used value >= 0x4E00 */
if (u >= 0x9FB0) {
return 0;
}
return eci_u_lookup_uro_int(u, gbk_uro_u, gbk_uro_mb_ind, gbk_mb, d);
}
if (u >= gbk_u[0] && u <= gbk_u[ARRAY_SIZE(gbk_u) - 1]) {
s = 0;
e = ARRAY_SIZE(gbk_u) - 1;
while (s <= e) {
const int m = (s + e) >> 1;
if (gbk_u[m] < u) {
s = m + 1;
} else if (gbk_u[m] > u) {
e = m - 1;
} else {
*d = gbk_mb[u >= 0x4E00 ? m + 14139 : m]; /* Adjust for URO block */
return 2;
}
}
}
return 0;
}
#ifdef ZINT_TEST /* Wrapper for direct testing */
INTERNAL int u_gbk_int_test(const unsigned int u, unsigned int *d) {
return u_gbk_int(u, d);
}
#endif
/* Version of `u_gbk_int()` taking unsigned char destination, for use by `utf8_to_eci()` */
static int u_gbk(const unsigned int u, unsigned char *dest) {
unsigned int d;
int ret = u_gbk_int(u, &d);
if (ret) {
if (ret == 1) {
dest[0] = (unsigned char) d;
} else {
dest[0] = (unsigned char) (d >> 8);
dest[1] = (unsigned char) d;
}
}
return ret;
}
/* Helper for `u_gb18030_int()` to output 4-byte sequential blocks */
static int u_gb18030_4_sequential_int(unsigned int u2, unsigned int mb_lead, unsigned int *d1, unsigned int *d2) {
unsigned int dv;
dv = u2 / 10;
*d2 = u2 - dv * 10 + 0x30;
u2 = dv;
dv = u2 / 126;
*d2 |= (u2 - dv * 126 + 0x81) << 8;
u2 = dv;
dv = u2 / 10;
*d1 = ((dv + mb_lead) << 8) | (u2 - dv * 10 + 0x30);
return 4;
}
/* ECI 32 GB 18030 Chinese - assumes valid Unicode */
static int u_gb18030_int(const unsigned int u, unsigned int *d1, unsigned int *d2) {
unsigned int u2, dv;
int s, e;
if (u < 0x80) {
*d1 = u;
return 1;
}
/* Check GBK first */
if (u_gbk_int(u, d1)) {
return 2;
}
if (u >= 0x10000) {
/* Non-PUA, non-BMP, see Table 3-37, Lunde, 2nd ed. */
if (u == 0x20087) {
*d1 = 0xFE51;
return 2;
}
if (u == 0x20089) {
*d1 = 0xFE52;
return 2;
}
if (u == 0x200CC) {
*d1 = 0xFE53;
return 2;
}
if (u == 0x215D7) {
*d1 = 0xFE6C;
return 2;
}
if (u == 0x2298F) {
*d1 = 0xFE76;
return 2;
}
if (u == 0x241FE) {
*d1 = 0xFE91;
return 2;
}
/* All other non-BMP U+10000-10FFFF */
return u_gb18030_4_sequential_int(u - 0x10000, 0x90, d1, d2);
}
if (u >= 0xE000 && u <= 0xE765) { /* PUA to user-defined */
if (u <= 0xE4C5) {
u2 = u - 0xE000;
dv = u2 / 94;
*d1 = ((dv + (dv < 6 ? 0xAA : 0xF2)) << 8) | (u2 - dv * 94 + 0xA1);
} else {
unsigned int md;
u2 = u - 0xE4C6;
dv = u2 / 96;
md = u2 - dv * 96;
*d1 = ((dv + 0xA1) << 8) | (md + 0x40 + (md >= 0x3F));
}
return 2;
}
if (u >= gb18030_2_u[0] && u <= gb18030_2_u[ARRAY_SIZE(gb18030_2_u) - 1]) {
s = 0;
e = ARRAY_SIZE(gb18030_2_u) - 1;
while (s <= e) {
const int m = (s + e) >> 1;
if (gb18030_2_u[m] < u) {
s = m + 1;
} else if (gb18030_2_u[m] > u) {
e = m - 1;
} else {
*d1 = gb18030_2_mb[m];
return 2;
}
}
}
/* All other BMP U+0080-FFFF */
if (u == 0xE7C7) { /* PUA change to non-PUA, see Table 3-39, Lunde, 2nd ed. */
*d1 = 0x8135;
*d2 = 0xF437;
return 4;
}
s = 0;
e = ARRAY_SIZE(gb18030_4_u_e) - 1;
while (s < e) { /* Lower bound */
const int m = (s + e) >> 1;
if (gb18030_4_u_e[m] < u) {
s = m + 1;
} else {
e = m;
}
}
assert(s < ARRAY_SIZE(gb18030_4_u_e));
return u_gb18030_4_sequential_int(u - gb18030_4_mb_o[s] - 0x80, 0x81, d1, d2);
}
#ifdef ZINT_TEST /* Wrapper for direct testing */
INTERNAL int u_gb18030_int_test(const unsigned int u, unsigned int *d1, unsigned int *d2) {
return u_gb18030_int(u, d1, d2);
}
#endif
/* Version of `u_gb18030_int()` taking unsigned char destination, for use by `utf8_to_eci()` */
static int u_gb18030(const unsigned int u, unsigned char *dest) {
unsigned int d1, d2;
int ret = u_gb18030_int(u, &d1, &d2);
if (ret) {
if (ret == 1) {
dest[0] = (unsigned char) d1;
} else {
dest[0] = (unsigned char) (d1 >> 8);
dest[1] = (unsigned char) d1;
if (ret == 4) {
dest[2] = (unsigned char) (d2 >> 8);
dest[3] = (unsigned char) d2;
}
}
}
return ret;
}
/* Main ECI stuff */
/* Helper to count the number of chars in a string within a range */
static int chr_range_cnt(const unsigned char string[], const int length, const unsigned char c1,
const unsigned char c2) {
int count = 0;
int i;
if (c1) {
for (i = 0; i < length; i++) {
if (string[i] >= c1 && string[i] <= c2) {
count++;
}
}
} else {
for (i = 0; i < length; i++) {
if (string[i] <= c2) {
count++;
}
}
}
return count;
}
/* Is ECI convertible from UTF-8? */
INTERNAL int is_eci_convertible(const int eci) {
if (eci == 26 || (eci > 35 && eci != 170)) { /* Exclude ECI 170 - ASCII Invariant */
/* UTF-8 (26) or 8-bit binary data (899) or undefined (> 35 and < 899) or not character set (> 899) */
return 0;
}
return 1;
}
/* Are any of the ECIs in the segments convertible from UTF-8?
Sets `convertible[]` for each, which must be at least `seg_count` in size */
INTERNAL int is_eci_convertible_segs(const struct zint_seg segs[], const int seg_count, int convertible[]) {
int ret = 0;
int i;
for (i = 0; i < seg_count; i++) {
convertible[i] = is_eci_convertible(segs[i].eci);
ret |= convertible[i];
}
return ret;
}
/* Calculate length required to convert UTF-8 to (double-byte) encoding */
INTERNAL int get_eci_length(const int eci, const unsigned char source[], int length) {
if (eci == 20) { /* Shift JIS */
/* Only ASCII backslash (reverse solidus) exceeds UTF-8 length */
length += chr_cnt(source, length, '\\');
} else if (eci == 25 || eci == 33) { /* UTF-16 */
/* All ASCII chars take 2 bytes */
length += chr_range_cnt(source, length, 0, 0x7F);
/* Surrogate pairs are 4 UTF-8 bytes long so fit */
} else if (eci == 32) { /* GB 18030 */
/* Allow for GB 18030 4 byters */
length *= 2;
} else if (eci == 34 || eci == 35) { /* UTF-32 */
/* Quadruple-up ASCII and double-up non-ASCII */
length += chr_range_cnt(source, length, 0, 0x7F) * 2 + length;
}
/* Big5, GB 2312, EUC-KR and GBK fit in UTF-8 length */
return length;
}
/* Call `get_eci_length()` for each segment, returning total */
INTERNAL int get_eci_length_segs(const struct zint_seg segs[], const int seg_count) {
int length = 0;
int i;
for (i = 0; i < seg_count; i++) {
length += get_eci_length(segs[i].eci, segs[i].source, segs[i].length);
}
return length;
}
/* Convert UTF-8 to other character encodings */
typedef int (*eci_func_t)(const unsigned int u, unsigned char *dest);
INTERNAL int utf8_to_eci(const int eci, const unsigned char source[], unsigned char dest[], int *p_length) {
static const eci_func_t eci_funcs[36] = {
NULL, NULL, u_cp437, NULL, u_iso8859_2, /*0-4*/
u_iso8859_3, u_iso8859_4, u_iso8859_5, u_iso8859_6, u_iso8859_7, /*5-9*/
u_iso8859_8, u_iso8859_9, u_iso8859_10, u_iso8859_11, NULL, /*10-14*/
u_iso8859_13, u_iso8859_14, u_iso8859_15, u_iso8859_16, NULL, /*15-19*/
u_sjis, u_cp1250, u_cp1251, u_cp1252, u_cp1256, /*20-24*/
u_utf16be, NULL, u_ascii, u_big5, u_gb2312, /*25-29*/
u_ksx1001, u_gbk, u_gb18030, u_utf16le, u_utf32be, /*30-34*/
u_utf32le,
};
eci_func_t eci_func;
unsigned int codepoint, state = 0;
int in_posn = 0;
int out_posn = 0;
int length = *p_length;
/* Special case ISO/IEC 8859-1 */
/* Default ECI 0 to ISO/IEC 8859-1 (and ECI 1 for libzueci compatibility) */
if (eci == 0 || eci == 3 || eci == 1) {
while (in_posn < length) {
do {
decode_utf8(&state, &codepoint, source[in_posn++]);
} while (in_posn < length && state != 0 && state != 12);
if (state != 0) {
return ZINT_ERROR_INVALID_DATA;
}
if (codepoint >= 0x80 && (codepoint < 0xA0 || codepoint >= 0x100)) {
return ZINT_ERROR_INVALID_DATA;
}
dest[out_posn++] = (unsigned char) codepoint;
}
dest[out_posn] = '\0';
*p_length = out_posn;
return 0;
}
if (eci == 170) { /* ASCII Invariant (archaic subset) */
eci_func = u_ascii_inv;
} else if (eci == 899) { /* Binary, for libzueci compatibility */
eci_func = u_binary;
} else {
eci_func = eci_funcs[eci];
if (eci_func == NULL) {
return ZINT_ERROR_INVALID_DATA;
}
}
while (in_posn < length) {
int incr;
do {
decode_utf8(&state, &codepoint, source[in_posn++]);
} while (in_posn < length && state != 0 && state != 12);
if (state != 0) {
return ZINT_ERROR_INVALID_DATA;
}
incr = (*eci_func)(codepoint, dest + out_posn);
if (incr == 0) {
return ZINT_ERROR_INVALID_DATA;
}
out_posn += incr;
}
dest[out_posn] = '\0';
*p_length = out_posn;
return 0;
}
/* Find the lowest single-byte ECI mode which will encode a given set of Unicode text, assuming valid UTF-8 */
INTERNAL int get_best_eci(const unsigned char source[], int length) {
int eci = 3;
/* Note: attempting single-byte conversions only, so get_eci_length() unnecessary */
unsigned char *local_source = (unsigned char *) z_alloca(length + 1);
do {
if (eci == 14) { /* Reserved */
eci = 15;
} else if (eci == 19) { /* Reserved */
eci = 21; /* Skip 20 Shift JIS */
}
if (utf8_to_eci(eci, source, local_source, &length) == 0) {
return eci;
}
eci++;
} while (eci < 25);
assert(is_valid_utf8(source, length));
return 26; /* If all of these fail, use UTF-8! */
}
/* Call `get_best_eci()` for each segment, assuming valid UTF-8. Returns 0 on failure, first ECI set on success */
INTERNAL int get_best_eci_segs(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count) {
const int default_eci = symbol->symbology == BARCODE_GRIDMATRIX ? 29 : symbol->symbology == BARCODE_UPNQR ? 4 : 3;
int first_eci_set = 0;
int i;
for (i = 0; i < seg_count; i++) {
if (segs[i].eci == 0) {
const int eci = get_best_eci(segs[i].source, segs[i].length);
if (eci == default_eci) {
if (i != 0 && segs[i - 1].eci != 0 && segs[i - 1].eci != default_eci) {
segs[i].eci = eci;
if (first_eci_set == 0) {
first_eci_set = eci;
}
}
} else {
segs[i].eci = eci;
if (first_eci_set == 0) {
first_eci_set = eci;
if (i == 0) {
symbol->eci = eci;
}
}
}
}
}
return first_eci_set;
}
/* QRCODE Shift JIS helpers */
/* Convert UTF-8 string to Shift JIS and place in array of ints */
INTERNAL int sjis_utf8(struct zint_symbol *symbol, const unsigned char source[], int *p_length,
unsigned int *ddata) {
int error_number;
unsigned int i, length;
unsigned int *utfdata = (unsigned int *) z_alloca(sizeof(unsigned int) * (*p_length + 1));
error_number = utf8_to_unicode(symbol, source, utfdata, p_length, 1 /*disallow_4byte*/);
if (error_number != 0) {
return error_number;
}
for (i = 0, length = *p_length; i < length; i++) {
if (!u_sjis_int(utfdata[i], ddata + i)) {
return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 800, "Invalid character in input");
}
}
return 0;
}
/* If `full_multibyte` set, copy byte input stream to array of ints, putting double-bytes that match QR Kanji mode in
* a single entry. If `full_multibyte` not set, do a straight copy */
INTERNAL void sjis_cpy(const unsigned char source[], int *p_length, unsigned int *ddata, const int full_multibyte) {
unsigned int i, j, length;
unsigned char c1, c2;
if (full_multibyte) {
for (i = 0, j = 0, length = *p_length; i < length; i++, j++) {
c1 = source[i];
/* Now using stricter interpretation of standard, and excluding certain trailing bytes */
if (((c1 >= 0x81 && c1 <= 0x9F) || (c1 >= 0xE0 && c1 <= 0xEB)) && length - i >= 2) {
c2 = source[i + 1];
if ((c2 >= 0x40 && c2 <= 0xFC) && c2 != 0x7F && (c1 != 0xEB || c2 <= 0xBF)) {
/* This may or may not be valid Shift JIS, but don't care as long as it can be encoded in
* QR Kanji mode */
ddata[j] = (c1 << 8) | c2;
i++;
} else {
ddata[j] = c1;
}
} else {
ddata[j] = c1;
}
}
*p_length = j;
} else {
/* Straight copy */
for (i = 0, length = *p_length; i < length; i++) {
ddata[i] = source[i];
}
}
}
/* Call `sjis_cpy()` for each segment */
INTERNAL int sjis_cpy_segs(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count,
unsigned int *ddata, const int full_multibyte) {
int i;
unsigned int *dd = ddata;
const int raw_text = symbol->output_options & BARCODE_RAW_TEXT; /* Note only called for DATA_MODE */
for (i = 0; i < seg_count; i++) {
sjis_cpy(segs[i].source, &segs[i].length, dd, full_multibyte);
if (raw_text && rt_cpy_seg_ddata(symbol, i, &segs[i], 0 /*eci*/, dd)) {
return ZINT_ERROR_MEMORY; /* `rt_cpy_seg_ddata()` only fails with OOM */
}
dd += segs[i].length;
}
return 0;
}
/* Convert UTF-8 string to ECI and place in array of ints using `sjis_cpy()` */
INTERNAL int sjis_utf8_to_eci(const int eci, const unsigned char source[], int *p_length, unsigned int *ddata,
const int full_multibyte) {
if (is_eci_convertible(eci)) {
int error_number;
const int eci_length = get_eci_length(eci, source, *p_length);
unsigned char *converted = (unsigned char *) z_alloca(eci_length + 1);
error_number = utf8_to_eci(eci, source, converted, p_length);
if (error_number != 0) {
/* Note not setting `symbol->errtxt`, up to caller */
return error_number;
}
sjis_cpy(converted, p_length, ddata, full_multibyte || eci == 20);
} else {
sjis_cpy(source, p_length, ddata, full_multibyte);
}
return 0;
}
/* GRIDMATRIX GB 2312 helpers */
/* Convert UTF-8 string to GB 2312 (EUC-CN) and place in array of ints */
INTERNAL int gb2312_utf8(struct zint_symbol *symbol, const unsigned char source[], int *p_length,
unsigned int *ddata) {
int error_number;
unsigned int i, length;
unsigned int *utfdata = (unsigned int *) z_alloca(sizeof(unsigned int) * (*p_length + 1));
error_number = utf8_to_unicode(symbol, source, utfdata, p_length, 1 /*disallow_4byte*/);
if (error_number != 0) {
return error_number;
}
for (i = 0, length = *p_length; i < length; i++) {
if (utfdata[i] < 0x80) {
ddata[i] = utfdata[i];
} else {
if (!u_gb2312_int(utfdata[i], ddata + i)) {
return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 810, "Invalid character in input");
}
}
}
return 0;
}
/* If `full_multibyte` set, copy byte input stream to array of ints, putting double-bytes that match GRIDMATRIX
* Chinese mode in a single entry. If `full_multibyte` not set, do a straight copy */
static void gb2312_cpy(const unsigned char source[], int *p_length, unsigned int *ddata,
const int full_multibyte) {
unsigned int i, j, length;
unsigned char c1, c2;
if (full_multibyte) {
for (i = 0, j = 0, length = *p_length; i < length; i++, j++) {
if (length - i >= 2) {
c1 = source[i];
c2 = source[i + 1];
if (((c1 >= 0xA1 && c1 <= 0xA9) || (c1 >= 0xB0 && c1 <= 0xF7)) && c2 >= 0xA1 && c2 <= 0xFE) {
/* This may or may not be valid GB 2312 (EUC-CN), but don't care as long as it can be encoded in
* GRIDMATRIX Chinese mode */
ddata[j] = (c1 << 8) | c2;
i++;
} else {
ddata[j] = c1;
}
} else {
ddata[j] = source[i];
}
}
*p_length = j;
} else {
/* Straight copy */
for (i = 0, length = *p_length; i < length; i++) {
ddata[i] = source[i];
}
}
}
#ifdef ZINT_TEST /* Wrapper for direct testing */
INTERNAL void gb2312_cpy_test(const unsigned char source[], int *p_length, unsigned int *ddata,
const int full_multibyte) {
gb2312_cpy(source, p_length, ddata, full_multibyte);
}
#endif
/* Call `gb2312_cpy()` for each segment */
INTERNAL int gb2312_cpy_segs(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count,
unsigned int *ddata, const int full_multibyte) {
int i;
unsigned int *dd = ddata;
const int raw_text = symbol->output_options & BARCODE_RAW_TEXT;
for (i = 0; i < seg_count; i++) {
const int eci = segs[i].eci ? segs[i].eci : 29; /* Need to set as `rt_cpy_seg_ddata()` defaults to 3 */
gb2312_cpy(segs[i].source, &segs[i].length, dd, full_multibyte);
if (raw_text && rt_cpy_seg_ddata(symbol, i, &segs[i], eci, dd)) {
return ZINT_ERROR_MEMORY; /* `rt_cpy_seg_ddata()` only fails with OOM */
}
dd += segs[i].length;
}
return 0;
}
/* Convert UTF-8 string to ECI and place in array of ints using `gb2312_cpy()` */
INTERNAL int gb2312_utf8_to_eci(const int eci, const unsigned char source[], int *p_length, unsigned int *ddata,
const int full_multibyte) {
if (is_eci_convertible(eci)) {
int error_number;
const int eci_length = get_eci_length(eci, source, *p_length);
unsigned char *converted = (unsigned char *) z_alloca(eci_length + 1);
error_number = utf8_to_eci(eci, source, converted, p_length);
if (error_number != 0) {
/* Note not setting `symbol->errtxt`, up to caller */
return error_number;
}
gb2312_cpy(converted, p_length, ddata, full_multibyte || eci == 29);
} else {
gb2312_cpy(source, p_length, ddata, full_multibyte);
}
return 0;
}
/* HANXIN GB 18030 helpers */
/* Convert UTF-8 string to GB 18030 and place in array of ints */
INTERNAL int gb18030_utf8(struct zint_symbol *symbol, const unsigned char source[], int *p_length,
unsigned int *ddata) {
int error_number, ret;
unsigned int i, j, length;
unsigned int *utfdata = (unsigned int *) z_alloca(sizeof(unsigned int) * (*p_length + 1));
error_number = utf8_to_unicode(symbol, source, utfdata, p_length, 0 /*disallow_4byte*/);
if (error_number != 0) {
return error_number;
}
for (i = 0, j = 0, length = *p_length; i < length; i++, j++) {
if (utfdata[i] < 0x80) {
ddata[j] = utfdata[i];
} else {
ret = u_gb18030_int(utfdata[i], ddata + j, ddata + j + 1);
if (ret == 0) { /* Should never happen, as GB 18030 is a UTF i.e. maps all Unicode codepoints */
return errtxt(ZINT_ERROR_INVALID_DATA, symbol, 820, "Invalid character in input"); /* Not reached */
}
if (ret == 4) {
j++;
}
}
}
*p_length = j;
return 0;
}
/* If `full_multibyte` set, copy byte input stream to array of ints, putting double-bytes that match HANXIN
* Chinese mode in single entry, and quad-bytes in 2 entries. If `full_multibyte` not set, do a straight copy */
static void gb18030_cpy(const unsigned char source[], int *p_length, unsigned int *ddata,
const int full_multibyte) {
unsigned int i, j, length;
int done;
unsigned char c1, c2, c3, c4;
if (full_multibyte) {
for (i = 0, j = 0, length = *p_length; i < length; i++, j++) {
done = 0;
c1 = source[i];
if (length - i >= 2) {
if (c1 >= 0x81 && c1 <= 0xFE) {
c2 = source[i + 1];
if ((c2 >= 0x40 && c2 <= 0x7E) || (c2 >= 0x80 && c2 <= 0xFE)) {
ddata[j] = (c1 << 8) | c2;
i++;
done = 1;
} else if (length - i >= 4 && (c2 >= 0x30 && c2 <= 0x39)) {
c3 = source[i + 2];
c4 = source[i + 3];
if ((c3 >= 0x81 && c3 <= 0xFE) && (c4 >= 0x30 && c4 <= 0x39)) {
ddata[j++] = (c1 << 8) | c2;
ddata[j] = (c3 << 8) | c4;
i += 3;
done = 1;
}
}
}
}
if (!done) {
ddata[j] = c1;
}
}
*p_length = j;
} else {
/* Straight copy */
for (i = 0, length = *p_length; i < length; i++) {
ddata[i] = source[i];
}
}
}
#ifdef ZINT_TEST /* Wrapper for direct testing */
INTERNAL void gb18030_cpy_test(const unsigned char source[], int *p_length, unsigned int *ddata,
const int full_multibyte) {
gb18030_cpy(source, p_length, ddata, full_multibyte);
}
#endif
/* Call `gb18030_cpy()` for each segment */
INTERNAL int gb18030_cpy_segs(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count,
unsigned int *ddata, const int full_multibyte) {
int i;
unsigned int *dd = ddata;
const int raw_text = symbol->output_options & BARCODE_RAW_TEXT;
for (i = 0; i < seg_count; i++) {
gb18030_cpy(segs[i].source, &segs[i].length, dd, full_multibyte);
if (raw_text && rt_cpy_seg_ddata(symbol, i, &segs[i], 0 /*eci*/, dd)) {
return ZINT_ERROR_MEMORY; /* `rt_cpy_seg_ddata()` only fails with OOM */
}
dd += segs[i].length;
}
return 0;
}
/* Convert UTF-8 string to ECI and place in array of ints using `gb18030_cpy()` */
INTERNAL int gb18030_utf8_to_eci(const int eci, const unsigned char source[], int *p_length, unsigned int *ddata,
const int full_multibyte) {
if (is_eci_convertible(eci)) {
int error_number;
const int eci_length = get_eci_length(eci, source, *p_length);
unsigned char *converted = (unsigned char *) z_alloca(eci_length + 1);
error_number = utf8_to_eci(eci, source, converted, p_length);
if (error_number != 0) {
/* Note not setting `symbol->errtxt`, up to caller */
return error_number;
}
/* GB 18030 (ECI 32) superset of GB 2312 (ECI 29) and GBK (ECI 31) */
gb18030_cpy(converted, p_length, ddata, full_multibyte || eci == 32 || eci == 29 || eci == 31);
} else {
gb18030_cpy(source, p_length, ddata, full_multibyte);
}
return 0;
}
/* vim: set ts=4 sw=4 et : */
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.