/src/liblouis/tests/fuzzing/fuzz_backtranslate.c
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // liblouis Braille Translation and Back-Translation Library |
3 | | // |
4 | | // Copyright (C) 2023 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 <assert.h> |
23 | | #include <fcntl.h> |
24 | | #include <internal.h> |
25 | | #include <liblouis.h> |
26 | | #include <stdint.h> |
27 | | #include <stdio.h> |
28 | | #include <stdlib.h> |
29 | | #include <string.h> |
30 | | #include <unistd.h> |
31 | | |
32 | | #define LANGUAGE "en" |
33 | | |
34 | | static int initialized = 0; |
35 | | |
36 | | #define BOLDRED(x) "\x1b[31m\x1b[1m" x "\x1b[0m" |
37 | | |
38 | | static const char *table_default; |
39 | | |
40 | 0 | static void __attribute__((destructor)) free_ressources(void) { lou_free(); } |
41 | | |
42 | 303k | void avoid_log(logLevels level, const char *msg) { |
43 | 303k | (void)level; |
44 | 303k | (void)msg; |
45 | 303k | } |
46 | | |
47 | | extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
48 | | |
49 | 436 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
50 | 436 | int inputLen = 0; |
51 | 436 | int outputLen = 0; |
52 | 436 | static int counter = 0; |
53 | 436 | if (!initialized) { |
54 | 1 | lou_registerLogCallback(avoid_log); |
55 | 1 | initialized = 1; |
56 | 1 | } |
57 | | |
58 | 436 | if (size < 512) { |
59 | 0 | return 0; |
60 | 0 | } |
61 | | |
62 | | // Write first half of fuzz data to a table file. |
63 | 436 | char new_file[256]; |
64 | 436 | sprintf(new_file, "/tmp/libfuzzer-%d.ctb", counter); |
65 | 436 | counter++; |
66 | | |
67 | 436 | FILE *fp = fopen(new_file, "wb"); |
68 | 436 | if (!fp) { |
69 | 0 | return 0; |
70 | 0 | } |
71 | 436 | fwrite(data, 512, 1, fp); |
72 | 436 | fclose(fp); |
73 | | |
74 | | // Adjust data pointer to after the table file data. |
75 | 436 | data += 512; |
76 | 436 | size -= 512; |
77 | | |
78 | | /* check if this works, otherwise bail */ |
79 | 436 | if (lou_checkTable(new_file) == 0) { |
80 | 18 | lou_free(); |
81 | 18 | unlink(new_file); |
82 | 18 | return 0; |
83 | 18 | } |
84 | 418 | char *mutable_data = NULL; |
85 | 418 | table_default = new_file; |
86 | | |
87 | 418 | mutable_data = strndup((char *)data, size); |
88 | 418 | if (!mutable_data) { |
89 | 0 | perror("malloc"); |
90 | 0 | exit(1); |
91 | 0 | } |
92 | | |
93 | 418 | widechar *inputText = malloc((size * 16 + 1) * sizeof(widechar)); |
94 | 418 | int len = (int)_lou_extParseChars(mutable_data, inputText); |
95 | 418 | free(mutable_data); |
96 | 418 | if (len <= 0) { |
97 | 38 | free(inputText); |
98 | 38 | unlink(new_file); |
99 | 38 | lou_free(); |
100 | 38 | return -1; |
101 | 38 | } |
102 | | |
103 | 380 | assert(len <= (size * 16)); |
104 | 380 | inputLen = len; |
105 | 380 | outputLen = len * 16; |
106 | 380 | widechar *outputText = malloc((outputLen + 1) * sizeof(widechar)); |
107 | | |
108 | 380 | lou_backTranslateString(table_default, inputText, &inputLen, outputText, |
109 | 380 | &outputLen, NULL, NULL, ucBrl); |
110 | | |
111 | 380 | free(inputText); |
112 | 380 | free(outputText); |
113 | | |
114 | 380 | lou_free(); |
115 | 380 | unlink(new_file); |
116 | | |
117 | 380 | return 0; |
118 | 380 | } |