tesseract 5.2.0
Loading...
Searching...
No Matches
fuzzer-api.cpp
Go to the documentation of this file.
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
17class BitReader {
18private:
19 uint8_t const *data;
20 size_t size;
21 size_t shift;
22
23public:
24 BitReader(const uint8_t *data, size_t size) : data(data), size(size), shift(0) {}
25
26 int Read(void) {
27 if (size == 0) {
28 return 0;
29 }
30
31 const int ret = ((*data) >> shift) & 1;
32
33 shift++;
34 if (shift >= 8) {
35 shift = 0;
36 data++;
37 size--;
38 }
39
40 return ret;
41 }
42};
43
44static tesseract::TessBaseAPI *api = nullptr;
45
46extern "C" int LLVMFuzzerInitialize(int * /*pArgc*/, char ***pArgv) {
47 if (std::getenv("TESSDATA_PREFIX") == nullptr) {
48 std::string binary_path = *pArgv[0];
49 const std::string filepath = dirname(&binary_path[0]);
50
51 const std::string tessdata_path = filepath + "/" + "tessdata";
52 if (setenv("TESSDATA_PREFIX", tessdata_path.c_str(), 1) != 0) {
53 printf("Setenv failed\n");
54 std::abort();
55 }
56 }
57
58 api = new tesseract::TessBaseAPI();
59 if (api->Init(nullptr, "eng") != 0) {
60 printf("Cannot initialize API\n");
61 abort();
62 }
63
64 /* Silence output */
65 api->SetVariable("debug_file", "/dev/null");
66
67 return 0;
68}
69
70static PIX *createPix(BitReader &BR, const size_t width, const size_t height) {
71 Pix *pix = pixCreate(width, height, 1);
72
73 if (pix == nullptr) {
74 printf("pix creation failed\n");
75 abort();
76 }
77
78 for (size_t i = 0; i < width; i++) {
79 for (size_t j = 0; j < height; j++) {
80 pixSetPixel(pix, i, j, BR.Read());
81 }
82 }
83
84 return pix;
85}
86
87extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
88 BitReader BR(data, size);
89
90 auto pix = createPix(BR, TESSERACT_FUZZER_WIDTH, TESSERACT_FUZZER_HEIGHT);
91
92 api->SetImage(pix);
93
94 char *outText = api->GetUTF8Text();
95
96 pixDestroy(&pix);
97 delete[] outText;
98
99 return 0;
100}
#define TESSERACT_FUZZER_WIDTH
Definition: fuzzer-api.cpp:10
int LLVMFuzzerInitialize(int *, char ***pArgv)
Definition: fuzzer-api.cpp:46
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: fuzzer-api.cpp:87
#define TESSERACT_FUZZER_HEIGHT
Definition: fuzzer-api.cpp:14
bool SetVariable(const char *name, const char *value)
Definition: baseapi.cpp:279
int Init(const char *datapath, const char *language, OcrEngineMode mode, char **configs, int configs_size, const std::vector< std::string > *vars_vec, const std::vector< std::string > *vars_values, bool set_only_non_debug_params)
Definition: baseapi.cpp:368
void SetImage(const unsigned char *imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line)
Definition: baseapi.cpp:576
int Read(void)
Definition: fuzzer-api.cpp:26
BitReader(const uint8_t *data, size_t size)
Definition: fuzzer-api.cpp:24