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