Skip to content

Commit 7e9970b

Browse files
committed
Format fuzzer code with clang-format
Signed-off-by: Stefan Weil <[email protected]>
1 parent 270e466 commit 7e9970b

File tree

1 file changed

+67
-71
lines changed

1 file changed

+67
-71
lines changed

unittest/fuzzers/fuzzer-api.cpp

+67-71
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,99 @@
11
#include "baseapi.h"
22
#include "leptonica/allheaders.h"
33

4-
#include <stdint.h>
4+
#include <libgen.h>
55
#include <stddef.h>
6+
#include <stdint.h>
67
#include <stdlib.h>
7-
#include <string>
88
#include <string.h>
9-
#include <libgen.h>
9+
#include <string>
1010

1111
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+
}
3825

39-
tesseract::TessBaseAPI *api = nullptr;
26+
const int ret = ((*data) >> shift) & 1;
4027

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+
};
4538

39+
tesseract::TessBaseAPI* api = nullptr;
4640

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;
5144

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);
5849

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();
6354
}
55+
}
6456

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+
}
6762

68-
return 0;
69-
}
63+
/* Silence output */
64+
api->SetVariable("debug_file", "/dev/null");
7065

66+
return 0;
67+
}
7168

7269
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);
7471

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+
}
7976

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());
8480
}
81+
}
8582

86-
return pix;
83+
return pix;
8784
}
8885

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);
9288

93-
auto pix = createPix(BR, 100, 100);
89+
auto pix = createPix(BR, 100, 100);
9490

95-
api->SetImage(pix);
91+
api->SetImage(pix);
9692

97-
char* outText = api->GetUTF8Text();
93+
char* outText = api->GetUTF8Text();
9894

99-
pixDestroy(&pix);
100-
delete[] outText;
95+
pixDestroy(&pix);
96+
delete[] outText;
10197

102-
return 0;
98+
return 0;
10399
}

0 commit comments

Comments
 (0)