zxing-cpp 0.5.2

A rust wrapper for the zxing-cpp barcode library.
Documentation
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
/* postal.c - Handles POSTNET, PLANET, CEPNet, FIM. RM4SCC and Flattermarken */
/*
    libzint - the open source barcode library
    Copyright (C) 2008-2025 Robin Stuart <rstuart114@gmail.com>
    Including bug fixes by Bryan Hatton

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

static const char DAFTSET[] = "FADT";
static const char KRSET[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char KASUTSET[] = "1234567890-abcdefgh";
static const char CHKASUTSET[] = "0123456789-abcdefgh";
#define SHKASUTSET_F (IS_NUM_F | IS_MNS_F | IS_UPR_F) /* SHKASUTSET "1234567890-ABCDEFGHIJKLMNOPQRSTUVWXYZ" */

/* PostNet number encoding table - In this table L is long as S is short */
static const char PNTable[10][5] = {
    {'L','L','S','S','S'}, {'S','S','S','L','L'}, {'S','S','L','S','L'}, {'S','S','L','L','S'}, {'S','L','S','S','L'},
    {'S','L','S','L','S'}, {'S','L','L','S','S'}, {'L','S','S','S','L'}, {'L','S','S','L','S'}, {'L','S','L','S','S'}
};

static const char PLTable[10][5] = {
    {'S','S','L','L','L'}, {'L','L','L','S','S'}, {'L','L','S','L','S'}, {'L','L','S','S','L'}, {'L','S','L','L','S'},
    {'L','S','L','S','L'}, {'L','S','S','L','L'}, {'S','L','L','L','S'}, {'S','L','L','S','L'}, {'S','L','S','L','L'}
};

static const char RoyalValues[36][2] = {
    { 1, 1 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, { 1, 5 }, { 1, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, { 2, 4 },
    { 2, 5 }, { 2, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }, { 3, 4 }, { 3, 5 }, { 3, 0 }, { 4, 1 }, { 4, 2 },
    { 4, 3 }, { 4, 4 }, { 4, 5 }, { 4, 0 }, { 5, 1 }, { 5, 2 }, { 5, 3 }, { 5, 4 }, { 5, 5 }, { 5, 0 },
    { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 }, { 0, 5 }, { 0, 0 }
};

/* 0 = Full, 1 = Ascender, 2 = Descender, 3 = Tracker */
static const char RoyalTable[36][4] = {
    {'3','3','0','0'}, {'3','2','1','0'}, {'3','2','0','1'}, {'2','3','1','0'}, {'2','3','0','1'}, {'2','2','1','1'},
    {'3','1','2','0'}, {'3','0','3','0'}, {'3','0','2','1'}, {'2','1','3','0'}, {'2','1','2','1'}, {'2','0','3','1'},
    {'3','1','0','2'}, {'3','0','1','2'}, {'3','0','0','3'}, {'2','1','1','2'}, {'2','1','0','3'}, {'2','0','1','3'},
    {'1','3','2','0'}, {'1','2','3','0'}, {'1','2','2','1'}, {'0','3','3','0'}, {'0','3','2','1'}, {'0','2','3','1'},
    {'1','3','0','2'}, {'1','2','1','2'}, {'1','2','0','3'}, {'0','3','1','2'}, {'0','3','0','3'}, {'0','2','1','3'},
    {'1','1','2','2'}, {'1','0','3','2'}, {'1','0','2','3'}, {'0','1','3','2'}, {'0','1','2','3'}, {'0','0','3','3'}
};

static const char FlatTable[10][4] = {
    {'0','5','0','4'}, {     "18"      }, {'0','1','1','7'}, {'0','2','1','6'}, {'0','3','1','5'},
    {'0','4','1','4'}, {'0','5','1','3'}, {'0','6','1','2'}, {'0','7','1','1'}, {'0','8','1','0'}
};

static const char KoreaTable[10][10] = {
    {'1','3','1','3','1','5','0','6','1','3'}, {'0','7','1','3','1','3','1','3','1','3'},
    {'0','4','1','7','1','3','1','3','1','3'}, {'1','5','0','6','1','3','1','3','1','3'},
    {'0','4','1','3','1','7','1','3','1','3'}, {              "17171313"               },
    {'1','3','1','5','0','6','1','3','1','3'}, {'0','4','1','3','1','3','1','7','1','3'},
    {              "17131713"               }, {              "13171713"               }
};

static const char JapanTable[19][3] = {
    {'1','1','4'}, {'1','3','2'}, {'3','1','2'}, {'1','2','3'}, {'1','4','1'},
    {'3','2','1'}, {'2','1','3'}, {'2','3','1'}, {'4','1','1'}, {'1','4','4'},
    {'4','1','4'}, {'3','2','4'}, {'3','4','2'}, {'2','3','4'}, {'4','3','2'},
    {'2','4','3'}, {'4','2','3'}, {'4','4','1'}, {'1','1','1'}
};

/* Set height for POSTNET/PLANET/CEPNet codes, maintaining ratio */
static int usps_set_height(struct zint_symbol *symbol, const int no_errtxt) {
    /* USPS Domestic Mail Manual (USPS DMM 300) Jan 8, 2006 (updated 2011) 708.4.2.5 POSTNET Barcode Dimensions and
       Spacing
       http://web.archive.org/web/20061113174253/http://pe.usps.com/cpim/ftp/manuals/dmm300/full/mailingStandards.pdf
       Using bar pitch as X (1" / 43) ~ 0.023" based on 22 bars + 21 spaces per inch (bar width 0.015" - 0.025")
       Half bar height 0.05" +- 0.01;  0.040" (min) / 0.025" (X max) = 1.6 min, 0.060" (max) / 0.015" (X min) = 4 max
       Full bar height 0.125" +- 0.01; 0.115" (min) / 0.025" (X max) = 4.6 min, 0.135" (max) / 0.015" (X min) = 9 max
     */
     /* CEPNet e Código Bidimensional Datamatrix 2D (26/05/2021) 3.3.2 Arquitetura das barras - same as POSTNET */
    int error_number = 0;

    /* No legacy for CEPNet as new */
    if ((symbol->output_options & COMPLIANT_HEIGHT) || symbol->symbology == BARCODE_CEPNET) {
        symbol->row_height[0] = 3.2249999f; /* 0.075 * 43 */
        symbol->row_height[1] = 2.1500001f; /* 0.05 * 43 */
    } else {
        symbol->row_height[0] = 6.0f;
        symbol->row_height[1] = 6.0f;
    }
    if (symbol->height) {
        /* Half ratio */
        const float h_ratio = symbol->row_height[1] / (symbol->row_height[0] + symbol->row_height[1]); /* 0.4 */
        symbol->row_height[1] = z_stripf(symbol->height * h_ratio);
        if (symbol->row_height[1] < 0.5f) { /* Absolute minimum */
            symbol->row_height[1] = 0.5f;
            symbol->row_height[0] = z_stripf(0.5f / h_ratio - 0.5f); /* 0.75 */
        } else {
            symbol->row_height[0] = z_stripf(symbol->height - symbol->row_height[1]);
        }
    }
    symbol->height = z_stripf(symbol->row_height[0] + symbol->row_height[1]);

    if (symbol->output_options & COMPLIANT_HEIGHT) {
        if (symbol->height < 4.6f || symbol->height > 9.0f) {
            error_number = ZINT_WARN_NONCOMPLIANT;
            if (!no_errtxt) {
                z_errtxt(0, symbol, 498, "Height not compliant with standards");
            }
        }
    }

    return error_number;
}

/* Handles the POSTNET system used for Zip codes in the US */
/* Also handles Brazilian CEPNet - more information CEPNet e Código Bidimensional Datamatrix 2D (26/05/2021) at
   https://www.correios.com.br/enviar/correspondencia/arquivos/nacional/
    guia-tecnico-cepnet-e-2d-triagem-enderecamento-27-04-2021.pdf/view
 */
static int postnet_enc(struct zint_symbol *symbol, const unsigned char source[], char *d, const int length) {
    int i, sum, check_digit;
    int error_number = 0;
    const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;

    if (length > 38) {
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 480, "Input length %d too long (maximum 38)", length);
    }

    if (symbol->symbology == BARCODE_CEPNET) {
        if (length != 8) {
            error_number = z_errtxtf(ZINT_WARN_NONCOMPLIANT, symbol, 780,
                                    "Input length %d wrong (should be 8 digits)", length);
        }
    } else {
        if (length != 5 && length != 9 && length != 11) {
            error_number = z_errtxtf(ZINT_WARN_NONCOMPLIANT, symbol, 479,
                                    "Input length %d is not standard (should be 5, 9 or 11 digits)", length);
        }
    }
    if ((i = z_not_sane(NEON_F, source, length))) {
        return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 481,
                        "Invalid character at position %d in input (digits only)", i);
    }
    sum = 0;

    /* Start character */
    *d++ = 'L';

    for (i = 0; i < length; i++, d += 5) {
        const int val = source[i] - '0';
        memcpy(d, PNTable[val], 5);
        sum += val;
    }

    check_digit = (10 - (sum % 10)) % 10;
    memcpy(d, PNTable[check_digit], 5);
    d += 5;

    if (symbol->debug & ZINT_DEBUG_PRINT) printf("Check digit: %d\n", check_digit);

    /* Stop character */
    memcpy(d, "L", 2); /* Include terminating NUL */

    if (content_segs && z_ct_cpy_cat(symbol, source, length, (char) z_itoc(check_digit), NULL /*cat*/, 0)) {
        return ZINT_ERROR_MEMORY; /* `z_ct_cpy_cat()` only fails with OOM */
    }

    return error_number;
}

/* Puts POSTNET barcodes into the pattern matrix */
INTERNAL int zint_postnet(struct zint_symbol *symbol, unsigned char source[], int length) {
    /* Suppress clang-tidy-20 garbage value false positive by initializing (see "vector.c" `vection_add_rect()`) */
    char height_pattern[256] = {0}; /* 5 + 38 * 5 + 5 + 5 + 1 = 206 */
    unsigned int loopey, h;
    int writer;
    int error_number, warn_number;

    error_number = postnet_enc(symbol, source, height_pattern, length);
    if (error_number >= ZINT_ERROR) {
        return error_number;
    }

    writer = 0;
    h = (int) strlen(height_pattern);
    for (loopey = 0; loopey < h; loopey++) {
        if (height_pattern[loopey] == 'L') {
            z_set_module(symbol, 0, writer);
        }
        z_set_module(symbol, 1, writer);
        writer += 2;
    }
    warn_number = usps_set_height(symbol, error_number /*no_errtxt*/);
    symbol->rows = 2;
    symbol->width = writer - 1;

    return error_number ? error_number : warn_number;
}

/* Handles the PLANET system used for item tracking in the US */
static int planet_enc(struct zint_symbol *symbol, const unsigned char source[], char *d, const int length) {
    int i, sum, check_digit;
    int error_number = 0;
    const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;

    if (length > 38) {
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 482, "Input length %d too long (maximum 38)", length);
    }
    if (length != 11 && length != 13) {
        error_number = z_errtxtf(ZINT_WARN_NONCOMPLIANT, symbol, 478,
                                "Input length %d is not standard (should be 11 or 13 digits)", length);
    }
    if ((i = z_not_sane(NEON_F, source, length))) {
        return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 483,
                        "Invalid character at position %d in input (digits only)", i);
    }
    sum = 0;

    /* Start character */
    *d++ = 'L';

    for (i = 0; i < length; i++, d += 5) {
        const int val = source[i] - '0';
        memcpy(d, PLTable[val], 5);
        sum += val;
    }

    check_digit = (10 - (sum % 10)) % 10;
    memcpy(d, PLTable[check_digit], 5);
    d += 5;

    if (symbol->debug & ZINT_DEBUG_PRINT) printf("Check digit: %d\n", check_digit);

    /* Stop character */
    memcpy(d, "L", 2); /* Include terminating NUL */

    if (content_segs && z_ct_cpy_cat(symbol, source, length, (char) z_itoc(check_digit), NULL /*cat*/, 0)) {
        return ZINT_ERROR_MEMORY; /* `z_ct_cpy_cat()` only fails with OOM */
    }

    return error_number;
}

/* Puts PLANET barcodes into the pattern matrix */
INTERNAL int zint_planet(struct zint_symbol *symbol, unsigned char source[], int length) {
    /* Suppress clang-tidy-20 garbage value false positive by initializing (see "vector.c" `vection_add_rect()`) */
    char height_pattern[256] = {0}; /* 5 + 38 * 5 + 5 + 5 + 1 = 206 */
    unsigned int loopey, h;
    int writer;
    int error_number, warn_number;

    error_number = planet_enc(symbol, source, height_pattern, length);
    if (error_number >= ZINT_ERROR) {
        return error_number;
    }

    writer = 0;
    h = (int) strlen(height_pattern);
    for (loopey = 0; loopey < h; loopey++) {
        if (height_pattern[loopey] == 'L') {
            z_set_module(symbol, 0, writer);
        }
        z_set_module(symbol, 1, writer);
        writer += 2;
    }
    warn_number = usps_set_height(symbol, error_number /*no_errtxt*/);
    symbol->rows = 2;
    symbol->width = writer - 1;

    return error_number ? error_number : warn_number;
}

/* Korean Postal Authority */
INTERNAL int zint_koreapost(struct zint_symbol *symbol, unsigned char source[], int length) {
    int total, i, check, zeroes, error_number = 0;
    unsigned char local_source[8];
    char dest[80];
    char *d = dest;
    int posns[6];
    const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;

    if (length > 6) {
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 484, "Input length %d too long (maximum 6)", length);
    }
    if ((i = z_not_sane(NEON_F, source, length))) {
        return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 485,
                        "Invalid character at position %d in input (digits only)", i);
    }
    zeroes = 6 - length;
    memset(local_source, '0', zeroes);
    memcpy(local_source + zeroes, source, length);

    total = 0;
    for (i = 0; i < 6; i++) {
        posns[i] = z_ctoi(local_source[i]);
        total += posns[i];
    }
    check = 10 - (total % 10);
    if (check == 10) {
        check = 0;
    }
    local_source[6] = z_itoc(check);

    for (i = 5; i >= 0; i--) {
        const char *const entry = KoreaTable[posns[i]];
        memcpy(d, entry, 10);
        d += entry[8] ? 10 : 8;
    }
    memcpy(d, KoreaTable[check], 10);
    d += KoreaTable[check][8] ? 10 : 8;

    z_expand(symbol, dest, d - dest);

    /* TODO: Find documentation on BARCODE_KOREAPOST dimensions/height */

    z_hrt_cpy_nochk(symbol, local_source, 7);

    if (content_segs && z_ct_cpy(symbol, local_source, 7)) {
        return ZINT_ERROR_MEMORY; /* `z_ct_cpy()` only fails with OOM */
    }

    return error_number;
}

/* The simplest barcode symbology ever! Supported by MS Word, so here it is!
    glyphs from http://en.wikipedia.org/wiki/Facing_Identification_Mark */
INTERNAL int zint_fim(struct zint_symbol *symbol, unsigned char source[], int length) {
    int error_number = 0;
    const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;

    if (length > 1) {
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 486, "Input length %d too long (maximum 1)", length);
    }

    z_to_upper(source, length);

    switch ((char) source[0]) {
        case 'A':
            z_expand(symbol, "111515111", 9);
            break;
        case 'B':
            z_expand(symbol, "13111311131", 11);
            break;
        case 'C':
            z_expand(symbol, "11131313111", 11);
            break;
        case 'D':
            z_expand(symbol, "1111131311111", 13);
            break;
        case 'E':
            z_expand(symbol, "1317131", 7);
            break;
        default:
            return z_errtxt(ZINT_ERROR_INVALID_DATA, symbol, 487,
                            "Invalid character in input (\"A\", \"B\", \"C\", \"D\" or \"E\" only)");
            break;
    }

    if (symbol->output_options & COMPLIANT_HEIGHT) {
        /* USPS Domestic Mail Manual (USPS DMM 300) Jan 8, 2006 (updated 2011) 708.9.3
           X 0.03125" (1/32) +- 0.008" so X max 0.03925", height 0.625" (5/8) +- 0.125" (1/8) */
        const float min_height = 12.7388535f; /* 0.5 / 0.03925 */
        const float default_height = 20.0f; /* 0.625 / 0.03125 */
        const float max_height = 31.0559006f; /* 0.75 / 0.02415 */
        error_number = z_set_height(symbol, min_height, default_height, max_height, 0 /*no_errtxt*/);
    } else {
        (void) z_set_height(symbol, 0.0f, 50.0f, 0.0f, 1 /*no_errtxt*/);
    }

    if (content_segs && z_ct_cpy(symbol, source, length)) {
        return ZINT_ERROR_MEMORY; /* `z_ct_cpy()` only fails with OOM */
    }

    return error_number;
}

/* Set height for DAFT-type codes, maintaining ratio. Expects row_height[0] & row_height[1] to be set */
/* Used by auspost.c also */
INTERNAL int zint_daft_set_height(struct zint_symbol *symbol, const float min_height, const float max_height) {
    int error_number = 0;

    if (symbol->height) {
        /* Tracker ratio */
        const float t_ratio = z_stripf(symbol->row_height[1] / z_stripf(symbol->row_height[0] * 2
                                        + symbol->row_height[1]));
        symbol->row_height[1] = z_stripf(symbol->height * t_ratio);
        if (symbol->row_height[1] < 0.5f) { /* Absolute minimum */
            symbol->row_height[1] = 0.5f;
            symbol->row_height[0] = z_stripf(0.25f / t_ratio - 0.25f);
        } else {
            symbol->row_height[0] = z_stripf(z_stripf(symbol->height - symbol->row_height[1]) / 2.0f);
        }
        if (symbol->row_height[0] < 0.5f) {
            symbol->row_height[0] = 0.5f;
            symbol->row_height[1] = z_stripf(t_ratio / (1.0f - t_ratio));
        }
    }
    symbol->row_height[2] = symbol->row_height[0];
    symbol->height = z_stripf(z_stripf(symbol->row_height[0] + symbol->row_height[1]) + symbol->row_height[2]);

    if (symbol->output_options & COMPLIANT_HEIGHT) {
        if ((min_height && symbol->height < min_height) || (max_height && symbol->height > max_height)) {
            error_number = z_errtxt(ZINT_WARN_NONCOMPLIANT, symbol, 499, "Height not compliant with standards");
        }
    }

    return error_number;
}

/* Handles the 4 State barcodes used in the UK by Royal Mail. Returns check_character */
static int rm4scc_enc(struct zint_symbol *symbol, const int *posns, char *d, const int length) {
    int i;
    int top, bottom, row, column, check_digit;

    top = 0;
    bottom = 0;

    /* Start character */
    *d++ = '1';

    for (i = 0; i < length; i++, d += 4) {
        const int p = posns[i];
        memcpy(d, RoyalTable[p], 4);
        top += RoyalValues[p][0];
        bottom += RoyalValues[p][1];
    }

    /* Calculate the check digit */
    row = (top % 6) - 1;
    column = (bottom % 6) - 1;
    if (row == -1) {
        row = 5;
    }
    if (column == -1) {
        column = 5;
    }
    check_digit = (6 * row) + column;
    memcpy(d, RoyalTable[check_digit], 4);
    d += 4;

    if (symbol->debug & ZINT_DEBUG_PRINT) printf("Check digit: %d\n", check_digit);

    /* Stop character */
    memcpy(d, "0", 2); /* Include terminating NUL */

    return KRSET[check_digit];
}

/* Puts RM4SCC into the data matrix */
INTERNAL int zint_rm4scc(struct zint_symbol *symbol, unsigned char source[], int length) {
    int i;
    char height_pattern[210];
    int posns[50];
    int loopey, h;
    int writer;
    char check_digit;
    int error_number = 0;
    const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;

    if (length > 50) {
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 488, "Input length %d too long (maximum 50)", length);
    }
    z_to_upper(source, length);
    if ((i = z_not_sane_lookup(KRSET, 36, source, length, posns))) {
        return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 489,
                        "Invalid character at position %d in input (alphanumerics only)", i);
    }

    check_digit = rm4scc_enc(symbol, posns, height_pattern, length);

    writer = 0;
    h = (int) strlen(height_pattern);
    for (loopey = 0; loopey < h; loopey++) {
        if (height_pattern[loopey] == '1' || height_pattern[loopey] == '0') {
            z_set_module(symbol, 0, writer);
        }
        z_set_module(symbol, 1, writer);
        if (height_pattern[loopey] == '2' || height_pattern[loopey] == '0') {
            z_set_module(symbol, 2, writer);
        }
        writer += 2;
    }

    if (symbol->output_options & COMPLIANT_HEIGHT) {
        /* Royal Mail Know How User's Manual Appendix C: using CBC
           (https://web.archive.org/web/20120120060743/
            http://www.royalmail.com/sites/default/files/docs/pdf/Know How 2006 PIP vs 1.6a Accepted Changes.pdf)
           Bar pitch and min/maxes same as Mailmark, so using recommendations from
           Royal Mail Mailmark Barcode Definition Document (15 Sept 2015) Section 3.5.1
         */
        const float min_height = 6.47952747f; /* (4.22 * 39) / 25.4 */
        const float max_height = 10.8062992f; /* (5.84 * 47) / 25.4 */
        symbol->row_height[0] = 3.16417313f; /* (1.9 * 42.3) / 25.4 */
        symbol->row_height[1] = 2.16496062f; /* (1.3 * 42.3) / 25.4 */
        /* Note using max X for minimum and min X for maximum */
        error_number = zint_daft_set_height(symbol, min_height, max_height);
    } else {
        symbol->row_height[0] = 3.0f;
        symbol->row_height[1] = 2.0f;
        (void) zint_daft_set_height(symbol, 0.0f, 0.0f);
    }
    symbol->rows = 3;
    symbol->width = writer - 1;

    if (content_segs && z_ct_cpy_cat(symbol, source, length, check_digit, NULL /*cat*/, 0)) {
        return ZINT_ERROR_MEMORY; /* `z_ct_cpy_cat()` only fails with OOM */
    }

    return error_number;
}

/* Handles Dutch Post TNT KIX symbols
   The same as RM4SCC but without check digit or stop/start chars
   Specification at http://www.tntpost.nl/zakelijk/klantenservice/downloads/kIX_code/download.aspx */
INTERNAL int zint_kix(struct zint_symbol *symbol, unsigned char source[], int length) {
    char height_pattern[75];
    char *d = height_pattern;
    int posns[18];
    int loopey;
    int writer, i, h;
    int error_number = 0;
    const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;

    if (length > 18) {
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 490, "Input length %d too long (maximum 18)", length);
    }
    z_to_upper(source, length);
    if ((i = z_not_sane_lookup(KRSET, 36, source, length, posns))) {
        return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 491,
                        "Invalid character at position %d in input (alphanumerics only)", i);
    }

    /* Encode data */
    for (i = 0; i < length; i++, d += 4) {
        memcpy(d, RoyalTable[posns[i]], 4);
    }

    writer = 0;
    h = d - height_pattern;
    for (loopey = 0; loopey < h; loopey++) {
        if (height_pattern[loopey] == '1' || height_pattern[loopey] == '0') {
            z_set_module(symbol, 0, writer);
        }
        z_set_module(symbol, 1, writer);
        if (height_pattern[loopey] == '2' || height_pattern[loopey] == '0') {
            z_set_module(symbol, 2, writer);
        }
        writer += 2;
    }

    if (symbol->output_options & COMPLIANT_HEIGHT) {
        /* Dimensions same as RM4SCC */
        const float min_height = 6.47952747f; /* (4.22 * 39) / 25.4 */
        const float max_height = 10.8062992f; /* (5.84 * 47) / 25.4 */
        symbol->row_height[0] = 3.16417313f; /* (1.9 * 42.3) / 25.4 */
        symbol->row_height[1] = 2.16496062f; /* (1.3 * 42.3) / 25.4 */
        /* Note using max X for minimum and min X for maximum */
        error_number = zint_daft_set_height(symbol, min_height, max_height);
    } else {
        symbol->row_height[0] = 3.0f;
        symbol->row_height[1] = 2.0f;
        (void) zint_daft_set_height(symbol, 0.0f, 0.0f);
    }
    symbol->rows = 3;
    symbol->width = writer - 1;

    if (content_segs && z_ct_cpy(symbol, source, length)) {
        return ZINT_ERROR_MEMORY; /* `z_ct_cpy()` only fails with OOM */
    }

    return error_number;
}

/* Handles DAFT Code symbols */
INTERNAL int zint_daft(struct zint_symbol *symbol, unsigned char source[], int length) {
    int i;
    int posns[576];
    int loopey;
    int writer;
    const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;

    if (length > 576) { /* 576 * 2 = 1152 */
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 492, "Input length %d too long (maximum 576)", length);
    }
    z_to_upper(source, length);

    if ((i = z_not_sane_lookup(DAFTSET, 4, source, length, posns))) {
        return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 493,
                        "Invalid character at position %d in input (\"D\", \"A\", \"F\" and \"T\" only)", i);
    }

    writer = 0;
    for (loopey = 0; loopey < length; loopey++) {
        if (posns[loopey] == 1 || posns[loopey] == 0) {
            z_set_module(symbol, 0, writer);
        }
        z_set_module(symbol, 1, writer);
        if (posns[loopey] == 2 || posns[loopey] == 0) {
            z_set_module(symbol, 2, writer);
        }
        writer += 2;
    }

    /* Allow ratio of tracker to be specified in thousandths */
    if (symbol->option_2 >= 50 && symbol->option_2 <= 900) {
        const float t_ratio = symbol->option_2 / 1000.0f;
        if (symbol->height < 0.5f) {
            symbol->height = 8.0f;
        }
        symbol->row_height[1] = z_stripf(symbol->height * t_ratio);
        symbol->row_height[0] = z_stripf((symbol->height - symbol->row_height[1]) / 2.0f);
    } else {
        symbol->row_height[0] = 3.0f;
        symbol->row_height[1] = 2.0f;
    }

    /* DAFT generic barcode so no dimensions/height specification */
    (void) zint_daft_set_height(symbol, 0.0f, 0.0f);
    symbol->rows = 3;
    symbol->width = writer - 1;

    if (content_segs && z_ct_cpy(symbol, source, length)) {
        return ZINT_ERROR_MEMORY; /* `z_ct_cpy()` only fails with OOM */
    }

    return 0;
}

/* Flattermarken - Not really a barcode symbology! */
INTERNAL int zint_flat(struct zint_symbol *symbol, unsigned char source[], int length) {
    int i, error_number = 0;
    char dest[512]; /* 128 * 4 = 512 */
    char *d = dest;
    const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;

    if (length > 128) { /* 128 * 9 = 1152 */
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 494, "Input length %d too long (maximum 128)", length);
    }
    if ((i = z_not_sane(NEON_F, source, length))) {
        return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 495,
                        "Invalid character at position %d in input (digits only)", i);
    }

    for (i = 0; i < length; i++) {
        const char *const entry = FlatTable[source[i] - '0'];
        memcpy(d, entry, 4);
        d += entry[2] ? 4 : 2;
    }

    z_expand(symbol, dest, d - dest);

    /* TODO: Find documentation on BARCODE_FLAT dimensions/height */

    if (content_segs && z_ct_cpy(symbol, source, length)) {
        return ZINT_ERROR_MEMORY; /* `z_ct_cpy()` only fails with OOM */
    }

    return error_number;
}

/* Japanese Postal Code (Kasutama Barcode) */
INTERNAL int zint_japanpost(struct zint_symbol *symbol, unsigned char source[], int length) {
    int error_number = 0, h;
    char pattern[69];
    char *d = pattern;
    int writer, loopey, inter_posn, i, sum, check;
    char check_char;
    unsigned char inter[20 + 1];
    const int content_segs = symbol->output_options & BARCODE_CONTENT_SEGS;

    if (length > 20) {
        return z_errtxtf(ZINT_ERROR_TOO_LONG, symbol, 496, "Input length %d too long (maximum 20)", length);
    }

    z_to_upper(source, length);

    if ((i = z_not_sane(SHKASUTSET_F, source, length))) {
        return z_errtxtf(ZINT_ERROR_INVALID_DATA, symbol, 497,
                        "Invalid character at position %d in input (alphanumerics and \"-\" only)", i);
    }
    memset(inter, 'd', 20); /* Pad character CC4 */
    inter[20] = '\0';

    i = 0;
    inter_posn = 0;
    do {
        if (z_isdigit(source[i]) || source[i] == '-') {
            inter[inter_posn] = source[i];
            inter_posn++;
        } else {
            if (source[i] <= 'J') {
                inter[inter_posn] = 'a';
                inter[inter_posn + 1] = source[i] - 'A' + '0';
            } else if (source[i] <= 'T') {
                inter[inter_posn] = 'b';
                inter[inter_posn + 1] = source[i] - 'K' + '0';
            } else { /* (source[i] >= 'U') && (source[i] <= 'Z') */
                inter[inter_posn] = 'c';
                inter[inter_posn + 1] = source[i] - 'U' + '0';
            }
            inter_posn += 2;
        }
        i++;
    } while (i < length && inter_posn < 20);

    if (i != length || inter[20] != '\0') {
        return z_errtxt(ZINT_ERROR_TOO_LONG, symbol, 477,
                        "Input too long, requires too many symbol characters (maximum 20)");
    }

    memcpy(d, "13", 2); /* Start */
    d += 2;

    sum = 0;
    for (i = 0; i < 20; i++, d += 3) {
        memcpy(d, JapanTable[z_posn(KASUTSET, inter[i])], 3);
        sum += z_posn(CHKASUTSET, inter[i]);
    }

    /* Calculate check digit */
    check = 19 - (sum % 19);
    if (check == 19) {
        check = 0;
    }
    if (check <= 9) {
        check_char = check + '0';
    } else if (check == 10) {
        check_char = '-';
    } else {
        check_char = (check - 11) + 'a';
    }
    memcpy(d, JapanTable[z_posn(KASUTSET, check_char)], 3);
    d += 3;

    if (symbol->debug & ZINT_DEBUG_PRINT) printf("Check: %d, char: %c\n", check, check_char);

    memcpy(d, "31", 2); /* Stop */
    d += 2;

    /* Resolve pattern to 4-state symbols */
    writer = 0;
    h = d - pattern;
    for (loopey = 0; loopey < h; loopey++) {
        if (pattern[loopey] == '2' || pattern[loopey] == '1') {
            z_set_module(symbol, 0, writer);
        }
        z_set_module(symbol, 1, writer);
        if (pattern[loopey] == '3' || pattern[loopey] == '1') {
            z_set_module(symbol, 2, writer);
        }
        writer += 2;
    }

    symbol->rows = 3;
    symbol->width = writer - 1;

    if (symbol->output_options & COMPLIANT_HEIGHT) {
        /* Japan Post Zip/Barcode Manual pp.11-12 https://www.post.japanpost.jp/zipcode/zipmanual/p11.html
           X 0.6mm (0.5mm - 0.7mm)
           Tracker height 1.2mm (1.05mm - 1.35mm) / 0.6mm = 2,
           Ascender/descender = 1.2mm (Full 3.6mm (3.4mm - 3.6mm, max preferred) less T divided by 2) / 0.6mm = 2 */
        const float min_height = 4.85714293f; /* 3.4 / 0.7 */
        const float max_height = 7.19999981f; /* 3.6 / 0.5 */
        symbol->row_height[0] = 2.0f;
        symbol->row_height[1] = 2.0f;
        error_number = zint_daft_set_height(symbol, min_height, max_height);
    } else {
        symbol->row_height[0] = 3.0f;
        symbol->row_height[1] = 2.0f;
        (void) zint_daft_set_height(symbol, 0.0f, 0.0f);
    }

    /* Note: check char is in KASUTSET and not truly representable in raw text's SHKASUTSET_F */
    if (content_segs && z_ct_cpy(symbol, source, length)) {
        return ZINT_ERROR_MEMORY; /* `z_ct_cpy()` only fails with OOM */
    }

    return error_number;
}

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