comparison mupdf-source/thirdparty/zint/backend/common.h @ 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 /* common.h - Header for all common functions in common.c */
2 /*
3 libzint - the open source barcode library
4 Copyright (C) 2009-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 #ifndef Z_COMMON_H
34 #define Z_COMMON_H
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif /* __cplusplus */
39
40 #include "zint.h"
41 #include "zintconfig.h"
42 #include <stdlib.h>
43 #include <string.h>
44
45 /* Determine if C89 (excluding MSVC, which doesn't define __STDC_VERSION__) */
46 #ifndef _MSC_VER
47 # if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199000L
48 # define ZINT_IS_C89
49 # endif
50 #endif
51
52 #ifdef _MSC_VER
53 typedef unsigned __int8 uint8_t;
54 typedef unsigned __int16 uint16_t;
55 typedef __int32 int32_t;
56 typedef unsigned __int32 uint32_t;
57 typedef unsigned __int64 uint64_t;
58 #else
59 #include <stdint.h> /* Introduced C99 */
60 #endif
61
62 /* Note if change following must also change "frontend/main.c" copy */
63
64 #define ARRAY_SIZE(x) ((int) (sizeof(x) / sizeof((x)[0])))
65
66 #ifdef _MSC_VER
67 # include <malloc.h>
68 # define z_alloca(nmemb) _alloca(nmemb)
69 #elif defined(__COMPCERT__)
70 # define z_alloca(nmemb) malloc(nmemb) /* So links - leads to loads of leaks obs */
71 #else
72 # if (defined(__GNUC__) && !defined(alloca) && !defined(__NetBSD__)) || defined(__NuttX__) || defined(_AIX) \
73 || (defined(__sun) && defined(__SVR4) /*Solaris*/)
74 # include <alloca.h>
75 # endif
76 # define z_alloca(nmemb) alloca(nmemb)
77 #endif
78
79 /* End of "frontend/main.c" copy */
80
81 #ifdef _MSC_VER
82 # pragma warning(disable: 4125) /* decimal digit terminates octal escape sequence */
83 # pragma warning(disable: 4244) /* conversion from int to float */
84 # if _MSC_VER > 1200 /* VC6 */
85 # pragma warning(disable: 4996) /* function or variable may be unsafe */
86 # endif
87 #endif
88
89 #if defined(__GNUC__) && __GNUC__ >= 3
90 # define ZINT_FORMAT_PRINTF(fmt_idx, first_idx) __attribute__((__format__(printf, fmt_idx, first_idx)))
91 #else
92 # define ZINT_FORMAT_PRINTF(fmt_idx, first_idx)
93 #endif
94
95 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(ZINT_TEST) && !defined(__MINGW32__)
96 # define INTERNAL __attribute__((__visibility__("hidden")))
97 #elif defined(ZINT_TEST)
98 # define INTERNAL ZINT_EXTERN /* The test suite references INTERNAL functions, so they need to be exported */
99 #else
100 # define INTERNAL
101 #endif
102
103 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(__MINGW32__)
104 # define INTERNAL_DATA_EXTERN __attribute__((__visibility__("hidden"))) extern
105 # define INTERNAL_DATA __attribute__((__visibility__("hidden")))
106 #else
107 # define INTERNAL_DATA_EXTERN extern
108 # define INTERNAL_DATA
109 #endif
110
111 #ifdef _MSC_VER
112 # if _MSC_VER >= 1400 /* MSVC 2005 (C++ 8.0) */
113 # define restrict __restrict
114 # else
115 # define restrict
116 # endif
117 #else
118 # ifdef ZINT_IS_C89
119 # define restrict
120 # endif
121 #endif
122
123 #if (defined(_MSC_VER) && _MSC_VER <= 1200) || defined(ZINT_IS_C89) /* VC6 or C89 */
124 # define ceilf (float) ceil
125 # define floorf (float) floor
126 # define fmodf (float) fmod
127 #endif
128 /* `round()` (C99) not before MSVC 2013 (C++ 12.0) */
129 #if (defined(_MSC_VER) && _MSC_VER < 1800) || defined(ZINT_IS_C89)
130 # define round(arg) floor((arg) + 0.5)
131 # define roundf(arg) floorf((arg) + 0.5f)
132 #endif
133
134 /* Is float integral value? (https://stackoverflow.com/a/40404149) */
135 #define isfintf(arg) (fmodf(arg, 1.0f) == 0.0f)
136
137 /* Simple versions of <ctype.h> functions with no dependence on locale */
138 #define z_isdigit(c) ((c) <= '9' && (c) >= '0')
139 #define z_isupper(c) ((c) >= 'A' && (c) <= 'Z')
140 #define z_islower(c) ((c) >= 'a' && (c) <= 'z')
141
142 /* Helpers to cast away char pointer signedness */
143 #define ustrlen(source) strlen((const char *) (source))
144 #define ustrcpy(target, source) strcpy((char *) (target), (const char *) (source))
145 #define ustrcat(target, source) strcat((char *) (target), (const char *) (source))
146 #define ustrncat(target, source, count) strncat((char *) (target), (const char *) (source), (count))
147
148 /* Converts an integer value to its value + '0' (so >= 10 becomes ':', ';' etc) */
149 #define itoc(i) ((i) + '0')
150
151 /* Converts a character 0-9, A-F to its equivalent integer value */
152 INTERNAL int ctoi(const char source);
153
154 /* Converts decimal string of length <= 9 to integer value. Returns -1 if not numeric */
155 INTERNAL int to_int(const unsigned char source[], const int length);
156
157 /* Converts lower case characters to upper case in string `source` */
158 INTERNAL void to_upper(unsigned char source[], const int length);
159
160 /* Returns the number of times a character occurs in `source` */
161 INTERNAL int chr_cnt(const unsigned char source[], const int length, const unsigned char c);
162
163 /* `is_chr()` & `not_sane()` flags */
164 #define IS_SPC_F 0x0001 /* Space */
165 #define IS_HSH_F 0x0002 /* Hash sign # */
166 #define IS_AST_F 0x0004 /* Asterisk sign * */
167 #define IS_PLS_F 0x0008 /* Plus sign + */
168 #define IS_MNS_F 0x0010 /* Minus sign - */
169 #define IS_NUM_F 0x0020 /* Number 0-9 */
170 #define IS_UPO_F 0x0040 /* Uppercase letter, apart from A-F and X */
171 #define IS_UHX_F 0x0080 /* Uppercase hex A-F */
172 #define IS_UX__F 0x0100 /* Uppercase X */
173 #define IS_LWO_F 0x0200 /* Lowercase letter, apart from a-f and x */
174 #define IS_LHX_F 0x0400 /* Lowercase hex a-f */
175 #define IS_LX__F 0x0800 /* Lowercase x */
176 #define IS_C82_F 0x1000 /* CSET82 punctuation (apart from *, + and -) */
177 #define IS_SIL_F 0x2000 /* SILVER/TECHNETIUM punctuation .$/% (apart from space, + and -) */
178 #define IS_CLI_F 0x4000 /* CALCIUM INNER punctuation $:/. (apart from + and -) (Codabar) */
179 #define IS_ARS_F 0x8000 /* ARSENIC uppercase subset (VIN) */
180
181 #define IS_UPR_F (IS_UPO_F | IS_UHX_F | IS_UX__F) /* Uppercase letters */
182 #define IS_LWR_F (IS_LWO_F | IS_LHX_F | IS_LX__F) /* Lowercase letters */
183
184 /* The most commonly used set */
185 #define NEON_F IS_NUM_F /* NEON "0123456789" */
186
187 /* Whether a character matches `flg` */
188 INTERNAL int is_chr(const unsigned int flg, const unsigned int c);
189
190 /* Verifies if a string only uses valid characters, returning 1-based position in `source` if not, 0 for success */
191 INTERNAL int not_sane(const unsigned int flg, const unsigned char source[], const int length);
192
193 /* Verifies if a string only uses valid characters as above, but also returns `test_string` position of each in
194 `posns` array */
195 INTERNAL int not_sane_lookup(const char test_string[], const int test_length, const unsigned char source[],
196 const int length, int *posns);
197
198 /* Returns the position of `data` in `set_string`, or -1 if not found */
199 INTERNAL int posn(const char set_string[], const char data);
200
201
202 /* Converts `arg` to a string representing its binary equivalent of length `length` and places in `binary` at
203 `bin_posn`. Returns `bin_posn` + `length` */
204 INTERNAL int bin_append_posn(const int arg, const int length, char *binary, const int bin_posn);
205
206
207 #define Z_COMMON_INLINE 1
208
209 #ifdef Z_COMMON_INLINE
210
211 # define module_is_set(s, y, x) (((s)->encoded_data[y][(x) >> 3] >> ((x) & 0x07)) & 1)
212 # define set_module(s, y, x) do { (s)->encoded_data[y][(x) >> 3] |= 1 << ((x) & 0x07); } while (0)
213 # define module_colour_is_set(s, y, x) ((s)->encoded_data[y][x])
214 # define set_module_colour(s, y, x, c) do { (s)->encoded_data[y][x] = (c); } while (0)
215 # define unset_module(s, y, x) do { (s)->encoded_data[y][(x) >> 3] &= ~(1 << ((x) & 0x07)); } while (0)
216
217 #else /* Z_COMMON_INLINE */
218
219 /* Returns true (1) if a module is dark/black, otherwise false (0) */
220 INTERNAL int module_is_set(const struct zint_symbol *symbol, const int y_coord, const int x_coord);
221
222 /* Sets a module to dark/black */
223 INTERNAL void set_module(struct zint_symbol *symbol, const int y_coord, const int x_coord);
224
225 /* Returns true (1-8) if a module is colour, otherwise false (0) */
226 INTERNAL int module_colour_is_set(const struct zint_symbol *symbol, const int y_coord, const int x_coord);
227
228 /* Sets a module to a colour */
229 INTERNAL void set_module_colour(struct zint_symbol *symbol, const int y_coord, const int x_coord,
230 const int colour);
231
232 /* Sets a dark/black module to white (i.e. unsets) */
233 INTERNAL void unset_module(struct zint_symbol *symbol, const int y_coord, const int x_coord);
234
235 #endif /* Z_COMMON_INLINE */
236
237
238 /* Expands from a width pattern to a bit pattern */
239 INTERNAL void expand(struct zint_symbol *symbol, const char data[], const int length);
240
241
242 /* Set `symbol->errtxt` to "err_id: msg", returning `error_number`. If `err_id` is -1, the "err_id: " prefix is
243 omitted */
244 INTERNAL int errtxt(const int error_number, struct zint_symbol *symbol, const int err_id, const char *msg);
245
246 /* Set `symbol->errtxt` to "err_id: msg" with restricted subset of `printf()` formatting, returning `error_number`.
247 If `err_id` is -1, the "err_id: " prefix is omitted. Only the following specifiers are supported: "c", "d", "f",
248 "g" and "s", with no modifiers apart from "<n>$" numbering for l10n ("<n>" 1-9), in which case all specifiers must
249 be numbered, "%s" with length precisions: "%.*s", "%<n+1>$.*<n>$s", "%.<p>s" and "%<n>$.<p>s", and "%d" with
250 zero-padded minimum field lengths: "%0<m>d" or %<n>$0<m>d" ("<m>" 1-99) */
251 INTERNAL int errtxtf(const int error_number, struct zint_symbol *symbol, const int err_id, const char *fmt, ...)
252 ZINT_FORMAT_PRINTF(4, 5);
253
254 /* Helper to prepend/append to existing `symbol->errtxt` by calling `errtxtf(fmt)` with 2 arguments (copy of `errtxt`
255 & `msg`) if `msg` not NULL, or 1 argument (just copy of `errtxt`) if `msg` NULL, returning `error_number` */
256 INTERNAL int errtxt_adj(const int error_number, struct zint_symbol *symbol, const char *fmt, const char *msg);
257
258
259 /* Whether `symbology` can have row binding */
260 INTERNAL int is_stackable(const int symbology);
261
262 /* Whether `symbology` is EAN/UPC */
263 INTERNAL int is_upcean(const int symbology);
264
265 /* Whether `symbology` can have composite 2D component data */
266 INTERNAL int is_composite(const int symbology);
267
268 /* Whether `symbology` is a matrix design renderable as dots */
269 INTERNAL int is_dotty(const int symbology);
270
271 /* Whether `symbology` has a fixed aspect ratio (matrix design) */
272 INTERNAL int is_fixed_ratio(const int symbology);
273
274
275 /* Whether next two characters are digits */
276 INTERNAL int is_twodigits(const unsigned char source[], const int length, const int position);
277
278 /* Returns how many consecutive digits lie immediately ahead up to `max`, or all if `max` is -1 */
279 INTERNAL int cnt_digits(const unsigned char source[], const int length, const int position, const int max);
280
281
282 /* State machine to decode UTF-8 to Unicode codepoints (state 0 means done, state 12 means error) */
283 INTERNAL unsigned int decode_utf8(unsigned int *state, unsigned int *codep, const unsigned char byte);
284
285 /* Is string valid UTF-8? */
286 INTERNAL int is_valid_utf8(const unsigned char source[], const int length);
287
288 /* Converts UTF-8 to Unicode. If `disallow_4byte` unset, allows all values (UTF-32). If `disallow_4byte` set,
289 * only allows codepoints <= U+FFFF (ie four-byte sequences not allowed) (UTF-16, no surrogates) */
290 INTERNAL int utf8_to_unicode(struct zint_symbol *symbol, const unsigned char source[], unsigned int vals[],
291 int *length, const int disallow_4byte);
292
293 /* Treats source as ISO/IEC 8859-1 and copies into `symbol->text`, converting to UTF-8. Control chars (incl. DEL) and
294 non-ISO/IEC 8859-1 (0x80-9F) are replaced with spaces. Returns warning if truncated, else 0 */
295 INTERNAL int hrt_cpy_iso8859_1(struct zint_symbol *symbol, const unsigned char source[], const int length);
296
297
298 /* Sets symbol height, returning a warning if not within minimum and/or maximum if given.
299 `default_height` does not include height of fixed-height rows (i.e. separators/composite data) */
300 INTERNAL int set_height(struct zint_symbol *symbol, const float min_row_height, const float default_height,
301 const float max_height, const int no_errtxt);
302
303
304 /* Removes excess precision from floats - see https://stackoverflow.com/q/503436 */
305 INTERNAL float stripf(const float arg);
306
307
308 /* Returns total length of segments */
309 INTERNAL int segs_length(const struct zint_seg segs[], const int seg_count);
310
311 /* Shallow copies segments, adjusting default ECIs */
312 INTERNAL void segs_cpy(const struct zint_symbol *symbol, const struct zint_seg segs[], const int seg_count,
313 struct zint_seg local_segs[]);
314
315
316 /* Helper for ZINT_DEBUG_PRINT to put all but graphical ASCII in hex escapes. Output to `buf` if non-NULL, else
317 stdout */
318 INTERNAL char *debug_print_escape(const unsigned char *source, const int first_len, char *buf);
319
320 #ifdef ZINT_TEST
321 /* Dumps hex-formatted codewords in symbol->errtxt (for use in testing) */
322 INTERNAL void debug_test_codeword_dump(struct zint_symbol *symbol, const unsigned char *codewords, const int length);
323 /* Dumps decimal-formatted codewords in symbol->errtxt (for use in testing) */
324 INTERNAL void debug_test_codeword_dump_short(struct zint_symbol *symbol, const short *codewords, const int length);
325 /* Dumps decimal-formatted codewords in symbol->errtxt (for use in testing) */
326 INTERNAL void debug_test_codeword_dump_int(struct zint_symbol *symbol, const int *codewords, const int length);
327 #endif
328
329 #ifdef __cplusplus
330 }
331 #endif /* __cplusplus */
332
333 /* vim: set ts=4 sw=4 et : */
334 #endif /* Z_COMMON_H */