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