Imported Upstream version 1.2.7
[platform/upstream/harfbuzz.git] / test / fuzzing / hb-fuzzer.cc
1 #include <stddef.h>
2 #include <hb.h>
3 #include <hb-ot.h>
4 #include <string.h>
5
6 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
7
8   hb_blob_t *blob = hb_blob_create((const char *)data, size,
9                                    HB_MEMORY_MODE_READONLY, NULL, NULL);
10   hb_face_t *face = hb_face_create(blob, 0);
11   hb_font_t *font = hb_font_create(face);
12   hb_ot_font_set_funcs(font);
13   hb_font_set_scale(font, 12, 12);
14
15   {
16     const char text[] = "ABCDEXYZ123@_%&)*$!";
17     hb_buffer_t *buffer = hb_buffer_create();
18     hb_buffer_add_utf8(buffer, text, -1, 0, -1);
19     hb_buffer_guess_segment_properties(buffer);
20     hb_shape(font, buffer, NULL, 0);
21     hb_buffer_destroy(buffer);
22   }
23
24   uint32_t text32[16];
25   if (size > sizeof(text32)) {
26     memcpy(text32, data + size - sizeof(text32), sizeof(text32));
27     hb_buffer_t *buffer = hb_buffer_create();
28     hb_buffer_add_utf32(buffer, text32, sizeof(text32)/sizeof(text32[0]), 0, -1);
29     hb_buffer_guess_segment_properties(buffer);
30     hb_shape(font, buffer, NULL, 0);
31     hb_buffer_destroy(buffer);
32   }
33
34
35   hb_font_destroy(font);
36   hb_face_destroy(face);
37   hb_blob_destroy(blob);
38   return 0;
39 }
40
41 #ifdef MAIN
42 #include <iostream>
43 #include <iterator>
44 #include <fstream>
45 #include <assert.h>
46
47 std::string FileToString(const std::string &Path) {
48   /* TODO This silently passes if file does not exist.  Fix it! */
49   std::ifstream T(Path.c_str());
50   return std::string((std::istreambuf_iterator<char>(T)),
51                      std::istreambuf_iterator<char>());
52 }
53
54 int main(int argc, char **argv) {
55   for (int i = 1; i < argc; i++) {
56     std::string s = FileToString(argv[i]);
57     std::cout << argv[i] << std::endl;
58     LLVMFuzzerTestOneInput((const unsigned char*)s.data(), s.size());
59   }
60 }
61 #endif