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