|
| 1 | +/** |
| 2 | + * GS1 Syntax Engine |
| 3 | + * |
| 4 | + * @author Copyright (c) 2021-2024 GS1 AISBL. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include <stdbool.h> |
| 22 | +#include <stdint.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <stdio.h> |
| 25 | +#include <string.h> |
| 26 | + |
| 27 | +#include "gs1encoders.h" |
| 28 | +#include "enc-private.h" |
| 29 | + |
| 30 | +static gs1_encoder *ctx = NULL; |
| 31 | + |
| 32 | + |
| 33 | +int LLVMFuzzerInitialize(int *argc, char ***argv) { |
| 34 | + |
| 35 | + (void)argc; |
| 36 | + (void)argv; |
| 37 | + |
| 38 | + ctx = gs1_encoder_init(NULL); |
| 39 | + gs1_encoder_setPermitUnknownAIs(ctx, true); |
| 40 | + |
| 41 | + return 0; |
| 42 | + |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +int LLVMFuzzerTestOneInput(const uint8_t* const buf, size_t len) { |
| 47 | + |
| 48 | + char in[MAX_DATA+1]; |
| 49 | + char pristine[MAX_DATA+1]; |
| 50 | + const char *out; |
| 51 | + char **hri; |
| 52 | + |
| 53 | + if (len > MAX_DATA) |
| 54 | + return 0; |
| 55 | + |
| 56 | + memcpy(in, buf, len); |
| 57 | + in[len] = '\0'; |
| 58 | + |
| 59 | + memcpy(pristine, in, len); |
| 60 | + |
| 61 | + if (!gs1_encoder_setDataStr(ctx, in)) |
| 62 | + return 0; |
| 63 | + |
| 64 | + // Test that the input hasn't been corrupted |
| 65 | + if (memcmp(in, pristine, len) != 0) { |
| 66 | + printf("\n:IN %s\nPRISTINE: %s\n", in, pristine); |
| 67 | + abort(); |
| 68 | + }; |
| 69 | + |
| 70 | + // Validate the round trip |
| 71 | + out = gs1_encoder_getDataStr(ctx); |
| 72 | + if (strcmp(in, out) != 0) { |
| 73 | + printf("\nIN: %s\nOUT: %s\n", in, out); |
| 74 | + abort(); |
| 75 | + } |
| 76 | + |
| 77 | + gs1_encoder_getHRI(ctx, &hri); |
| 78 | + |
| 79 | + return 0; |
| 80 | + |
| 81 | +} |
0 commit comments