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