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