fuzzer-api.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <allheaders.h>
  2. #include <tesseract/baseapi.h>
  3. #include <libgen.h> // for dirname
  4. #include <cstdio> // for printf
  5. #include <cstdlib> // for std::getenv, std::setenv
  6. #include <string> // for std::string
  7. #ifndef TESSERACT_FUZZER_WIDTH
  8. # define TESSERACT_FUZZER_WIDTH 100
  9. #endif
  10. #ifndef TESSERACT_FUZZER_HEIGHT
  11. # define TESSERACT_FUZZER_HEIGHT 100
  12. #endif
  13. class BitReader {
  14. private:
  15. uint8_t const *data;
  16. size_t size;
  17. size_t shift;
  18. public:
  19. BitReader(const uint8_t *data, size_t size) : data(data), size(size), shift(0) {}
  20. int Read(void) {
  21. if (size == 0) {
  22. return 0;
  23. }
  24. const int ret = ((*data) >> shift) & 1;
  25. shift++;
  26. if (shift >= 8) {
  27. shift = 0;
  28. data++;
  29. size--;
  30. }
  31. return ret;
  32. }
  33. };
  34. static tesseract::TessBaseAPI *api = nullptr;
  35. extern "C" int LLVMFuzzerInitialize(int * /*pArgc*/, char ***pArgv) {
  36. if (std::getenv("TESSDATA_PREFIX") == nullptr) {
  37. std::string binary_path = *pArgv[0];
  38. const std::string filepath = dirname(&binary_path[0]);
  39. const std::string tessdata_path = filepath + "/" + "tessdata";
  40. if (setenv("TESSDATA_PREFIX", tessdata_path.c_str(), 1) != 0) {
  41. printf("Setenv failed\n");
  42. std::abort();
  43. }
  44. }
  45. api = new tesseract::TessBaseAPI();
  46. if (api->Init(nullptr, "eng") != 0) {
  47. printf("Cannot initialize API\n");
  48. abort();
  49. }
  50. /* Silence output */
  51. api->SetVariable("debug_file", "/dev/null");
  52. return 0;
  53. }
  54. static PIX *createPix(BitReader &BR, const size_t width, const size_t height) {
  55. Pix *pix = pixCreate(width, height, 1);
  56. if (pix == nullptr) {
  57. printf("pix creation failed\n");
  58. abort();
  59. }
  60. for (size_t i = 0; i < width; i++) {
  61. for (size_t j = 0; j < height; j++) {
  62. pixSetPixel(pix, i, j, BR.Read());
  63. }
  64. }
  65. return pix;
  66. }
  67. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  68. BitReader BR(data, size);
  69. auto pix = createPix(BR, TESSERACT_FUZZER_WIDTH, TESSERACT_FUZZER_HEIGHT);
  70. api->SetImage(pix);
  71. char *outText = api->GetUTF8Text();
  72. pixDestroy(&pix);
  73. delete[] outText;
  74. return 0;
  75. }