1 // Copyright 2018 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <stdlib.h>
18 #include <string.h>
19 
20 #include "tidy.h"
21 #include "tidybuffio.h"
22 
run_tidy_parser(TidyBuffer * data_buffer,TidyBuffer * output_buffer,TidyBuffer * error_buffer)23 void run_tidy_parser(TidyBuffer* data_buffer,
24                      TidyBuffer* output_buffer,
25                      TidyBuffer* error_buffer) {
26     TidyDoc tdoc = tidyCreate();
27     if (tidySetErrorBuffer(tdoc, error_buffer) < 0) {
28         abort();
29     }
30     tidyOptSetBool(tdoc, TidyXhtmlOut, yes);
31     tidyOptSetBool(tdoc, TidyForceOutput, yes);
32 
33     if (tidyParseBuffer(tdoc, data_buffer) >= 0 &&
34             tidyCleanAndRepair(tdoc) >= 0 &&
35             tidyRunDiagnostics(tdoc) >= 0) {
36         tidySaveBuffer(tdoc, output_buffer);
37     }
38     tidyRelease(tdoc);
39 }
40 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)41 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
42     TidyBuffer data_buffer;
43     TidyBuffer output_buffer;
44     TidyBuffer error_buffer;
45     tidyBufInit(&data_buffer);
46     tidyBufInit(&output_buffer);
47     tidyBufInit(&error_buffer);
48 
49     tidyBufAttach(&data_buffer, (byte*)data, size);
50     run_tidy_parser(&data_buffer, &output_buffer, &error_buffer);
51 
52     tidyBufFree(&error_buffer);
53     tidyBufFree(&output_buffer);
54     tidyBufDetach(&data_buffer);
55     return 0;
56 }
57