wxMaxima
Loading...
Searching...
No Matches
common.h
1/* common.h - Header for all common functions in common.c */
2/*
3 libzint - the open source barcode library
4 Copyright (C) 2009-2026 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
37extern "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
53typedef unsigned __int8 uint8_t;
54typedef unsigned __int16 uint16_t;
55typedef __int32 int32_t;
56typedef unsigned __int32 uint32_t;
57typedef 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#elif defined(__MINGW64__) && defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 3
88# pragma GCC diagnostic ignored "-Wlong-long"
89#endif
90
91#if defined(__GNUC__) && __GNUC__ >= 3
92# define ZINT_FORMAT_PRINTF(fmt_idx, first_idx) __attribute__((__format__(printf, fmt_idx, first_idx)))
93#else
94# define ZINT_FORMAT_PRINTF(fmt_idx, first_idx)
95#endif
96
97#if defined(__GNUC__) && __GNUC__ >= 4 && !defined(ZINT_TEST) && !defined(__MINGW32__)
98# define INTERNAL __attribute__((__visibility__("hidden")))
99#elif defined(ZINT_TEST)
100# define INTERNAL ZINT_EXTERN /* The test suite references INTERNAL functions, so they need to be exported */
101#else
102# define INTERNAL
103#endif
104
105#if defined(__GNUC__) && __GNUC__ >= 4 && !defined(__MINGW32__)
106# define INTERNAL_DATA_EXTERN __attribute__((__visibility__("hidden"))) extern
107# define INTERNAL_DATA __attribute__((__visibility__("hidden")))
108#else
109# define INTERNAL_DATA_EXTERN extern
110# define INTERNAL_DATA
111#endif
112
113#ifdef _MSC_VER
114# if _MSC_VER >= 1400 /* MSVC 2005 (C++ 8.0) */
115# define restrict __restrict
116# else
117# define restrict
118# endif
119#else
120# ifdef ZINT_IS_C89
121# define restrict
122# endif
123#endif
124
125#if (defined(_MSC_VER) && _MSC_VER <= 1200) || defined(ZINT_IS_C89) /* VC6 or C89 */
126# define ceilf (float) ceil
127# define floorf (float) floor
128# define fmodf (float) fmod
129#endif
130/* `round()` (C99) not before MSVC 2013 (C++ 12.0) */
131#if (defined(_MSC_VER) && _MSC_VER < 1800) || defined(ZINT_IS_C89)
132# define round(arg) floor((arg) + 0.5)
133# define roundf(arg) floorf((arg) + 0.5f)
134#endif
135
136/* Whether `vsnprintf()` available */
137#if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(ZINT_IS_C89) /* Pre-MSVC 2015 (C++ 14.0) or C89 */
138#define Z_NO_VSNPRINTF
139#endif
140
141/* Is float integral value? (https://stackoverflow.com/a/40404149) */
142#define z_isfintf(arg) (fmodf(arg, 1.0f) == 0.0f)
143
144/* Simple versions of <ctype.h> functions with no dependence on locale */
145#define z_isdigit(ch) ((ch) <= '9' && (ch) >= '0')
146#define z_isupper(ch) ((ch) >= 'A' && (ch) <= 'Z')
147#define z_islower(ch) ((ch) >= 'a' && (ch) <= 'z')
148#define z_isalpha(ch) (z_isupper(ch) || z_islower(ch))
149#define z_isascii(ch) (!((ch) & ~0x7F))
150#define z_iscntrl(ch) (!((ch) & ~0x1F) || (ch) == 127)
151
152/* Shorthands to cast away char pointer signedness */
153#define ZUCP(p) ((unsigned char *) (p))
154#define ZCUCP(p) ((const unsigned char *) (p))
155#define ZCCP(p) ((const char *) (p))
156#define z_ustrlen(p) strlen(ZCCP(p))
157
158/* Converts an integer value to its value + '0' (so >= 10 becomes ':', ';' etc) */
159#define z_itoc(i) ((i) + '0')
160/* Converts an integer value to its hexadecimal digit */
161#define z_xtoc(i) ((i) < 10 ? z_itoc(i) : ((i) - 10) + 'A')
162
163/* Converts a character 0-9, A-F to its equivalent integer value */
164INTERNAL int z_ctoi(const char ch);
165
166/* Converts decimal string of length <= 9 to integer value. Returns -1 if not numeric */
167INTERNAL int z_to_int(const unsigned char source[], const int length);
168
169/* Converts lower case characters to upper case in string `source` */
170INTERNAL void z_to_upper(unsigned char source[], const int length);
171
172/* Returns the number of times a character `ch` occurs in `source` */
173INTERNAL int z_chr_cnt(const unsigned char source[], const int length, const unsigned char ch);
174
175/* Zero-fill `dest` buffer, appending `source'. Returns no. of zeroes added */
176INTERNAL int z_zero_fill(const unsigned char source[], const int length, unsigned char *dest, const int dest_length);
177
178/* `z_is_chr()` & `z_not_sane()` flags */
179#define IS_SPC_F 0x0001 /* Space */
180#define IS_HSH_F 0x0002 /* Hash sign # */
181#define IS_AST_F 0x0004 /* Asterisk sign * */
182#define IS_PLS_F 0x0008 /* Plus sign + */
183#define IS_MNS_F 0x0010 /* Minus sign - */
184#define IS_NUM_F 0x0020 /* Number 0-9 */
185#define IS_UPO_F 0x0040 /* Uppercase letter, apart from A-F and X */
186#define IS_UHX_F 0x0080 /* Uppercase hex A-F */
187#define IS_UX__F 0x0100 /* Uppercase X */
188#define IS_LWO_F 0x0200 /* Lowercase letter, apart from a-f and x */
189#define IS_LHX_F 0x0400 /* Lowercase hex a-f */
190#define IS_LX__F 0x0800 /* Lowercase x */
191#define IS_C82_F 0x1000 /* CSET82 punctuation (apart from *, + and -) */
192#define IS_SIL_F 0x2000 /* SILVER/TECHNETIUM punctuation .$/% (apart from space, + and -) */
193#define IS_CLI_F 0x4000 /* CALCIUM INNER punctuation $:/. (apart from + and -) (Codabar) */
194#define IS_ARS_F 0x8000 /* ARSENIC uppercase subset (VIN) */
195
196#define IS_UPR_F (IS_UPO_F | IS_UHX_F | IS_UX__F) /* Uppercase letters */
197#define IS_LWR_F (IS_LWO_F | IS_LHX_F | IS_LX__F) /* Lowercase letters */
198
199/* The most commonly used set */
200#define NEON_F IS_NUM_F /* NEON "0123456789" */
201
202/* Whether a character `ch` matches `flag` */
203INTERNAL int z_is_chr(const unsigned int flag, const unsigned int ch);
204
205/* Verifies if a string only uses valid characters, returning 1-based position in `source` if not, 0 for success */
206INTERNAL int z_not_sane(const unsigned int flag, const unsigned char source[], const int length);
207
208/* Verifies if a string only uses valid characters as above, but also returns `test_string` position of each in
209 `posns` array */
210INTERNAL int z_not_sane_lookup(const char test_string[], const int test_length, const unsigned char source[],
211 const int length, int *posns);
212
213/* Returns the position of `data` in `set_string`, or -1 if not found */
214INTERNAL int z_posn(const char set_string[], const char data);
215
216
217/* Converts `arg` to a string representing its binary equivalent of length `length` and places in `binary` at
218 `bin_posn`. Returns `bin_posn` + `length` */
219INTERNAL int z_bin_append_posn(const int arg, const int length, char *binary, const int bin_posn);
220
221#define Z_COMMON_INLINE 1
222
223#ifdef Z_COMMON_INLINE
224
225# define z_module_is_set(s, y, x) (((s)->encoded_data[y][(x) >> 3] >> ((x) & 0x07)) & 1)
226# define z_set_module(s, y, x) do { (s)->encoded_data[y][(x) >> 3] |= 1 << ((x) & 0x07); } while (0)
227# define z_module_colour_is_set(s, y, x) ((s)->encoded_data[y][x])
228# define z_set_module_colour(s, y, x, c) do { (s)->encoded_data[y][x] = (c); } while (0)
229# define z_unset_module(s, y, x) do { (s)->encoded_data[y][(x) >> 3] &= ~(1 << ((x) & 0x07)); } while (0)
230
231#else /* Z_COMMON_INLINE */
232
233/* Returns true (1) if a module is dark/black, otherwise false (0) */
234INTERNAL int z_module_is_set(const struct zint_symbol *symbol, const int y_coord, const int x_coord);
235
236/* Sets a module to dark/black */
237INTERNAL void z_set_module(struct zint_symbol *symbol, const int y_coord, const int x_coord);
238
239/* Returns true (1-8) if a module is colour, otherwise false (0) */
240INTERNAL int z_module_colour_is_set(const struct zint_symbol *symbol, const int y_coord, const int x_coord);
241
242/* Sets a module to a colour */
243INTERNAL void z_set_module_colour(struct zint_symbol *symbol, const int y_coord, const int x_coord,
244 const int colour);
245
246/* Sets a dark/black module to white (i.e. unsets) */
247INTERNAL void z_unset_module(struct zint_symbol *symbol, const int y_coord, const int x_coord);
248
249#endif /* Z_COMMON_INLINE */
250
251
252/* Expands from a width pattern to a bit pattern */
253INTERNAL void z_expand(struct zint_symbol *symbol, const char data[], const int length);
254
255
256/* Set `symbol->errtxt` to "err_id: msg", returning `error_number`. If `err_id` is -1, the "err_id: " prefix is
257 omitted */
258INTERNAL int z_errtxt(const int error_number, struct zint_symbol *symbol, const int err_id, const char *msg);
259
260#if defined(__GNUC__) && !defined(__clang__)
261#define ZEXT __extension__ /* Suppress gcc pedantic warnings including when using format "%<n>$" with `z_errtxtf()` */
262#else
263#define ZEXT
264#endif
265
266/* Set `symbol->errtxt` to "err_id: msg" with restricted subset of `printf()` formatting, returning `error_number`.
267 If `err_id` is -1, the "err_id: " prefix is omitted. Only the following specifiers are supported: "c", "d", "f",
268 "g" and "s", with no modifiers apart from "<n>$" numbering for l10n ("<n>" 1-9), in which case all specifiers must
269 be numbered, "%s" with length precisions: "%.*s", "%<n+1>$.*<n>$s", "%.<p>s" and "%<n>$.<p>s", "%d" with
270 zero-padded minimum field lengths: "%0<m>d" or %<n>$0<m>d" ("<m>" 1-99), and "%f"/"%g" with single-digit precision:
271 "%.<m>f" or "%<n>$.<m>f" */
272INTERNAL int z_errtxtf(const int error_number, struct zint_symbol *symbol, const int err_id, const char *fmt, ...)
273 ZINT_FORMAT_PRINTF(4, 5);
274
275/* Helper to prepend/append to existing `symbol->errtxt` by calling `z_errtxtf(fmt)` with 2 arguments (copy of
276 `errtxt` & `msg`) if `msg` not NULL, or 1 argument (just copy of `errtxt`) if `msg` NULL, returning `error_number`
277*/
278INTERNAL int z_errtxt_adj(const int error_number, struct zint_symbol *symbol, const char *fmt, const char *msg);
279
280
281/* Whether `symbology` can have row binding */
282INTERNAL int z_is_bindable(const int symbology);
283
284/* Whether `symbology` is EAN */
285INTERNAL int z_is_ean(const int symbology);
286
287/* Whether `symbology` is EAN/UPC */
288INTERNAL int z_is_upcean(const int symbology);
289
290/* Whether `symbology` can have composite 2D component data */
291INTERNAL int z_is_composite(const int symbology);
292
293/* Whether `symbology` is a matrix design renderable as dots */
294INTERNAL int z_is_dotty(const int symbology);
295
296/* Whether `symbology` has a fixed aspect ratio (matrix design) */
297INTERNAL int z_is_fixed_ratio(const int symbology);
298
299
300/* Whether next two characters are digits */
301INTERNAL int z_is_twodigits(const unsigned char source[], const int length, const int position);
302
303/* Returns how many consecutive digits lie immediately ahead up to `max`, or all if `max` is -1 */
304INTERNAL int z_cnt_digits(const unsigned char source[], const int length, const int position, const int max);
305
306
307/* State machine to decode UTF-8 to Unicode codepoints (state 0 means done, state 12 means error) */
308INTERNAL unsigned int z_decode_utf8(unsigned int *state, unsigned int *codep, const unsigned char byte);
309
310/* Is string valid UTF-8? */
311INTERNAL int z_is_valid_utf8(const unsigned char source[], const int length);
312
313/* Converts UTF-8 to Unicode. If `disallow_4byte` unset, allows all values (UTF-32). If `disallow_4byte` set,
314 * only allows codepoints <= U+FFFF (ie four-byte sequences not allowed) (UTF-16, no surrogates) */
315INTERNAL int z_utf8_to_unicode(struct zint_symbol *symbol, const unsigned char source[], unsigned int vals[],
316 int *length, const int disallow_4byte);
317
318
319/* Check if `source` starts with manual FNC1 in 1st or 2nd position, returning length of extra escape sequence if so,
320 else 0 */
321INTERNAL int z_extra_escape_position_fnc1(const unsigned char source[], const int length);
322
323/* Process `source` for extra escape sequences, placing result in `dest`, updating `p_length`, and setting `fncs` with
324 any found FNC1s. Sets `p_have_extra_escapes` if any sequences found. `eci` is checked to be ASCII-compatible (UTF-8
325 & single-byte ECIs, excl. Binary 899). On error sets `errtxt` & returns error no. */
326INTERNAL int z_extra_escapes(struct zint_symbol *symbol, const unsigned char source[], int *p_length, const int eci,
327 unsigned char *dest, char *fncs, int *p_have_extra_escapes);
328
329
330/* Treats source as ISO/IEC 8859-1 and copies into `symbol->text`, converting to UTF-8. Control chars (incl. DEL) and
331 non-ISO/IEC 8859-1 (0x80-9F) are replaced with spaces. Returns warning if truncated, else 0 */
332INTERNAL int z_hrt_cpy_iso8859_1(struct zint_symbol *symbol, const unsigned char source[], const int length);
333
334/* No-check as-is copy of ASCII into `symbol->text`, assuming `length` fits */
335INTERNAL void z_hrt_cpy_nochk(struct zint_symbol *symbol, const unsigned char source[], const int length);
336
337/* No-check as-is copy of ASCII into `symbol->text`, appending `separator` (if ASCII - use `\xFF` for none) and then
338 `cat`, assuming total length fits */
339INTERNAL void z_hrt_cpy_cat_nochk(struct zint_symbol *symbol, const unsigned char source[], const int length,
340 const char separator, const unsigned char cat[], const int cat_length);
341
342/* Copy a single ASCII character `ch` into `symbol->text` (i.e. replaces content) */
343INTERNAL void z_hrt_cpy_chr(struct zint_symbol *symbol, const char ch);
344
345/* No-check as-is append of ASCII to `symbol->text`, assuming current `symbol->text_length` + `length` fits */
346INTERNAL void z_hrt_cat_nochk(struct zint_symbol *symbol, const unsigned char source[], const int length);
347
348/* No-check append of `ch` to `symbol->text`, assuming current `symbol->text_length` + 1 fits */
349INTERNAL void z_hrt_cat_chr_nochk(struct zint_symbol *symbol, const char ch);
350
351/* No-check `sprintf()` into `symbol->text`, assuming it fits */
352INTERNAL void z_hrt_printf_nochk(struct zint_symbol *symbol, const char *fmt, ...) ZINT_FORMAT_PRINTF(2, 3);
353
354/* No-check copy of `source` into `symbol->text`, converting GS1 square brackets into round ones. Assumes it fits */
355INTERNAL void z_hrt_conv_gs1_brackets_nochk(struct zint_symbol *symbol, const unsigned char source[],
356 const int length);
357
358
359/* Initialize `content_segs` for `seg_count` segments. On error sets `errtxt`, returning BARCODE_ERROR_MEMORY */
360INTERNAL int z_ct_init_segs(struct zint_symbol *symbol, const int seg_count);
361
362/* Free `content_segs` along with any `source` buffers */
363INTERNAL void z_ct_free_segs(struct zint_symbol *symbol);
364
365/* Copy `segs` to content segs. Seg source copied as-is. If seg length <= 0, content reg length set to `strlen()`.
366 If seg eci not set, content seg eci set to 3. On error sets `errxtxt`, returning BARCODE_ERROR_MEMORY */
367INTERNAL int z_ct_cpy_segs(struct zint_symbol *symbol, const struct zint_seg segs[], const int seg_count);
368
369/* Process content seg `seg_idx` buffer for manual FNC1 extra escape sequences (which must exist),
370 and update its ECI to `eci`, if set, to reflect (feedback) the actual ECI used */
371INTERNAL void z_ct_set_seg_extra_escapes_eci(struct zint_symbol *symbol, const int seg_idx, const int eci);
372
373/* Update the ECI of content seg `seg_idx` to `eci`, to reflect (feedback) the actual ECI used */
374INTERNAL void z_ct_set_seg_eci(struct zint_symbol *symbol, const int seg_idx, const int eci);
375
376/* Copy `source` to content seg 0 buffer, setting content seg ECI to 3. On error sets `errtxt`, returning
377 BARCODE_ERROR_MEMORY */
378INTERNAL int z_ct_cpy(struct zint_symbol *symbol, const unsigned char source[], const int length);
379
380/* Copy `source` to content seg 0 buffer, appending `separator` (if ASCII - use `\xFF` for none) and then `cat`, and
381 setting content seg ECI to 3. On error sets `errtxt`, returning BARCODE_ERROR_MEMORY */
382INTERNAL int z_ct_cpy_cat(struct zint_symbol *symbol, const unsigned char source[], const int length,
383 const char separator, const unsigned char cat[], const int cat_length);
384
385/* Convert ISO/IEC 8859-1 (binary) `source` to UTF-8, and copy to content seg 0 buffer, setting content seg ECI to 3.
386 On error sets `errtxt`, returning BARCODE_ERROR_MEMORY */
387INTERNAL int z_ct_cpy_iso8859_1(struct zint_symbol *symbol, const unsigned char source[], const int length);
388
389/* `sprintf()` into content seg 0 buffer, assuming formatted data less than 256 bytes. Sets content seg ECI to 3.
390 On error sets `errtxt`, returning BARCODE_ERROR_MEMORY */
391INTERNAL int z_ct_printf_256(struct zint_symbol *symbol, const char *fmt, ...) ZINT_FORMAT_PRINTF(2, 3);
392
393
394/* Sets symbol height, returning a warning if not within minimum and/or maximum if given.
395 `default_height` does not include height of fixed-height rows (i.e. separators/composite data) */
396INTERNAL int z_set_height(struct zint_symbol *symbol, const float min_row_height, const float default_height,
397 const float max_height, const int no_errtxt);
398
399
400/* Removes excess precision from floats - see https://stackoverflow.com/q/503436 */
401INTERNAL float z_stripf(const float arg);
402
403
404/* Returns total length of segments */
405INTERNAL int z_segs_length(const struct zint_seg segs[], const int seg_count);
406
407/* Shallow copies segments, adjusting default ECIs */
408INTERNAL void z_segs_cpy(const struct zint_symbol *symbol, const struct zint_seg segs[], const int seg_count,
409 struct zint_seg local_segs[]);
410
411
412/* Helper for ZINT_DEBUG_PRINT to put all but graphical ASCII in hex escapes. Output to `buf` if non-NULL, else
413 stdout */
414INTERNAL char *z_debug_print_escape(const unsigned char *source, const int first_len, char *buf);
415
416#ifdef ZINT_TEST
417/* Dumps hex-formatted codewords in symbol->errtxt (for use in testing) */
418INTERNAL void z_debug_test_codeword_dump(struct zint_symbol *symbol, const unsigned char *codewords,
419 const int length);
420/* Dumps decimal-formatted codewords in symbol->errtxt (for use in testing) */
421INTERNAL void z_debug_test_codeword_dump_short(struct zint_symbol *symbol, const short *codewords, const int length);
422/* Dumps decimal-formatted codewords in symbol->errtxt (for use in testing) */
423INTERNAL void z_debug_test_codeword_dump_int(struct zint_symbol *symbol, const int *codewords, const int length);
424#endif
425
426#ifdef __cplusplus
427}
428#endif /* __cplusplus */
429
430/* vim: set ts=4 sw=4 et : */
431#endif /* Z_COMMON_H */
Definition: zint.h:101
Definition: zint.h:108