kjlubick | e565450 | 2016-07-19 16:50:03 -0700 | [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 | #include "Fuzz.h" |
| 9 | #include "SkPath.h" |
| 10 | #include "SkPathOps.h" |
| 11 | |
| 12 | const int kLastOp = SkPathOp::kReverseDifference_SkPathOp; |
| 13 | |
| 14 | void BuildPath(Fuzz* fuzz, |
| 15 | SkPath* path, |
| 16 | int last_verb) { |
| 17 | uint8_t operation; |
| 18 | SkScalar a, b, c, d, e, f; |
| 19 | while (fuzz->next<uint8_t>(&operation)) { |
| 20 | |
| 21 | switch (operation % (last_verb + 1)) { |
| 22 | case SkPath::Verb::kMove_Verb: |
| 23 | if (!fuzz->next<SkScalar>(&a) || !fuzz->next<SkScalar>(&b)) |
| 24 | return; |
| 25 | path->moveTo(a, b); |
| 26 | break; |
| 27 | |
| 28 | case SkPath::Verb::kLine_Verb: |
| 29 | if (!fuzz->next<SkScalar>(&a) || !fuzz->next<SkScalar>(&b)) |
| 30 | return; |
| 31 | path->lineTo(a, b); |
| 32 | break; |
| 33 | |
| 34 | case SkPath::Verb::kQuad_Verb: |
| 35 | if (!fuzz->next<SkScalar>(&a) || |
| 36 | !fuzz->next<SkScalar>(&b) || |
| 37 | !fuzz->next<SkScalar>(&c) || |
| 38 | !fuzz->next<SkScalar>(&d)) |
| 39 | return; |
| 40 | path->quadTo(a, b, c, d); |
| 41 | break; |
| 42 | |
| 43 | case SkPath::Verb::kConic_Verb: |
| 44 | if (!fuzz->next<SkScalar>(&a) || |
| 45 | !fuzz->next<SkScalar>(&b) || |
| 46 | !fuzz->next<SkScalar>(&c) || |
| 47 | !fuzz->next<SkScalar>(&d) || |
| 48 | !fuzz->next<SkScalar>(&e)) |
| 49 | return; |
| 50 | path->conicTo(a, b, c, d, e); |
| 51 | break; |
| 52 | |
| 53 | case SkPath::Verb::kCubic_Verb: |
| 54 | if (!fuzz->next<SkScalar>(&a) || |
| 55 | !fuzz->next<SkScalar>(&b) || |
| 56 | !fuzz->next<SkScalar>(&c) || |
| 57 | !fuzz->next<SkScalar>(&d) || |
| 58 | !fuzz->next<SkScalar>(&e) || |
| 59 | !fuzz->next<SkScalar>(&f)) |
| 60 | return; |
| 61 | path->cubicTo(a, b, c, d, e, f); |
| 62 | break; |
| 63 | |
| 64 | case SkPath::Verb::kClose_Verb: |
| 65 | path->close(); |
| 66 | break; |
| 67 | |
| 68 | case SkPath::Verb::kDone_Verb: |
| 69 | // In this case, simply exit. |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | DEF_FUZZ(Pathop, fuzz) { |
| 76 | SkOpBuilder builder; |
| 77 | while (fuzz->remaining() >= sizeof(uint8_t)) { |
| 78 | SkPath path; |
| 79 | uint8_t op = fuzz->nextB(); |
| 80 | |
| 81 | BuildPath(fuzz, &path, SkPath::Verb::kDone_Verb); |
| 82 | builder.add(path, static_cast<SkPathOp>(op % (kLastOp + 1))); |
| 83 | } |
| 84 | |
| 85 | SkPath result; |
| 86 | builder.resolve(&result); |
| 87 | } |