OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkDeflate.h" | 8 #include "SkDeflate.h" |
9 #include "SkRandom.h" | 9 #include "SkRandom.h" |
10 #include "Test.h" | 10 #include "Test.h" |
11 | 11 |
| 12 namespace { |
| 13 |
| 14 #ifdef ZLIB_INCLUDE |
| 15 #include ZLIB_INCLUDE |
| 16 #else |
| 17 #include "zlib.h" |
| 18 #endif |
| 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, |
| 30 * returning the result. Returns NULL if an error occurs. |
| 31 */ |
| 32 SkStreamAsset* stream_inflate(SkStream* src) { |
| 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; |
| 42 flateData.opaque = NULL; |
| 43 flateData.next_in = NULL; |
| 44 flateData.avail_in = 0; |
| 45 flateData.next_out = outputBuffer; |
| 46 flateData.avail_out = kBufferSize; |
| 47 int rc; |
| 48 rc = inflateInit(&flateData); |
| 49 if (rc != Z_OK) |
| 50 return nullptr; |
| 51 |
| 52 uint8_t* input = (uint8_t*)src->getMemoryBase(); |
| 53 size_t inputLength = src->getLength(); |
| 54 if (input == NULL || inputLength == 0) { |
| 55 input = NULL; |
| 56 flateData.next_in = inputBuffer; |
| 57 flateData.avail_in = 0; |
| 58 } else { |
| 59 flateData.next_in = input; |
| 60 flateData.avail_in = SkToUInt(inputLength); |
| 61 } |
| 62 |
| 63 rc = Z_OK; |
| 64 while (true) { |
| 65 if (flateData.avail_out < kBufferSize) { |
| 66 if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) { |
| 67 rc = Z_BUF_ERROR; |
| 68 break; |
| 69 } |
| 70 flateData.next_out = outputBuffer; |
| 71 flateData.avail_out = kBufferSize; |
| 72 } |
| 73 if (rc != Z_OK) |
| 74 break; |
| 75 if (flateData.avail_in == 0) { |
| 76 if (input != NULL) |
| 77 break; |
| 78 size_t read = src->read(&inputBuffer, kBufferSize); |
| 79 if (read == 0) |
| 80 break; |
| 81 flateData.next_in = inputBuffer; |
| 82 flateData.avail_in = SkToUInt(read); |
| 83 } |
| 84 rc = inflate(&flateData, Z_NO_FLUSH); |
| 85 } |
| 86 while (rc == Z_OK) { |
| 87 rc = inflate(&flateData, Z_FINISH); |
| 88 if (flateData.avail_out < kBufferSize) { |
| 89 if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) |
| 90 return nullptr; |
| 91 flateData.next_out = outputBuffer; |
| 92 flateData.avail_out = kBufferSize; |
| 93 } |
| 94 } |
| 95 |
| 96 inflateEnd(&flateData); |
| 97 if (rc != Z_STREAM_END) { |
| 98 return nullptr; |
| 99 } |
| 100 return decompressedDynamicMemoryWStream.detachAsStream(); |
| 101 } |
| 102 } // namespace |
| 103 |
12 DEF_TEST(SkDeflateWStream, r) { | 104 DEF_TEST(SkDeflateWStream, r) { |
13 SkRandom random(123456); | 105 SkRandom random(123456); |
14 for (int i = 0; i < 50; ++i) { | 106 for (int i = 0; i < 50; ++i) { |
15 uint32_t size = random.nextULessThan(10000); | 107 uint32_t size = random.nextULessThan(10000); |
16 SkAutoTMalloc<uint8_t> buffer(size); | 108 SkAutoTMalloc<uint8_t> buffer(size); |
17 for (uint32_t j = 0; j < size; ++j) { | 109 for (uint32_t j = 0; j < size; ++j) { |
18 buffer[j] = random.nextU() & 0xff; | 110 buffer[j] = random.nextU() & 0xff; |
19 } | 111 } |
20 | 112 |
21 SkDynamicMemoryWStream dynamicMemoryWStream; | 113 SkDynamicMemoryWStream dynamicMemoryWStream; |
22 { | 114 { |
23 SkDeflateWStream deflateWStream(&dynamicMemoryWStream); | 115 SkDeflateWStream deflateWStream(&dynamicMemoryWStream); |
24 uint32_t j = 0; | 116 uint32_t j = 0; |
25 while (j < size) { | 117 while (j < size) { |
26 uint32_t writeSize = | 118 uint32_t writeSize = |
27 SkTMin(size - j, random.nextRangeU(1, 400)); | 119 SkTMin(size - j, random.nextRangeU(1, 400)); |
28 if (!deflateWStream.write(&buffer[j], writeSize)) { | 120 if (!deflateWStream.write(&buffer[j], writeSize)) { |
29 ERRORF(r, "something went wrong."); | 121 ERRORF(r, "something went wrong."); |
30 return; | 122 return; |
31 } | 123 } |
32 j += writeSize; | 124 j += writeSize; |
33 } | 125 } |
34 } | 126 } |
35 SkAutoTDelete<SkStreamAsset> compressed( | 127 SkAutoTDelete<SkStreamAsset> compressed( |
36 dynamicMemoryWStream.detachAsStream()); | 128 dynamicMemoryWStream.detachAsStream()); |
37 | 129 SkAutoTDelete<SkStreamAsset> decompressed(stream_inflate(compressed)); |
38 SkDynamicMemoryWStream decompressedDynamicMemoryWStream; | |
39 SkAssertResult(SkFlate::Inflate(compressed, | |
40 &decompressedDynamicMemoryWStream)); | |
41 | |
42 SkAutoTDelete<SkStreamAsset> decompressed( | |
43 decompressedDynamicMemoryWStream.detachAsStream()); | |
44 | 130 |
45 if (decompressed->getLength() != size) { | 131 if (decompressed->getLength() != size) { |
46 ERRORF(r, "Decompression failed to get right size [%d]." | 132 ERRORF(r, "Decompression failed to get right size [%d]." |
47 " %u != %u", i, (unsigned)(decompressed->getLength()), | 133 " %u != %u", i, (unsigned)(decompressed->getLength()), |
48 (unsigned)size); | 134 (unsigned)size); |
49 SkString s = SkStringPrintf("/tmp/deftst_compressed_%d", i); | 135 SkString s = SkStringPrintf("/tmp/deftst_compressed_%d", i); |
50 SkFILEWStream o(s.c_str()); | 136 SkFILEWStream o(s.c_str()); |
51 o.writeStream(compressed.get(), compressed->getLength()); | 137 o.writeStream(compressed.get(), compressed->getLength()); |
52 compressed->rewind(); | 138 compressed->rewind(); |
53 | 139 |
54 s = SkStringPrintf("/tmp/deftst_input_%d", i); | 140 s = SkStringPrintf("/tmp/deftst_input_%d", i); |
55 SkFILEWStream o2(s.c_str()); | 141 SkFILEWStream o2(s.c_str()); |
56 o2.write(&buffer[0], size); | 142 o2.write(&buffer[0], size); |
57 | 143 |
58 continue; | 144 continue; |
59 } | 145 } |
60 uint32_t minLength = SkTMin(size, | 146 uint32_t minLength = SkTMin(size, |
61 (uint32_t)(decompressed->getLength())); | 147 (uint32_t)(decompressed->getLength())); |
62 for (uint32_t i = 0; i < minLength; ++i) { | 148 for (uint32_t i = 0; i < minLength; ++i) { |
63 uint8_t c; | 149 uint8_t c; |
64 SkDEBUGCODE(size_t rb =)decompressed->read(&c, sizeof(uint8_t)); | 150 SkDEBUGCODE(size_t rb =)decompressed->read(&c, sizeof(uint8_t)); |
65 SkASSERT(sizeof(uint8_t) == rb); | 151 SkASSERT(sizeof(uint8_t) == rb); |
66 if (buffer[i] != c) { | 152 if (buffer[i] != c) { |
67 ERRORF(r, "Decompression failed at byte %u.", (unsigned)i); | 153 ERRORF(r, "Decompression failed at byte %u.", (unsigned)i); |
68 break; | 154 break; |
69 } | 155 } |
70 } | 156 } |
71 } | 157 } |
72 } | 158 } |
OLD | NEW |