| 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 | |
| 23 const size_t kBufferSize = 1024; | |
| 24 | |
| 25 // Different zlib implementations use different T. | 22 // Different zlib implementations use different T. |
| 26 // We've seen size_t and unsigned. | 23 // We've seen size_t and unsigned. |
| 27 template <typename T> void* skia_alloc_func(void*, T items, T size) { | 24 template <typename T> void* skia_alloc_func(void*, T items, T size) { |
| 28 return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size)); | 25 return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size)); |
| 29 } | 26 } |
| 30 | 27 |
| 31 static void skia_free_func(void*, void* address) { sk_free(address); } | 28 void skia_free_func(void*, void* address) { sk_free(address); } |
| 32 | 29 |
| 33 bool doFlate(bool compress, SkStream* src, SkWStream* dst) { | 30 } // namespace |
| 34 uint8_t inputBuffer[kBufferSize]; | |
| 35 uint8_t outputBuffer[kBufferSize]; | |
| 36 z_stream flateData; | |
| 37 flateData.zalloc = &skia_alloc_func; | |
| 38 flateData.zfree = &skia_free_func; | |
| 39 flateData.opaque = NULL; | |
| 40 flateData.next_in = NULL; | |
| 41 flateData.avail_in = 0; | |
| 42 flateData.next_out = outputBuffer; | |
| 43 flateData.avail_out = kBufferSize; | |
| 44 int rc; | |
| 45 if (compress) | |
| 46 rc = deflateInit(&flateData, Z_DEFAULT_COMPRESSION); | |
| 47 else | |
| 48 rc = inflateInit(&flateData); | |
| 49 if (rc != Z_OK) | |
| 50 return false; | |
| 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 if (compress) | |
| 85 rc = deflate(&flateData, Z_NO_FLUSH); | |
| 86 else | |
| 87 rc = inflate(&flateData, Z_NO_FLUSH); | |
| 88 } | |
| 89 while (rc == Z_OK) { | |
| 90 if (compress) | |
| 91 rc = deflate(&flateData, Z_FINISH); | |
| 92 else | |
| 93 rc = inflate(&flateData, Z_FINISH); | |
| 94 if (flateData.avail_out < kBufferSize) { | |
| 95 if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) | |
| 96 return false; | |
| 97 flateData.next_out = outputBuffer; | |
| 98 flateData.avail_out = kBufferSize; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 if (compress) | |
| 103 deflateEnd(&flateData); | |
| 104 else | |
| 105 inflateEnd(&flateData); | |
| 106 if (rc == Z_STREAM_END) | |
| 107 return true; | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 } | |
| 112 | |
| 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 | 31 |
| 137 #define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096 | 32 #define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096 |
| 138 #define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big | 33 #define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big |
| 139 // enough to always do a | 34 // enough to always do a |
| 140 // single loop. | 35 // single loop. |
| 141 | 36 |
| 142 // called by both write() and finalize() | 37 // called by both write() and finalize() |
| 143 static void do_deflate(int flush, | 38 static void do_deflate(int flush, |
| 144 z_stream* zStream, | 39 z_stream* zStream, |
| 145 SkWStream* out, | 40 SkWStream* out, |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 fImpl->fInBuffer, fImpl->fInBufferIndex); | 111 fImpl->fInBuffer, fImpl->fInBufferIndex); |
| 217 fImpl->fInBufferIndex = 0; | 112 fImpl->fInBufferIndex = 0; |
| 218 } | 113 } |
| 219 } | 114 } |
| 220 return true; | 115 return true; |
| 221 } | 116 } |
| 222 | 117 |
| 223 size_t SkDeflateWStream::bytesWritten() const { | 118 size_t SkDeflateWStream::bytesWritten() const { |
| 224 return fImpl->fZStream.total_in + fImpl->fInBufferIndex; | 119 return fImpl->fZStream.total_in + fImpl->fInBufferIndex; |
| 225 } | 120 } |
| OLD | NEW |