Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zint/backend/general_field.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 /* general_field.c - Handles general field compaction (GS1 DataBar and composites) */ | |
| 2 /* | |
| 3 libzint - the open source barcode library | |
| 4 Copyright (C) 2019-2024 Robin Stuart <rstuart114@gmail.com> | |
| 5 | |
| 6 Redistribution and use in source and binary forms, with or without | |
| 7 modification, are permitted provided that the following conditions | |
| 8 are met: | |
| 9 | |
| 10 1. Redistributions of source code must retain the above copyright | |
| 11 notice, this list of conditions and the following disclaimer. | |
| 12 2. Redistributions in binary form must reproduce the above copyright | |
| 13 notice, this list of conditions and the following disclaimer in the | |
| 14 documentation and/or other materials provided with the distribution. | |
| 15 3. Neither the name of the project nor the names of its contributors | |
| 16 may be used to endorse or promote products derived from this software | |
| 17 without specific prior written permission. | |
| 18 | |
| 19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| 20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE | |
| 23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
| 25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
| 27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
| 28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 29 SUCH DAMAGE. | |
| 30 */ | |
| 31 /* SPDX-License-Identifier: BSD-3-Clause */ | |
| 32 | |
| 33 #include "common.h" | |
| 34 #include "general_field.h" | |
| 35 | |
| 36 static const char alphanum_puncs[] = "*,-./"; | |
| 37 static const char isoiec_puncs[] = "!\"%&'()*+,-./:;<=>?_ "; /* Note contains space, not in cset82 */ | |
| 38 #define IS_ISOIEC_F (IS_LWR_F | IS_C82_F | IS_AST_F | IS_PLS_F | IS_MNS_F | IS_SPC_F) | |
| 39 | |
| 40 /* Returns type of char at `i`. FNC1 counted as NUMERIC. Returns 0 if invalid char */ | |
| 41 static int general_field_type(const char *general_field, const int i) { | |
| 42 if (general_field[i] == '\x1D' || z_isdigit(general_field[i])) { | |
| 43 return NUMERIC; | |
| 44 } | |
| 45 if (z_isupper(general_field[i]) || posn(alphanum_puncs, general_field[i]) != -1) { | |
| 46 return ALPHANUMERIC; | |
| 47 } | |
| 48 if (!not_sane(IS_ISOIEC_F, (const unsigned char *) general_field + i, 1)) { | |
| 49 return ISOIEC; | |
| 50 } | |
| 51 return 0; | |
| 52 } | |
| 53 | |
| 54 /* Returns true if next (including `i`) `num` chars of type `type`, or if given (non-zero), `type2` */ | |
| 55 static int general_field_next(const char *general_field, int i, const int general_field_len, int num, const int type, | |
| 56 const int type2) { | |
| 57 if (i + num > general_field_len) { | |
| 58 return 0; | |
| 59 } | |
| 60 for (; i < general_field_len && num; i++, num--) { | |
| 61 int type_i = general_field_type(general_field, i); | |
| 62 if ((type_i != type && !type2) || (type_i != type && type_i != type2)) { | |
| 63 return 0; | |
| 64 } | |
| 65 } | |
| 66 return num == 0; | |
| 67 } | |
| 68 | |
| 69 /* Returns true if next (including `i`) `num` up to `max_num` chars of type `type` and occur at end */ | |
| 70 static int general_field_next_terminate(const char *general_field, int i, const int general_field_len, int num, | |
| 71 const int max_num, const int type) { | |
| 72 if (i + max_num < general_field_len) { | |
| 73 return 0; | |
| 74 } | |
| 75 for (; i < general_field_len; i++, num--) { | |
| 76 if (general_field_type(general_field, i) != type) { | |
| 77 return 0; | |
| 78 } | |
| 79 } | |
| 80 return i == general_field_len && num <= 0; | |
| 81 } | |
| 82 | |
| 83 /* Returns true if none of the next (including `i`) `num` chars (or end occurs) of type `type` */ | |
| 84 static int general_field_next_none(const char *general_field, int i, const int general_field_len, int num, | |
| 85 const int type) { | |
| 86 for (; i < general_field_len && num; i++, num--) { | |
| 87 if (general_field_type(general_field, i) == type) { | |
| 88 return 0; | |
| 89 } | |
| 90 } | |
| 91 return num == 0 || i == general_field_len; | |
| 92 } | |
| 93 | |
| 94 /* Attempts to apply encoding rules from sections 7.2.5.5.1 to 7.2.5.5.3 | |
| 95 * of ISO/IEC 24724:2011 (same as sections 5.4.1 to 5.4.3 of ISO/IEC 24723:2010) */ | |
| 96 INTERNAL int general_field_encode(const char *general_field, const int general_field_len, int *p_mode, | |
| 97 char *p_last_digit, char binary_string[], int *p_bp) { | |
| 98 int i, d1, d2; | |
| 99 int mode = *p_mode; | |
| 100 char last_digit = '\0'; /* Set to odd remaining digit at end if any */ | |
| 101 int bp = *p_bp; | |
| 102 | |
| 103 for (i = 0; i < general_field_len; ) { | |
| 104 int type = general_field_type(general_field, i); | |
| 105 if (!type) { | |
| 106 return 0; | |
| 107 } | |
| 108 switch (mode) { | |
| 109 case NUMERIC: | |
| 110 if (i < general_field_len - 1) { /* If at least 2 characters remain */ | |
| 111 if (type != NUMERIC || general_field_type(general_field, i + 1) != NUMERIC) { | |
| 112 /* 7.2.5.5.1/5.4.1 a) */ | |
| 113 bp = bin_append_posn(0, 4, binary_string, bp); /* Alphanumeric latch "0000" */ | |
| 114 mode = ALPHANUMERIC; | |
| 115 } else { | |
| 116 d1 = general_field[i] == '\x1D' ? 10 : ctoi(general_field[i]); | |
| 117 d2 = general_field[i + 1] == '\x1D' ? 10 : ctoi(general_field[i + 1]); | |
| 118 bp = bin_append_posn((11 * d1) + d2 + 8, 7, binary_string, bp); | |
| 119 i += 2; | |
| 120 } | |
| 121 } else { /* If 1 character remains */ | |
| 122 if (type != NUMERIC) { | |
| 123 /* 7.2.5.5.1/5.4.1 b) */ | |
| 124 bp = bin_append_posn(0, 4, binary_string, bp); /* Alphanumeric latch "0000" */ | |
| 125 mode = ALPHANUMERIC; | |
| 126 } else { | |
| 127 /* Ending with single digit. | |
| 128 * 7.2.5.5.1 c) and 5.4.1 c) dealt with separately outside this procedure */ | |
| 129 last_digit = general_field[i]; | |
| 130 i++; | |
| 131 } | |
| 132 } | |
| 133 break; | |
| 134 case ALPHANUMERIC: | |
| 135 if (general_field[i] == '\x1D') { | |
| 136 /* 7.2.5.5.2/5.4.2 a) */ | |
| 137 bp = bin_append_posn(15, 5, binary_string, bp); /* "01111" */ | |
| 138 mode = NUMERIC; | |
| 139 i++; | |
| 140 } else if (type == ISOIEC) { | |
| 141 /* 7.2.5.5.2/5.4.2 b) */ | |
| 142 bp = bin_append_posn(4, 5, binary_string, bp); /* ISO/IEC 646 latch "00100" */ | |
| 143 mode = ISOIEC; | |
| 144 } else if (general_field_next(general_field, i, general_field_len, 6, NUMERIC, 0)) { | |
| 145 /* 7.2.5.5.2/5.4.2 c) */ | |
| 146 bp = bin_append_posn(0, 3, binary_string, bp); /* Numeric latch "000" */ | |
| 147 mode = NUMERIC; | |
| 148 } else if (general_field_next_terminate(general_field, i, general_field_len, 4, | |
| 149 5 /*Can limit to 5 max due to above*/, NUMERIC)) { | |
| 150 /* 7.2.5.5.2/5.4.2 d) */ | |
| 151 bp = bin_append_posn(0, 3, binary_string, bp); /* Numeric latch "000" */ | |
| 152 mode = NUMERIC; | |
| 153 } else if (z_isdigit(general_field[i])) { | |
| 154 bp = bin_append_posn(general_field[i] - 43, 5, binary_string, bp); | |
| 155 i++; | |
| 156 } else if (z_isupper(general_field[i])) { | |
| 157 bp = bin_append_posn(general_field[i] - 33, 6, binary_string, bp); | |
| 158 i++; | |
| 159 } else { | |
| 160 bp = bin_append_posn(posn(alphanum_puncs, general_field[i]) + 58, 6, binary_string, bp); | |
| 161 i++; | |
| 162 } | |
| 163 break; | |
| 164 case ISOIEC: | |
| 165 if (general_field[i] == '\x1D') { | |
| 166 /* 7.2.5.5.3/5.4.3 a) */ | |
| 167 bp = bin_append_posn(15, 5, binary_string, bp); /* "01111" */ | |
| 168 mode = NUMERIC; | |
| 169 i++; | |
| 170 } else { | |
| 171 int next_10_not_isoiec = general_field_next_none(general_field, i, general_field_len, 10, ISOIEC); | |
| 172 if (next_10_not_isoiec && general_field_next(general_field, i, general_field_len, 4, | |
| 173 NUMERIC, 0)) { | |
| 174 /* 7.2.5.5.3/5.4.3 b) */ | |
| 175 bp = bin_append_posn(0, 3, binary_string, bp); /* Numeric latch "000" */ | |
| 176 mode = NUMERIC; | |
| 177 } else if (next_10_not_isoiec && general_field_next(general_field, i, general_field_len, 5, | |
| 178 ALPHANUMERIC, NUMERIC)) { | |
| 179 /* 7.2.5.5.3/5.4.3 c) */ | |
| 180 /* Note this rule can produce longer bitstreams if most of the alphanumerics are numeric */ | |
| 181 bp = bin_append_posn(4, 5, binary_string, bp); /* Alphanumeric latch "00100" */ | |
| 182 mode = ALPHANUMERIC; | |
| 183 } else if (z_isdigit(general_field[i])) { | |
| 184 bp = bin_append_posn(general_field[i] - 43, 5, binary_string, bp); | |
| 185 i++; | |
| 186 } else if (z_isupper(general_field[i])) { | |
| 187 bp = bin_append_posn(general_field[i] - 1, 7, binary_string, bp); | |
| 188 i++; | |
| 189 } else if (z_islower(general_field[i])) { | |
| 190 bp = bin_append_posn(general_field[i] - 7, 7, binary_string, bp); | |
| 191 i++; | |
| 192 } else { | |
| 193 bp = bin_append_posn(posn(isoiec_puncs, general_field[i]) + 232, 8, binary_string, bp); | |
| 194 i++; | |
| 195 } | |
| 196 } | |
| 197 break; | |
| 198 } | |
| 199 } | |
| 200 | |
| 201 *p_mode = mode; | |
| 202 *p_last_digit = last_digit; | |
| 203 *p_bp = bp; | |
| 204 | |
| 205 return 1; | |
| 206 } | |
| 207 | |
| 208 /* vim: set ts=4 sw=4 et : */ |
