Imported Upstream version 2.4.0
[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            bool retain_gids)
16 {
17   hb_subset_input_t *input = hb_subset_input_create_or_fail ();
18   hb_subset_input_set_drop_hints (input, drop_hints);
19   hb_subset_input_set_drop_layout (input, drop_layout);
20   hb_subset_input_set_retain_gids (input, retain_gids);
21   hb_set_t *codepoints = hb_subset_input_unicode_set (input);
22
23   for (int i = 0; i < text_length; i++)
24   {
25     hb_set_add (codepoints, text[i]);
26   }
27
28   hb_face_t *result = hb_subset (face, input);
29   hb_face_destroy (result);
30
31   hb_subset_input_destroy (input);
32 }
33
34 static void
35 trySubset (hb_face_t *face,
36            const hb_codepoint_t text[],
37            int text_length,
38            const uint8_t flags[1])
39 {
40   bool drop_hints =  flags[0] & (1 << 0);
41   bool drop_layout = flags[0] & (1 << 1);
42   bool retain_gids = flags[0] & (1 << 2);
43   trySubset (face, text, text_length,
44              drop_hints, drop_layout, retain_gids);
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   uint8_t flags[1] = {0};
59   const hb_codepoint_t text[] =
60       {
61         'A', 'B', 'C', 'D', 'E', 'X', 'Y', 'Z', '1', '2',
62         '3', '@', '_', '%', '&', ')', '*', '$', '!'
63       };
64
65   trySubset (face, text, sizeof (text) / sizeof (hb_codepoint_t), flags);
66
67   hb_codepoint_t text_from_data[16];
68   if (size > sizeof(text_from_data) + sizeof(flags)) {
69     memcpy (text_from_data,
70             data + size - sizeof(text_from_data),
71             sizeof(text_from_data));
72
73     memcpy (flags,
74             data + size - sizeof(text_from_data) - sizeof(flags),
75             sizeof(flags));
76     unsigned int text_size = sizeof (text_from_data) / sizeof (hb_codepoint_t);
77
78     trySubset (face, text_from_data, text_size, flags);
79   }
80
81   hb_face_destroy (face);
82   hb_blob_destroy (blob);
83
84   return 0;
85 }