/src/fuzz_webp_enc_dec.cc
Line | Count | Source (jump to first uncovered line) |
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 | | //////////////////////////////////////////////////////////////////////////////// |
16 | | |
17 | | #include <stdio.h> |
18 | | #include <stdlib.h> |
19 | | #include "fuzz.h" |
20 | | #include "webp/encode.h" |
21 | | #include "webp/decode.h" |
22 | | |
23 | | namespace { |
24 | | |
25 | | const VP8CPUInfo default_VP8GetCPUInfo = VP8GetCPUInfo; |
26 | | |
27 | | } // namespace |
28 | | |
29 | 2.61k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* const data, size_t size) { |
30 | 2.61k | uint32_t bit_pos = 0; |
31 | 2.61k | |
32 | 2.61k | ExtractAndDisableOptimizations(default_VP8GetCPUInfo, data, size, &bit_pos); |
33 | 2.61k | |
34 | 2.61k | // Init the source picture. |
35 | 2.61k | WebPPicture pic; |
36 | 2.61k | if (!WebPPictureInit(&pic)) { |
37 | 0 | fprintf(stderr, "WebPPictureInit failed.\n"); |
38 | 0 | abort(); |
39 | 0 | } |
40 | 2.61k | pic.use_argb = Extract(1, data, size, &bit_pos); |
41 | 2.61k | |
42 | 2.61k | // Read the source picture. |
43 | 2.61k | if (!ExtractSourcePicture(&pic, data, size, &bit_pos)) { |
44 | 0 | fprintf(stderr, "Can't read input image.\n"); |
45 | 0 | WebPPictureFree(&pic); |
46 | 0 | abort(); |
47 | 0 | } |
48 | 2.61k | |
49 | 2.61k | // Crop and scale. |
50 | 2.61k | if (!ExtractAndCropOrScale(&pic, data, size, &bit_pos)) { |
51 | 0 | fprintf(stderr, "ExtractAndCropOrScale failed."); |
52 | 0 | WebPPictureFree(&pic); |
53 | 0 | abort(); |
54 | 0 | } |
55 | 2.61k | |
56 | 2.61k | // Extract a configuration from the packed bits. |
57 | 2.61k | WebPConfig config; |
58 | 2.61k | if (!ExtractWebPConfig(&config, data, size, &bit_pos)) { |
59 | 0 | fprintf(stderr, "ExtractWebPConfig failed.\n"); |
60 | 0 | abort(); |
61 | 0 | } |
62 | 2.61k | // Skip slow settings on big images, it's likely to timeout. |
63 | 2.61k | if (pic.width * pic.height > 32 * 32) { |
64 | 1.34k | if (config.lossless) { |
65 | 609 | if (config.quality > 99.0f && config.method >= 5) { |
66 | 5 | config.quality = 99.0f; |
67 | 5 | config.method = 5; |
68 | 5 | } |
69 | 731 | } else { |
70 | 731 | if (config.quality > 99.0f && config.method == 6) { |
71 | 8 | config.quality = 99.0f; |
72 | 8 | } |
73 | 731 | } |
74 | 1.34k | if (config.alpha_quality == 100 && config.method == 6) { |
75 | 2 | config.alpha_quality = 99; |
76 | 2 | } |
77 | 1.34k | } |
78 | 2.61k | |
79 | 2.61k | // Encode. |
80 | 2.61k | WebPMemoryWriter memory_writer; |
81 | 2.61k | WebPMemoryWriterInit(&memory_writer); |
82 | 2.61k | pic.writer = WebPMemoryWrite; |
83 | 2.61k | pic.custom_ptr = &memory_writer; |
84 | 2.61k | if (!WebPEncode(&config, &pic)) { |
85 | 0 | fprintf(stderr, "WebPEncode failed. Error code: %d\n", pic.error_code); |
86 | 0 | WebPMemoryWriterClear(&memory_writer); |
87 | 0 | WebPPictureFree(&pic); |
88 | 0 | abort(); |
89 | 0 | } |
90 | 2.61k | |
91 | 2.61k | // Try decoding the result. |
92 | 2.61k | int w, h; |
93 | 2.61k | const uint8_t* const out_data = memory_writer.mem; |
94 | 2.61k | const size_t out_size = memory_writer.size; |
95 | 2.61k | uint8_t* const rgba = WebPDecodeBGRA(out_data, out_size, &w, &h); |
96 | 2.61k | if (rgba == nullptr || w != pic.width || h != pic.height) { |
97 | 0 | fprintf(stderr, "WebPDecodeBGRA failed.\n"); |
98 | 0 | WebPFree(rgba); |
99 | 0 | WebPMemoryWriterClear(&memory_writer); |
100 | 0 | WebPPictureFree(&pic); |
101 | 0 | abort(); |
102 | 0 | } |
103 | 2.61k | |
104 | 2.61k | // Compare the results if exact encoding. |
105 | 2.61k | if (pic.use_argb && config.lossless && config.near_lossless == 100) { |
106 | 43 | const uint32_t* src1 = (const uint32_t*)rgba; |
107 | 43 | const uint32_t* src2 = pic.argb; |
108 | 2.82k | for (int y = 0; y < h; ++y, src1 += w, src2 += pic.argb_stride) { |
109 | 238k | for (int x = 0; x < w; ++x) { |
110 | 235k | uint32_t v1 = src1[x], v2 = src2[x]; |
111 | 235k | if (!config.exact) { |
112 | 87.3k | if ((v1 & 0xff000000u) == 0 || (v2 & 0xff000000u) == 0) { |
113 | 0 | // Only keep alpha for comparison of fully transparent area. |
114 | 0 | v1 &= 0xff000000u; |
115 | 0 | v2 &= 0xff000000u; |
116 | 0 | } |
117 | 87.3k | } |
118 | 235k | if (v1 != v2) { |
119 | 0 | fprintf(stderr, "Lossless compression failed pixel-exactness.\n"); |
120 | 0 | WebPFree(rgba); |
121 | 0 | WebPMemoryWriterClear(&memory_writer); |
122 | 0 | WebPPictureFree(&pic); |
123 | 0 | abort(); |
124 | 0 | } |
125 | 235k | } |
126 | 2.78k | } |
127 | 43 | } |
128 | 2.61k | |
129 | 2.61k | WebPFree(rgba); |
130 | 2.61k | WebPMemoryWriterClear(&memory_writer); |
131 | 2.61k | WebPPictureFree(&pic); |
132 | 2.61k | return 0; |
133 | 2.61k | } |