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 #ifndef WEBP_TESTS_FUZZER_FUZZ_UTILS_H_
18 #define WEBP_TESTS_FUZZER_FUZZ_UTILS_H_
19
20 #include <stdint.h>
21 #include <stdlib.h>
22
23 #include "./img_alpha.h"
24 #include "./img_grid.h"
25 #include "./img_peak.h"
26 #include "src/dsp/dsp.h"
27 #include "src/webp/encode.h"
28
29 //------------------------------------------------------------------------------
30 // Arbitrary limits to prevent OOM, timeout, or slow execution.
31 //
32 // The decoded image size, and for animations additionally the canvas size.
33 static const size_t kFuzzPxLimit = 1024 * 1024;
34 // Demuxed or decoded animation frames.
35 static const int kFuzzFrameLimit = 3;
36
37 // Reads and sums (up to) 128 spread-out bytes.
FuzzHash(const uint8_t * const data,size_t size)38 static WEBP_INLINE uint8_t FuzzHash(const uint8_t* const data, size_t size) {
39 uint8_t value = 0;
40 size_t incr = size / 128;
41 if (!incr) incr = 1;
42 for (size_t i = 0; i < size; i += incr) value += data[i];
43 return value;
44 }
45
46 //------------------------------------------------------------------------------
47 // Extract an integer in [0, max_value].
48
Extract(uint32_t max_value,const uint8_t data[],size_t size,uint32_t * const bit_pos)49 static WEBP_INLINE uint32_t Extract(uint32_t max_value,
50 const uint8_t data[], size_t size,
51 uint32_t* const bit_pos) {
52 uint32_t v = 0;
53 uint32_t range = 1;
54 while (*bit_pos < 8 * size && range <= max_value) {
55 const uint8_t mask = 1u << (*bit_pos & 7);
56 v = (v << 1) | !!(data[*bit_pos >> 3] & mask);
57 range <<= 1;
58 ++*bit_pos;
59 }
60 return v % (max_value + 1);
61 }
62
63 //------------------------------------------------------------------------------
64 // Some functions to override VP8GetCPUInfo and disable some optimizations.
65
66 static VP8CPUInfo GetCPUInfo;
67
GetCPUInfoNoSSE41(CPUFeature feature)68 static WEBP_INLINE int GetCPUInfoNoSSE41(CPUFeature feature) {
69 if (feature == kSSE4_1 || feature == kAVX) return 0;
70 return GetCPUInfo(feature);
71 }
72
GetCPUInfoNoAVX(CPUFeature feature)73 static WEBP_INLINE int GetCPUInfoNoAVX(CPUFeature feature) {
74 if (feature == kAVX) return 0;
75 return GetCPUInfo(feature);
76 }
77
GetCPUInfoForceSlowSSSE3(CPUFeature feature)78 static WEBP_INLINE int GetCPUInfoForceSlowSSSE3(CPUFeature feature) {
79 if (feature == kSlowSSSE3 && GetCPUInfo(kSSE3)) {
80 return 1; // we have SSE3 -> force SlowSSSE3
81 }
82 return GetCPUInfo(feature);
83 }
84
GetCPUInfoOnlyC(CPUFeature feature)85 static WEBP_INLINE int GetCPUInfoOnlyC(CPUFeature feature) {
86 (void)feature;
87 return 0;
88 }
89
ExtractAndDisableOptimizations(VP8CPUInfo default_VP8GetCPUInfo,const uint8_t data[],size_t size,uint32_t * const bit_pos)90 static WEBP_INLINE void ExtractAndDisableOptimizations(
91 VP8CPUInfo default_VP8GetCPUInfo, const uint8_t data[], size_t size,
92 uint32_t* const bit_pos) {
93 GetCPUInfo = default_VP8GetCPUInfo;
94 const VP8CPUInfo kVP8CPUInfos[5] = {GetCPUInfoOnlyC, GetCPUInfoForceSlowSSSE3,
95 GetCPUInfoNoSSE41, GetCPUInfoNoAVX,
96 GetCPUInfo};
97 int VP8GetCPUInfo_index = Extract(4, data, size, bit_pos);
98 VP8GetCPUInfo = kVP8CPUInfos[VP8GetCPUInfo_index];
99 }
100
101 //------------------------------------------------------------------------------
102
ExtractWebPConfig(WebPConfig * const config,const uint8_t data[],size_t size,uint32_t * const bit_pos)103 static WEBP_INLINE int ExtractWebPConfig(WebPConfig* const config,
104 const uint8_t data[], size_t size,
105 uint32_t* const bit_pos) {
106 if (config == NULL || !WebPConfigInit(config)) return 0;
107 config->lossless = Extract(1, data, size, bit_pos);
108 config->quality = Extract(100, data, size, bit_pos);
109 config->method = Extract(6, data, size, bit_pos);
110 config->image_hint =
111 (WebPImageHint)Extract(WEBP_HINT_LAST - 1, data, size, bit_pos);
112 config->segments = 1 + Extract(3, data, size, bit_pos);
113 config->sns_strength = Extract(100, data, size, bit_pos);
114 config->filter_strength = Extract(100, data, size, bit_pos);
115 config->filter_sharpness = Extract(7, data, size, bit_pos);
116 config->filter_type = Extract(1, data, size, bit_pos);
117 config->autofilter = Extract(1, data, size, bit_pos);
118 config->alpha_compression = Extract(1, data, size, bit_pos);
119 config->alpha_filtering = Extract(2, data, size, bit_pos);
120 config->alpha_quality = Extract(100, data, size, bit_pos);
121 config->pass = 1 + Extract(9, data, size, bit_pos);
122 config->show_compressed = 1;
123 config->preprocessing = Extract(2, data, size, bit_pos);
124 config->partitions = Extract(3, data, size, bit_pos);
125 config->partition_limit = 10 * Extract(10, data, size, bit_pos);
126 config->emulate_jpeg_size = Extract(1, data, size, bit_pos);
127 config->thread_level = Extract(1, data, size, bit_pos);
128 config->low_memory = Extract(1, data, size, bit_pos);
129 config->near_lossless = 20 * Extract(5, data, size, bit_pos);
130 config->exact = Extract(1, data, size, bit_pos);
131 config->use_delta_palette = Extract(1, data, size, bit_pos);
132 config->use_sharp_yuv = Extract(1, data, size, bit_pos);
133 return WebPValidateConfig(config);
134 }
135
136 //------------------------------------------------------------------------------
137
ExtractSourcePicture(WebPPicture * const pic,const uint8_t data[],size_t size,uint32_t * const bit_pos)138 static WEBP_INLINE int ExtractSourcePicture(WebPPicture* const pic,
139 const uint8_t data[], size_t size,
140 uint32_t* const bit_pos) {
141 if (pic == NULL) return 0;
142
143 // Pick a source picture.
144 const uint8_t* kImagesData[] = {
145 kImgAlphaData,
146 kImgGridData,
147 kImgPeakData
148 };
149 const int kImagesWidth[] = {
150 kImgAlphaWidth,
151 kImgGridWidth,
152 kImgPeakWidth
153 };
154 const int kImagesHeight[] = {
155 kImgAlphaHeight,
156 kImgGridHeight,
157 kImgPeakHeight
158 };
159 const size_t kNbImages = sizeof(kImagesData) / sizeof(kImagesData[0]);
160 const size_t image_index = Extract(kNbImages - 1, data, size, bit_pos);
161 const uint8_t* const image_data = kImagesData[image_index];
162 pic->width = kImagesWidth[image_index];
163 pic->height = kImagesHeight[image_index];
164 pic->argb_stride = pic->width * 4 * sizeof(uint8_t);
165
166 // Read the bytes.
167 return WebPPictureImportRGBA(pic, image_data, pic->argb_stride);
168 }
169
170 //------------------------------------------------------------------------------
171
Max(int a,int b)172 static WEBP_INLINE int Max(int a, int b) { return ((a < b) ? b : a); }
173
ExtractAndCropOrScale(WebPPicture * const pic,const uint8_t data[],size_t size,uint32_t * const bit_pos)174 static WEBP_INLINE int ExtractAndCropOrScale(WebPPicture* const pic,
175 const uint8_t data[], size_t size,
176 uint32_t* const bit_pos) {
177 if (pic == NULL) return 0;
178 #if !defined(WEBP_REDUCE_SIZE)
179 const int alter_input = Extract(1, data, size, bit_pos);
180 const int crop_or_scale = Extract(1, data, size, bit_pos);
181 const int width_ratio = 1 + Extract(7, data, size, bit_pos);
182 const int height_ratio = 1 + Extract(7, data, size, bit_pos);
183 if (alter_input) {
184 if (crop_or_scale) {
185 const uint32_t left_ratio = 1 + Extract(7, data, size, bit_pos);
186 const uint32_t top_ratio = 1 + Extract(7, data, size, bit_pos);
187 const int cropped_width = Max(1, pic->width / width_ratio);
188 const int cropped_height = Max(1, pic->height / height_ratio);
189 const int cropped_left = (pic->width - cropped_width) / left_ratio;
190 const int cropped_top = (pic->height - cropped_height) / top_ratio;
191 return WebPPictureCrop(pic, cropped_left, cropped_top, cropped_width,
192 cropped_height);
193 } else {
194 const int scaled_width = 1 + (pic->width * width_ratio) / 8;
195 const int scaled_height = 1 + (pic->height * height_ratio) / 8;
196 return WebPPictureRescale(pic, scaled_width, scaled_height);
197 }
198 }
199 #else // defined(WEBP_REDUCE_SIZE)
200 (void)data;
201 (void)size;
202 (void)bit_pos;
203 #endif // !defined(WEBP_REDUCE_SIZE)
204 return 1;
205 }
206
207 #endif // WEBP_TESTS_FUZZER_FUZZ_UTILS_H_
208