vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
reed@google.com | 314e9b3 | 2011-06-27 17:41:22 +0000 | [diff] [blame] | 17 | #include "SkData.h" |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 18 | #include "SkFlate.h" |
| 19 | #include "SkStream.h" |
| 20 | |
| 21 | #ifndef SK_ZLIB_INCLUDE |
| 22 | bool SkFlate::HaveFlate() { return false; } |
reed@google.com | 7453d0e | 2011-06-24 20:45:40 +0000 | [diff] [blame] | 23 | bool SkFlate::Deflate(SkStream*, SkWStream*) { return false; } |
epoger@google.com | b14ef73 | 2011-07-21 13:56:34 +0000 | [diff] [blame^] | 24 | bool SkFlate::Deflate(const void*, size_t, SkWStream*) { return false; } |
| 25 | bool SkFlate::Deflate(const SkData*, SkWStream*) { return false; } |
reed@google.com | 7453d0e | 2011-06-24 20:45:40 +0000 | [diff] [blame] | 26 | bool SkFlate::Inflate(SkStream*, SkWStream*) { return false; } |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 27 | #else |
| 28 | |
| 29 | // static |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 30 | bool SkFlate::HaveFlate() { |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | #include SK_ZLIB_INCLUDE |
| 37 | |
vandebo@chromium.org | c7a38f3 | 2011-04-05 20:11:32 +0000 | [diff] [blame] | 38 | // static |
| 39 | const size_t kBufferSize = 1024; |
| 40 | |
reed@google.com | 7453d0e | 2011-06-24 20:45:40 +0000 | [diff] [blame] | 41 | bool doFlate(bool compress, SkStream* src, SkWStream* dst) { |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 42 | uint8_t inputBuffer[kBufferSize]; |
| 43 | uint8_t outputBuffer[kBufferSize]; |
| 44 | z_stream flateData; |
| 45 | flateData.zalloc = NULL; |
| 46 | flateData.zfree = NULL; |
| 47 | flateData.next_in = NULL; |
| 48 | flateData.avail_in = 0; |
| 49 | flateData.next_out = outputBuffer; |
| 50 | flateData.avail_out = kBufferSize; |
| 51 | int rc; |
| 52 | if (compress) |
| 53 | rc = deflateInit(&flateData, Z_DEFAULT_COMPRESSION); |
| 54 | else |
| 55 | rc = inflateInit(&flateData); |
| 56 | if (rc != Z_OK) |
| 57 | return false; |
| 58 | |
| 59 | uint8_t* input = (uint8_t*)src->getMemoryBase(); |
| 60 | size_t inputLength = src->getLength(); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 61 | if (input == NULL || inputLength == 0) { |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 62 | input = NULL; |
| 63 | flateData.next_in = inputBuffer; |
| 64 | flateData.avail_in = 0; |
| 65 | } else { |
| 66 | flateData.next_in = input; |
| 67 | flateData.avail_in = inputLength; |
| 68 | } |
| 69 | |
| 70 | rc = Z_OK; |
| 71 | while (true) { |
| 72 | if (flateData.avail_out < kBufferSize) { |
| 73 | if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) { |
| 74 | rc = Z_BUF_ERROR; |
| 75 | break; |
| 76 | } |
| 77 | flateData.next_out = outputBuffer; |
| 78 | flateData.avail_out = kBufferSize; |
| 79 | } |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 80 | if (rc != Z_OK) |
| 81 | break; |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 82 | if (flateData.avail_in == 0) { |
| 83 | if (input != NULL) |
| 84 | break; |
| 85 | size_t read = src->read(&inputBuffer, kBufferSize); |
| 86 | if (read == 0) |
| 87 | break; |
| 88 | flateData.next_in = inputBuffer; |
| 89 | flateData.avail_in = read; |
| 90 | } |
| 91 | if (compress) |
| 92 | rc = deflate(&flateData, Z_NO_FLUSH); |
| 93 | else |
| 94 | rc = inflate(&flateData, Z_NO_FLUSH); |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 95 | } |
| 96 | while (rc == Z_OK) { |
| 97 | if (compress) |
| 98 | rc = deflate(&flateData, Z_FINISH); |
| 99 | else |
| 100 | rc = inflate(&flateData, Z_FINISH); |
vandebo@chromium.org | ee34e35 | 2010-12-02 22:55:33 +0000 | [diff] [blame] | 101 | if (flateData.avail_out < kBufferSize) { |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 102 | if (!dst->write(outputBuffer, kBufferSize - flateData.avail_out)) |
| 103 | return false; |
| 104 | flateData.next_out = outputBuffer; |
| 105 | flateData.avail_out = kBufferSize; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (compress) |
| 110 | deflateEnd(&flateData); |
| 111 | else |
| 112 | inflateEnd(&flateData); |
| 113 | if (rc == Z_STREAM_END) |
| 114 | return true; |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |
| 120 | // static |
reed@google.com | 7453d0e | 2011-06-24 20:45:40 +0000 | [diff] [blame] | 121 | bool SkFlate::Deflate(SkStream* src, SkWStream* dst) { |
vandebo@chromium.org | c7a38f3 | 2011-04-05 20:11:32 +0000 | [diff] [blame] | 122 | return doFlate(true, src, dst); |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 123 | } |
| 124 | |
reed@google.com | 314e9b3 | 2011-06-27 17:41:22 +0000 | [diff] [blame] | 125 | bool SkFlate::Deflate(const void* ptr, size_t len, SkWStream* dst) { |
| 126 | SkMemoryStream stream(ptr, len); |
| 127 | return doFlate(true, &stream, dst); |
| 128 | } |
| 129 | |
| 130 | bool SkFlate::Deflate(const SkData* data, SkWStream* dst) { |
| 131 | if (data) { |
| 132 | SkMemoryStream stream(data->data(), data->size()); |
| 133 | return doFlate(true, &stream, dst); |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 138 | // static |
reed@google.com | 7453d0e | 2011-06-24 20:45:40 +0000 | [diff] [blame] | 139 | bool SkFlate::Inflate(SkStream* src, SkWStream* dst) { |
vandebo@chromium.org | c7a38f3 | 2011-04-05 20:11:32 +0000 | [diff] [blame] | 140 | return doFlate(false, src, dst); |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | #endif |
| 144 | |