| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#if defined(__SANITIZE_ADDRESS__) |
| 36 |
static const size_t kFuzzPxLimit = 1024 * 1024 / 10; |
| 37 |
#elif !defined(__has_feature) |
| 38 |
static const size_t kFuzzPxLimit = 1024 * 1024; |
| 39 |
#elif __has_feature(address_sanitizer) || __has_feature(memory_sanitizer) |
| 40 |
static const size_t kFuzzPxLimit = 1024 * 1024 / 18; |
| 41 |
#else |
| 42 |
static const size_t kFuzzPxLimit = 1024 * 1024; |
| 43 |
#endif |
| 44 |
|
| 45 |
|
| 46 |
static const int kFuzzFrameLimit = 3; |
| 47 |
|
| 48 |
|
| 49 |
static WEBP_INLINE uint8_t FuzzHash(const uint8_t* const data, size_t size) { |
| 50 |
uint8_t value = 0; |
| 51 |
size_t incr = size / 128; |
| 52 |
if (!incr) incr = 1; |
| 53 |
for (size_t i = 0; i < size; i += incr) value += data[i]; |
| 54 |
return value; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static WEBP_INLINE uint32_t Extract(uint32_t max_value, |
| 61 |
const uint8_t data[], size_t size, |
| 62 |
uint32_t* const bit_pos) { |
| 63 |
uint32_t v = 0; |
| 64 |
uint32_t range = 1; |
| 65 |
while (*bit_pos < 8 * size && range <= max_value) { |
| 66 |
const uint8_t mask = 1u << (*bit_pos & 7); |
| 67 |
v = (v << 1) | !!(data[*bit_pos >> 3] & mask); |
| 68 |
range <<= 1; |
| 69 |
++*bit_pos; |
| 70 |
} |
| 71 |
return v % (max_value + 1); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#ifdef __cplusplus |
| 78 |
extern "C" VP8CPUInfo VP8GetCPUInfo; |
| 79 |
#else |
| 80 |
extern VP8CPUInfo VP8GetCPUInfo; |
| 81 |
#endif |
| 82 |
static VP8CPUInfo GetCPUInfo; |
| 83 |
|
| 84 |
static WEBP_INLINE int GetCPUInfoNoSSE41(CPUFeature feature) { |
| 85 |
if (feature == kSSE4_1 || feature == kAVX) return 0; |
| 86 |
return GetCPUInfo(feature); |
| 87 |
} |
| 88 |
|
| 89 |
static WEBP_INLINE int GetCPUInfoNoAVX(CPUFeature feature) { |
| 90 |
if (feature == kAVX) return 0; |
| 91 |
return GetCPUInfo(feature); |
| 92 |
} |
| 93 |
|
| 94 |
static WEBP_INLINE int GetCPUInfoForceSlowSSSE3(CPUFeature feature) { |
| 95 |
if (feature == kSlowSSSE3 && GetCPUInfo(kSSE3)) { |
| 96 |
return 1; |
| 97 |
} |
| 98 |
return GetCPUInfo(feature); |
| 99 |
} |
| 100 |
|
| 101 |
static WEBP_INLINE int GetCPUInfoOnlyC(CPUFeature feature) { |
| 102 |
(void)feature; |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
static WEBP_INLINE void ExtractAndDisableOptimizations( |
| 107 |
VP8CPUInfo default_VP8GetCPUInfo, const uint8_t data[], size_t size, |
| 108 |
uint32_t* const bit_pos) { |
| 109 |
GetCPUInfo = default_VP8GetCPUInfo; |
| 110 |
const VP8CPUInfo kVP8CPUInfos[5] = {GetCPUInfoOnlyC, GetCPUInfoForceSlowSSSE3, |
| 111 |
GetCPUInfoNoSSE41, GetCPUInfoNoAVX, |
| 112 |
GetCPUInfo}; |
| 113 |
int VP8GetCPUInfo_index = Extract(4, data, size, bit_pos); |
| 114 |
VP8GetCPUInfo = kVP8CPUInfos[VP8GetCPUInfo_index]; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
static WEBP_INLINE int ExtractWebPConfig(WebPConfig* const config, |
| 120 |
const uint8_t data[], size_t size, |
| 121 |
uint32_t* const bit_pos) { |
| 122 |
if (config == NULL || !WebPConfigInit(config)) return 0; |
| 123 |
config->lossless = Extract(1, data, size, bit_pos); |
| 124 |
config->quality = Extract(100, data, size, bit_pos); |
| 125 |
config->method = Extract(6, data, size, bit_pos); |
| 126 |
config->image_hint = |
| 127 |
(WebPImageHint)Extract(WEBP_HINT_LAST - 1, data, size, bit_pos); |
| 128 |
config->segments = 1 + Extract(3, data, size, bit_pos); |
| 129 |
config->sns_strength = Extract(100, data, size, bit_pos); |
| 130 |
config->filter_strength = Extract(100, data, size, bit_pos); |
| 131 |
config->filter_sharpness = Extract(7, data, size, bit_pos); |
| 132 |
config->filter_type = Extract(1, data, size, bit_pos); |
| 133 |
config->autofilter = Extract(1, data, size, bit_pos); |
| 134 |
config->alpha_compression = Extract(1, data, size, bit_pos); |
| 135 |
config->alpha_filtering = Extract(2, data, size, bit_pos); |
| 136 |
config->alpha_quality = Extract(100, data, size, bit_pos); |
| 137 |
config->pass = 1 + Extract(9, data, size, bit_pos); |
| 138 |
config->show_compressed = 1; |
| 139 |
config->preprocessing = Extract(2, data, size, bit_pos); |
| 140 |
config->partitions = Extract(3, data, size, bit_pos); |
| 141 |
config->partition_limit = 10 * Extract(10, data, size, bit_pos); |
| 142 |
config->emulate_jpeg_size = Extract(1, data, size, bit_pos); |
| 143 |
config->thread_level = Extract(1, data, size, bit_pos); |
| 144 |
config->low_memory = Extract(1, data, size, bit_pos); |
| 145 |
config->near_lossless = 20 * Extract(5, data, size, bit_pos); |
| 146 |
config->exact = Extract(1, data, size, bit_pos); |
| 147 |
config->use_delta_palette = Extract(1, data, size, bit_pos); |
| 148 |
config->use_sharp_yuv = Extract(1, data, size, bit_pos); |
| 149 |
return WebPValidateConfig(config); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
static WEBP_INLINE int ExtractSourcePicture(WebPPicture* const pic, |
| 155 |
const uint8_t data[], size_t size, |
| 156 |
uint32_t* const bit_pos) { |
| 157 |
if (pic == NULL) return 0; |
| 158 |
|
| 159 |
|
| 160 |
const uint8_t* kImagesData[] = { |
| 161 |
kImgAlphaData, |
| 162 |
kImgGridData, |
| 163 |
kImgPeakData |
| 164 |
}; |
| 165 |
const int kImagesWidth[] = { |
| 166 |
kImgAlphaWidth, |
| 167 |
kImgGridWidth, |
| 168 |
kImgPeakWidth |
| 169 |
}; |
| 170 |
const int kImagesHeight[] = { |
| 171 |
kImgAlphaHeight, |
| 172 |
kImgGridHeight, |
| 173 |
kImgPeakHeight |
| 174 |
}; |
| 175 |
const size_t kNbImages = sizeof(kImagesData) / sizeof(kImagesData[0]); |
| 176 |
const size_t image_index = Extract(kNbImages - 1, data, size, bit_pos); |
| 177 |
const uint8_t* const image_data = kImagesData[image_index]; |
| 178 |
pic->width = kImagesWidth[image_index]; |
| 179 |
pic->height = kImagesHeight[image_index]; |
| 180 |
pic->argb_stride = pic->width * 4 * sizeof(uint8_t); |
| 181 |
|
| 182 |
|
| 183 |
return WebPPictureImportRGBA(pic, image_data, pic->argb_stride); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
static WEBP_INLINE int Max(int a, int b) { return ((a < b) ? b : a); } |
| 189 |
|
| 190 |
static WEBP_INLINE int ExtractAndCropOrScale(WebPPicture* const pic, |
| 191 |
const uint8_t data[], size_t size, |
| 192 |
uint32_t* const bit_pos) { |
| 193 |
if (pic == NULL) return 0; |
| 194 |
#if !defined(WEBP_REDUCE_SIZE) |
| 195 |
const int alter_input = Extract(1, data, size, bit_pos); |
| 196 |
const int crop_or_scale = Extract(1, data, size, bit_pos); |
| 197 |
const int width_ratio = 1 + Extract(7, data, size, bit_pos); |
| 198 |
const int height_ratio = 1 + Extract(7, data, size, bit_pos); |
| 199 |
if (alter_input) { |
| 200 |
if (crop_or_scale) { |
| 201 |
const uint32_t left_ratio = 1 + Extract(7, data, size, bit_pos); |
| 202 |
const uint32_t top_ratio = 1 + Extract(7, data, size, bit_pos); |
| 203 |
const int cropped_width = Max(1, pic->width / width_ratio); |
| 204 |
const int cropped_height = Max(1, pic->height / height_ratio); |
| 205 |
const int cropped_left = (pic->width - cropped_width) / left_ratio; |
| 206 |
const int cropped_top = (pic->height - cropped_height) / top_ratio; |
| 207 |
return WebPPictureCrop(pic, cropped_left, cropped_top, cropped_width, |
| 208 |
cropped_height); |
| 209 |
} else { |
| 210 |
const int scaled_width = 1 + (pic->width * width_ratio) / 8; |
| 211 |
const int scaled_height = 1 + (pic->height * height_ratio) / 8; |
| 212 |
return WebPPictureRescale(pic, scaled_width, scaled_height); |
| 213 |
} |
| 214 |
} |
| 215 |
#else |
| 216 |
(void)data; |
| 217 |
(void)size; |
| 218 |
(void)bit_pos; |
| 219 |
#endif |
| 220 |
return 1; |
| 221 |
} |
| 222 |
|
| 223 |
#endif |