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