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