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 "SkFlate.h" | 11 #include "SkFlate.h" |
12 #include "SkStream.h" | 12 #include "SkStream.h" |
13 | 13 |
14 #ifndef SK_NO_FLATE | 14 #ifndef SK_NO_FLATE |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 #ifdef ZLIB_INCLUDE | 18 #ifdef ZLIB_INCLUDE |
19 #include ZLIB_INCLUDE | 19 #include ZLIB_INCLUDE |
20 #else | 20 #else |
21 #include "zlib.h" | 21 #include "zlib.h" |
22 #endif | 22 #endif |
23 | 23 |
24 // static | 24 // static |
25 const size_t kBufferSize = 1024; | 25 const size_t kBufferSize = 1024; |
26 | 26 |
| 27 static void* skia_alloc_func(void*, size_t items, size_t size) { |
| 28 return sk_calloc_throw(items * size); |
| 29 } |
| 30 |
| 31 static void skia_free_func(void*, void* address) { sk_free(address); } |
| 32 |
27 bool doFlate(bool compress, SkStream* src, SkWStream* dst) { | 33 bool doFlate(bool compress, SkStream* src, SkWStream* dst) { |
28 uint8_t inputBuffer[kBufferSize]; | 34 uint8_t inputBuffer[kBufferSize]; |
29 uint8_t outputBuffer[kBufferSize]; | 35 uint8_t outputBuffer[kBufferSize]; |
30 z_stream flateData; | 36 z_stream flateData; |
31 flateData.zalloc = NULL; | 37 flateData.zalloc = &skia_alloc_func; |
32 flateData.zfree = NULL; | 38 flateData.zfree = &skia_free_func; |
| 39 flateData.opaque = NULL; |
33 flateData.next_in = NULL; | 40 flateData.next_in = NULL; |
34 flateData.avail_in = 0; | 41 flateData.avail_in = 0; |
35 flateData.next_out = outputBuffer; | 42 flateData.next_out = outputBuffer; |
36 flateData.avail_out = kBufferSize; | 43 flateData.avail_out = kBufferSize; |
37 int rc; | 44 int rc; |
38 if (compress) | 45 if (compress) |
39 rc = deflateInit(&flateData, Z_DEFAULT_COMPRESSION); | 46 rc = deflateInit(&flateData, Z_DEFAULT_COMPRESSION); |
40 else | 47 else |
41 rc = inflateInit(&flateData); | 48 rc = inflateInit(&flateData); |
42 if (rc != Z_OK) | 49 if (rc != Z_OK) |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 z_stream fZStream; | 170 z_stream fZStream; |
164 }; | 171 }; |
165 | 172 |
166 SkDeflateWStream::SkDeflateWStream(SkWStream* out) | 173 SkDeflateWStream::SkDeflateWStream(SkWStream* out) |
167 : fImpl(SkNEW(SkDeflateWStream::Impl)) { | 174 : fImpl(SkNEW(SkDeflateWStream::Impl)) { |
168 fImpl->fOut = out; | 175 fImpl->fOut = out; |
169 fImpl->fInBufferIndex = 0; | 176 fImpl->fInBufferIndex = 0; |
170 if (!fImpl->fOut) { | 177 if (!fImpl->fOut) { |
171 return; | 178 return; |
172 } | 179 } |
173 fImpl->fZStream.zalloc = Z_NULL; | 180 fImpl->fZStream.zalloc = &skia_alloc_func; |
174 fImpl->fZStream.zfree = Z_NULL; | 181 fImpl->fZStream.zfree = &skia_free_func; |
175 fImpl->fZStream.opaque = Z_NULL; | 182 fImpl->fZStream.opaque = NULL; |
176 SkDEBUGCODE(int r =) deflateInit(&fImpl->fZStream, Z_DEFAULT_COMPRESSION); | 183 SkDEBUGCODE(int r =) deflateInit(&fImpl->fZStream, Z_DEFAULT_COMPRESSION); |
177 SkASSERT(Z_OK == r); | 184 SkASSERT(Z_OK == r); |
178 } | 185 } |
179 | 186 |
180 SkDeflateWStream::~SkDeflateWStream() { this->finalize(); } | 187 SkDeflateWStream::~SkDeflateWStream() { this->finalize(); } |
181 | 188 |
182 void SkDeflateWStream::finalize() { | 189 void SkDeflateWStream::finalize() { |
183 if (!fImpl->fOut) { | 190 if (!fImpl->fOut) { |
184 return; | 191 return; |
185 } | 192 } |
(...skipping 27 matching lines...) Expand all Loading... |
213 return true; | 220 return true; |
214 } | 221 } |
215 | 222 |
216 size_t SkDeflateWStream::bytesWritten() const { | 223 size_t SkDeflateWStream::bytesWritten() const { |
217 return fImpl->fZStream.total_in + fImpl->fInBufferIndex; | 224 return fImpl->fZStream.total_in + fImpl->fInBufferIndex; |
218 } | 225 } |
219 | 226 |
220 | 227 |
221 #endif // SK_NO_FLATE | 228 #endif // SK_NO_FLATE |
222 | 229 |
OLD | NEW |