Imported Upstream version 2.3.1
[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 static void
10 trySubset (hb_face_t *face,
11            const hb_codepoint_t text[],
12            int text_length,
13            bool drop_hints,
14            bool drop_layout)
15 {
16   hb_subset_input_t *input = hb_subset_input_create_or_fail ();
17   hb_subset_input_set_drop_hints (input, drop_hints);
18   hb_subset_input_set_drop_layout (input, drop_layout);
19   hb_set_t *codepoints = hb_subset_input_unicode_set (input);
20
21   for (int i = 0; i < text_length; i++)
22   {
23     hb_set_add (codepoints, text[i]);
24   }
25
26   hb_face_t *result = hb_subset (face, input);
27   hb_face_destroy (result);
28
29   hb_subset_input_destroy (input);
30 }
31
32 static void
33 trySubset (hb_face_t *face,
34            const hb_codepoint_t text[],
35            int text_length)
36 {
37   for (unsigned int drop_hints = 0; drop_hints < 2; drop_hints++)
38   {
39     for (unsigned int drop_layout = 0; drop_layout < 2; drop_layout++)
40     {
41       trySubset (face, text, text_length,
42                  (bool) drop_hints, (bool) drop_layout);
43     }
44   }
45 }
46
47 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
48 {
49   hb_blob_t *blob = hb_blob_create ((const char *)data, size,
50                                     HB_MEMORY_MODE_READONLY, NULL, NULL);
51   hb_face_t *face = hb_face_create (blob, 0);
52
53   /* Just test this API here quickly. */
54   hb_set_t *output = hb_set_create();
55   hb_face_collect_unicodes (face, output);
56   hb_set_destroy (output);
57
58   const hb_codepoint_t text[] =
59       {
60         'A', 'B', 'C', 'D', 'E', 'X', 'Y', 'Z', '1', '2',
61         '3', '@', '_', '%', '&', ')', '*', '$', '!'
62       };
63
64   trySubset (face, text, sizeof (text) / sizeof (hb_codepoint_t));
65
66   hb_codepoint_t text_from_data[16];
67   if (size > sizeof(text_from_data)) {
68     memcpy (text_from_data,
69             data + size - sizeof(text_from_data),
70             sizeof(text_from_data));
71     unsigned int text_size = sizeof (text_from_data) / sizeof (hb_codepoint_t);
72     trySubset (face, text_from_data, text_size);
73   }
74
75   hb_face_destroy (face);
76   hb_blob_destroy (blob);
77
78   return 0;
79 }