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