#ifndef Z_COMMON_H
#define Z_COMMON_H
#ifdef __cplusplus
extern "C" {
#endif
#include "zint.h"
#include "zintconfig.h"
#include <stdlib.h>
#include <string.h>
#ifndef _MSC_VER
# if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199000L
# define ZINT_IS_C89
# endif
#endif
#ifdef _MSC_VER
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif
#define ARRAY_SIZE(x) ((int) (sizeof(x) / sizeof((x)[0])))
#ifdef _MSC_VER
# include <malloc.h>
# define z_alloca(nmemb) _alloca(nmemb)
#elif defined(__COMPCERT__)
# define z_alloca(nmemb) malloc(nmemb)
#else
# if (defined(__GNUC__) && !defined(alloca) && !defined(__NetBSD__)) || defined(__NuttX__) || defined(_AIX) \
|| (defined(__sun) && defined(__SVR4) )
# include <alloca.h>
# endif
# define z_alloca(nmemb) alloca(nmemb)
#endif
#ifdef _MSC_VER
# pragma warning(disable: 4125)
# pragma warning(disable: 4244)
# if _MSC_VER > 1200
# pragma warning(disable: 4996)
# endif
#elif defined(__MINGW64__) && defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 3
# pragma GCC diagnostic ignored "-Wlong-long"
#endif
#if defined(__GNUC__) && __GNUC__ >= 3
# define ZINT_FORMAT_PRINTF(fmt_idx, first_idx) __attribute__((__format__(printf, fmt_idx, first_idx)))
#else
# define ZINT_FORMAT_PRINTF(fmt_idx, first_idx)
#endif
#if defined(__GNUC__) && __GNUC__ >= 4 && !defined(ZINT_TEST) && !defined(__MINGW32__)
# define INTERNAL __attribute__((__visibility__("hidden")))
#elif defined(ZINT_TEST)
# define INTERNAL ZINT_EXTERN
#else
# define INTERNAL
#endif
#if defined(__GNUC__) && __GNUC__ >= 4 && !defined(__MINGW32__)
# define INTERNAL_DATA_EXTERN __attribute__((__visibility__("hidden"))) extern
# define INTERNAL_DATA __attribute__((__visibility__("hidden")))
#else
# define INTERNAL_DATA_EXTERN extern
# define INTERNAL_DATA
#endif
#ifdef _MSC_VER
# if _MSC_VER >= 1400
# define restrict __restrict
# else
# define restrict
# endif
#else
# ifdef ZINT_IS_C89
# define restrict
# endif
#endif
#if (defined(_MSC_VER) && _MSC_VER <= 1200) || defined(ZINT_IS_C89)
# define ceilf (float) ceil
# define floorf (float) floor
# define fmodf (float) fmod
#endif
#if (defined(_MSC_VER) && _MSC_VER < 1800) || defined(ZINT_IS_C89)
# define round(arg) floor((arg) + 0.5)
# define roundf(arg) floorf((arg) + 0.5f)
#endif
#define z_isfintf(arg) (fmodf(arg, 1.0f) == 0.0f)
#define z_isdigit(c) ((c) <= '9' && (c) >= '0')
#define z_isupper(c) ((c) >= 'A' && (c) <= 'Z')
#define z_islower(c) ((c) >= 'a' && (c) <= 'z')
#define z_isascii(c) (!((c) & ~0x7F))
#define z_iscntrl(c) (z_isascii(c) && ((c) < 32 || (c) == 127))
#define ZUCP(p) ((unsigned char *) (p))
#define ZCUCP(p) ((const unsigned char *) (p))
#define ZCCP(p) ((const char *) (p))
#define z_ustrlen(p) strlen(ZCCP(p))
#define z_itoc(i) ((i) + '0')
#define z_xtoc(i) ((i) < 10 ? z_itoc(i) : ((i) - 10) + 'A')
INTERNAL int z_ctoi(const char source);
INTERNAL int z_to_int(const unsigned char source[], const int length);
INTERNAL void z_to_upper(unsigned char source[], const int length);
INTERNAL int z_chr_cnt(const unsigned char source[], const int length, const unsigned char c);
#define IS_SPC_F 0x0001
#define IS_HSH_F 0x0002
#define IS_AST_F 0x0004
#define IS_PLS_F 0x0008
#define IS_MNS_F 0x0010
#define IS_NUM_F 0x0020
#define IS_UPO_F 0x0040
#define IS_UHX_F 0x0080
#define IS_UX__F 0x0100
#define IS_LWO_F 0x0200
#define IS_LHX_F 0x0400
#define IS_LX__F 0x0800
#define IS_C82_F 0x1000
#define IS_SIL_F 0x2000
#define IS_CLI_F 0x4000
#define IS_ARS_F 0x8000
#define IS_UPR_F (IS_UPO_F | IS_UHX_F | IS_UX__F)
#define IS_LWR_F (IS_LWO_F | IS_LHX_F | IS_LX__F)
#define NEON_F IS_NUM_F
INTERNAL int z_is_chr(const unsigned int flg, const unsigned int c);
INTERNAL int z_not_sane(const unsigned int flg, const unsigned char source[], const int length);
INTERNAL int z_not_sane_lookup(const char test_string[], const int test_length, const unsigned char source[],
const int length, int *posns);
INTERNAL int z_posn(const char set_string[], const char data);
INTERNAL int z_bin_append_posn(const int arg, const int length, char *binary, const int bin_posn);
#define Z_COMMON_INLINE 1
#ifdef Z_COMMON_INLINE
# define z_module_is_set(s, y, x) (((s)->encoded_data[y][(x) >> 3] >> ((x) & 0x07)) & 1)
# define z_set_module(s, y, x) do { (s)->encoded_data[y][(x) >> 3] |= 1 << ((x) & 0x07); } while (0)
# define z_module_colour_is_set(s, y, x) ((s)->encoded_data[y][x])
# define z_set_module_colour(s, y, x, c) do { (s)->encoded_data[y][x] = (c); } while (0)
# define z_unset_module(s, y, x) do { (s)->encoded_data[y][(x) >> 3] &= ~(1 << ((x) & 0x07)); } while (0)
#else
INTERNAL int z_module_is_set(const struct zint_symbol *symbol, const int y_coord, const int x_coord);
INTERNAL void z_set_module(struct zint_symbol *symbol, const int y_coord, const int x_coord);
INTERNAL int z_module_colour_is_set(const struct zint_symbol *symbol, const int y_coord, const int x_coord);
INTERNAL void z_set_module_colour(struct zint_symbol *symbol, const int y_coord, const int x_coord,
const int colour);
INTERNAL void z_unset_module(struct zint_symbol *symbol, const int y_coord, const int x_coord);
#endif
INTERNAL void z_expand(struct zint_symbol *symbol, const char data[], const int length);
INTERNAL int z_errtxt(const int error_number, struct zint_symbol *symbol, const int err_id, const char *msg);
#if defined(__GNUC__) && !defined(__clang__)
#define ZEXT __extension__
#else
#define ZEXT
#endif
INTERNAL int z_errtxtf(const int error_number, struct zint_symbol *symbol, const int err_id, const char *fmt, ...)
ZINT_FORMAT_PRINTF(4, 5);
INTERNAL int z_errtxt_adj(const int error_number, struct zint_symbol *symbol, const char *fmt, const char *msg);
INTERNAL int z_is_bindable(const int symbology);
INTERNAL int z_is_ean(const int symbology);
INTERNAL int z_is_upcean(const int symbology);
INTERNAL int z_is_composite(const int symbology);
INTERNAL int z_is_dotty(const int symbology);
INTERNAL int z_is_fixed_ratio(const int symbology);
INTERNAL int z_is_twodigits(const unsigned char source[], const int length, const int position);
INTERNAL int z_cnt_digits(const unsigned char source[], const int length, const int position, const int max);
INTERNAL unsigned int z_decode_utf8(unsigned int *state, unsigned int *codep, const unsigned char byte);
INTERNAL int z_is_valid_utf8(const unsigned char source[], const int length);
INTERNAL int z_utf8_to_unicode(struct zint_symbol *symbol, const unsigned char source[], unsigned int vals[],
int *length, const int disallow_4byte);
INTERNAL int z_hrt_cpy_iso8859_1(struct zint_symbol *symbol, const unsigned char source[], const int length);
INTERNAL void z_hrt_cpy_nochk(struct zint_symbol *symbol, const unsigned char source[], const int length);
INTERNAL void z_hrt_cpy_cat_nochk(struct zint_symbol *symbol, const unsigned char source[], const int length,
const char separator, const unsigned char cat[], const int cat_length);
INTERNAL void z_hrt_cpy_chr(struct zint_symbol *symbol, const char ch);
INTERNAL void z_hrt_cat_nochk(struct zint_symbol *symbol, const unsigned char source[], const int length);
INTERNAL void z_hrt_cat_chr_nochk(struct zint_symbol *symbol, const char ch);
INTERNAL void z_hrt_printf_nochk(struct zint_symbol *symbol, const char *fmt, ...) ZINT_FORMAT_PRINTF(2, 3);
INTERNAL void z_hrt_conv_gs1_brackets_nochk(struct zint_symbol *symbol, const unsigned char source[],
const int length);
INTERNAL int z_ct_init_segs(struct zint_symbol *symbol, const int seg_count);
INTERNAL void z_ct_free_segs(struct zint_symbol *symbol);
INTERNAL int z_ct_cpy_segs(struct zint_symbol *symbol, const struct zint_seg segs[], const int seg_count);
INTERNAL void z_ct_set_seg_eci(struct zint_symbol *symbol, const int seg_idx, const int eci);
INTERNAL int z_ct_cpy(struct zint_symbol *symbol, const unsigned char source[], const int length);
INTERNAL int z_ct_cpy_cat(struct zint_symbol *symbol, const unsigned char source[], const int length,
const char separator, const unsigned char cat[], const int cat_length);
INTERNAL int z_ct_cpy_iso8859_1(struct zint_symbol *symbol, const unsigned char source[], const int length);
INTERNAL int z_ct_printf_256(struct zint_symbol *symbol, const char *fmt, ...) ZINT_FORMAT_PRINTF(2, 3);
INTERNAL int z_set_height(struct zint_symbol *symbol, const float min_row_height, const float default_height,
const float max_height, const int no_errtxt);
INTERNAL float z_stripf(const float arg);
INTERNAL int z_segs_length(const struct zint_seg segs[], const int seg_count);
INTERNAL void z_segs_cpy(const struct zint_symbol *symbol, const struct zint_seg segs[], const int seg_count,
struct zint_seg local_segs[]);
INTERNAL char *z_debug_print_escape(const unsigned char *source, const int first_len, char *buf);
#ifdef ZINT_TEST
INTERNAL void z_debug_test_codeword_dump(struct zint_symbol *symbol, const unsigned char *codewords,
const int length);
INTERNAL void z_debug_test_codeword_dump_short(struct zint_symbol *symbol, const short *codewords, const int length);
INTERNAL void z_debug_test_codeword_dump_int(struct zint_symbol *symbol, const int *codewords, const int length);
#endif
#ifdef __cplusplus
}
#endif
#endif