Kevin Lubick | bc9a1a8 | 2018-09-17 14:46:57 -0400 | [diff] [blame] | 1 | /* |
| 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. |
| 13 | void Fuzz::next(bool* b) { |
| 14 | uint8_t n; |
| 15 | this->next(&n); |
| 16 | *b = (n & 1) == 1; |
| 17 | } |
| 18 | |
| 19 | void 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 Canary | 6d9a51a | 2018-11-19 13:15:21 -0500 | [diff] [blame] | 27 | void 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 Lubick | bc9a1a8 | 2018-09-17 14:46:57 -0400 | [diff] [blame] | 38 | void Fuzz::next(SkRegion* region) { |
| 39 | // See FuzzCommon.h |
| 40 | FuzzNiceRegion(this, region, 10); |
| 41 | } |
| 42 | |
| 43 | void 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 | } |