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 { | 12 namespace { |
13 | 13 |
14 #ifdef ZLIB_INCLUDE | 14 #ifdef ZLIB_INCLUDE |
15 #include ZLIB_INCLUDE | 15 #include ZLIB_INCLUDE |
16 #else | 16 #else |
17 #include "zlib.h" | 17 #include "zlib.h" |
18 #endif | 18 #endif |
19 | 19 |
20 // Different zlib implementations use different T. | 20 // Different zlib implementations use different T. |
21 // We've seen size_t and unsigned. | 21 // We've seen size_t and unsigned. |
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 NULL 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(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 = NULL; | 42 flateData.opaque = nullptr; |
43 flateData.next_in = NULL; | 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 return nullptr; | 50 return nullptr; |
51 | 51 |
52 uint8_t* input = (uint8_t*)src->getMemoryBase(); | 52 uint8_t* input = (uint8_t*)src->getMemoryBase(); |
53 size_t inputLength = src->getLength(); | 53 size_t inputLength = src->getLength(); |
54 if (input == NULL || inputLength == 0) { | 54 if (input == nullptr || inputLength == 0) { |
55 input = NULL; | 55 input = nullptr; |
56 flateData.next_in = inputBuffer; | 56 flateData.next_in = inputBuffer; |
57 flateData.avail_in = 0; | 57 flateData.avail_in = 0; |
58 } else { | 58 } else { |
59 flateData.next_in = input; | 59 flateData.next_in = input; |
60 flateData.avail_in = SkToUInt(inputLength); | 60 flateData.avail_in = SkToUInt(inputLength); |
61 } | 61 } |
62 | 62 |
63 rc = Z_OK; | 63 rc = Z_OK; |
64 while (true) { | 64 while (true) { |
65 if (flateData.avail_out < kBufferSize) { | 65 if (flateData.avail_out < kBufferSize) { |
66 if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) { | 66 if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) { |
67 rc = Z_BUF_ERROR; | 67 rc = Z_BUF_ERROR; |
68 break; | 68 break; |
69 } | 69 } |
70 flateData.next_out = outputBuffer; | 70 flateData.next_out = outputBuffer; |
71 flateData.avail_out = kBufferSize; | 71 flateData.avail_out = kBufferSize; |
72 } | 72 } |
73 if (rc != Z_OK) | 73 if (rc != Z_OK) |
74 break; | 74 break; |
75 if (flateData.avail_in == 0) { | 75 if (flateData.avail_in == 0) { |
76 if (input != NULL) | 76 if (input != nullptr) |
77 break; | 77 break; |
78 size_t read = src->read(&inputBuffer, kBufferSize); | 78 size_t read = src->read(&inputBuffer, kBufferSize); |
79 if (read == 0) | 79 if (read == 0) |
80 break; | 80 break; |
81 flateData.next_in = inputBuffer; | 81 flateData.next_in = inputBuffer; |
82 flateData.avail_in = SkToUInt(read); | 82 flateData.avail_in = SkToUInt(read); |
83 } | 83 } |
84 rc = inflate(&flateData, Z_NO_FLUSH); | 84 rc = inflate(&flateData, Z_NO_FLUSH); |
85 } | 85 } |
86 while (rc == Z_OK) { | 86 while (rc == Z_OK) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 uint8_t c; | 149 uint8_t c; |
150 SkDEBUGCODE(size_t rb =)decompressed->read(&c, sizeof(uint8_t)); | 150 SkDEBUGCODE(size_t rb =)decompressed->read(&c, sizeof(uint8_t)); |
151 SkASSERT(sizeof(uint8_t) == rb); | 151 SkASSERT(sizeof(uint8_t) == rb); |
152 if (buffer[i] != c) { | 152 if (buffer[i] != c) { |
153 ERRORF(r, "Decompression failed at byte %u.", (unsigned)i); | 153 ERRORF(r, "Decompression failed at byte %u.", (unsigned)i); |
154 break; | 154 break; |
155 } | 155 } |
156 } | 156 } |
157 } | 157 } |
158 } | 158 } |
OLD | NEW |