Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / test / fuzzing / hb-set-fuzzer.cc
1 #include "hb-fuzzer.hh"
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <assert.h>
7
8 #include "hb.h"
9
10 enum set_operation_t : uint8_t
11 {
12   INTERSECT = 0,
13   UNION = 1,
14   SUBTRACT = 2,
15   SYMMETRIC_DIFFERENCE = 3
16 };
17
18 struct instructions_t
19 {
20   set_operation_t operation;
21   uint32_t first_set_size;
22 };
23
24 static hb_set_t *create_set (const uint32_t *value_array, int count)
25 {
26   hb_set_t *set = hb_set_create ();
27   for (int i = 0; i < count; i++)
28     hb_set_add (set, value_array[i]);
29   return set;
30 }
31
32
33 extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
34 {
35   if (size < sizeof (instructions_t))
36     return 0;
37
38   const instructions_t &instructions = reinterpret_cast<const instructions_t &> (data);
39   data += sizeof (instructions_t);
40   size -= sizeof (instructions_t);
41
42   const uint32_t *values = reinterpret_cast<const uint32_t *> (data);
43   size = size / sizeof (uint32_t);
44
45   if (size < instructions.first_set_size)
46     return 0;
47
48   hb_set_t *set_a = create_set (values, instructions.first_set_size);
49
50   values += instructions.first_set_size;
51   size -= instructions.first_set_size;
52   hb_set_t *set_b = create_set (values, size);
53
54   switch (instructions.operation)
55   {
56   case INTERSECT:
57     hb_set_intersect (set_a, set_b);
58     break;
59   case UNION:
60     hb_set_union (set_a, set_b);
61     break;
62   case SUBTRACT:
63     hb_set_subtract (set_a, set_b);
64     break;
65   case SYMMETRIC_DIFFERENCE:
66     hb_set_symmetric_difference (set_a, set_b);
67     break;
68   default:
69     break;
70   }
71
72   hb_set_destroy (set_a);
73   hb_set_destroy (set_b);
74
75   return 0;
76 }