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.
next(bool * b)13 void Fuzz::next(bool* b) {
14   uint8_t n;
15   this->next(&n);
16   *b = (n & 1) == 1;
17 }
18 
next(SkImageFilter::CropRect * cropRect)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 
nextBytes(void * n,size_t size)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 
next(SkRegion * region)38 void Fuzz::next(SkRegion* region) {
39     // See FuzzCommon.h
40     FuzzNiceRegion(this, region, 10);
41 }
42 
nextRange(float * f,float min,float max)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 }
51