halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | |
| 10 | #ifdef SK_SUPPORT_PDF |
| 11 | |
halcanary | d9e5715 | 2015-08-12 11:24:40 -0700 | [diff] [blame] | 12 | #include "SkDeflate.h" |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 13 | #include "SkRandom.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 14 | #include "SkTo.h" |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 15 | |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 16 | namespace { |
| 17 | |
halcanary | 650e20d | 2016-08-25 09:07:02 -0700 | [diff] [blame] | 18 | #include "zlib.h" |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 19 | |
| 20 | // Different zlib implementations use different T. |
| 21 | // We've seen size_t and unsigned. |
| 22 | template <typename T> void* skia_alloc_func(void*, T items, T size) { |
| 23 | return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size)); |
| 24 | } |
| 25 | |
| 26 | void skia_free_func(void*, void* address) { sk_free(address); } |
| 27 | |
| 28 | /** |
| 29 | * Use the un-deflate compression algorithm to decompress the data in src, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 30 | * returning the result. Returns nullptr if an error occurs. |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 31 | */ |
Hal Canary | 0b83319 | 2017-03-14 14:35:02 -0400 | [diff] [blame] | 32 | std::unique_ptr<SkStreamAsset> stream_inflate(skiatest::Reporter* reporter, SkStream* src) { |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 33 | SkDynamicMemoryWStream decompressedDynamicMemoryWStream; |
| 34 | SkWStream* dst = &decompressedDynamicMemoryWStream; |
| 35 | |
| 36 | static const size_t kBufferSize = 1024; |
| 37 | uint8_t inputBuffer[kBufferSize]; |
| 38 | uint8_t outputBuffer[kBufferSize]; |
| 39 | z_stream flateData; |
| 40 | flateData.zalloc = &skia_alloc_func; |
| 41 | flateData.zfree = &skia_free_func; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | flateData.opaque = nullptr; |
| 43 | flateData.next_in = nullptr; |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 44 | flateData.avail_in = 0; |
| 45 | flateData.next_out = outputBuffer; |
| 46 | flateData.avail_out = kBufferSize; |
| 47 | int rc; |
| 48 | rc = inflateInit(&flateData); |
halcanary | d5722a4 | 2016-02-26 08:29:25 -0800 | [diff] [blame] | 49 | if (rc != Z_OK) { |
| 50 | ERRORF(reporter, "Zlib: inflateInit failed"); |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 51 | return nullptr; |
halcanary | d5722a4 | 2016-02-26 08:29:25 -0800 | [diff] [blame] | 52 | } |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 53 | uint8_t* input = (uint8_t*)src->getMemoryBase(); |
| 54 | size_t inputLength = src->getLength(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 55 | if (input == nullptr || inputLength == 0) { |
| 56 | input = nullptr; |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 57 | flateData.next_in = inputBuffer; |
| 58 | flateData.avail_in = 0; |
| 59 | } else { |
| 60 | flateData.next_in = input; |
| 61 | flateData.avail_in = SkToUInt(inputLength); |
| 62 | } |
| 63 | |
| 64 | rc = Z_OK; |
| 65 | while (true) { |
| 66 | if (flateData.avail_out < kBufferSize) { |
| 67 | if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) { |
| 68 | rc = Z_BUF_ERROR; |
| 69 | break; |
| 70 | } |
| 71 | flateData.next_out = outputBuffer; |
| 72 | flateData.avail_out = kBufferSize; |
| 73 | } |
| 74 | if (rc != Z_OK) |
| 75 | break; |
| 76 | if (flateData.avail_in == 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | if (input != nullptr) |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 78 | break; |
| 79 | size_t read = src->read(&inputBuffer, kBufferSize); |
| 80 | if (read == 0) |
| 81 | break; |
| 82 | flateData.next_in = inputBuffer; |
| 83 | flateData.avail_in = SkToUInt(read); |
| 84 | } |
| 85 | rc = inflate(&flateData, Z_NO_FLUSH); |
| 86 | } |
| 87 | while (rc == Z_OK) { |
| 88 | rc = inflate(&flateData, Z_FINISH); |
| 89 | if (flateData.avail_out < kBufferSize) { |
halcanary | d5722a4 | 2016-02-26 08:29:25 -0800 | [diff] [blame] | 90 | if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) { |
| 91 | ERRORF(reporter, "write failed"); |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 92 | return nullptr; |
halcanary | d5722a4 | 2016-02-26 08:29:25 -0800 | [diff] [blame] | 93 | } |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 94 | flateData.next_out = outputBuffer; |
| 95 | flateData.avail_out = kBufferSize; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | inflateEnd(&flateData); |
| 100 | if (rc != Z_STREAM_END) { |
halcanary | d5722a4 | 2016-02-26 08:29:25 -0800 | [diff] [blame] | 101 | ERRORF(reporter, "Zlib: inflateEnd failed"); |
halcanary | 48305e8 | 2015-08-18 13:30:25 -0700 | [diff] [blame] | 102 | return nullptr; |
| 103 | } |
| 104 | return decompressedDynamicMemoryWStream.detachAsStream(); |
| 105 | } |
| 106 | } // namespace |
| 107 | |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 108 | DEF_TEST(SkPDF_DeflateWStream, r) { |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 109 | SkRandom random(123456); |
| 110 | for (int i = 0; i < 50; ++i) { |
| 111 | uint32_t size = random.nextULessThan(10000); |
| 112 | SkAutoTMalloc<uint8_t> buffer(size); |
mtklein | e6cf9cb | 2015-02-26 13:25:05 -0800 | [diff] [blame] | 113 | for (uint32_t j = 0; j < size; ++j) { |
| 114 | buffer[j] = random.nextU() & 0xff; |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | SkDynamicMemoryWStream dynamicMemoryWStream; |
| 118 | { |
| 119 | SkDeflateWStream deflateWStream(&dynamicMemoryWStream); |
| 120 | uint32_t j = 0; |
| 121 | while (j < size) { |
| 122 | uint32_t writeSize = |
| 123 | SkTMin(size - j, random.nextRangeU(1, 400)); |
| 124 | if (!deflateWStream.write(&buffer[j], writeSize)) { |
| 125 | ERRORF(r, "something went wrong."); |
| 126 | return; |
| 127 | } |
| 128 | j += writeSize; |
| 129 | } |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 130 | REPORTER_ASSERT(r, deflateWStream.bytesWritten() == size); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 131 | } |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 132 | std::unique_ptr<SkStreamAsset> compressed(dynamicMemoryWStream.detachAsStream()); |
| 133 | std::unique_ptr<SkStreamAsset> decompressed(stream_inflate(r, compressed.get())); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 134 | |
halcanary | d5722a4 | 2016-02-26 08:29:25 -0800 | [diff] [blame] | 135 | if (!decompressed) { |
| 136 | ERRORF(r, "Decompression failed."); |
| 137 | return; |
| 138 | } |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 139 | if (decompressed->getLength() != size) { |
| 140 | ERRORF(r, "Decompression failed to get right size [%d]." |
| 141 | " %u != %u", i, (unsigned)(decompressed->getLength()), |
| 142 | (unsigned)size); |
| 143 | SkString s = SkStringPrintf("/tmp/deftst_compressed_%d", i); |
| 144 | SkFILEWStream o(s.c_str()); |
| 145 | o.writeStream(compressed.get(), compressed->getLength()); |
| 146 | compressed->rewind(); |
| 147 | |
| 148 | s = SkStringPrintf("/tmp/deftst_input_%d", i); |
| 149 | SkFILEWStream o2(s.c_str()); |
| 150 | o2.write(&buffer[0], size); |
| 151 | |
| 152 | continue; |
| 153 | } |
| 154 | uint32_t minLength = SkTMin(size, |
| 155 | (uint32_t)(decompressed->getLength())); |
| 156 | for (uint32_t i = 0; i < minLength; ++i) { |
| 157 | uint8_t c; |
| 158 | SkDEBUGCODE(size_t rb =)decompressed->read(&c, sizeof(uint8_t)); |
| 159 | SkASSERT(sizeof(uint8_t) == rb); |
| 160 | if (buffer[i] != c) { |
| 161 | ERRORF(r, "Decompression failed at byte %u.", (unsigned)i); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | } |
halcanary | 57f744e | 2016-09-09 11:41:59 -0700 | [diff] [blame] | 166 | SkDeflateWStream emptyDeflateWStream(nullptr); |
| 167 | REPORTER_ASSERT(r, !emptyDeflateWStream.writeText("FOO")); |
halcanary | 1b5c604 | 2015-02-18 11:29:56 -0800 | [diff] [blame] | 168 | } |
Hal Canary | 43fb7a0 | 2016-12-30 13:09:03 -0500 | [diff] [blame] | 169 | |
| 170 | #endif |