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 #include <sys/types.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <stdint.h>
20 #include "tidy.h"
21 #include "tidybuffio.h"
22 #include "tidyenum.h"
23 #include "tidyplatform.h"
24
TidyXml(char * fuzz_inp,TidyBuffer * toutput,TidyBuffer * terror)25 void TidyXml(char *fuzz_inp, TidyBuffer *toutput,
26 TidyBuffer *terror) {
27 TidyDoc tdoc = tidyCreate();
28 tidyBufClear(toutput);
29 tidyBufClear(terror);
30 if (tidyOptSetBool(tdoc, TidyXmlOut, yes)) {
31 tidySetCharEncoding(tdoc, "utf8");
32 tidySetErrorBuffer(tdoc, terror);
33 tidyOptSetInt(tdoc, TidyWrapLen, 0);
34 tidyOptSetBool(tdoc, TidyXmlTags, yes);
35 tidyOptSetBool(tdoc, TidyQuoteNbsp, no);
36 tidyOptSetBool(tdoc, TidyNumEntities, yes);
37 tidyOptSetBool(tdoc, TidyQuiet, yes);
38 tidyOptSetBool(tdoc, TidyMark, no);
39 tidyOptSetBool(tdoc, TidyShowWarnings, no);
40 tidyParseString(tdoc, fuzz_inp);
41 tidyCleanAndRepair(tdoc);
42 tidyRunDiagnostics(tdoc);
43 tidySaveBuffer(tdoc, toutput);
44 }
45
46 tidyRelease(tdoc);
47 }
48
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)49 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
50 char *fuzz_inp = malloc(size+1);
51 memcpy(fuzz_inp, data, size);
52 fuzz_inp[size] = '\0';
53
54 TidyBuffer fuzz_toutput;
55 TidyBuffer fuzz_terror;
56
57 tidyBufInit(&fuzz_toutput);
58 tidyBufInit(&fuzz_terror);
59
60 TidyXml(fuzz_inp, &fuzz_toutput, &fuzz_terror);
61
62 free(fuzz_inp);
63 tidyBufFree(&fuzz_toutput);
64 tidyBufFree(&fuzz_terror);
65 return 0;
66 }
67