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