1 /*
2 # Copyright 2021 Google LLC
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 ################################################################################
17 */
18
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "h3api.h"
25 #include "utility.h"
26
27 static const Direction DIGITS[7] = {CENTER_DIGIT, K_AXES_DIGIT, J_AXES_DIGIT,
28 JK_AXES_DIGIT, I_AXES_DIGIT, IK_AXES_DIGIT,
29 IJ_AXES_DIGIT};
30
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)31 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
32 if (size < sizeof(H3Index)) {
33 return 0;
34 }
35 H3Index h3;
36 memcpy(&h3, data, sizeof(H3Index));
37
38 H3Index input[] = {h3, h3};
39 int inputSize = sizeof(input) / sizeof(H3Index);
40
41 // fuzz compactCells
42 H3Index *compacted = calloc(inputSize, sizeof(H3Index));
43 H3Error errCompact = compactCells(input, compacted, inputSize);
44
45 // fuzz uncompactCells
46 int compactedCount = 0;
47 for (int i = 0; i < inputSize; i++) {
48 if (compacted[i] != 0) {
49 compactedCount++;
50 }
51 }
52 if (compactedCount < 2) {
53 int uncompactRes = 10;
54 int64_t uncompactedSize;
55 H3Error err2 =
56 uncompactCellsSize(compacted, inputSize, uncompactRes, &uncompactedSize);
57
58 H3Index *uncompacted = calloc(uncompactedSize, sizeof(H3Index));
59 H3Error err3 = uncompactCells(compacted, compactedCount, uncompacted,
60 uncompactedSize, uncompactRes);
61 free(uncompacted);
62 }
63
64 // fuzz h3NeighborRotations
65 int rotations = 0;
66 for (int i = 0; i < 7; i++) {
67 h3NeighborRotations(h3, DIGITS[i], &rotations);
68 }
69 free(compacted);
70 return 0;
71 }
72