1 /*
2 * Copyright 2010 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8
9 #include "SkData.h"
10 #include "SkDeflate.h"
11 #include "SkMakeUnique.h"
12 #include "SkMalloc.h"
13 #include "SkTraceEvent.h"
14
15 #include "zlib.h"
16
17 namespace {
18
19 // Different zlib implementations use different T.
20 // We've seen size_t and unsigned.
skia_alloc_func(void *,T items,T size)21 template <typename T> void* skia_alloc_func(void*, T items, T size) {
22 return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size));
23 }
24
skia_free_func(void *,void * address)25 void skia_free_func(void*, void* address) { sk_free(address); }
26
27 } // namespace
28
29 #define SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE 4096
30 #define SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE 4224 // 4096 + 128, usually big
31 // enough to always do a
32 // single loop.
33
34 // called by both write() and finalize()
do_deflate(int flush,z_stream * zStream,SkWStream * out,unsigned char * inBuffer,size_t inBufferSize)35 static void do_deflate(int flush,
36 z_stream* zStream,
37 SkWStream* out,
38 unsigned char* inBuffer,
39 size_t inBufferSize) {
40 zStream->next_in = inBuffer;
41 zStream->avail_in = SkToInt(inBufferSize);
42 unsigned char outBuffer[SKDEFLATEWSTREAM_OUTPUT_BUFFER_SIZE];
43 SkDEBUGCODE(int returnValue;)
44 do {
45 zStream->next_out = outBuffer;
46 zStream->avail_out = sizeof(outBuffer);
47 SkDEBUGCODE(returnValue =) deflate(zStream, flush);
48 SkASSERT(!zStream->msg);
49
50 out->write(outBuffer, sizeof(outBuffer) - zStream->avail_out);
51 } while (zStream->avail_in || !zStream->avail_out);
52 SkASSERT(flush == Z_FINISH
53 ? returnValue == Z_STREAM_END
54 : returnValue == Z_OK);
55 }
56
57 // Hide all zlib impl details.
58 struct SkDeflateWStream::Impl {
59 SkWStream* fOut;
60 unsigned char fInBuffer[SKDEFLATEWSTREAM_INPUT_BUFFER_SIZE];
61 size_t fInBufferIndex;
62 z_stream fZStream;
63 };
64
SkDeflateWStream(SkWStream * out,int compressionLevel,bool gzip)65 SkDeflateWStream::SkDeflateWStream(SkWStream* out,
66 int compressionLevel,
67 bool gzip)
68 : fImpl(skstd::make_unique<SkDeflateWStream::Impl>()) {
69 fImpl->fOut = out;
70 fImpl->fInBufferIndex = 0;
71 if (!fImpl->fOut) {
72 return;
73 }
74 fImpl->fZStream.next_in = nullptr;
75 fImpl->fZStream.zalloc = &skia_alloc_func;
76 fImpl->fZStream.zfree = &skia_free_func;
77 fImpl->fZStream.opaque = nullptr;
78 SkASSERT(compressionLevel <= 9 && compressionLevel >= -1);
79 SkDEBUGCODE(int r =) deflateInit2(&fImpl->fZStream, compressionLevel,
80 Z_DEFLATED, gzip ? 0x1F : 0x0F,
81 8, Z_DEFAULT_STRATEGY);
82 SkASSERT(Z_OK == r);
83 }
84
~SkDeflateWStream()85 SkDeflateWStream::~SkDeflateWStream() { this->finalize(); }
86
finalize()87 void SkDeflateWStream::finalize() {
88 TRACE_EVENT0("skia", TRACE_FUNC);
89 if (!fImpl->fOut) {
90 return;
91 }
92 do_deflate(Z_FINISH, &fImpl->fZStream, fImpl->fOut, fImpl->fInBuffer,
93 fImpl->fInBufferIndex);
94 (void)deflateEnd(&fImpl->fZStream);
95 fImpl->fOut = nullptr;
96 }
97
write(const void * void_buffer,size_t len)98 bool SkDeflateWStream::write(const void* void_buffer, size_t len) {
99 TRACE_EVENT0("skia", TRACE_FUNC);
100 if (!fImpl->fOut) {
101 return false;
102 }
103 const char* buffer = (const char*)void_buffer;
104 while (len > 0) {
105 size_t tocopy =
106 SkTMin(len, sizeof(fImpl->fInBuffer) - fImpl->fInBufferIndex);
107 memcpy(fImpl->fInBuffer + fImpl->fInBufferIndex, buffer, tocopy);
108 len -= tocopy;
109 buffer += tocopy;
110 fImpl->fInBufferIndex += tocopy;
111 SkASSERT(fImpl->fInBufferIndex <= sizeof(fImpl->fInBuffer));
112
113 // if the buffer isn't filled, don't call into zlib yet.
114 if (sizeof(fImpl->fInBuffer) == fImpl->fInBufferIndex) {
115 do_deflate(Z_NO_FLUSH, &fImpl->fZStream, fImpl->fOut,
116 fImpl->fInBuffer, fImpl->fInBufferIndex);
117 fImpl->fInBufferIndex = 0;
118 }
119 }
120 return true;
121 }
122
bytesWritten() const123 size_t SkDeflateWStream::bytesWritten() const {
124 return fImpl->fZStream.total_in + fImpl->fInBufferIndex;
125 }
126