epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 3 | * Copyright 2010 The Android Open Source Project |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 9 | |
reed@google.com | 314e9b3 | 2011-06-27 17:41:22 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 11 | #include "SkFlate.h" |
| 12 | #include "SkStream.h" |
| 13 | |
| 14 | #ifndef SK_ZLIB_INCLUDE |
| 15 | bool SkFlate::HaveFlate() { return false; } |
reed@google.com | 7453d0e | 2011-06-24 20:45:40 +0000 | [diff] [blame] | 16 | bool SkFlate::Deflate(SkStream*, SkWStream*) { return false; } |
epoger@google.com | b14ef73 | 2011-07-21 13:56:34 +0000 | [diff] [blame] | 17 | bool SkFlate::Deflate(const void*, size_t, SkWStream*) { return false; } |
| 18 | bool SkFlate::Deflate(const SkData*, SkWStream*) { return false; } |
reed@google.com | 7453d0e | 2011-06-24 20:45:40 +0000 | [diff] [blame] | 19 | bool SkFlate::Inflate(SkStream*, SkWStream*) { return false; } |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 20 | #else |
| 21 | |
| 22 | // static |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 23 | bool SkFlate::HaveFlate() { |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | #include SK_ZLIB_INCLUDE |
| 30 | |
vandebo@chromium.org | c7a38f3 | 2011-04-05 20:11:32 +0000 | [diff] [blame] | 31 | // static |
| 32 | const size_t kBufferSize = 1024; |
| 33 | |
reed@google.com | 7453d0e | 2011-06-24 20:45:40 +0000 | [diff] [blame] | 34 | bool doFlate(bool compress, SkStream* src, SkWStream* dst) { |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 35 | uint8_t inputBuffer[kBufferSize]; |
| 36 | uint8_t outputBuffer[kBufferSize]; |
| 37 | z_stream flateData; |
| 38 | flateData.zalloc = NULL; |
| 39 | flateData.zfree = NULL; |
| 40 | flateData.next_in = NULL; |
| 41 | flateData.avail_in = 0; |
| 42 | flateData.next_out = outputBuffer; |
| 43 | flateData.avail_out = kBufferSize; |
| 44 | int rc; |
| 45 | if (compress) |
| 46 | rc = deflateInit(&flateData, Z_DEFAULT_COMPRESSION); |
| 47 | else |
| 48 | rc = inflateInit(&flateData); |
| 49 | if (rc != Z_OK) |
| 50 | return false; |
| 51 | |
| 52 | uint8_t* input = (uint8_t*)src->getMemoryBase(); |
| 53 | size_t inputLength = src->getLength(); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 54 | if (input == NULL || inputLength == 0) { |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 55 | input = NULL; |
| 56 | flateData.next_in = inputBuffer; |
| 57 | flateData.avail_in = 0; |
| 58 | } else { |
| 59 | flateData.next_in = input; |
| 60 | flateData.avail_in = inputLength; |
| 61 | } |
| 62 | |
| 63 | rc = Z_OK; |
| 64 | while (true) { |
| 65 | if (flateData.avail_out < kBufferSize) { |
| 66 | if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) { |
| 67 | rc = Z_BUF_ERROR; |
| 68 | break; |
| 69 | } |
| 70 | flateData.next_out = outputBuffer; |
| 71 | flateData.avail_out = kBufferSize; |
| 72 | } |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 73 | if (rc != Z_OK) |
| 74 | break; |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 75 | if (flateData.avail_in == 0) { |
| 76 | if (input != NULL) |
| 77 | break; |
| 78 | size_t read = src->read(&inputBuffer, kBufferSize); |
| 79 | if (read == 0) |
| 80 | break; |
| 81 | flateData.next_in = inputBuffer; |
| 82 | flateData.avail_in = read; |
| 83 | } |
| 84 | if (compress) |
| 85 | rc = deflate(&flateData, Z_NO_FLUSH); |
| 86 | else |
| 87 | rc = inflate(&flateData, Z_NO_FLUSH); |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 88 | } |
| 89 | while (rc == Z_OK) { |
| 90 | if (compress) |
| 91 | rc = deflate(&flateData, Z_FINISH); |
| 92 | else |
| 93 | rc = inflate(&flateData, Z_FINISH); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 94 | if (flateData.avail_out < kBufferSize) { |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 95 | if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) |
| 96 | return false; |
| 97 | flateData.next_out = outputBuffer; |
| 98 | flateData.avail_out = kBufferSize; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (compress) |
| 103 | deflateEnd(&flateData); |
| 104 | else |
| 105 | inflateEnd(&flateData); |
| 106 | if (rc == Z_STREAM_END) |
| 107 | return true; |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | // static |
reed@google.com | 7453d0e | 2011-06-24 20:45:40 +0000 | [diff] [blame] | 114 | bool SkFlate::Deflate(SkStream* src, SkWStream* dst) { |
vandebo@chromium.org | c7a38f3 | 2011-04-05 20:11:32 +0000 | [diff] [blame] | 115 | return doFlate(true, src, dst); |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 116 | } |
| 117 | |
reed@google.com | 314e9b3 | 2011-06-27 17:41:22 +0000 | [diff] [blame] | 118 | bool SkFlate::Deflate(const void* ptr, size_t len, SkWStream* dst) { |
| 119 | SkMemoryStream stream(ptr, len); |
| 120 | return doFlate(true, &stream, dst); |
| 121 | } |
| 122 | |
| 123 | bool SkFlate::Deflate(const SkData* data, SkWStream* dst) { |
| 124 | if (data) { |
| 125 | SkMemoryStream stream(data->data(), data->size()); |
| 126 | return doFlate(true, &stream, dst); |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 131 | // static |
reed@google.com | 7453d0e | 2011-06-24 20:45:40 +0000 | [diff] [blame] | 132 | bool SkFlate::Inflate(SkStream* src, SkWStream* dst) { |
vandebo@chromium.org | c7a38f3 | 2011-04-05 20:11:32 +0000 | [diff] [blame] | 133 | return doFlate(false, src, dst); |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | #endif |
| 137 | |