/src/libcups/ossfuzz/fuzzhttp.c
Line | Count | Source |
1 | | /* |
2 | | Copyright The libcups Developers. |
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 | | http://www.apache.org/licenses/LICENSE-2.0 |
7 | | Unless required by applicable law or agreed to in writing, software |
8 | | distributed under the License is distributed on an "AS IS" BASIS, |
9 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
10 | | See the License for the specific language governing permissions and |
11 | | limitations under the License. |
12 | | */ |
13 | | |
14 | | #include <stdint.h> |
15 | | #include <stdlib.h> |
16 | | #include <string.h> |
17 | | #include "cups.h" |
18 | | |
19 | | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
20 | 1.20k | { |
21 | 1.20k | if (size == 0 || size > 65536) |
22 | 10 | return 0; |
23 | | |
24 | | // NUL-terminate the input so it can be used as a C string. |
25 | 1.19k | char *str = (char *)malloc(size + 1); |
26 | 1.19k | if (!str) |
27 | 0 | return 0; |
28 | 1.19k | memcpy(str, data, size); |
29 | 1.19k | str[size] = '\0'; |
30 | | |
31 | | // 1. URI parser: scheme://user@host:port/resource splitter. |
32 | 1.19k | char scheme[256], username[256], host[256], resource[1024]; |
33 | 1.19k | int port = 0; |
34 | 1.19k | http_uri_status_t st = httpSeparateURI(HTTP_URI_CODING_ALL, str, |
35 | 1.19k | scheme, sizeof(scheme), |
36 | 1.19k | username, sizeof(username), |
37 | 1.19k | host, sizeof(host), |
38 | 1.19k | &port, |
39 | 1.19k | resource, sizeof(resource)); |
40 | | |
41 | | // Round-trip the separated components back into a URI. |
42 | 1.19k | if (st == HTTP_URI_STATUS_OK) |
43 | 241 | { |
44 | 241 | char rebuilt[2048]; |
45 | 241 | httpAssembleURI(HTTP_URI_CODING_ALL, rebuilt, sizeof(rebuilt), |
46 | 241 | scheme, username, host, port, resource); |
47 | 241 | } |
48 | | |
49 | | // 2. base64 decoder (output is at most ~3/4 of the input). |
50 | 1.19k | { |
51 | 1.19k | size_t outlen = size; |
52 | 1.19k | char *out = (char *)malloc(outlen + 1); |
53 | 1.19k | if (out) |
54 | 1.19k | { |
55 | 1.19k | const char *end = NULL; |
56 | 1.19k | httpDecode64(out, &outlen, str, &end); |
57 | 1.19k | free(out); |
58 | 1.19k | } |
59 | 1.19k | } |
60 | | |
61 | | // 3. HTTP date-string parser. |
62 | 1.19k | httpGetDateTime(str); |
63 | | |
64 | 1.19k | free(str); |
65 | 1.19k | return 0; |
66 | 1.19k | } |