Debian Codegraph @

tests/fuzzing/fuzz_translate.c

raw
1 //
2 // liblouis Braille Translation and Back-Translation Library
3 //
4 // Copyright (C) 2022 Anna Stan, Nicolas Morel, Kalilou Mamadou Dramé
5 //
6 // This file is part of liblouis.
7 //
8 // liblouis is free software: you can redistribute it and/or modify it
9 // under the terms of the GNU Lesser General Public License as published
10 // by the Free Software Foundation, either version 2.1 of the License, or
11 // (at your option) any later version.
12 //
13 // liblouis is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with liblouis. If not, see <http://www.gnu.org/licenses/>.
20 //
21
22 #include <config.h>
23
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <internal.h>
30 #include <liblouis.h>
31 #include <assert.h>
32
33 #define LANGUAGE "en"
34
35 static int initialized = 0;
36
37 #define BOLDRED(x) "\x1b[31m\x1b[1m" x "\x1b[0m"
38
39 static const char *table_default;
40
41 static void __attribute__((destructor))
42 free_ressources(void) {
43 lou_free();
44 }
45
46 void
47 avoid_log(logLevels level, const char *msg) {
48 (void) level;
49 (void) msg;
50 }
51
52 extern int LLVMFuzzerRunDriver(int *argc, char ***argv,
53 int (*UserCb)(const uint8_t *Data, size_t Size));
54
55
56 int
57 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
58 int inputLen = 0;
59 int outputLen = 0;
60 char *mutable_data = NULL;
61
62 if (!initialized)
63 {
64 lou_registerLogCallback(avoid_log);
65 table_default = getenv("FUZZ_TABLE");
66 initialized = 1;
67 }
68
69 mutable_data = strndup((char*)data, size);
70 if (!mutable_data)
71 {
72 perror("malloc");
73 exit(1);
74 }
75
76 widechar *inputText = malloc((size*16+1)*sizeof(widechar));
77 int len = (int)_lou_extParseChars(mutable_data, inputText);
78 free(mutable_data);
79 if (len <= 0) {
80 free(inputText);
81 return -1;
82 }
83
84 assert(len <= (size*16));
85 inputLen = len;
86 outputLen = len*16;
87 widechar *outputText = malloc((outputLen+1)*sizeof(widechar));
88 if (table_default == NULL)
89 {
90 fprintf(stderr, "\n" BOLDRED("[Please set up FUZZ_TABLE env var before starting fuzzer]")"\nThis environment variable is supposed to contain the table you want to test with lou_translateString()\n\n");
91 exit(0);
92 }
93 lou_translateString(table_default, inputText, &inputLen, outputText, &outputLen, NULL, NULL, ucBrl);
94 free(inputText);
95 free(outputText);
96
97 return 0;
98 }
99
100 int main(int argc, char *argv[]) {
101 LLVMFuzzerRunDriver(&argc, &argv, LLVMFuzzerTestOneInput);
102 }