Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/zint/backend/tests/fuzz/fuzz_gs1.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 /* fuzz_gs1.c - fuzzer for libzint (GS1-enabled symbologies, GS1_MODE) */ | |
| 2 /* | |
| 3 libzint - the open source barcode library | |
| 4 Copyright (C) 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 #ifdef __cplusplus | |
| 34 extern "C" { | |
| 35 #endif /* __cplusplus */ | |
| 36 | |
| 37 #if 0 | |
| 38 #define Z_FUZZ_DEBUG /* Set `symbol->debug` flag */ | |
| 39 #define Z_FUZZ_SET_OUTPUT_OPTIONS /* Set `symbol->output_options` */ | |
| 40 #endif | |
| 41 #include "fuzz.h" | |
| 42 | |
| 43 int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) { | |
| 44 static const int symbologies[] = { | |
| 45 BARCODE_AZTEC, BARCODE_CODE16K, BARCODE_CODE49, BARCODE_CODEONE, BARCODE_DATAMATRIX, BARCODE_DBAR_EXP, | |
| 46 BARCODE_DBAR_EXPSTK, BARCODE_DOTCODE, BARCODE_GS1_128, BARCODE_QRCODE, BARCODE_RMQR, BARCODE_ULTRA, | |
| 47 BARCODE_EANX_CC, BARCODE_GS1_128_CC, BARCODE_DBAR_OMN_CC, BARCODE_DBAR_LTD_CC, BARCODE_DBAR_EXP_CC, | |
| 48 BARCODE_UPCA_CC, BARCODE_UPCE_CC, BARCODE_DBAR_STK_CC, BARCODE_DBAR_OMNSTK_CC, BARCODE_DBAR_EXPSTK_CC, | |
| 49 }; | |
| 50 struct zint_symbol *symbol; | |
| 51 unsigned char *gs1_buf; | |
| 52 int i; | |
| 53 | |
| 54 /* Ignore empty or very large input */ | |
| 55 if (size < 1 || size > 10000) { | |
| 56 return 0; | |
| 57 } | |
| 58 | |
| 59 symbol = ZBarcode_Create(); | |
| 60 assert(symbol); | |
| 61 | |
| 62 gs1_buf = (unsigned char *) malloc(size + 2); | |
| 63 assert(gs1_buf); | |
| 64 gs1_buf[0] = '['; /* Add dummy AI - along with GS1NOCHECK_MODE disables GS1 verification */ | |
| 65 gs1_buf[1] = ']'; | |
| 66 | |
| 67 for (i = 0; i < ZARRAY_SIZE(symbologies); i++) { | |
| 68 static const char primary_ai[] = "[01]12345678901231"; | |
| 69 static const char primary_upce[] = "12345670"; | |
| 70 static const char primary[] = "123456789012"; | |
| 71 const int idx = symbologies[i]; | |
| 72 const int is_composite = ZBarcode_Cap(idx, ZINT_CAP_COMPOSITE) & ZINT_CAP_COMPOSITE; | |
| 73 const unsigned char *input; | |
| 74 int length; | |
| 75 int ret; | |
| 76 | |
| 77 assert(ZBarcode_ValidID(idx)); | |
| 78 | |
| 79 input = data; | |
| 80 length = set_symbol(symbol, idx, !is_composite /*chk_sane*/, 1 /*no_eci*/, &input, size); | |
| 81 if (!length) { | |
| 82 continue; | |
| 83 } | |
| 84 | |
| 85 if (is_composite) { | |
| 86 if (idx == BARCODE_GS1_128_CC || idx == BARCODE_DBAR_EXP_CC || idx == BARCODE_DBAR_EXPSTK_CC) { | |
| 87 strcpy(symbol->primary, primary_ai); | |
| 88 } else if (idx == BARCODE_UPCE_CC) { | |
| 89 strcpy(symbol->primary, primary_upce); | |
| 90 } else { | |
| 91 strcpy(symbol->primary, primary); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 /* Try it first without GS1NOCHECK_MODE */ | |
| 96 symbol->input_mode = (symbol->input_mode & ~0x07) | GS1_MODE; | |
| 97 | |
| 98 ret = ZBarcode_Encode(symbol, input, length); | |
| 99 assert(ret != ZINT_ERROR_ENCODING_PROBLEM); | |
| 100 | |
| 101 ZBarcode_Clear(symbol); | |
| 102 | |
| 103 /* Now with GS1NOCHECK_MODE */ | |
| 104 symbol->input_mode = (symbol->input_mode & ~0x07) | GS1_MODE | GS1NOCHECK_MODE; | |
| 105 symbol->input_mode &= ~GS1PARENS_MODE; | |
| 106 | |
| 107 memcpy(gs1_buf + 2, input, length); | |
| 108 | |
| 109 ret = ZBarcode_Encode(symbol, gs1_buf, length + 2); | |
| 110 assert(ret != ZINT_ERROR_ENCODING_PROBLEM); | |
| 111 } | |
| 112 | |
| 113 (void) free(gs1_buf); | |
| 114 ZBarcode_Delete(symbol); | |
| 115 | |
| 116 return 0; | |
| 117 } | |
| 118 | |
| 119 #ifdef __cplusplus | |
| 120 } | |
| 121 #endif /* __cplusplus */ | |
| 122 | |
| 123 /* vim: set ts=4 sw=4 et : */ |
