/src/tesseract/unittest/fuzzers/fuzzer-api.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include <allheaders.h> |
2 | | #include <tesseract/baseapi.h> |
3 | | |
4 | | #include <libgen.h> // for dirname |
5 | | #include <cstdio> // for printf |
6 | | #include <cstdlib> // for std::getenv, std::setenv |
7 | | #include <string> // for std::string |
8 | | |
9 | | #ifndef TESSERACT_FUZZER_WIDTH |
10 | | # define TESSERACT_FUZZER_WIDTH 100 |
11 | | #endif |
12 | | |
13 | | #ifndef TESSERACT_FUZZER_HEIGHT |
14 | | # define TESSERACT_FUZZER_HEIGHT 100 |
15 | | #endif |
16 | | |
17 | | class BitReader { |
18 | | private: |
19 | | uint8_t const *data; |
20 | | size_t size; |
21 | | size_t shift; |
22 | | |
23 | | public: |
24 | 7.72k | BitReader(const uint8_t *data, size_t size) : data(data), size(size), shift(0) {} |
25 | | |
26 | 1.01G | int Read(void) { |
27 | 1.01G | if (size == 0) { |
28 | 887M | return 0; |
29 | 887M | } |
30 | | |
31 | 125M | const int ret = ((*data) >> shift) & 1; |
32 | | |
33 | 125M | shift++; |
34 | 125M | if (shift >= 8) { |
35 | 15.7M | shift = 0; |
36 | 15.7M | data++; |
37 | 15.7M | size--; |
38 | 15.7M | } |
39 | | |
40 | 125M | return ret; |
41 | 1.01G | } |
42 | | }; |
43 | | |
44 | | static tesseract::TessBaseAPI *api = nullptr; |
45 | | |
46 | 2 | extern "C" int LLVMFuzzerInitialize(int * /*pArgc*/, char ***pArgv) { |
47 | 2 | if (std::getenv("TESSDATA_PREFIX") == nullptr) { |
48 | 1 | std::string binary_path = *pArgv[0]; |
49 | 1 | const std::string filepath = dirname(&binary_path[0]); |
50 | | |
51 | 1 | const std::string tessdata_path = filepath + "/" + "tessdata"; |
52 | 1 | if (setenv("TESSDATA_PREFIX", tessdata_path.c_str(), 1) != 0) { |
53 | 0 | printf("Setenv failed\n"); |
54 | 0 | std::abort(); |
55 | 0 | } |
56 | 1 | } |
57 | | |
58 | 2 | api = new tesseract::TessBaseAPI(); |
59 | 2 | if (api->Init(nullptr, "eng") != 0) { |
60 | 0 | printf("Cannot initialize API\n"); |
61 | 0 | abort(); |
62 | 0 | } |
63 | | |
64 | | /* Silence output */ |
65 | 2 | api->SetVariable("debug_file", "/dev/null"); |
66 | | |
67 | 2 | return 0; |
68 | 2 | } |
69 | | |
70 | 7.72k | static PIX *createPix(BitReader &BR, const size_t width, const size_t height) { |
71 | 7.72k | Pix *pix = pixCreate(width, height, 1); |
72 | | |
73 | 7.72k | if (pix == nullptr) { |
74 | 0 | printf("pix creation failed\n"); |
75 | 0 | abort(); |
76 | 0 | } |
77 | | |
78 | 3.96M | for (size_t i = 0; i < width; i++) { |
79 | 1.01G | for (size_t j = 0; j < height; j++) { |
80 | 1.01G | pixSetPixel(pix, i, j, BR.Read()); |
81 | 1.01G | } |
82 | 3.95M | } |
83 | | |
84 | 7.72k | return pix; |
85 | 7.72k | } |
86 | | |
87 | 7.72k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
88 | 7.72k | BitReader BR(data, size); |
89 | | |
90 | 7.72k | auto pix = createPix(BR, TESSERACT_FUZZER_WIDTH, TESSERACT_FUZZER_HEIGHT); |
91 | | |
92 | 7.72k | api->SetImage(pix); |
93 | | |
94 | 7.72k | char *outText = api->GetUTF8Text(); |
95 | | |
96 | 7.72k | pixDestroy(&pix); |
97 | 7.72k | delete[] outText; |
98 | | |
99 | 7.72k | return 0; |
100 | 7.72k | } |