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" |
(...skipping 11 matching lines...) Expand all Loading... |
22 template <typename T> void* skia_alloc_func(void*, T items, T size) { | 22 template <typename T> void* skia_alloc_func(void*, T items, T size) { |
23 return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size)); | 23 return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size)); |
24 } | 24 } |
25 | 25 |
26 void skia_free_func(void*, void* address) { sk_free(address); } | 26 void skia_free_func(void*, void* address) { sk_free(address); } |
27 | 27 |
28 /** | 28 /** |
29 * Use the un-deflate compression algorithm to decompress the data in src, | 29 * Use the un-deflate compression algorithm to decompress the data in src, |
30 * returning the result. Returns nullptr if an error occurs. | 30 * returning the result. Returns nullptr if an error occurs. |
31 */ | 31 */ |
32 SkStreamAsset* stream_inflate(SkStream* src) { | 32 SkStreamAsset* stream_inflate(skiatest::Reporter* reporter, SkStream* src) { |
33 SkDynamicMemoryWStream decompressedDynamicMemoryWStream; | 33 SkDynamicMemoryWStream decompressedDynamicMemoryWStream; |
34 SkWStream* dst = &decompressedDynamicMemoryWStream; | 34 SkWStream* dst = &decompressedDynamicMemoryWStream; |
35 | 35 |
36 static const size_t kBufferSize = 1024; | 36 static const size_t kBufferSize = 1024; |
37 uint8_t inputBuffer[kBufferSize]; | 37 uint8_t inputBuffer[kBufferSize]; |
38 uint8_t outputBuffer[kBufferSize]; | 38 uint8_t outputBuffer[kBufferSize]; |
39 z_stream flateData; | 39 z_stream flateData; |
40 flateData.zalloc = &skia_alloc_func; | 40 flateData.zalloc = &skia_alloc_func; |
41 flateData.zfree = &skia_free_func; | 41 flateData.zfree = &skia_free_func; |
42 flateData.opaque = nullptr; | 42 flateData.opaque = nullptr; |
43 flateData.next_in = nullptr; | 43 flateData.next_in = nullptr; |
44 flateData.avail_in = 0; | 44 flateData.avail_in = 0; |
45 flateData.next_out = outputBuffer; | 45 flateData.next_out = outputBuffer; |
46 flateData.avail_out = kBufferSize; | 46 flateData.avail_out = kBufferSize; |
47 int rc; | 47 int rc; |
48 rc = inflateInit(&flateData); | 48 rc = inflateInit(&flateData); |
49 if (rc != Z_OK) | 49 if (rc != Z_OK) { |
| 50 ERRORF(reporter, "Zlib: inflateInit failed"); |
50 return nullptr; | 51 return nullptr; |
51 | 52 } |
52 uint8_t* input = (uint8_t*)src->getMemoryBase(); | 53 uint8_t* input = (uint8_t*)src->getMemoryBase(); |
53 size_t inputLength = src->getLength(); | 54 size_t inputLength = src->getLength(); |
54 if (input == nullptr || inputLength == 0) { | 55 if (input == nullptr || inputLength == 0) { |
55 input = nullptr; | 56 input = nullptr; |
56 flateData.next_in = inputBuffer; | 57 flateData.next_in = inputBuffer; |
57 flateData.avail_in = 0; | 58 flateData.avail_in = 0; |
58 } else { | 59 } else { |
59 flateData.next_in = input; | 60 flateData.next_in = input; |
60 flateData.avail_in = SkToUInt(inputLength); | 61 flateData.avail_in = SkToUInt(inputLength); |
61 } | 62 } |
(...skipping 17 matching lines...) Expand all Loading... |
79 if (read == 0) | 80 if (read == 0) |
80 break; | 81 break; |
81 flateData.next_in = inputBuffer; | 82 flateData.next_in = inputBuffer; |
82 flateData.avail_in = SkToUInt(read); | 83 flateData.avail_in = SkToUInt(read); |
83 } | 84 } |
84 rc = inflate(&flateData, Z_NO_FLUSH); | 85 rc = inflate(&flateData, Z_NO_FLUSH); |
85 } | 86 } |
86 while (rc == Z_OK) { | 87 while (rc == Z_OK) { |
87 rc = inflate(&flateData, Z_FINISH); | 88 rc = inflate(&flateData, Z_FINISH); |
88 if (flateData.avail_out < kBufferSize) { | 89 if (flateData.avail_out < kBufferSize) { |
89 if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) | 90 if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) { |
| 91 ERRORF(reporter, "write failed"); |
90 return nullptr; | 92 return nullptr; |
| 93 } |
91 flateData.next_out = outputBuffer; | 94 flateData.next_out = outputBuffer; |
92 flateData.avail_out = kBufferSize; | 95 flateData.avail_out = kBufferSize; |
93 } | 96 } |
94 } | 97 } |
95 | 98 |
96 inflateEnd(&flateData); | 99 inflateEnd(&flateData); |
97 if (rc != Z_STREAM_END) { | 100 if (rc != Z_STREAM_END) { |
| 101 ERRORF(reporter, "Zlib: inflateEnd failed"); |
98 return nullptr; | 102 return nullptr; |
99 } | 103 } |
100 return decompressedDynamicMemoryWStream.detachAsStream(); | 104 return decompressedDynamicMemoryWStream.detachAsStream(); |
101 } | 105 } |
102 } // namespace | 106 } // namespace |
103 | 107 |
104 DEF_TEST(SkDeflateWStream, r) { | 108 DEF_TEST(SkDeflateWStream, r) { |
105 SkRandom random(123456); | 109 SkRandom random(123456); |
106 for (int i = 0; i < 50; ++i) { | 110 for (int i = 0; i < 50; ++i) { |
107 uint32_t size = random.nextULessThan(10000); | 111 uint32_t size = random.nextULessThan(10000); |
(...skipping 11 matching lines...) Expand all Loading... |
119 SkTMin(size - j, random.nextRangeU(1, 400)); | 123 SkTMin(size - j, random.nextRangeU(1, 400)); |
120 if (!deflateWStream.write(&buffer[j], writeSize)) { | 124 if (!deflateWStream.write(&buffer[j], writeSize)) { |
121 ERRORF(r, "something went wrong."); | 125 ERRORF(r, "something went wrong."); |
122 return; | 126 return; |
123 } | 127 } |
124 j += writeSize; | 128 j += writeSize; |
125 } | 129 } |
126 } | 130 } |
127 SkAutoTDelete<SkStreamAsset> compressed( | 131 SkAutoTDelete<SkStreamAsset> compressed( |
128 dynamicMemoryWStream.detachAsStream()); | 132 dynamicMemoryWStream.detachAsStream()); |
129 SkAutoTDelete<SkStreamAsset> decompressed(stream_inflate(compressed)); | 133 SkAutoTDelete<SkStreamAsset> decompressed(stream_inflate(r, compressed))
; |
130 | 134 |
| 135 if (!decompressed) { |
| 136 ERRORF(r, "Decompression failed."); |
| 137 return; |
| 138 } |
131 if (decompressed->getLength() != size) { | 139 if (decompressed->getLength() != size) { |
132 ERRORF(r, "Decompression failed to get right size [%d]." | 140 ERRORF(r, "Decompression failed to get right size [%d]." |
133 " %u != %u", i, (unsigned)(decompressed->getLength()), | 141 " %u != %u", i, (unsigned)(decompressed->getLength()), |
134 (unsigned)size); | 142 (unsigned)size); |
135 SkString s = SkStringPrintf("/tmp/deftst_compressed_%d", i); | 143 SkString s = SkStringPrintf("/tmp/deftst_compressed_%d", i); |
136 SkFILEWStream o(s.c_str()); | 144 SkFILEWStream o(s.c_str()); |
137 o.writeStream(compressed.get(), compressed->getLength()); | 145 o.writeStream(compressed.get(), compressed->getLength()); |
138 compressed->rewind(); | 146 compressed->rewind(); |
139 | 147 |
140 s = SkStringPrintf("/tmp/deftst_input_%d", i); | 148 s = SkStringPrintf("/tmp/deftst_input_%d", i); |
141 SkFILEWStream o2(s.c_str()); | 149 SkFILEWStream o2(s.c_str()); |
142 o2.write(&buffer[0], size); | 150 o2.write(&buffer[0], size); |
143 | 151 |
144 continue; | 152 continue; |
145 } | 153 } |
146 uint32_t minLength = SkTMin(size, | 154 uint32_t minLength = SkTMin(size, |
147 (uint32_t)(decompressed->getLength())); | 155 (uint32_t)(decompressed->getLength())); |
148 for (uint32_t i = 0; i < minLength; ++i) { | 156 for (uint32_t i = 0; i < minLength; ++i) { |
149 uint8_t c; | 157 uint8_t c; |
150 SkDEBUGCODE(size_t rb =)decompressed->read(&c, sizeof(uint8_t)); | 158 SkDEBUGCODE(size_t rb =)decompressed->read(&c, sizeof(uint8_t)); |
151 SkASSERT(sizeof(uint8_t) == rb); | 159 SkASSERT(sizeof(uint8_t) == rb); |
152 if (buffer[i] != c) { | 160 if (buffer[i] != c) { |
153 ERRORF(r, "Decompression failed at byte %u.", (unsigned)i); | 161 ERRORF(r, "Decompression failed at byte %u.", (unsigned)i); |
154 break; | 162 break; |
155 } | 163 } |
156 } | 164 } |
157 } | 165 } |
158 } | 166 } |
OLD | NEW |