fuzz_data.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* fuzz_data.c - fuzzer for libzint (DATA_MODE, all symbologies except GS1_128, DBAR_EXP/STK & composites) */
  2. /*
  3. libzint - the open source barcode library
  4. Copyright (C) 2024 Robin Stuart <rstuart114@gmail.com>
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. Neither the name of the project nor the names of its contributors
  14. may be used to endorse or promote products derived from this software
  15. without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  20. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. SUCH DAMAGE.
  27. */
  28. /* SPDX-License-Identifier: BSD-3-Clause */
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif /* __cplusplus */
  32. #if 0
  33. #define Z_FUZZ_DEBUG /* Set `symbol->debug` flag */
  34. #define Z_FUZZ_SET_OUTPUT_OPTIONS /* Set `symbol->output_options` */
  35. #endif
  36. #include "fuzz.h"
  37. int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) {
  38. struct zint_symbol *symbol;
  39. int idx;
  40. /* Ignore empty or very large input */
  41. if (size < 1 || size > 10000) {
  42. return 0;
  43. }
  44. symbol = ZBarcode_Create();
  45. assert(symbol);
  46. for (idx = 0; idx < ZARRAY_SIZE(settings); idx++) {
  47. const unsigned char *input;
  48. int length;
  49. int ret;
  50. if (!ZBarcode_ValidID(idx)) {
  51. continue;
  52. }
  53. if (idx == BARCODE_GS1_128 || idx == BARCODE_DBAR_EXP || idx == BARCODE_DBAR_EXPSTK
  54. || (ZBarcode_Cap(idx, ZINT_CAP_COMPOSITE) & ZINT_CAP_COMPOSITE)) {
  55. continue;
  56. }
  57. input = data;
  58. length = set_symbol(symbol, idx, 1 /*chk_sane*/, 0 /*no_eci*/, &input, size);
  59. if (!length) {
  60. continue;
  61. }
  62. ret = ZBarcode_Encode(symbol, input, length);
  63. assert(ret != ZINT_ERROR_ENCODING_PROBLEM);
  64. }
  65. ZBarcode_Delete(symbol);
  66. return 0;
  67. }
  68. #ifdef __cplusplus
  69. }
  70. #endif /* __cplusplus */
  71. /* vim: set ts=4 sw=4 et : */