File: dmatrix.c

package info (click to toggle)
zxing-cpp 3.0.2%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,204 kB
  • sloc: ansic: 69,384; cpp: 34,624; php: 2,790; python: 199; makefile: 31; sh: 3
file content (1980 lines) | stat: -rw-r--r-- 77,628 bytes parent folder | download
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
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
/* dmatrix.c Handles Data Matrix ECC 200 symbols */
/*
    libzint - the open source barcode library
    Copyright (C) 2009-2025 Robin Stuart <rstuart114@gmail.com>

    developed from and including some functions from:
        IEC16022 bar code generation
        Adrian Kennard, Andrews & Arnold Ltd
        with help from Cliff Hones on the RS coding

        (c) 2004 Adrian Kennard, Andrews & Arnold Ltd
        (c) 2006 Stefan Schmidt <stefan@datenfreihafen.org>

    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 <limits.h>
#include <stdio.h>
#include "common.h"
#include "reedsol.h"
#include "dmatrix.h"

/* Annex F placement algorithm low level */
static void dm_placementbit(int *array, const int NR, const int NC, int r, int c, const int p, const char b) {
    if (r < 0) {
        r += NR;
        c += 4 - ((NR + 4) % 8);
    }
    if (c < 0) {
        c += NC;
        r += 4 - ((NC + 4) % 8);
    }
    /* Necessary for DMRE (ISO/IEC 21471:2020 Annex E) */
    if (r >= NR) {
        r -= NR;
    }
    /* Check index limits */
    assert(r < NR);
    assert(c < NC);
    /* Check double-assignment */
    assert(0 == array[r * NC + c]);
    array[r * NC + c] = (p << 3) + b;
}

static void dm_placementblock(int *array, const int NR, const int NC, const int r,
        const int c, const int p) {
    dm_placementbit(array, NR, NC, r - 2, c - 2, p, 7);
    dm_placementbit(array, NR, NC, r - 2, c - 1, p, 6);
    dm_placementbit(array, NR, NC, r - 1, c - 2, p, 5);
    dm_placementbit(array, NR, NC, r - 1, c - 1, p, 4);
    dm_placementbit(array, NR, NC, r - 1, c - 0, p, 3);
    dm_placementbit(array, NR, NC, r - 0, c - 2, p, 2);
    dm_placementbit(array, NR, NC, r - 0, c - 1, p, 1);
    dm_placementbit(array, NR, NC, r - 0, c - 0, p, 0);
}

static void dm_placementcornerA(int *array, const int NR, const int NC, const int p) {
    dm_placementbit(array, NR, NC, NR - 1, 0, p, 7);
    dm_placementbit(array, NR, NC, NR - 1, 1, p, 6);
    dm_placementbit(array, NR, NC, NR - 1, 2, p, 5);
    dm_placementbit(array, NR, NC, 0, NC - 2, p, 4);
    dm_placementbit(array, NR, NC, 0, NC - 1, p, 3);
    dm_placementbit(array, NR, NC, 1, NC - 1, p, 2);
    dm_placementbit(array, NR, NC, 2, NC - 1, p, 1);
    dm_placementbit(array, NR, NC, 3, NC - 1, p, 0);
}

static void dm_placementcornerB(int *array, const int NR, const int NC, const int p) {
    dm_placementbit(array, NR, NC, NR - 3, 0, p, 7);
    dm_placementbit(array, NR, NC, NR - 2, 0, p, 6);
    dm_placementbit(array, NR, NC, NR - 1, 0, p, 5);
    dm_placementbit(array, NR, NC, 0, NC - 4, p, 4);
    dm_placementbit(array, NR, NC, 0, NC - 3, p, 3);
    dm_placementbit(array, NR, NC, 0, NC - 2, p, 2);
    dm_placementbit(array, NR, NC, 0, NC - 1, p, 1);
    dm_placementbit(array, NR, NC, 1, NC - 1, p, 0);
}

static void dm_placementcornerC(int *array, const int NR, const int NC, const int p) {
    dm_placementbit(array, NR, NC, NR - 3, 0, p, 7);
    dm_placementbit(array, NR, NC, NR - 2, 0, p, 6);
    dm_placementbit(array, NR, NC, NR - 1, 0, p, 5);
    dm_placementbit(array, NR, NC, 0, NC - 2, p, 4);
    dm_placementbit(array, NR, NC, 0, NC - 1, p, 3);
    dm_placementbit(array, NR, NC, 1, NC - 1, p, 2);
    dm_placementbit(array, NR, NC, 2, NC - 1, p, 1);
    dm_placementbit(array, NR, NC, 3, NC - 1, p, 0);
}

static void dm_placementcornerD(int *array, const int NR, const int NC, const int p) {
    dm_placementbit(array, NR, NC, NR - 1, 0, p, 7);
    dm_placementbit(array, NR, NC, NR - 1, NC - 1, p, 6);
    dm_placementbit(array, NR, NC, 0, NC - 3, p, 5);
    dm_placementbit(array, NR, NC, 0, NC - 2, p, 4);
    dm_placementbit(array, NR, NC, 0, NC - 1, p, 3);
    dm_placementbit(array, NR, NC, 1, NC - 3, p, 2);
    dm_placementbit(array, NR, NC, 1, NC - 2, p, 1);
    dm_placementbit(array, NR, NC, 1, NC - 1, p, 0);
}

/* Annex F placement algorithm main function */
static void dm_placement(int *array, const int NR, const int NC) {
    int r, c, p;
    /* start */
    p = 1;
    r = 4;
    c = 0;
    do {
        /* check corner */
        if (r == NR && !c)
            dm_placementcornerA(array, NR, NC, p++);
        if (r == NR - 2 && !c && NC % 4)
            dm_placementcornerB(array, NR, NC, p++);
        if (r == NR - 2 && !c && (NC % 8) == 4)
            dm_placementcornerC(array, NR, NC, p++);
        if (r == NR + 4 && c == 2 && !(NC % 8))
            dm_placementcornerD(array, NR, NC, p++);
        /* up/right */
        do {
            if (r < NR && c >= 0 && !array[r * NC + c])
                dm_placementblock(array, NR, NC, r, c, p++);
            r -= 2;
            c += 2;
        } while (r >= 0 && c < NC);
        r++;
        c += 3;
        /* down/left */
        do {
            if (r >= 0 && c < NC && !array[r * NC + c])
                dm_placementblock(array, NR, NC, r, c, p++);
            r += 2;
            c -= 2;
        } while (r < NR && c >= 0);
        r += 3;
        c++;
    } while (r < NR || c < NC);
    /* unfilled corner */
    if (!array[NR * NC - 1])
        array[NR * NC - 1] = array[NR * NC - NC - 2] = 1;
}

/* calculate and append ecc code, and if necessary interleave */
static void dm_ecc(unsigned char *binary, const int bytes, const int datablock, const int rsblock, const int skew) {
    int blocks = (bytes + 2) / datablock, b;
    int rsblocks = rsblock * blocks;
    int n;
    rs_t rs;

    zint_rs_init_gf(&rs, 0x12d);
    zint_rs_init_code(&rs, rsblock, 1);
    for (b = 0; b < blocks; b++) {
        unsigned char buf[256], ecc[256];
        int p = 0;
        for (n = b; n < bytes; n += blocks)
            buf[p++] = binary[n];
        zint_rs_encode(&rs, p, buf, ecc);
        if (skew) {
            /* Rotate ecc data to make 144x144 size symbols acceptable */
            /* See http://groups.google.com/group/postscriptbarcode/msg/5ae8fda7757477da
               or https://github.com/nu-book/zxing-cpp/issues/259 */
            for (n = b, p = 0; n < rsblocks; n += blocks, p++) {
                if (b < 8) {
                    binary[bytes + n + 2] = ecc[p];
                } else {
                    binary[bytes + n - 8] = ecc[p];
                }
            }
        } else {
            for (n = b, p = 0; n < rsblocks; n += blocks, p++) {
                binary[bytes + n] = ecc[p];
            }
        }
    }
}

/* Is basic (non-shifted) C40? */
static int dm_isc40(const unsigned char input) {
    if (input <= '9') {
        return input >= '0' || input == ' ';
    }
    return z_isupper(input);
}

/* Is basic (non-shifted) TEXT? */
static int dm_istext(const unsigned char input) {
    if (input <= '9') {
        return input >= '0' || input == ' ';
    }
    return z_islower(input);
}

/* Is basic (non-shifted) C40/TEXT? */
static int dm_isc40text(const int current_mode, const unsigned char input) {
    return current_mode == DM_C40 ? dm_isc40(input) : dm_istext(input);
}

/* Return true (1) if a character is valid in X12 set */
static int dm_isX12(const unsigned char input) {
    return dm_isc40(input) || input == 13 || input == '*' || input == '>';
}

/* Return true (1) if a character is valid in EDIFACT set */
static int dm_isedifact(const unsigned char input) {
    return input >= ' ' && input <= '^';
}

/* Does Annex J section (r)(6)(ii)(I) apply? */
static int dm_substep_r_6_2_1(const unsigned char source[], const int length, const int sp) {
    /* Annex J section (r)(6)(ii)(I)
       "If one of the three X12 terminator/separator characters first
        occurs in the yet to be processed data before a non-X12 character..."
     */
    int i;

    for (i = sp; i < length && dm_isX12(source[i]); i++) {
        if (source[i] == 13 || source[i] == '*' || source[i] == '>') {
            return 1;
        }
    }

    return 0;
}

/* Count number of TEXT characters around `sp` between `position` and `length`
   - helper to avoid exiting from Base 256 too early if have series of TEXT characters */
static int dm_text_sp_cnt(const unsigned char source[], const int position, const int length, const int sp) {
    int i;
    int cnt = 0;

    /* Count from `sp` forward */
    for (i = sp; i < length && dm_istext(source[i]); i++, cnt++);
    /* Count backwards from `sp` */
    for (i = sp - 1; i >= position && dm_istext(source[i]); i--, cnt++);

    return cnt;
}

/* Character counts are multiplied by this, so as to be whole integer divisible by 2, 3 and 4 */
#define DM_MULT             12

#define DM_MULT_1_DIV_2     6
#define DM_MULT_2_DIV_3     8
#define DM_MULT_3_DIV_4     9
#define DM_MULT_1           12
#define DM_MULT_4_DIV_3     16
#define DM_MULT_2           24
#define DM_MULT_8_DIV_3     32
#define DM_MULT_3           26
#define DM_MULT_13_DIV_4    39
#define DM_MULT_10_DIV_3    40
#define DM_MULT_4           48
#define DM_MULT_17_DIV_4    51
#define DM_MULT_13_DIV_3    52

#define DM_MULT_MINUS_1     11
#define DM_MULT_CEIL(n)     ((((n) + DM_MULT_MINUS_1) / DM_MULT) * DM_MULT)

/* 'look ahead test' from Annex J */
static int dm_look_ahead_test(const unsigned char source[], const int length, const int position,
            const int current_mode, const int mode_arg, const int gs1, const int debug_print) {
    int ascii_count, c40_count, text_count, x12_count, edf_count, b256_count;
    int ascii_rnded, c40_rnded, text_rnded, x12_rnded, edf_rnded, b256_rnded;
    int cnt_1;
    int sp;

    /* step (j) */
    if (current_mode == DM_ASCII || current_mode == DM_BASE256) { /* Adjusted to use for DM_BASE256 also */
        ascii_count = 0;
        c40_count = DM_MULT_1;
        text_count = DM_MULT_1;
        x12_count = DM_MULT_1;
        edf_count = DM_MULT_1;
        b256_count = DM_MULT_2; /* Adjusted from DM_MULT_5_DIV_4 (1.25) */
    } else {
        ascii_count = DM_MULT_1;
        c40_count = DM_MULT_2;
        text_count = DM_MULT_2;
        x12_count = DM_MULT_2;
        edf_count = DM_MULT_2;
        b256_count = DM_MULT_3; /* Adjusted from DM_MULT_9_DIV_4 (2.25) */
    }

    switch (current_mode) {
        case DM_C40: c40_count = 0; break;
        case DM_TEXT: text_count = 0; break;
        case DM_X12: x12_count = 0; break;
        case DM_EDIFACT: edf_count = 0; break;
        case DM_BASE256:
            b256_count = mode_arg == 249 ? DM_MULT_1 : 0; /* Adjusted to use no. of bytes written */
            break;
    }

    for (sp = position; sp < length; sp++) {
        const unsigned char c = source[sp];
        const int is_extended = c & 0x80;

        /* ASCII ... step (l) */
        if (z_isdigit(c)) {
            ascii_count += DM_MULT_1_DIV_2; /* (l)(1) */
        } else {
            if (is_extended) {
                ascii_count = DM_MULT_CEIL(ascii_count) + DM_MULT_2; /* (l)(2) */
            } else {
                ascii_count = DM_MULT_CEIL(ascii_count) + DM_MULT_1; /* (l)(3) */
            }
        }

        /* C40 ... step (m) */
        if (dm_isc40(c)) {
            c40_count += DM_MULT_2_DIV_3; /* (m)(1) */
        } else {
            if (is_extended) {
                c40_count += DM_MULT_8_DIV_3; /* (m)(2) */
            } else {
                c40_count += DM_MULT_4_DIV_3; /* (m)(3) */
            }
        }

        /* TEXT ... step (n) */
        if (dm_istext(c)) {
            text_count += DM_MULT_2_DIV_3; /* (n)(1) */
        } else {
            if (is_extended) {
                text_count += DM_MULT_8_DIV_3; /* (n)(2) */
            } else {
                text_count += DM_MULT_4_DIV_3; /* (n)(3) */
            }
        }

        /* X12 ... step (o) */
        if (dm_isX12(c)) {
            x12_count += DM_MULT_2_DIV_3; /* (o)(1) */
        } else {
            if (is_extended) {
                x12_count += DM_MULT_13_DIV_3; /* (o)(2) */
            } else {
                x12_count += DM_MULT_10_DIV_3; /* (o)(3) */
            }
        }

        /* EDIFACT ... step (p) */
        if (dm_isedifact(c)) {
            edf_count += DM_MULT_3_DIV_4; /* (p)(1) */
        } else {
            if (is_extended) {
                edf_count += DM_MULT_17_DIV_4; /* (p)(2) */
            } else {
                edf_count += DM_MULT_13_DIV_4; /* (p)(3) */
            }
        }

        /* Base 256 ... step (q) */
        if (gs1 == 1 && c == '\x1D') {
            /* FNC1 separator */
            b256_count += DM_MULT_4; /* (q)(1) */
        } else {
            b256_count += DM_MULT_1; /* (q)(2) */
        }

        if (sp >= position + 3) {
            /* At least 4 data characters processed ... step (r) */
            /* NOTE: previous behaviour was at least 5 (same as BWIPP) */

            if (debug_print) {
                printf("\n(m:%d, p:%d, sp:%d, a:%d): ascii_count %d, b256_count %d, edf_count %d, text_count %d"
                        ", x12_count %d, c40_count %d ",
                        current_mode, position, sp, mode_arg, ascii_count, b256_count, edf_count, text_count,
                        x12_count, c40_count);
            }

            cnt_1 = ascii_count + DM_MULT_1;
            /* Adjusted from <= b256_count */
            if (cnt_1 < b256_count && cnt_1 <= edf_count && cnt_1 <= text_count && cnt_1 <= x12_count
                    && cnt_1 <= c40_count) {
                if (debug_print) fputs("ASC->", stdout);
                return DM_ASCII; /* step (r)(1) */
            }
            cnt_1 = b256_count + DM_MULT_1;
            if (cnt_1 <= ascii_count || (cnt_1 < edf_count && cnt_1 < text_count && cnt_1 < x12_count
                                            && cnt_1 < c40_count)) {
                if (debug_print) fputs("BAS->", stdout);
                return DM_BASE256; /* step (r)(2) */
            }
            cnt_1 = edf_count + DM_MULT_1;
            if (cnt_1 < ascii_count && cnt_1 < b256_count && cnt_1 < text_count && cnt_1 < x12_count
                    && cnt_1 < c40_count) {
                if (debug_print) fputs("EDI->", stdout);
                return DM_EDIFACT; /* step (r)(3) */
            }
            cnt_1 = text_count + DM_MULT_1;
            if (cnt_1 < ascii_count && cnt_1 < b256_count && cnt_1 < edf_count && cnt_1 < x12_count
                    && cnt_1 < c40_count) {
                /* Adjusted to avoid early exit from Base 256 if have less than break-even sequence of TEXT chars */
                if (current_mode == DM_BASE256 && position + 6 < length) {
                    if (dm_text_sp_cnt(source, position, length, sp) >= 12) {
                        if (debug_print) fputs("TEX->", stdout);
                        return DM_TEXT; /* step (r)(4) */
                    }
                } else {
                    if (debug_print) fputs("TEX->", stdout);
                    return DM_TEXT; /* step (r)(4) */
                }
            }
            cnt_1 = x12_count + DM_MULT_1;
            if (cnt_1 < ascii_count && cnt_1 < b256_count && cnt_1 < edf_count && cnt_1 < text_count
                    && cnt_1 < c40_count) {
                if (debug_print) fputs("X12->", stdout);
                return DM_X12; /* step (r)(5) */
            }
            cnt_1 = c40_count + DM_MULT_1;
            if (cnt_1 < ascii_count && cnt_1 < b256_count && cnt_1 < edf_count && cnt_1 < text_count) {
                if (c40_count < x12_count) {
                    if (debug_print) fputs("C40->", stdout);
                    return DM_C40; /* step (r)(6)(i) */
                }
                if (c40_count == x12_count) {
                    if (dm_substep_r_6_2_1(source, length, sp) == 1) {
                        if (debug_print) fputs("X12->", stdout);
                        return DM_X12; /* step (r)(6)(ii)(I) */
                    }
                    if (debug_print) fputs("C40->", stdout);
                    return DM_C40; /* step (r)(6)(ii)(II) */
                }
            }
        }
    }

    /* At the end of data ... step (k) */
    /* step (k)(1) */
    ascii_rnded = DM_MULT_CEIL(ascii_count);
    b256_rnded = DM_MULT_CEIL(b256_count);
    edf_rnded = DM_MULT_CEIL(edf_count);
    text_rnded = DM_MULT_CEIL(text_count);
    x12_rnded = DM_MULT_CEIL(x12_count);
    c40_rnded = DM_MULT_CEIL(c40_count);
    if (debug_print) {
        printf("\nEOD(m:%d, p:%d, a:%d): ascii_rnded %d, b256_rnded %d, edf_rnded %d, text_rnded %d"
                ", x12_rnded %d (%d), c40_rnded %d (%d) ",
                current_mode, position, mode_arg, ascii_rnded, b256_rnded, edf_rnded, text_rnded,
                x12_rnded, x12_count, c40_rnded, c40_count);
    }

    if (ascii_rnded <= b256_rnded && ascii_rnded <= edf_rnded && ascii_rnded <= text_rnded && ascii_rnded <= x12_rnded
            && ascii_rnded <= c40_rnded) {
        if (debug_print) fputs("ASC->", stdout);
        return DM_ASCII; /* step (k)(2) */
    }
    if (b256_rnded < ascii_rnded && b256_rnded < edf_rnded && b256_rnded < text_rnded && b256_rnded < x12_rnded
            && b256_rnded < c40_rnded) {
        if (debug_print) fputs("BAS->", stdout);
        return DM_BASE256; /* step (k)(3) */
    }
    /* Adjusted from < x12_rnded */
    if (edf_rnded < ascii_rnded && edf_rnded < b256_rnded && edf_rnded < text_rnded && edf_rnded <= x12_rnded
            && edf_rnded < c40_rnded) {
        if (debug_print) fputs("EDI->", stdout);
        return DM_EDIFACT; /* step (k)(4) */
    }
    if (text_rnded < ascii_rnded && text_rnded < b256_rnded && text_rnded < edf_rnded && text_rnded < x12_rnded
            && text_rnded < c40_rnded) {
        if (debug_print) fputs("TEX->", stdout);
        return DM_TEXT; /* step (k)(5) */
    }
    /* Adjusted from < edf_rnded */
    if (x12_rnded < ascii_rnded && x12_rnded < b256_rnded && x12_rnded <= edf_rnded && x12_rnded < text_rnded
            && x12_rnded < c40_rnded) {
        if (debug_print) fputs("X12->", stdout);
        return DM_X12; /* step (k)(6) */
    }
    if (debug_print) fputs("C40->", stdout);
    return DM_C40; /* step (k)(7) */
}

/* Copy C40/TEXT/X12 triplets from buffer to target. Returns elements left in buffer (< 3) */
static int dm_ctx_buffer_xfer(int process_buffer[8], int process_p, unsigned char target[], int *p_tp,
            const int debug_print) {
    int i, process_e;
    int tp = *p_tp;

    process_e = (process_p / 3) * 3;

    for (i = 0; i < process_e; i += 3) {
        int iv = (1600 * process_buffer[i]) + (40 * process_buffer[i + 1]) + (process_buffer[i + 2]) + 1;
        target[tp++] = (unsigned char) (iv >> 8);
        target[tp++] = (unsigned char) (iv & 0xFF);
        if (debug_print) {
            printf("[%d %d %d (%d %d)] ", process_buffer[i], process_buffer[i + 1], process_buffer[i + 2],
                target[tp - 2], target[tp - 1]);
        }
    }

    process_p -= process_e;

    if (process_p) {
        memmove(process_buffer, process_buffer + process_e, sizeof(int) * process_p);
    }

    *p_tp = tp;

    return process_p;
}

/* Copy EDIFACT quadruplets from buffer to target. Returns elements left in buffer (< 4) */
static int dm_edi_buffer_xfer(int process_buffer[8], int process_p, unsigned char target[], int *p_tp,
            const int empty, const int debug_print) {
    int i, process_e;
    int tp = *p_tp;

    process_e = (process_p / 4) * 4;

    for (i = 0; i < process_e; i += 4) {
        target[tp++] = (unsigned char) (process_buffer[i] << 2 | (process_buffer[i + 1] & 0x30) >> 4);
        target[tp++] = (unsigned char) ((process_buffer[i + 1] & 0x0F) << 4 | (process_buffer[i + 2] & 0x3C) >> 2);
        target[tp++] = (unsigned char) ((process_buffer[i + 2] & 0x03) << 6 | process_buffer[i + 3]);
        if (debug_print) {
            printf("[%d %d %d %d (%d %d %d)] ", process_buffer[i], process_buffer[i + 1], process_buffer[i + 2],
                process_buffer[i + 3], target[tp - 3], target[tp - 2], target[tp - 1]);
        }
    }

    process_p -= process_e;

    if (process_p) {
        memmove(process_buffer, process_buffer + process_e, sizeof(int) * process_p);
        if (empty) {
            if (process_p == 3) {
                assert(i < 6); /* Suppress clang-tidy-21 clang-analyzer-security.ArrayBound */
                target[tp++] = (unsigned char) (process_buffer[i] << 2 | (process_buffer[i + 1] & 0x30) >> 4);
                target[tp++] = (unsigned char) ((process_buffer[i + 1] & 0x0F) << 4
                                                | (process_buffer[i + 2] & 0x3C) >> 2);
                target[tp++] = (unsigned char) ((process_buffer[i + 2] & 0x03) << 6);
                if (debug_print) {
                    printf("[%d %d %d (%d %d %d)] ", process_buffer[i], process_buffer[i + 1], process_buffer[i + 2],
                            target[tp - 3], target[tp - 2], target[tp - 1]);
                }
            } else if (process_p == 2) {
                assert(i < 7); /* Suppress clang-tidy-21 clang-analyzer-security.ArrayBound */
                target[tp++] = (unsigned char) (process_buffer[i] << 2 | (process_buffer[i + 1] & 0x30) >> 4);
                target[tp++] = (unsigned char) ((process_buffer[i + 1] & 0x0F) << 4);
                if (debug_print) {
                    printf("[%d %d (%d %d)] ", process_buffer[i], process_buffer[i + 1], target[tp - 2],
                            target[tp - 1]);
                }
            } else {
                assert(i < 8); /* Suppress clang-tidy-21 clang-analyzer-security.ArrayBound */
                target[tp++] = (unsigned char) (process_buffer[i] << 2);
                if (debug_print) printf("[%d (%d)] ", process_buffer[i], target[tp - 1]);
            }
            process_p = 0;
        }
    }

    *p_tp = tp;

    return process_p;
}

/* Get index of symbol size in codewords array `dm_matrixbytes`, as specified or
   else smallest containing `minimum` codewords */
static int dm_get_symbolsize(struct zint_symbol *symbol, const int minimum) {
    int i;

    if (symbol->option_2 >= 1 && symbol->option_2 <= DMSIZESCOUNT) {
        return dm_intsymbol[symbol->option_2 - 1];
    }
    if (minimum > 1304) {
        return minimum <= 1558 ? DMSIZESCOUNT - 1 : 0;
    }
    for (i = minimum >= 62 ? 23 : 0; minimum > dm_matrixbytes[i]; i++);

    if ((symbol->option_3 & 0x7F) == DM_DMRE) {
        return i;
    }
    if ((symbol->option_3 & 0x7F) == DM_SQUARE) {
        /* Skip rectangular symbols in square only mode */
        for (; dm_matrixH[i] != dm_matrixW[i]; i++);
        return i;
    }
    /* Skip DMRE symbols in no dmre mode */
    for (; dm_isDMRE[i]; i++);
    return i;
}

/* Number of codewords remaining in a particular version (may be negative) */
static int dm_codewords_remaining(struct zint_symbol *symbol, const int tp, const int process_p) {
    int symbolsize = dm_get_symbolsize(symbol, tp + process_p); /* Allow for the remaining data characters */

    return dm_matrixbytes[symbolsize] - tp;
}

/* Number of C40/TEXT elements needed to encode `input` */
static int dm_c40text_cnt(const int current_mode, const int gs1, unsigned char input) {
    int cnt;

    if (gs1 && input == '\x1D') {
        return 2;
    }
    cnt = 1;
    if (input & 0x80) {
        cnt += 2;
        input = input - 128;
    }
    if ((current_mode == DM_C40 && dm_c40_shift[input]) || (current_mode == DM_TEXT && dm_text_shift[input])) {
        cnt += 1;
    }

    return cnt;
}

/* Update Base 256 field length */
static int dm_update_b256_field_length(unsigned char target[], int tp, int b256_start) {
    int b256_count = tp - (b256_start + 1);
    if (b256_count <= 249) {
        target[b256_start] = b256_count;
    } else {
        /* Insert extra codeword */
        memmove(target + b256_start + 2, target + b256_start + 1, b256_count);
        target[b256_start] = (unsigned char) (249 + (b256_count / 250));
        target[b256_start + 1] = (unsigned char) (b256_count % 250);
        tp++;
    }

    return tp;
}

/* Switch from ASCII or Base 256 to another mode */
static int dm_switch_mode(const int next_mode, unsigned char target[], int tp, int *p_b256_start,
            const int debug_print) {
    switch (next_mode) {
        case DM_ASCII:
            if (debug_print) fputs("ASC ", stdout);
            break;
        case DM_C40:
            target[tp++] = 230;
            if (debug_print) fputs("C40 ", stdout);
            break;
        case DM_TEXT:
            target[tp++] = 239;
            if (debug_print) fputs("TEX ", stdout);
            break;
        case DM_X12:
            target[tp++] = 238;
            if (debug_print) fputs("X12 ", stdout);
            break;
        case DM_EDIFACT:
            target[tp++] = 240;
            if (debug_print) fputs("EDI ", stdout);
            break;
        case DM_BASE256:
            target[tp++] = 231;
            *p_b256_start = tp;
            target[tp++] = 0; /* Byte count holder (may be expanded to 2 codewords) */
            if (debug_print) fputs("BAS ", stdout);
            break;
    }

    return tp;
}

/* Minimal encoding using Dijkstra-based algorithm by Alex Geller
   Note due to the complicated end-of-data (EOD) conditions that Data Matrix has, this may not be fully minimal;
   however no counter-examples are known at present */

#define DM_NUM_MODES        6

static const char dm_smodes[DM_NUM_MODES + 1][6] = { "?", "ASCII", "C40", "TEXT", "X12", "EDF", "B256" };

/* The size of this structure could be significantly reduced using techniques pointed out by Alex Geller,
   but not done currently to avoid the processing overhead */
struct dm_edge {
    unsigned char mode;
    unsigned char endMode; /* Mode returned by `dm_getEndMode()` */
    unsigned short from; /* Position in input data, 0-based */
    unsigned short len;
    unsigned short size; /* Cumulative number of codewords */
    unsigned short bytes; /* DM_BASE256 byte count, kept to avoid runtime calc */
    unsigned short previous; /* Index into edges array */
};

/* Note 1st row of edges not used so valid previous cannot point there, i.e. won't be zero */
#define DM_PREVIOUS(edges, edge) \
    ((edge)->previous ? (edges) + (edge)->previous : NULL)

/* Determine if next 1 to 4 chars are at EOD and can be encoded as 1 or 2 ASCII codewords */
static int dm_last_ascii(const unsigned char source[], const int length, const int from) {
    if (length - from > 4 || from >= length) {
        return 0;
    }
    if (length - from == 1) {
        if (source[from] & 0x80) {
            return 0;
        }
        return 1;
    }
    if (length - from == 2) {
        if ((source[from] & 0x80) || (source[from + 1] & 0x80)) {
            return 0;
        }
        if (z_isdigit(source[from]) && z_isdigit(source[from + 1])) {
            return 1;
        }
        return 2;
    }
    if (length - from == 3) {
        if (z_isdigit(source[from]) && z_isdigit(source[from + 1]) && !(source[from + 2] & 0x80)) {
            return 2;
        }
        if (z_isdigit(source[from + 1]) && z_isdigit(source[from + 2]) && !(source[from] & 0x80)) {
            return 2;
        }
        return 0;
    }
    if (z_isdigit(source[from]) && z_isdigit(source[from + 1]) && z_isdigit(source[from + 2])
            && z_isdigit(source[from + 3])) {
        return 2;
    }
    return 0;
}

/* Treat EDIFACT edges specially, returning DM_ASCII mode if not full (i.e. encoding < 4 chars), or if
   full and at EOD where 1 or 2 ASCII chars can be encoded */
static int dm_getEndMode(struct zint_symbol *symbol, const unsigned char *source, const int length,
            const int last_seg, const int mode, const int from, const int len, const int size) {
    if (mode == DM_EDIFACT) {
        if (len < 4) {
            return DM_ASCII;
        }
        if (last_seg) {
            const int last_ascii = dm_last_ascii(source, length, from + len);
            if (last_ascii) { /* At EOD with remaining chars ASCII-encodable in 1 or 2 codewords */
                const int symbols_left = dm_codewords_remaining(symbol, size + last_ascii, 0);
                /* If no codewords left and 1 or 2 ASCII-encodables or 1 codeword left and 1 ASCII-encodable */
                if (symbols_left <= 2 - last_ascii) {
                    return DM_ASCII;
                }
            }
        }
    }
    return mode;
}

#if 0
#include "dmatrix_trace.h"
#else
#define DM_TRACE_Edges(px, s, l, p, v)
#define DM_TRACE_AddEdge(s, l, es, p, v, e)
#define DM_TRACE_NotAddEdge(s, l, es, p, v, ij, e)
#endif

/* Return number of C40/TEXT codewords needed to encode characters in full batches of 3 (or less if EOD).
   The number of characters encoded is returned in `len` */
static int dm_getNumberOfC40Words(const unsigned char *source, const int length, const int from, const int mode,
            int *len) {
    int thirdsCount = 0;
    int i;

    for (i = from; i < length; i++) {
        const unsigned char ci = source[i];
        int remainder;

        if (dm_isc40text(mode, ci)) {
            thirdsCount++; /* Native */
        } else if (!(ci & 0x80)) {
            thirdsCount += 2; /* Shift */
        } else if (dm_isc40text(mode, (unsigned char) (ci & 0x7F))) {
            thirdsCount += 3; /* Shift, Upper shift */
        } else {
            thirdsCount += 4; /* Shift, Upper shift, shift */
        }

        remainder = thirdsCount % 3;
        if (remainder == 0 || (remainder == 2 && i + 1 == length)) {
            *len = i - from + 1;
            return ((thirdsCount + 2) / 3) * 2;
        }
    }
    *len = 0;
    return 0;
}

/* Initialize a new edge. Returns endMode */
static int dm_new_Edge(struct zint_symbol *symbol, const unsigned char *source, const int length, const int last_seg,
            struct dm_edge *edges, const int mode, const int from, const int len, struct dm_edge *previous,
            struct dm_edge *edge, const int cwds) {
    int previousMode;
    int size;

    edge->mode = mode;
    edge->endMode = mode;
    edge->from = from;
    edge->len = len;
    edge->bytes = 0;
    if (previous) {
        assert(previous->mode && previous->len && previous->size && previous->endMode);
        previousMode = previous->endMode;
        edge->previous = previous - edges;
        size = previous->size;
    } else {
        previousMode = DM_ASCII;
        edge->previous = 0;
        size = 0;
    }

    switch (mode) {
        case DM_ASCII:
            assert(previousMode != DM_EDIFACT);
            size++;
            if (source[from] & 0x80) {
                size++;
            }
            if (previousMode != DM_ASCII && previousMode != DM_BASE256) {
                size++; /* Unlatch to ASCII */
            }
            break;

        case DM_BASE256:
            assert(previousMode != DM_EDIFACT);
            size++;
            if (previousMode != DM_BASE256) {
                size += 2; /* Byte count + latch to BASE256 */
                if (previousMode != DM_ASCII) {
                    size++; /* Unlatch to ASCII */
                }
                edge->bytes = 1;
            } else {
                assert(previous);
                edge->bytes = 1 + previous->bytes;
                if (edge->bytes == 250) {
                    size++; /* Extra byte count */
                }
            }
            break;

        case DM_C40:
        case DM_TEXT:
            assert(previousMode != DM_EDIFACT);
            size += cwds;
            if (previousMode != mode) {
                size++; /* Latch to this mode */
                if (previousMode != DM_ASCII && previousMode != DM_BASE256) {
                    size++; /* Unlatch to ASCII */
                }
            }
            if (last_seg && from + len + 2 >= length) { /* If less than batch of 3 away from EOD */
                const int last_ascii = dm_last_ascii(source, length, from + len);
                const int symbols_left = dm_codewords_remaining(symbol, size + last_ascii, 0);
                if (symbols_left > 0) {
                    size++; /* We need an extra unlatch at the end */
                }
            }
            break;

        case DM_X12:
            assert(previousMode != DM_EDIFACT);
            size += 2;
            if (previousMode != DM_X12) {
                size++; /* Latch to this mode */
                if (previousMode != DM_ASCII && previousMode != DM_BASE256) {
                    size++; /* Unlatch to ASCII */
                }
            }
            if (last_seg && from + len + 2 >= length) { /* If less than batch of 3 away from EOD */
                const int last_ascii = dm_last_ascii(source, length, from + len);
                if (last_ascii == 2) { /* Only 1 ASCII-encodable allowed at EOD for X12, unlike C40/TEXT */
                    size++; /* We need an extra unlatch at the end */
                } else {
                    const int symbols_left = dm_codewords_remaining(symbol, size + last_ascii, 0);
                    if (symbols_left > 0) {
                        size++; /* We need an extra unlatch at the end */
                    }
                }
            }
            break;

        case DM_EDIFACT:
            size += 3;
            if (previousMode != DM_EDIFACT) {
                size++; /* Latch to this mode */
                if (previousMode != DM_ASCII && previousMode != DM_BASE256) {
                    size++; /* Unlatch to ASCII */
                }
            }
            edge->endMode = dm_getEndMode(symbol, source, length, last_seg, mode, from, len, size);
            break;
    }
    edge->size = size;

    return edge->endMode;
}

/* Add an edge for a mode at a vertex if no existing edge or if more optimal than existing edge */
static void dm_addEdge(struct zint_symbol *symbol, const unsigned char *source, const int length, const int last_seg,
            struct dm_edge *edges, const int mode, const int from, const int len, struct dm_edge *previous,
            const int cwds) {
    struct dm_edge edge;
    const int endMode = dm_new_Edge(symbol, source, length, last_seg, edges, mode, from, len, previous, &edge, cwds);
    const int vertexIndex = from + len;
    const int v_ij = vertexIndex * DM_NUM_MODES + endMode - 1;

    if (edges[v_ij].mode == 0 || edges[v_ij].size > edge.size) {
        DM_TRACE_AddEdge(source, length, edges, previous, vertexIndex, &edge);
        edges[v_ij] = edge;
    } else {
        DM_TRACE_NotAddEdge(source, length, edges, previous, vertexIndex, v_ij, &edge);
    }
}

/* Add edges for the various modes at a vertex */
static void dm_addEdges(struct zint_symbol *symbol, const unsigned char source[], const int length,
            const int last_seg, struct dm_edge *edges, const int from, struct dm_edge *previous, const int gs1) {
    int i, pos;

    assert(from < length); /* Suppress clang-tidy-21 clang-analyzer-security.ArrayBound */

    /* Not possible to unlatch a full EDF edge to something else */
    if (previous == NULL || previous->endMode != DM_EDIFACT) {

        static const char c40text_modes[] = { DM_C40, DM_TEXT };

        if (z_isdigit(source[from]) && from + 1 < length && z_isdigit(source[from + 1])) {
            dm_addEdge(symbol, source, length, last_seg, edges, DM_ASCII, from, 2, previous, 0);
            /* If ASCII vertex, don't bother adding other edges as this will be optimal; suggested by Alex Geller */
            if (previous && previous->mode == DM_ASCII) {
                return;
            }
        } else {
            dm_addEdge(symbol, source, length, last_seg, edges, DM_ASCII, from, 1, previous, 0);
        }

        for (i = 0; i < ARRAY_SIZE(c40text_modes); i++) {
            int len;
            int cwds = dm_getNumberOfC40Words(source, length, from, c40text_modes[i], &len);
            if (cwds) {
                dm_addEdge(symbol, source, length, last_seg, edges, c40text_modes[i], from, len, previous, cwds);
            }
        }

        if (from + 2 < length && dm_isX12(source[from]) && dm_isX12(source[from + 1]) && dm_isX12(source[from + 2])) {
            dm_addEdge(symbol, source, length, last_seg, edges, DM_X12, from, 3, previous, 0);
        }

        if (gs1 != 1 || source[from] != '\x1D') {
            dm_addEdge(symbol, source, length, last_seg, edges, DM_BASE256, from, 1, previous, 0);
        }
    }

    if (dm_isedifact(source[from])) {
        /* We create 3 EDF edges, 2, 3 or 4 characters length. The 4-char normally doesn't have a latch to ASCII
           unless it is 2 characters away from the end of the input. */
        for (i = 1, pos = from + i; i < 4 && pos < length && dm_isedifact(source[pos]); i++, pos++) {
            dm_addEdge(symbol, source, length, last_seg, edges, DM_EDIFACT, from, i + 1, previous, 0);
        }
    }
}

/* Calculate optimized encoding modes */
static int dm_define_modes(struct zint_symbol *symbol, char modes[], const unsigned char source[], const int length,
            const int last_seg, const int gs1, const int debug_print) {

    int i, j, v_i;
    int minimalJ, minimalSize;
    struct dm_edge *edge;
    int current_mode;
    int mode_end, mode_len;

    struct dm_edge *edges = (struct dm_edge *) calloc((length + 1) * DM_NUM_MODES, sizeof(struct dm_edge));
    if (!edges) {
        return 0;
    }
    dm_addEdges(symbol, source, length, last_seg, edges, 0, NULL, gs1);

    DM_TRACE_Edges("DEBUG Initial situation\n", source, length, edges, 0);

    for (i = 1; i < length; i++) {
        v_i = i * DM_NUM_MODES;
        for (j = 0; j < DM_NUM_MODES; j++) {
            if (edges[v_i + j].mode) {
                dm_addEdges(symbol, source, length, last_seg, edges, i, edges + v_i + j, gs1);
            }
        }
        DM_TRACE_Edges("DEBUG situation after adding edges to vertices at position %d\n", source, length, edges, i);
    }

    DM_TRACE_Edges("DEBUG Final situation\n", source, length, edges, length);

    v_i = length * DM_NUM_MODES;
    minimalJ = -1;
    minimalSize = INT_MAX;
    for (j = 0; j < DM_NUM_MODES; j++) {
        edge = edges + v_i + j;
        if (edge->mode) {
            if (debug_print) printf("edges[%d][%d][0] size %d\n", length, j, edge->size);
            if (edge->size < minimalSize) {
                minimalSize = edge->size;
                minimalJ = j;
                if (debug_print) printf(" set minimalJ %d\n", minimalJ);
            }
        } else {
            if (debug_print) printf("edges[%d][%d][0] NULL\n", length, j);
        }
    }
    assert(minimalJ >= 0);

    edge = edges + v_i + minimalJ;
    mode_len = 0;
    mode_end = length;
    while (edge) {
        current_mode = edge->mode;
        mode_len += edge->len;
        edge = DM_PREVIOUS(edges, edge);
        if (!edge || edge->mode != current_mode) {
            for (i = mode_end - mode_len; i < mode_end; i++) {
                modes[i] = current_mode;
            }
            mode_end = mode_end - mode_len;
            mode_len = 0;
        }
    }

    if (debug_print) {
        printf("modes (%d): ", length);
        for (i = 0; i < length; i++) printf("%c", dm_smodes[(int) modes[i]][0]);
        fputc('\n', stdout);
    }
    assert(mode_end == 0);

    free(edges);

    return 1;
}

/* Do default minimal encodation */
static int dm_minimalenc(struct zint_symbol *symbol, const unsigned char source[], const int length,
            const int last_seg, int *p_sp, unsigned char target[], int *p_tp, int process_buffer[8], int *p_process_p,
            int *p_b256_start, int *p_current_mode, const int gs1, const int debug_print) {
    int sp = *p_sp;
    int tp = *p_tp;
    int process_p = *p_process_p;
    int current_mode = *p_current_mode;
    int i;
    char *modes = (char *) z_alloca(length);

    assert(length <= 10921); /* Can only handle (10921 + 1) * 6 = 65532 < 65536 (2*16) due to sizeof(previous) */

    if (!dm_define_modes(symbol, modes, source, length, last_seg, gs1, debug_print)) {
        return z_errtxt(ZINT_ERROR_MEMORY, symbol, 728, "Insufficient memory for mode buffers");
    }

    while (sp < length) {

        if (modes[sp] != current_mode) {
            switch (current_mode) {
                case DM_C40:
                case DM_TEXT:
                case DM_X12:
                    process_p = 0; /* Throw away buffer if any */
                    target[tp++] = 254; /* Unlatch */
                    break;
                case DM_EDIFACT:
                    if (last_seg) {
                        const int last_ascii = dm_last_ascii(source, length, sp);
                        if (!last_ascii) {
                            process_buffer[process_p++] = 31; /* Unlatch */
                        } else {
                            const int symbols_left = dm_codewords_remaining(symbol, tp + last_ascii, process_p);
                            if (debug_print) {
                                printf("process_p %d, last_ascii %d, symbols_left %d, last_seg %d\n",
                                        process_p, last_ascii, symbols_left, last_seg);
                            }
                            if (symbols_left > 2 - last_ascii) {
                                process_buffer[process_p++] = 31; /* Unlatch */
                            }
                        }
                    }
                    process_p = dm_edi_buffer_xfer(process_buffer, process_p, target, &tp, 1 /*empty*/, debug_print);
                    break;
                case DM_BASE256:
                    tp = dm_update_b256_field_length(target, tp, *p_b256_start);
                    /* B.2.1 255-state randomising algorithm */
                    for (i = *p_b256_start; i < tp; i++) {
                        const int prn = (149 * (i + 1)) % 255 + 1;
                        target[i] = (unsigned char) ((target[i] + prn) & 0xFF);
                    }
                    break;
            }
            tp = dm_switch_mode(modes[sp], target, tp, p_b256_start, debug_print);
        }

        current_mode = modes[sp];
        assert(current_mode);

        if (current_mode == DM_ASCII) {

            if (z_is_twodigits(source, length, sp)) {
                target[tp++] = (unsigned char) (10 * z_ctoi(source[sp]) + z_ctoi(source[sp + 1]) + 130);
                if (debug_print) printf("N%02d ", target[tp - 1] - 130);
                sp += 2;
            } else {
                if (source[sp] & 0x80) {
                    target[tp++] = 235; /* FNC4 */
                    target[tp++] = (source[sp] - 128) + 1;
                    if (debug_print) printf("FN4 A%02X ", target[tp - 1] - 1);
                } else {
                    if (gs1 && source[sp] == '\x1D') {
                        if (gs1 == 2) {
                            target[tp++] = 29 + 1; /* GS */
                            if (debug_print) fputs("GS ", stdout);
                        } else {
                            target[tp++] = 232; /* FNC1 */
                            if (debug_print) fputs("FN1 ", stdout);
                        }
                    } else {
                        target[tp++] = source[sp] + 1;
                        if (debug_print) printf("A%02X ", target[tp - 1] - 1);
                    }
                }
                sp++;
            }

        } else if (current_mode == DM_C40 || current_mode == DM_TEXT) {

            int shift_set, value;
            const char *ct_shift, *ct_value;

            if (current_mode == DM_C40) {
                ct_shift = dm_c40_shift;
                ct_value = dm_c40_value;
            } else {
                ct_shift = dm_text_shift;
                ct_value = dm_text_value;
            }

            if (source[sp] & 0x80) {
                process_buffer[process_p++] = 1;
                process_buffer[process_p++] = 30; /* Upper Shift */
                shift_set = ct_shift[source[sp] - 128];
                value = ct_value[source[sp] - 128];
            } else {
                if (gs1 && source[sp] == '\x1D') {
                    if (gs1 == 2) {
                        shift_set = ct_shift[29];
                        value = ct_value[29]; /* GS */
                    } else {
                        shift_set = 2;
                        value = 27; /* FNC1 */
                    }
                } else {
                    shift_set = ct_shift[source[sp]];
                    value = ct_value[source[sp]];
                }
            }

            if (shift_set != 0) {
                process_buffer[process_p++] = shift_set - 1;
            }
            process_buffer[process_p++] = value;

            if (process_p >= 3) {
                process_p = dm_ctx_buffer_xfer(process_buffer, process_p, target, &tp, debug_print);
            }
            sp++;

        } else if (current_mode == DM_X12) {

            static const char x12_nonalphanum_chars[] = "\015*> ";
            int value = 0;

            if (z_isdigit(source[sp])) {
                value = (source[sp] - '0') + 4;
            } else if (z_isupper(source[sp])) {
                value = (source[sp] - 'A') + 14;
            } else {
                value = z_posn(x12_nonalphanum_chars, source[sp]);
            }

            process_buffer[process_p++] = value;

            if (process_p >= 3) {
                process_p = dm_ctx_buffer_xfer(process_buffer, process_p, target, &tp, debug_print);
            }
            sp++;

        } else if (current_mode == DM_EDIFACT) {

            int value = source[sp];

            if (value >= 64) { /* '@' */
                value -= 64;
            }

            process_buffer[process_p++] = value;
            sp++;

            if (process_p >= 4) {
                process_p = dm_edi_buffer_xfer(process_buffer, process_p, target, &tp, 0 /*empty*/, debug_print);
            }

        } else if (current_mode == DM_BASE256) {

            target[tp++] = source[sp++];
            if (debug_print) printf("B%02X ", target[tp - 1]);
        }

        if (tp > 1558) {
            return z_errtxt(ZINT_ERROR_TOO_LONG, symbol, 729,
                            "Input too long, requires too many codewords (maximum 1558)");
        }

    } /* while */

    *p_sp = sp;
    *p_tp = tp;
    *p_process_p = process_p;
    *p_current_mode = current_mode;

    return 0;
}

/* Encode using algorithm based on ISO/IEC 21471:2020 Annex J (was ISO/IEC 21471:2006 Annex P) */
static int dm_isoenc(struct zint_symbol *symbol, const unsigned char source[], const int length, int *p_sp,
            unsigned char target[], int *p_tp, int process_buffer[8], int *p_process_p, int *p_b256_start,
            int *p_current_mode, const int gs1, const int debug_print) {
    const int mailmark = symbol->symbology == BARCODE_MAILMARK_2D;
    int sp = *p_sp;
    int tp = *p_tp;
    int process_p = *p_process_p;
    int current_mode = *p_current_mode;
    int not_first = 0;
    int i;

    /* step (a) */
    int next_mode = DM_ASCII;

    if (mailmark) { /* First 45 characters C40 */
        assert(length >= 45);
        next_mode = DM_C40;
        tp = dm_switch_mode(next_mode, target, tp, p_b256_start, debug_print);
        while (sp < 45) {
            assert(dm_isc40(source[sp]));
            process_buffer[process_p++] = dm_c40_value[source[sp]];

            if (process_p >= 3) {
                process_p = dm_ctx_buffer_xfer(process_buffer, process_p, target, &tp, debug_print);
            }
            sp++;
        }
        current_mode = next_mode;
        not_first = 1;
    }

    while (sp < length) {

        current_mode = next_mode;

        /* step (b) - ASCII encodation */
        if (current_mode == DM_ASCII) {
            next_mode = DM_ASCII;

            if (z_is_twodigits(source, length, sp)) {
                target[tp++] = (unsigned char) (10 * z_ctoi(source[sp]) + z_ctoi(source[sp + 1]) + 130);
                if (debug_print) printf("N%02d ", target[tp - 1] - 130);
                sp += 2;
            } else {
                next_mode = dm_look_ahead_test(source, length, sp, current_mode, 0, gs1, debug_print);

                if (next_mode != DM_ASCII) {
                    tp = dm_switch_mode(next_mode, target, tp, p_b256_start, debug_print);
                    not_first = 0;
                } else {
                    if (source[sp] & 0x80) {
                        target[tp++] = 235; /* FNC4 */
                        target[tp++] = (source[sp] - 128) + 1;
                        if (debug_print) printf("FN4 A%02X ", target[tp - 1] - 1);
                    } else {
                        if (gs1 && source[sp] == '\x1D') {
                            if (gs1 == 2) {
                                target[tp++] = 29 + 1; /* GS */
                                if (debug_print) fputs("GS ", stdout);
                            } else {
                                target[tp++] = 232; /* FNC1 */
                                if (debug_print) fputs("FN1 ", stdout);
                            }
                        } else {
                            target[tp++] = source[sp] + 1;
                            if (debug_print) printf("A%02X ", target[tp - 1] - 1);
                        }
                    }
                    sp++;
                }
            }

        /* step (c)/(d) C40/TEXT encodation */
        } else if (current_mode == DM_C40 || current_mode == DM_TEXT) {

            next_mode = current_mode;
            if (process_p == 0 && not_first) {
                next_mode = dm_look_ahead_test(source, length, sp, current_mode, process_p, gs1, debug_print);
            }

            if (next_mode != current_mode) {
                target[tp++] = 254; /* Unlatch */
                next_mode = DM_ASCII;
                if (debug_print) fputs("ASC ", stdout);
            } else {
                int shift_set, value;
                const char *ct_shift, *ct_value;

                if (current_mode == DM_C40) {
                    ct_shift = dm_c40_shift;
                    ct_value = dm_c40_value;
                } else {
                    ct_shift = dm_text_shift;
                    ct_value = dm_text_value;
                }

                if (source[sp] & 0x80) {
                    process_buffer[process_p++] = 1;
                    process_buffer[process_p++] = 30; /* Upper Shift */
                    shift_set = ct_shift[source[sp] - 128];
                    value = ct_value[source[sp] - 128];
                } else {
                    if (gs1 && source[sp] == '\x1D') {
                        if (gs1 == 2) {
                            shift_set = ct_shift[29];
                            value = ct_value[29]; /* GS */
                        } else {
                            shift_set = 2;
                            value = 27; /* FNC1 */
                        }
                    } else {
                        shift_set = ct_shift[source[sp]];
                        value = ct_value[source[sp]];
                    }
                }

                if (shift_set != 0) {
                    process_buffer[process_p++] = shift_set - 1;
                }
                process_buffer[process_p++] = value;

                if (process_p >= 3) {
                    process_p = dm_ctx_buffer_xfer(process_buffer, process_p, target, &tp, debug_print);
                }
                sp++;
                not_first = 1;
            }

        /* step (e) X12 encodation */
        } else if (current_mode == DM_X12) {

            if (!dm_isX12(source[sp])) {
                next_mode = DM_ASCII;
            } else {
                next_mode = DM_X12;
                if (process_p == 0 && not_first) {
                    next_mode = dm_look_ahead_test(source, length, sp, current_mode, process_p, gs1, debug_print);
                }
            }

            if (next_mode != DM_X12) {
                sp -= process_p; /* About to throw away buffer, need to re-process input, cf Okapi commit [fb7981e] */
                process_p = 0; /* Throw away buffer if any */
                target[tp++] = 254; /* Unlatch */
                next_mode = DM_ASCII;
                if (debug_print) fputs("ASC ", stdout);
            } else {
                static const char x12_nonalphanum_chars[] = "\015*> ";
                int value = 0;

                if (z_isdigit(source[sp])) {
                    value = (source[sp] - '0') + 4;
                } else if (z_isupper(source[sp])) {
                    value = (source[sp] - 'A') + 14;
                } else {
                    value = z_posn(x12_nonalphanum_chars, source[sp]);
                }

                process_buffer[process_p++] = value;

                if (process_p >= 3) {
                    process_p = dm_ctx_buffer_xfer(process_buffer, process_p, target, &tp, debug_print);
                }
                sp++;
                not_first = 1;
            }

        /* step (f) EDIFACT encodation */
        } else if (current_mode == DM_EDIFACT) {

            if (!dm_isedifact(source[sp])) {
                next_mode = DM_ASCII;
            } else {
                next_mode = DM_EDIFACT;
                if (process_p == 3) {
                    /* Note different than spec Step (f)(2), which suggests checking when 0, but this seems to
                       work better in many cases as the switch to ASCII is "free" */
                    next_mode = dm_look_ahead_test(source, length, sp, current_mode, process_p, gs1, debug_print);
                }
            }

            if (next_mode != DM_EDIFACT) {
                process_buffer[process_p++] = 31;
                process_p = dm_edi_buffer_xfer(process_buffer, process_p, target, &tp, 1 /*empty*/, debug_print);
                next_mode = DM_ASCII;
                if (debug_print) fputs("ASC ", stdout);
            } else {
                int value = source[sp];

                if (value >= 64) { /* '@' */
                    value -= 64;
                }

                process_buffer[process_p++] = value;
                sp++;
                not_first = 1;

                if (process_p >= 4) {
                    process_p = dm_edi_buffer_xfer(process_buffer, process_p, target, &tp, 0 /*empty*/,
                                                    debug_print);
                }
            }

        /* step (g) Base 256 encodation */
        } else if (current_mode == DM_BASE256) {

            if (gs1 == 1 && source[sp] == '\x1D') {
                next_mode = DM_ASCII;
            } else {
                next_mode = DM_BASE256;
                if (not_first) {
                    next_mode = dm_look_ahead_test(source, length, sp, current_mode, tp - (*p_b256_start + 1), gs1,
                                                    debug_print);
                }
            }

            if (next_mode != DM_BASE256) {
                tp = dm_update_b256_field_length(target, tp, *p_b256_start);
                /* B.2.1 255-state randomising algorithm */
                for (i = *p_b256_start; i < tp; i++) {
                    const int prn = (149 * (i + 1)) % 255 + 1;
                    target[i] = (unsigned char) ((target[i] + prn) & 0xFF);
                }
                /* We switch directly here to avoid flipping back to Base 256 due to `dm_text_sp_cnt()` */
                tp = dm_switch_mode(next_mode, target, tp, p_b256_start, debug_print);
                not_first = 0;
            } else {
                if (gs1 == 2 && source[sp] == '\x1D') {
                    target[tp++] = 29; /* GS */
                } else {
                    target[tp++] = source[sp];
                }
                sp++;
                not_first = 1;
                if (debug_print) printf("B%02X ", target[tp - 1]);
            }
        }

        if (tp > 1558) {
            return z_errtxt(ZINT_ERROR_TOO_LONG, symbol, 520,
                            "Input too long, requires too many codewords (maximum 1558)");
        }

    } /* while */

    *p_sp = sp;
    *p_tp = tp;
    *p_process_p = process_p;
    *p_current_mode = current_mode;

    return 0;
}

/* Encodes data using ASCII, C40, Text, X12, EDIFACT or Base 256 modes as appropriate
   Supports encoding FNC1 in supporting systems */
static int dm_encode(struct zint_symbol *symbol, const unsigned char source[], const int length, const int eci,
            const int last_seg, const int gs1, unsigned char target[], int *p_tp) {
    int sp = 0;
    int tp = *p_tp;
    int current_mode = DM_ASCII;
    int i;
    int process_buffer[8]; /* holds remaining data to finalised */
    int process_p = 0; /* number of characters left to finalise */
    int b256_start = 0;
    int symbols_left;
    int error_number;
    const int debug_print = symbol->debug & ZINT_DEBUG_PRINT;

    if (eci > 0) {
        /* Encode ECI numbers according to Table 6 */
        target[tp++] = 241; /* ECI Character */
        if (eci <= 126) {
            target[tp++] = (unsigned char) (eci + 1);
        } else if (eci <= 16382) {
            target[tp++] = (unsigned char) ((eci - 127) / 254 + 128);
            target[tp++] = (unsigned char) ((eci - 127) % 254 + 1);
        } else {
            target[tp++] = (unsigned char) ((eci - 16383) / 64516 + 192);
            target[tp++] = (unsigned char) (((eci - 16383) / 254) % 254 + 1);
            target[tp++] = (unsigned char) ((eci - 16383) % 254 + 1);
        }
        if (debug_print) printf("ECI %d ", eci + 1);
    }

    /* If FAST_MODE or MAILMARK_2D, do Annex J-based encodation */
    if ((symbol->input_mode & FAST_MODE) || symbol->symbology == BARCODE_MAILMARK_2D) {
        error_number = dm_isoenc(symbol, source, length, &sp, target, &tp, process_buffer, &process_p,
                                    &b256_start, &current_mode, gs1, debug_print);
    } else { /* Do default minimal encodation */
        error_number = dm_minimalenc(symbol, source, length, last_seg, &sp, target, &tp, process_buffer, &process_p,
                                        &b256_start, &current_mode, gs1, debug_print);
    }
    if (error_number) {
        assert(error_number >= ZINT_ERROR);
        return error_number;
    }

    symbols_left = last_seg ? dm_codewords_remaining(symbol, tp, process_p) : 3;

    if (debug_print) {
        printf("\nsymbols_left %d, tp %d, process_p %d, last_seg %d, ", symbols_left, tp, process_p, last_seg);
    }

    if (current_mode == DM_C40 || current_mode == DM_TEXT) {
        /* NOTE: changed to follow spec exactly here, only using Shift 1 padded triplets when 2 symbol chars remain.
           This matches the behaviour of BWIPP but not TEC-IT, nor figures 4.15.1-1 and 4.15-1-2 in GS1 General
           Specifications 21.0.1.
         */
        if (debug_print) fputs(current_mode == DM_C40 ? "C40 " : "TEX ", stdout);
        if (process_p == 0) {
            if (symbols_left > 0) {
                target[tp++] = 254; /* Unlatch */
                if (debug_print) fputs("ASC ", stdout);
            }
        } else {
            if (process_p == 2 && symbols_left == 2) {
                /* 5.2.5.2 (b) */
                process_buffer[process_p++] = 0; /* Shift 1 */
                (void) dm_ctx_buffer_xfer(process_buffer, process_p, target, &tp, debug_print);

            } else if (process_p == 1 && symbols_left <= 2 && dm_isc40text(current_mode, source[length - 1])) {
                /* 5.2.5.2 (c)/(d) */
                if (symbols_left > 1) {
                    /* 5.2.5.2 (c) */
                    target[tp++] = 254; /* Unlatch and encode remaining data in ASCII. */
                    if (debug_print) fputs("ASC ", stdout);
                }
                target[tp++] = source[length - 1] + 1;
                if (debug_print) printf("A%02X ", target[tp - 1] - 1);

            } else {
                int cnt, total_cnt = 0;
                /* Backtrack to last complete triplet (same technique as BWIPP) */
                while (sp > 0 && process_p % 3) {
                    sp--;
                    cnt = dm_c40text_cnt(current_mode, gs1, source[sp]);
                    total_cnt += cnt;
                    process_p -= cnt;
                }
                tp -= (total_cnt / 3) * 2;

                target[tp++] = 254; /* Unlatch */
                if (debug_print) fputs("ASC ", stdout);
                for (; sp < length; sp++) {
                    if (z_is_twodigits(source, length, sp)) {
                        target[tp++] = (unsigned char) (10 * z_ctoi(source[sp]) + z_ctoi(source[sp + 1]) + 130);
                        if (debug_print) printf("N%02d ", target[tp - 1] - 130);
                        sp++;
                    } else if (source[sp] & 0x80) {
                        target[tp++] = 235; /* FNC4 */
                        target[tp++] = (source[sp] - 128) + 1;
                        if (debug_print) printf("FN4 A%02X ", target[tp - 1] - 1);
                    } else if (gs1 && source[sp] == '\x1D') {
                        if (gs1 == 2) {
                            target[tp++] = 29 + 1; /* GS */
                            if (debug_print) fputs("GS ", stdout);
                        } else {
                            target[tp++] = 232; /* FNC1 */
                            if (debug_print) fputs("FN1 ", stdout);
                        }
                    } else {
                        target[tp++] = source[sp] + 1;
                        if (debug_print) printf("A%02X ", target[tp - 1] - 1);
                    }
                }
            }
        }

    } else if (current_mode == DM_X12) {
        if (debug_print) fputs("X12 ", stdout);
        if (symbols_left == 1 && process_p == 1) {
            /* Unlatch not required! */
            target[tp++] = source[length - 1] + 1;
            if (debug_print) printf("A%02X ", target[tp - 1] - 1);
        } else {
            if (symbols_left > 0) {
                target[tp++] = (254); /* Unlatch. */
                if (debug_print) fputs("ASC ", stdout);
            }

            if (process_p == 1) {
                target[tp++] = source[length - 1] + 1;
                if (debug_print) printf("A%02X ", target[tp - 1] - 1);
            } else if (process_p == 2) {
                target[tp++] = source[length - 2] + 1;
                target[tp++] = source[length - 1] + 1;
                if (debug_print) printf("A%02X A%02X ", target[tp - 2] - 1, target[tp - 1] - 1);
            }
        }

    } else if (current_mode == DM_EDIFACT) {
        if (debug_print) fputs("EDI ", stdout);
        if (symbols_left <= 2 && process_p <= symbols_left) { /* Unlatch not required! */
            if (process_p == 1) {
                target[tp++] = source[length - 1] + 1;
                if (debug_print) printf("A%02X ", target[tp - 1] - 1);
            } else if (process_p == 2) {
                target[tp++] = source[length - 2] + 1;
                target[tp++] = source[length - 1] + 1;
                if (debug_print) printf("A%02X A%02X ", target[tp - 2] - 1, target[tp - 1] - 1);
            }
        } else {
            /* Append EDIFACT unlatch value (31) and empty buffer */
            if (process_p <= 3) {
                process_buffer[process_p++] = 31;
            }
            (void) dm_edi_buffer_xfer(process_buffer, process_p, target, &tp, 1 /*empty*/, debug_print);
        }

    } else if (current_mode == DM_BASE256) {
        if (symbols_left > 0) {
            tp = dm_update_b256_field_length(target, tp, b256_start);
        }
        /* B.2.1 255-state randomising algorithm */
        for (i = b256_start; i < tp; i++) {
            const int prn = (149 * (i + 1)) % 255 + 1;
            target[i] = (unsigned char) ((target[i] + prn) & 0xFF);
        }
    }

    if (debug_print) {
        printf("\nData (%d):", tp);
        for (i = 0; i < tp; i++)
            printf(" %d", target[i]);

        fputc('\n', stdout);
    }

    *p_tp = tp;

    return 0;
}

#ifdef ZINT_TEST /* Wrapper for direct testing */
INTERNAL int zint_test_dm_encode(struct zint_symbol *symbol, const unsigned char source[], const int length,
                const int eci, const int last_seg, const int gs1, unsigned char target[], int *p_tp) {
    return dm_encode(symbol, source, length, eci, last_seg, gs1, target, p_tp);
}
#endif

/* Call `dm_encode()` for each segment, dealing with Structured Append, GS1, READER_INIT and macro headers
   beforehand */
static int dm_encode_segs(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count,
            unsigned char target[], int *p_binlen) {
    int error_number;
    int i;
    int tp = 0;
    int in_macro = 0;
    const struct zint_seg *last_seg = &segs[seg_count - 1];
    /* gs1 flag values: 0: no GS1, 1: GS1 with FNC1 serparator, 2: GS separator */
    const int gs1 = (symbol->input_mode & 0x07) == GS1_MODE ? 1 + !!(symbol->output_options & GS1_GS_SEPARATOR) : 0;
    /* Raw text dealt with by `ZBarcode_Encode_Segs()`, except for `eci` feedback.
       Note not updating `eci` for GS1 mode as not converted */
    const int content_segs = !gs1 && (symbol->output_options & BARCODE_CONTENT_SEGS);
    const int debug_print = symbol->debug & ZINT_DEBUG_PRINT;

    if ((i = z_segs_length(segs, seg_count)) > 3116) { /* Max is 3166 digits */
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 719, "Input length %d too long (maximum 3116)", i);
    }

    if (symbol->structapp.count) {
        int id1, id2;

        if (symbol->structapp.count < 2 || symbol->structapp.count > 16) {
            return z_errtxtf(ZINT_ERROR_INVALID_OPTION, symbol, 720,
                            "Structured Append count '%d' out of range (2 to 16)", symbol->structapp.count);
        }
        if (symbol->structapp.index < 1 || symbol->structapp.index > symbol->structapp.count) {
            return ZEXT z_errtxtf(ZINT_ERROR_INVALID_OPTION, symbol, 721,
                                    "Structured Append index '%1$d' out of range (1 to count %2$d)",
                                    symbol->structapp.index, symbol->structapp.count);
        }
        if (symbol->structapp.id[0]) {
            int id, id_len, id1_err, id2_err;

            for (id_len = 1; id_len < 7 && symbol->structapp.id[id_len]; id_len++);

            if (id_len > 6) { /* ID1 * 1000 + ID2 */
                return z_errtxtf(ZINT_ERROR_INVALID_OPTION, symbol, 722,
                                "Structured Append ID length %d too long (6 digit maximum)", id_len);
            }

            id = z_to_int(ZCUCP(symbol->structapp.id), id_len);
            if (id == -1) {
                return z_errtxt(ZINT_ERROR_INVALID_OPTION, symbol, 723, "Invalid Structured Append ID (digits only)");
            }
            id1 = id / 1000;
            id2 = id % 1000;
            id1_err = id1 < 1 || id1 > 254;
            id2_err = id2 < 1 || id2 > 254;
            if (id1_err || id2_err) {
                if (id1_err && id2_err) {
                    return ZEXT z_errtxtf(ZINT_ERROR_INVALID_OPTION, symbol, 724,
                                            "Structured Append ID1 '%1$03d' and ID2 '%2$03d' out of range"
                                            " (001 to 254) (ID \"%3$03d%4$03d\")",
                                            id1, id2, id1, id2);
                }
                if (id1_err) {
                    return ZEXT z_errtxtf(ZINT_ERROR_INVALID_OPTION, symbol, 725,
                                    "Structured Append ID1 '%1$03d' out of range (001 to 254) (ID \"%2$03d%3$03d\")",
                                    id1, id1, id2);
                }
                return ZEXT z_errtxtf(ZINT_ERROR_INVALID_OPTION, symbol, 726,
                                    "Structured Append ID2 '%1$03d' out of range (001 to 254) (ID \"%2$03d%3$03d\")",
                                    id2, id1, id2);
            }
        } else {
            id1 = id2 = 1;
        }

        target[tp++] = 233;
        target[tp++] = (17 - symbol->structapp.count) | ((symbol->structapp.index - 1) << 4);
        target[tp++] = id1;
        target[tp++] = id2;
    }

    if (gs1) {
        target[tp++] = 232;
        if (debug_print) fputs("FN1 ", stdout);
    } /* FNC1 */

    if (symbol->output_options & READER_INIT) {
        if (gs1) {
            return z_errtxt(ZINT_ERROR_INVALID_OPTION, symbol, 521, "Cannot use Reader Initialisation in GS1 mode");
        }
        if (symbol->structapp.count) {
            return z_errtxt(ZINT_ERROR_INVALID_OPTION, symbol, 727,
                            "Cannot have Structured Append and Reader Initialisation at the same time");
        }
        target[tp++] = 234; /* Reader Programming */
        if (debug_print) fputs("RP ", stdout);
    }

    /* Check for Macro05/Macro06 */
    /* "[)>[RS]05[GS]...[RS][EOT]" -> CW 236 */
    /* "[)>[RS]06[GS]...[RS][EOT]" -> CW 237 */
    if (tp == 0 && segs[0].length >= 9 && last_seg->length >= 2
            && segs[0].source[0] == '[' && segs[0].source[1] == ')' && segs[0].source[2] == '>'
            && segs[0].source[3] == '\x1e' /*RS*/ && segs[0].source[4] == '0'
            && (segs[0].source[5] == '5' || segs[0].source[5] == '6')
            && segs[0].source[6] == '\x1d' /*GS*/
            && last_seg->source[last_seg->length - 1] == '\x04' /*EOT*/
            && last_seg->source[last_seg->length - 2] == '\x1e' /*RS*/) {

        /* Output macro Codeword */
        if (segs[0].source[5] == '5') {
            target[tp++] = 236;
            if (debug_print) fputs("Macro05 ", stdout);
        } else {
            target[tp++] = 237;
            if (debug_print) fputs("Macro06 ", stdout);
        }
        /* Remove macro characters from input string */
        in_macro = 1;
    }

    for (i = 0; i < seg_count; i++) {
        int src_inc = 0, len_dec = 0;
        if (in_macro) {
            if (i == 0) {
                src_inc = len_dec = 7; /* Skip over macro characters at beginning */
            }
            if (i + 1 == seg_count) {
                len_dec += 2;  /* Remove RS + EOT from end */
            }
        }
        if ((error_number = dm_encode(symbol, segs[i].source + src_inc, segs[i].length - len_dec, segs[i].eci,
                                        i + 1 == seg_count, gs1, target, &tp))) {
            assert(error_number >= ZINT_ERROR);
            return error_number;
        }
        if (content_segs && segs[i].eci) {
            z_ct_set_seg_eci(symbol, i, segs[i].eci);
        }
    }

    *p_binlen = tp;

    return 0;
}

/* add pad bits */
static void dm_add_tail(unsigned char target[], int tp, const int tail_length) {
    int i;

    target[tp++] = 129; /* Pad */
    for (i = 1; i < tail_length; i++) {
        /* B.1.1 253-state randomising algorithm */
        const int prn = (149 * (tp + 1)) % 253 + 1;
        const int temp = 129 + prn;
        if (temp <= 254) {
            target[tp++] = (unsigned char) temp;
        } else {
            target[tp++] = (unsigned char) (temp - 254);
        }
    }
}

static int dm_ecc200(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count) {
    int i, skew = 0;
    unsigned char binary[2200];
    int binlen = 0; /* Suppress clang-tidy-20 uninitialized value false positive */
    int symbolsize;
    int taillength, error_number;
    int H, W, FH, FW, datablock, bytes, rsblock;
    const int debug_print = symbol->debug & ZINT_DEBUG_PRINT;

    /* `length` may be decremented by 2 if macro character is used */
    error_number = dm_encode_segs(symbol, segs, seg_count, binary, &binlen);
    if (error_number != 0) {
        return error_number;
    }

    symbolsize = dm_get_symbolsize(symbol, binlen);

    if (binlen > dm_matrixbytes[symbolsize]) {
        if (symbol->option_2 >= 1 && symbol->option_2 <= DMSIZESCOUNT) {
            /* The symbol size was given by --ver (option_2) */
            return ZEXT z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 522,
                                    "Input too long for Version %1$d, requires %2$d codewords (maximum %3$d)",
                                    symbol->option_2, binlen, dm_matrixbytes[symbolsize]);
        }
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 523, "Input too long, requires %d codewords (maximum 1558)",
                        binlen);
    }

    /* Feedback options */
    if (symbol->option_2 < 1 || symbol->option_2 > DMSIZESCOUNT) {
        for (i = 0; i < DMSIZESCOUNT && symbolsize != dm_intsymbol[i]; i++); /* TODO: replace with reverse table? */
        assert(i < DMSIZESCOUNT);
        symbol->option_2 = i + 1;
    }

    if (debug_print) printf("Symbol size: %d, output option 2: %d\n", symbolsize, symbol->option_2);

    H = dm_matrixH[symbolsize];
    W = dm_matrixW[symbolsize];
    FH = dm_matrixFH[symbolsize];
    FW = dm_matrixFW[symbolsize];
    bytes = dm_matrixbytes[symbolsize];
    datablock = dm_matrixdatablock[symbolsize];
    rsblock = dm_matrixrsblock[symbolsize];

    assert(H > 1 && W > 1); /* Suppress clang-tidy-21 clang-analyzer-security.ArrayBound */

    taillength = bytes - binlen;

    if (taillength != 0) {
        dm_add_tail(binary, binlen, taillength);
    }
    if (debug_print) {
        printf("HxW: %dx%d\nPads (%d): ", H, W, taillength);
        for (i = binlen; i < binlen + taillength; i++) printf("%d ", binary[i]);
        fputc('\n', stdout);
    }

    /* ecc code */
    if (symbolsize == DMINTSYMBOL144 && !(symbol->option_3 & DM_ISO_144)) {
        skew = 1;
    }
    dm_ecc(binary, bytes, datablock, rsblock, skew);
    if (debug_print) {
        printf("ECC (%d): ", rsblock * (bytes / datablock));
        assert(bytes > 0); /* Suppress clang-tidy-21 clang-analyzer-security.ArrayBound */
        for (i = bytes; i < bytes + rsblock * (bytes / datablock); i++) printf("%d ", binary[i]);
        fputc('\n', stdout);
    }

#ifdef ZINT_TEST
    if (symbol->debug & ZINT_DEBUG_TEST) {
        z_debug_test_codeword_dump(symbol, binary, skew ? 1558 + 620 : bytes + rsblock * (bytes / datablock));
    }
#endif
    { /* placement */
        const int NC = W - 2 * (W / FW);
        const int NR = H - 2 * (H / FH);
        int x, y, *places;
        if (!(places = (int *) calloc((size_t) NC * (size_t) NR, sizeof(int)))) {
            return z_errtxt(ZINT_ERROR_MEMORY, symbol, 718, "Insufficient memory for placement array");
        }
        dm_placement(places, NR, NC);
        for (y = 0; y < H; y += FH) {
            for (x = 0; x < W; x++)
                z_set_module(symbol, (H - y) - 1, x);
            for (x = 0; x < W; x += 2)
                z_set_module(symbol, y, x);
        }
        for (x = 0; x < W; x += FW) {
            for (y = 0; y < H; y++)
                z_set_module(symbol, (H - y) - 1, x);
            for (y = 0; y < H; y += 2)
                z_set_module(symbol, (H - y) - 1, x + FW - 1);
        }
#ifdef DM_DEBUG
        /* Print position matrix as in standard */
        for (y = NR - 1; y >= 0; y--) {
            for (x = 0; x < NC; x++) {
                const int v = places[(NR - y - 1) * NC + x];
                if (x != 0) fprintf(stderr, "|");
                fprintf(stderr, "%3d.%2d", (v >> 3), 8 - (v & 7));
            }
            fprintf(stderr, "\n");
        }
#endif
        for (y = 0; y < NR; y++) {
            for (x = 0; x < NC; x++) {
                const int v = places[(NR - y - 1) * NC + x];
                if (v == 1 || (v > 7 && (binary[(v >> 3) - 1] & (1 << (v & 7))))) {
                    z_set_module(symbol, H - (1 + y + 2 * (y / (FH - 2))) - 1, 1 + x + 2 * (x / (FW - 2)));
                }
            }
        }
        for (y = 0; y < H; y++) {
            symbol->row_height[y] = 1;
        }
        free(places);
    }

    symbol->height = H;
    symbol->rows = H;
    symbol->width = W;

    return error_number;
}

INTERNAL int zint_datamatrix(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count) {

    if (symbol->option_1 <= 1) {
        /* ECC 200 */
        return dm_ecc200(symbol, segs, seg_count);
    }
    /* ECC 000 - 140 */
    return z_errtxt(ZINT_ERROR_INVALID_OPTION, symbol, 524, "Older Data Matrix standards are no longer supported");
}

/* vim: set ts=4 sw=4 et : */