blob: 35a00bf85a237f65d530282383ece39ef49f49c5 [file] [log] [blame]
Kevin Lubickbc9a1a82018-09-17 14:46:57 -04001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8
9#include "Fuzz.h"
10#include "FuzzCommon.h"
11
12// UBSAN reminds us that bool can only legally hold 0 or 1.
13void Fuzz::next(bool* b) {
14 uint8_t n;
15 this->next(&n);
16 *b = (n & 1) == 1;
17}
18
19void Fuzz::next(SkImageFilter::CropRect* cropRect) {
20 SkRect rect;
21 uint8_t flags;
22 this->next(&rect);
23 this->nextRange(&flags, 0, 0xF);
24 *cropRect = SkImageFilter::CropRect(rect, flags);
25}
26
Hal Canary6d9a51a2018-11-19 13:15:21 -050027void Fuzz::nextBytes(void* n, size_t size) {
28 if ((fNextByte + size) > fBytes->size()) {
29 sk_bzero(n, size);
30 memcpy(n, fBytes->bytes() + fNextByte, fBytes->size() - fNextByte);
31 fNextByte = fBytes->size();
32 return;
33 }
34 memcpy(n, fBytes->bytes() + fNextByte, size);
35 fNextByte += size;
36}
37
Kevin Lubickbc9a1a82018-09-17 14:46:57 -040038void Fuzz::next(SkRegion* region) {
39 // See FuzzCommon.h
40 FuzzNiceRegion(this, region, 10);
41}
42
43void Fuzz::nextRange(float* f, float min, float max) {
44 this->next(f);
45 if (!std::isnormal(*f) && *f != 0.0f) {
46 // Don't deal with infinity or other strange floats.
47 *f = max;
48 }
49 *f = min + std::fmod(std::abs(*f), (max - min + 1));
50}