tesseract/unittest/fuzzers/fuzzer-api.cpp

100 lines
1.8 KiB
C++
Raw Normal View History

2019-03-05 23:59:24 +08:00
#include "baseapi.h"
#include "leptonica/allheaders.h"
#include <libgen.h>
2019-03-05 23:59:24 +08:00
#include <stddef.h>
#include <stdint.h>
2019-03-05 23:59:24 +08:00
#include <stdlib.h>
#include <string.h>
#include <string>
2019-03-05 23:59:24 +08:00
class BitReader {
private:
uint8_t const* data;
size_t size;
size_t shift;
public:
BitReader(const uint8_t* data, size_t size)
: data(data), size(size), shift(0) {}
int Read(void) {
if (size == 0) {
return 0;
}
2019-03-05 23:59:24 +08:00
const int ret = ((*data) >> shift) & 1;
2019-03-05 23:59:24 +08:00
shift++;
if (shift >= 8) {
shift = 0;
data++;
size--;
}
return ret;
}
};
2019-03-05 23:59:24 +08:00
tesseract::TessBaseAPI* api = nullptr;
2019-03-05 23:59:24 +08:00
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
(void)argc;
(void)argv;
{
char* binary_path = strdup(*argv[0]);
const std::string filepath = dirname(binary_path);
free(binary_path);
2019-03-05 23:59:24 +08:00
const std::string tessdata_path = filepath + "/" + "tessdata";
if (setenv("TESSDATA_PREFIX", tessdata_path.c_str(), 1) != 0) {
printf("Setenv failed\n");
abort();
2019-03-05 23:59:24 +08:00
}
}
2019-03-05 23:59:24 +08:00
api = new tesseract::TessBaseAPI();
if (api->Init(nullptr, "eng") != 0) {
printf("Cannot initialize API\n");
abort();
}
2019-03-05 23:59:24 +08:00
/* Silence output */
api->SetVariable("debug_file", "/dev/null");
2019-03-05 23:59:24 +08:00
return 0;
}
2019-03-05 23:59:24 +08:00
static PIX* createPix(BitReader& BR, const size_t width, const size_t height) {
Pix* pix = pixCreate(width, height, 1);
2019-03-05 23:59:24 +08:00
if (pix == nullptr) {
printf("pix creation failed\n");
abort();
}
2019-03-05 23:59:24 +08:00
for (size_t i = 0; i < width; i++) {
for (size_t j = 0; j < height; j++) {
pixSetPixel(pix, i, j, BR.Read());
2019-03-05 23:59:24 +08:00
}
}
2019-03-05 23:59:24 +08:00
return pix;
2019-03-05 23:59:24 +08:00
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
BitReader BR(data, size);
2019-03-05 23:59:24 +08:00
auto pix = createPix(BR, 100, 100);
2019-03-05 23:59:24 +08:00
api->SetImage(pix);
2019-03-05 23:59:24 +08:00
char* outText = api->GetUTF8Text();
2019-03-05 23:59:24 +08:00
pixDestroy(&pix);
delete[] outText;
2019-03-05 23:59:24 +08:00
return 0;
2019-03-05 23:59:24 +08:00
}