Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / test / fuzzing / hb-shape-fuzzer.cc
1 #include "hb-fuzzer.hh"
2
3 #include <hb-ot.h>
4 #include <string.h>
5
6 #include <stdlib.h>
7
8 #define TEST_OT_FACE_NO_MAIN 1
9 #include "../api/test-ot-face.c"
10 #undef TEST_OT_FACE_NO_MAIN
11
12 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
13 {
14   hb_blob_t *blob = hb_blob_create ((const char *)data, size,
15                                     HB_MEMORY_MODE_READONLY, nullptr, nullptr);
16   hb_face_t *face = hb_face_create (blob, 0);
17   hb_font_t *font = hb_font_create (face);
18   hb_ot_font_set_funcs (font);
19   hb_font_set_scale (font, 12, 12);
20
21   unsigned num_coords = 0;
22   if (size) num_coords = data[size - 1];
23   num_coords = hb_ot_var_get_axis_count (face) > num_coords ? num_coords : hb_ot_var_get_axis_count (face);
24   int *coords = (int *) calloc (num_coords, sizeof (int));
25   if (size > num_coords + 1)
26     for (unsigned i = 0; i < num_coords; ++i)
27       coords[i] = ((int) data[size - num_coords + i - 1] - 128) * 10;
28   hb_font_set_var_coords_normalized (font, coords, num_coords);
29   free (coords);
30
31   {
32     const char text[] = "ABCDEXYZ123@_%&)*$!";
33     hb_buffer_t *buffer = hb_buffer_create ();
34     hb_buffer_add_utf8 (buffer, text, -1, 0, -1);
35     hb_buffer_guess_segment_properties (buffer);
36     hb_shape (font, buffer, nullptr, 0);
37     hb_buffer_destroy (buffer);
38   }
39
40   uint32_t text32[16] = {0};
41   unsigned int len = sizeof (text32);
42   if (size < len)
43     len = size;
44   if (len)
45     memcpy(text32, data + size - len, len);
46
47   hb_buffer_t *buffer = hb_buffer_create ();
48   hb_buffer_add_utf32 (buffer, text32, sizeof (text32) / sizeof (text32[0]), 0, -1);
49   hb_buffer_guess_segment_properties (buffer);
50   hb_shape (font, buffer, nullptr, 0);
51   hb_buffer_destroy (buffer);
52
53   /* Misc calls on font. */
54   test_font (font, text32[15]);
55
56   hb_font_destroy (font);
57   hb_face_destroy (face);
58   hb_blob_destroy (blob);
59   return 0;
60 }