Skip to content

Commit 98f8672

Browse files
committed
Add BARCODE_MEMORY_FILE to symbol->output_options to allow
outputting to in-memory buffer `symbol->memfile` instead of to file `symbol->outfile`, ticket #301 Add "README.clang-tidy" and ".clang-tidy" options file Suppress some warnings
1 parent 0701622 commit 98f8672

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2407
-1262
lines changed

.clang-tidy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
Checks: 'clang-diagnostic-*,clang-analyzer-*,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,-clang-analyzer-security.insecureAPI.strcpy'
3+
HeaderFilterRegex: '.*'

ChangeLog

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ Version 2.13.0.9 (dev) not released yet
33

44
**Incompatible changes**
55
------------------------
6-
None so far
6+
- New `memfile` & `memfile_size` fields in `symbol` for use with new output
7+
option `BARCODE_MEMORY_FILE`
78

89
Changes
910
-------
1011
- BMP: lessen heap memory usage by only `malloc()`ing a row
1112
- GIF: lessen heap memory usage by paging; use standard colour char map
13+
- Add `BARCODE_MEMORY_FILE` to `symbol->output_options` to allow outputting to
14+
in-memory buffer `symbol->memfile` instead of to file `symbol->outfile`,
15+
ticket #301
1216

1317
Bugs
1418
----

README.clang-tidy

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Current as of latest clang-tidy-18 from Ubuntu 22.04 apt package (2023-12-26)
2+
3+
Requires cmake in "build" sub-directory with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON (for "build/compile_commands.json")
4+
and -DCMAKE_BUILD_TYPE=Debug (so `assert()`s defined), and then make (for Qt generated includes).
5+
6+
In project root directory (warning, slow):
7+
8+
clang-tidy-18 backend/*.c frontend/*.c backend_qt/*.cpp frontend_qt/*.cpp -p build/compile_commands.json
9+
10+
For "backend_tcl", which has no "compile_commands.json", specify the tcl include directory, e.g.
11+
12+
clang-tidy-18 backend_tcl/*.c -- -I/usr/include/tcl8.6
13+
14+
Options are in ".clang-tidy" (in the project root directory). The excluded checks are
15+
`clang-analyzer-security.insecureAPI.strcpy` (for `strcpy()`, `strcat()` etc), and
16+
`clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling` (for `sprintf()`).
17+
18+
The test suite (cmake given -DZINT_TEST=ON) can also be analysed with an additional check disabled:
19+
20+
clang-tidy-18 backend/tests/*.c frontend/tests/*.c backend_qt/tests/*.cpp \
21+
-checks='-clang-analyzer-optin.performance.Padding' -p build/compile_commands.json

backend/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if(ZINT_USE_PNG)
88
find_package(PNG)
99
endif()
1010

11-
set(zint_COMMON_SRCS common.c library.c large.c reedsol.c gs1.c eci.c general_field.c)
11+
set(zint_COMMON_SRCS common.c library.c large.c reedsol.c gs1.c eci.c filemem.c general_field.c)
1212
set(zint_ONEDIM_SRCS bc412.c code.c code128.c 2of5.c upcean.c telepen.c medical.c plessey.c rss.c)
1313
set(zint_POSTAL_SRCS postal.c auspost.c imail.c mailmark.c)
1414
set(zint_TWODIM_SRCS code16k.c codablock.c dmatrix.c pdf417.c qr.c maxicode.c composite.c aztec.c code49.c code1.c gridmtx.c hanxin.c dotcode.c ultra.c)

backend/Makefile.mingw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ APP:=zint
2424
DLL:=$(APP).dll
2525
STATLIB:=lib$(APP).a
2626

27-
COMMON_OBJ:= common.o library.o large.o reedsol.o gs1.o eci.o general_field.o sjis.o gb2312.o gb18030.o
27+
COMMON_OBJ:= common.o library.o large.o reedsol.o gs1.o eci.o filemem.o general_field.o sjis.o gb2312.o gb18030.o
2828
ONEDIM_OBJ:= code.o code128.o 2of5.o upcean.o telepen.o medical.o plessey.o rss.o
2929
POSTAL_OBJ:= postal.o auspost.o imail.o mailmark.o
3030
TWODIM_OBJ:= code16k.o codablock.o dmatrix.o pdf417.o qr.o maxicode.o composite.o aztec.o code49.o code1.o gridmtx.o hanxin.o dotcode.o ultra.o

backend/bmp.c

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@
3333
#include <errno.h>
3434
#include <math.h>
3535
#include <stdio.h>
36-
#ifdef _MSC_VER
37-
#include <io.h>
38-
#include <fcntl.h>
39-
#endif
4036
#include "common.h"
37+
#include "filemem.h"
4138
#include "output.h"
4239
#include "bmp.h" /* Bitmap header structure */
4340

@@ -47,15 +44,15 @@ INTERNAL int bmp_pixel_plot(struct zint_symbol *symbol, const unsigned char *pix
4744
int colour_count;
4845
int resolution;
4946
size_t row_size, data_offset, file_size;
50-
FILE *bmp_file;
47+
struct filemem fm;
48+
struct filemem *const fmp = &fm;
5149
bitmap_file_header_t file_header;
5250
bitmap_info_header_t info_header;
5351
color_ref_t bg;
5452
color_ref_t fg;
5553
color_ref_t palette[8];
5654
int ultra_fg_index = 9;
5755
unsigned char map[128];
58-
const int output_to_stdout = symbol->output_options & BARCODE_STDOUT; /* Suppress gcc -fanalyzer warning */
5956
unsigned char *rowbuf;
6057

6158
(void) out_colour_get_rgb(symbol->fgcolour, &fg.red, &fg.green, &fg.blue, NULL /*alpha*/);
@@ -119,36 +116,25 @@ INTERNAL int bmp_pixel_plot(struct zint_symbol *symbol, const unsigned char *pix
119116
info_header.important_colours = colour_count;
120117

121118
/* Open output file in binary mode */
122-
if (output_to_stdout) {
123-
#ifdef _MSC_VER
124-
if (-1 == _setmode(_fileno(stdout), _O_BINARY)) {
125-
sprintf(symbol->errtxt, "600: Could not set stdout to binary (%d: %.30s)", errno, strerror(errno));
126-
free(rowbuf);
127-
return ZINT_ERROR_FILE_ACCESS;
128-
}
129-
#endif
130-
bmp_file = stdout;
131-
} else {
132-
if (!(bmp_file = out_fopen(symbol->outfile, "wb"))) {
133-
sprintf(symbol->errtxt, "601: Could not open output file (%d: %.30s)", errno, strerror(errno));
134-
free(rowbuf);
135-
return ZINT_ERROR_FILE_ACCESS;
136-
}
119+
if (!fm_open(fmp, symbol, "wb")) {
120+
sprintf(symbol->errtxt, "601: Could not open output file (%d: %.30s)", fmp->err, strerror(fmp->err));
121+
free(rowbuf);
122+
return ZINT_ERROR_FILE_ACCESS;
137123
}
138124

139-
fwrite(&file_header, sizeof(bitmap_file_header_t), 1, bmp_file);
140-
fwrite(&info_header, sizeof(bitmap_info_header_t), 1, bmp_file);
125+
fm_write(&file_header, sizeof(bitmap_file_header_t), 1, fmp);
126+
fm_write(&info_header, sizeof(bitmap_info_header_t), 1, fmp);
141127

142-
fwrite(&bg, sizeof(color_ref_t), 1, bmp_file);
128+
fm_write(&bg, sizeof(color_ref_t), 1, fmp);
143129
if (bits_per_pixel == 4) {
144130
for (i = 0; i < 8; i++) {
145-
fwrite(&palette[i], sizeof(color_ref_t), 1, bmp_file);
131+
fm_write(&palette[i], sizeof(color_ref_t), 1, fmp);
146132
}
147133
if (ultra_fg_index == 9) {
148-
fwrite(&fg, sizeof(color_ref_t), 1, bmp_file);
134+
fm_write(&fg, sizeof(color_ref_t), 1, fmp);
149135
}
150136
} else {
151-
fwrite(&fg, sizeof(color_ref_t), 1, bmp_file);
137+
fm_write(&fg, sizeof(color_ref_t), 1, fmp);
152138
}
153139

154140
/* Pixel Plotting */
@@ -159,7 +145,7 @@ INTERNAL int bmp_pixel_plot(struct zint_symbol *symbol, const unsigned char *pix
159145
for (column = 0; column < symbol->bitmap_width; column++) {
160146
rowbuf[column >> 1] |= map[pb[column]] << (!(column & 1) << 2);
161147
}
162-
fwrite(rowbuf, 1, row_size, bmp_file);
148+
fm_write(rowbuf, 1, row_size, fmp);
163149
}
164150
} else { /* bits_per_pixel == 1 */
165151
for (row = 0; row < symbol->bitmap_height; row++) {
@@ -168,29 +154,20 @@ INTERNAL int bmp_pixel_plot(struct zint_symbol *symbol, const unsigned char *pix
168154
for (column = 0; column < symbol->bitmap_width; column++) {
169155
rowbuf[column >> 3] |= map[pb[column]] >> (column & 7);
170156
}
171-
fwrite(rowbuf, 1, row_size, bmp_file);
157+
fm_write(rowbuf, 1, row_size, fmp);
172158
}
173159
}
174160
free(rowbuf);
175161

176-
if (ferror(bmp_file)) {
177-
sprintf(symbol->errtxt, "603: Incomplete write to output (%d: %.30s)", errno, strerror(errno));
178-
if (!output_to_stdout) {
179-
(void) fclose(bmp_file);
180-
}
162+
if (fm_error(fmp)) {
163+
sprintf(symbol->errtxt, "603: Incomplete write to output (%d: %.30s)", fmp->err, strerror(fmp->err));
164+
(void) fm_close(fmp, symbol);
181165
return ZINT_ERROR_FILE_WRITE;
182166
}
183167

184-
if (output_to_stdout) {
185-
if (fflush(bmp_file) != 0) {
186-
sprintf(symbol->errtxt, "604: Incomplete flush to output (%d: %.30s)", errno, strerror(errno));
187-
return ZINT_ERROR_FILE_WRITE;
188-
}
189-
} else {
190-
if (fclose(bmp_file) != 0) {
191-
sprintf(symbol->errtxt, "605: Failure on closing output file (%d: %.30s)", errno, strerror(errno));
192-
return ZINT_ERROR_FILE_WRITE;
193-
}
168+
if (!fm_close(fmp, symbol)) {
169+
sprintf(symbol->errtxt, "605: Failure on closing output file (%d: %.30s)", fmp->err, strerror(fmp->err));
170+
return ZINT_ERROR_FILE_WRITE;
194171
}
195172

196173
return 0;

backend/common.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,11 @@ INTERNAL char *debug_print_escape(const unsigned char *source, const int first_l
606606
}
607607

608608
#ifdef ZINT_TEST
609+
/* Suppress gcc warning null destination pointer [-Wformat-overflow=] false-positive */
610+
#if defined(__GNUC__) && !defined(__clang__)
611+
#pragma GCC diagnostic push
612+
#pragma GCC diagnostic ignored "-Wformat-overflow="
613+
#endif
609614
/* Dumps hex-formatted codewords in symbol->errtxt (for use in testing) */
610615
INTERNAL void debug_test_codeword_dump(struct zint_symbol *symbol, const unsigned char *codewords, const int length) {
611616
int i, max = length, cnt_len = 0;
@@ -655,6 +660,9 @@ INTERNAL void debug_test_codeword_dump_int(struct zint_symbol *symbol, const int
655660
}
656661
symbol->errtxt[strlen(symbol->errtxt) - 1] = '\0'; /* Zap last space */
657662
}
663+
#if defined(__GNUC__) && !defined(__clang__)
664+
#pragma GCC diagnostic pop
658665
#endif
666+
#endif /*ZINT_TEST*/
659667

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

backend/common.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,37 @@ extern "C" {
4141
#define ARRAY_SIZE(x) ((int) (sizeof(x) / sizeof((x)[0])))
4242
#endif
4343

44-
/* Determine if C89 (excluding MSVC, which doesn't define __STDC_VERSION__) */
45-
#if !defined(_MSC_VER) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199000L)
46-
#define ZINT_IS_C89
44+
/* Determine if C89 or C99 (excluding MSVC, which doesn't define __STDC_VERSION__) */
45+
#ifndef _MSC_VER
46+
# if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199000L
47+
# define ZINT_IS_C89
48+
# elif __STDC_VERSION__ <= 199901L /* Actually includes pseudo-standards "C94/C95" as well */
49+
# define ZINT_IS_C99
50+
# endif
4751
#endif
4852

4953
#ifdef _MSC_VER
5054
# include <malloc.h>
5155
# define z_alloca(nmemb) _alloca(nmemb)
5256
#else
53-
# if defined(ZINT_IS_C89) || defined(__NuttX__) /* C89 or NuttX RTOS */
57+
# if defined(ZINT_IS_C89) || defined(ZINT_IS_C99) || defined(__NuttX__) /* C89 or C99 or NuttX RTOS */
5458
# include <alloca.h>
5559
# endif
5660
# define z_alloca(nmemb) alloca(nmemb)
5761
#endif
5862

63+
#ifdef _MSC_VER
64+
# if _MSC_VER >= 1400 /* MSVC 2005 (C++ 8.0) */
65+
# define restrict __restrict
66+
# else
67+
# define restrict
68+
# endif
69+
#else
70+
# ifdef ZINT_IS_C89
71+
# define restrict
72+
# endif
73+
#endif
74+
5975
#ifdef _MSC_VER
6076
typedef unsigned __int8 uint8_t;
6177
typedef unsigned __int16 uint16_t;

0 commit comments

Comments
 (0)