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