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 <unistd.h>
21 #include "tidybuffio.h"
22 #include "tidy.h"
23
24
TidyXhtml(const uint8_t * data,size_t size,TidyBuffer * output,TidyBuffer * errbuf)25 int TidyXhtml(const uint8_t* data, size_t size, TidyBuffer* output, TidyBuffer* errbuf) {
26 Bool ok;
27
28 TidyDoc tdoc = tidyCreate();
29
30 ok = tidyOptSetBool( tdoc, TidyXhtmlOut, yes );
31 if (ok) tidySetErrorBuffer(tdoc, errbuf);
32
33 char filename[256];
34 sprintf(filename, "/tmp/libfuzzer.%d", getpid());
35
36 FILE *fp = fopen(filename, "wb");
37 if (!fp) {
38 return 0;
39 }
40 fwrite(data, size, 1, fp);
41 fclose(fp);
42
43 tidyParseFile(tdoc, filename);
44
45 tidyRelease( tdoc );
46 unlink(filename);
47
48 return 0;
49 }
50
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)51 int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
52 TidyBuffer fuzz_toutput;
53 TidyBuffer fuzz_terror;
54
55 tidyBufInit(&fuzz_toutput);
56 tidyBufInit(&fuzz_terror);
57
58 TidyXhtml(data, size, &fuzz_toutput, &fuzz_terror);
59
60 tidyBufFree(&fuzz_toutput);
61 tidyBufFree(&fuzz_terror);
62 return 0;
63 }
64
65