Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2010 The Android Open Source Project | 3 * Copyright 2010 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #include "SkData.h" | 10 #include "SkData.h" |
| 11 #include "SkDeflate.h" | 11 #include "SkDeflate.h" |
| 12 #include "SkStream.h" | 12 #include "SkStream.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 #ifdef ZLIB_INCLUDE | 16 #ifdef ZLIB_INCLUDE |
| 17 #include ZLIB_INCLUDE | 17 #include ZLIB_INCLUDE |
| 18 #else | 18 #else |
| 19 #include "zlib.h" | 19 #include "zlib.h" |
| 20 #endif | 20 #endif |
| 21 | 21 |
| 22 // static | 22 // static |
| 23 const size_t kBufferSize = 1024; | 23 const size_t kBufferSize = 1024; |
|
mtklein_C
2015/08/18 19:33:09
Make this and skia_alloc_func() actually static, t
hal.canary
2015/08/18 20:06:23
Acknowledged.
| |
| 24 | 24 |
| 25 // Different zlib implementations use different T. | 25 // Different zlib implementations use different T. |
| 26 // We've seen size_t and unsigned. | 26 // We've seen size_t and unsigned. |
| 27 template <typename T> void* skia_alloc_func(void*, T items, T size) { | 27 template <typename T> void* skia_alloc_func(void*, T items, T size) { |
| 28 return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size)); | 28 return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size)); |
| 29 } | 29 } |
| 30 | 30 |
| 31 static void skia_free_func(void*, void* address) { sk_free(address); } | 31 static void skia_free_func(void*, void* address) { sk_free(address); } |
| 32 | 32 |
| 33 bool doFlate(bool compress, SkStream* src, SkWStream* dst) { | 33 static bool do_inflate(SkStream* src, SkWStream* dst) { |
| 34 uint8_t inputBuffer[kBufferSize]; | 34 uint8_t inputBuffer[kBufferSize]; |
| 35 uint8_t outputBuffer[kBufferSize]; | 35 uint8_t outputBuffer[kBufferSize]; |
| 36 z_stream flateData; | 36 z_stream flateData; |
| 37 flateData.zalloc = &skia_alloc_func; | 37 flateData.zalloc = &skia_alloc_func; |
| 38 flateData.zfree = &skia_free_func; | 38 flateData.zfree = &skia_free_func; |
| 39 flateData.opaque = NULL; | 39 flateData.opaque = NULL; |
| 40 flateData.next_in = NULL; | 40 flateData.next_in = NULL; |
| 41 flateData.avail_in = 0; | 41 flateData.avail_in = 0; |
| 42 flateData.next_out = outputBuffer; | 42 flateData.next_out = outputBuffer; |
| 43 flateData.avail_out = kBufferSize; | 43 flateData.avail_out = kBufferSize; |
| 44 int rc; | 44 int rc; |
| 45 if (compress) | 45 rc = inflateInit(&flateData); |
| 46 rc = deflateInit(&flateData, Z_DEFAULT_COMPRESSION); | |
| 47 else | |
| 48 rc = inflateInit(&flateData); | |
| 49 if (rc != Z_OK) | 46 if (rc != Z_OK) |
| 50 return false; | 47 return false; |
| 51 | 48 |
| 52 uint8_t* input = (uint8_t*)src->getMemoryBase(); | 49 uint8_t* input = (uint8_t*)src->getMemoryBase(); |
| 53 size_t inputLength = src->getLength(); | 50 size_t inputLength = src->getLength(); |
| 54 if (input == NULL || inputLength == 0) { | 51 if (input == NULL || inputLength == 0) { |
| 55 input = NULL; | 52 input = NULL; |
| 56 flateData.next_in = inputBuffer; | 53 flateData.next_in = inputBuffer; |
| 57 flateData.avail_in = 0; | 54 flateData.avail_in = 0; |
| 58 } else { | 55 } else { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 74 break; | 71 break; |
| 75 if (flateData.avail_in == 0) { | 72 if (flateData.avail_in == 0) { |
| 76 if (input != NULL) | 73 if (input != NULL) |
| 77 break; | 74 break; |
| 78 size_t read = src->read(&inputBuffer, kBufferSize); | 75 size_t read = src->read(&inputBuffer, kBufferSize); |
| 79 if (read == 0) | 76 if (read == 0) |
| 80 break; | 77 break; |
| 81 flateData.next_in = inputBuffer; | 78 flateData.next_in = inputBuffer; |
| 82 flateData.avail_in = SkToUInt(read); | 79 flateData.avail_in = SkToUInt(read); |
| 83 } | 80 } |
| 84 if (compress) | 81 rc = inflate(&flateData, Z_NO_FLUSH); |
| 85 rc = deflate(&flateData, Z_NO_FLUSH); | |
| 86 else | |
| 87 rc = inflate(&flateData, Z_NO_FLUSH); | |
| 88 } | 82 } |
| 89 while (rc == Z_OK) { | 83 while (rc == Z_OK) { |
| 90 if (compress) | 84 rc = inflate(&flateData, Z_FINISH); |
| 91 rc = deflate(&flateData, Z_FINISH); | |
| 92 else | |
| 93 rc = inflate(&flateData, Z_FINISH); | |
| 94 if (flateData.avail_out < kBufferSize) { | 85 if (flateData.avail_out < kBufferSize) { |
| 95 if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) | 86 if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) |
| 96 return false; | 87 return false; |
| 97 flateData.next_out = outputBuffer; | 88 flateData.next_out = outputBuffer; |
| 98 flateData.avail_out = kBufferSize; | 89 flateData.avail_out = kBufferSize; |
| 99 } | 90 } |
| 100 } | 91 } |
| 101 | 92 |
| 102 if (compress) | 93 inflateEnd(&flateData); |
| 103 deflateEnd(&flateData); | 94 return rc == Z_STREAM_END; |
| 104 else | |
| 105 inflateEnd(&flateData); | |
| 106 if (rc == Z_STREAM_END) | |
| 107 return true; | |
| 108 return false; | |
| 109 } | 95 } |
| 96 } // namespace | |
| 110 | 97 |
| 98 SkStreamAsset* SkInflate(SkStream* src) { | |
| 99 SkDynamicMemoryWStream decompressedDynamicMemoryWStream; | |
| 100 if (!do_inflate(src, &decompressedDynamicMemoryWStream)) { | |
| 101 return nullptr; | |
| 102 } | |
| 103 return decompressedDynamicMemoryWStream.detachAsStream(); | |
| 111 } | 104 } |
| 112 | 105 |
| 113 // static | |
| 114 bool SkFlate::Deflate(SkStream* src, SkWStream* dst) { | |
| 115 return doFlate(true, src, dst); | |
| 116 } | |
| 117 | |
| 118 bool SkFlate::Deflate(const void* ptr, size_t len, SkWStream* dst) { | |
| 119 SkMemoryStream stream(ptr, len); | |
| 120 return doFlate(true, &stream, dst); | |
| 121 } | |
| 122 | |
| 123 bool SkFlate::Deflate(const SkData* data, SkWStream* dst) { | |
| 124 if (data) { | |
| 125 SkMemoryStream stream(data->data(), data->size()); | |
| 126 return doFlate(true, &stream, dst); | |
| 127 } | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 // static | |
| 132 bool SkFlate::Inflate(SkStream* src, SkWStream* dst) { | |
| 133 return doFlate(false, src, dst); | |
| 134 } | |
| 135 | |
| 136 | |
| 137 #define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096 | 106 #define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096 |
| 138 #define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big | 107 #define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big |
| 139 // enough to always do a | 108 // enough to always do a |
| 140 // single loop. | 109 // single loop. |
| 141 | 110 |
| 142 // called by both write() and finalize() | 111 // called by both write() and finalize() |
| 143 static void do_deflate(int flush, | 112 static void do_deflate(int flush, |
| 144 z_stream* zStream, | 113 z_stream* zStream, |
| 145 SkWStream* out, | 114 SkWStream* out, |
| 146 unsigned char* inBuffer, | 115 unsigned char* inBuffer, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 fImpl->fInBuffer, fImpl->fInBufferIndex); | 185 fImpl->fInBuffer, fImpl->fInBufferIndex); |
| 217 fImpl->fInBufferIndex = 0; | 186 fImpl->fInBufferIndex = 0; |
| 218 } | 187 } |
| 219 } | 188 } |
| 220 return true; | 189 return true; |
| 221 } | 190 } |
| 222 | 191 |
| 223 size_t SkDeflateWStream::bytesWritten() const { | 192 size_t SkDeflateWStream::bytesWritten() const { |
| 224 return fImpl->fZStream.total_in + fImpl->fInBufferIndex; | 193 return fImpl->fZStream.total_in + fImpl->fInBufferIndex; |
| 225 } | 194 } |
| OLD | NEW |