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