28ce921c897c3d988db9bd52009c0bcb3901a5f2
[platform/upstream/harfbuzz.git] / test / fuzzing / hb-subset-fuzzer.cc
1 #include "hb-fuzzer.hh"
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #include "hb-subset.h"
8
9 void trySubset (hb_face_t *face,
10                 const hb_codepoint_t text[],
11                 int text_length,
12                 bool drop_hints,
13                 bool drop_ot_layout)
14 {
15   hb_subset_profile_t *profile = hb_subset_profile_create ();
16
17   hb_subset_input_t *input = hb_subset_input_create_or_fail ();
18   *hb_subset_input_drop_hints (input) = drop_hints;
19   *hb_subset_input_drop_ot_layout (input) = drop_ot_layout;
20   hb_set_t *codepoints = hb_subset_input_unicode_set (input);
21
22   for (int i = 0; i < text_length; i++)
23   {
24     hb_set_add (codepoints, text[i]);
25   }
26
27   hb_face_t *result = hb_subset (face, profile, input);
28   hb_face_destroy (result);
29
30   hb_subset_input_destroy (input);
31   hb_subset_profile_destroy (profile);
32 }
33
34 void trySubset (hb_face_t *face,
35                 const hb_codepoint_t text[],
36                 int text_length)
37 {
38   for (unsigned int drop_hints = 0; drop_hints < 2; drop_hints++)
39   {
40     for (unsigned int drop_ot_layout = 0; drop_ot_layout < 2; drop_ot_layout++)
41     {
42       trySubset (face, text, text_length,
43                  (bool) drop_hints, (bool) drop_ot_layout);
44     }
45   }
46 }
47
48 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
49 {
50   hb_blob_t *blob = hb_blob_create ((const char *)data, size,
51                                     HB_MEMORY_MODE_READONLY, NULL, NULL);
52   hb_face_t *face = hb_face_create (blob, 0);
53
54   const hb_codepoint_t text[] =
55       {
56         'A', 'B', 'C', 'D', 'E', 'X', 'Y', 'Z', '1', '2',
57         '3', '@', '_', '%', '&', ')', '*', '$', '!'
58       };
59
60   trySubset (face, text, sizeof (text) / sizeof (hb_codepoint_t));
61
62   hb_codepoint_t text_from_data[16];
63   if (size > sizeof(text_from_data)) {
64     memcpy(text_from_data,
65            data + size - sizeof(text_from_data),
66            sizeof(text_from_data));
67     unsigned int text_size = sizeof (text_from_data) / sizeof (hb_codepoint_t);
68     trySubset (face, text_from_data, text_size);
69   }
70
71   hb_face_destroy (face);
72   hb_blob_destroy (blob);
73
74   return 0;
75 }