static SkImageFilter* make_serialized_image_filter() { SkAutoTUnref<SkImageFilter> filter(make_image_filter(false)); SkAutoTUnref<SkData> data(SkValidatingSerializeFlattenable(filter)); const unsigned char* ptr = static_cast<const unsigned char*>(data->data()); size_t len = data->size(); #ifdef SK_ADD_RANDOM_BIT_FLIPS unsigned char* p = const_cast<unsigned char*>(ptr); for (size_t i = 0; i < len; ++i, ++p) { if (R(250) == 1) { // 0.4% of the time, flip a bit or byte if (R(10) == 1) { // Then 10% of the time, change a whole byte switch(R(3)) { case 0: *p ^= 0xFF; // Flip entire byte break; case 1: *p = 0xFF; // Set all bits to 1 break; case 2: *p = 0x00; // Set all bits to 0 break; } } else { *p ^= (1 << R(8)); } } } #endif // SK_ADD_RANDOM_BIT_FLIPS SkFlattenable* flattenable = SkValidatingDeserializeFlattenable(ptr, len, SkImageFilter::GetFlattenableType()); return static_cast<SkImageFilter*>(flattenable); }
static sk_sp<SkImageFilter> make_serialized_image_filter() { sk_sp<SkImageFilter> filter(make_image_filter(false)); sk_sp<SkData> data(SkValidatingSerializeFlattenable(filter.get())); const unsigned char* ptr = static_cast<const unsigned char*>(data->data()); size_t len = data->size(); #ifdef SK_ADD_RANDOM_BIT_FLIPS unsigned char* p = const_cast<unsigned char*>(ptr); for (size_t i = 0; i < len; ++i, ++p) { uint8_t j; fuzz->nextRange(&j, 1, 250); if (j == 1) { // 0.4% of the time, flip a bit or byte uint8_t k; fuzz->nextRange(&k, 1, 10); if (k == 1) { // Then 10% of the time, change a whole byte uint8_t s; fuzz->nextRange(&s, 0, 2); switch(s) { case 0: *p ^= 0xFF; // Flip entire byte break; case 1: *p = 0xFF; // Set all bits to 1 break; case 2: *p = 0x00; // Set all bits to 0 break; } } else { uint8_t s; fuzz->nextRange(&s, 0, 7); *p ^= (1 << 7); } } } #endif // SK_ADD_RANDOM_BIT_FLIPS return SkValidatingDeserializeImageFilter(ptr, len); }