OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/client/plugin/compression.h" |
| 6 |
| 7 #include <assert.h> |
| 8 |
| 9 namespace remoting { |
| 10 |
| 11 static const int kZLIB_CHUNK = 256 * 1024; |
| 12 |
| 13 ZCompressor::ZCompressor() { |
| 14 stream_.zalloc = Z_NULL; |
| 15 stream_.zfree = Z_NULL; |
| 16 stream_.opaque = Z_NULL; |
| 17 |
| 18 deflateInit(&stream_, Z_BEST_SPEED); |
| 19 } |
| 20 |
| 21 void ZCompressor::WriteInternal(char* buffer, int size, int flush) { |
| 22 stream_.avail_in = size; |
| 23 stream_.next_in = reinterpret_cast<Bytef*>(buffer); |
| 24 |
| 25 // Expand the internal buffer. |
| 26 while (true) { |
| 27 int last_size = buffer_.size(); |
| 28 buffer_.resize(last_size + kZLIB_CHUNK); |
| 29 stream_.avail_out = kZLIB_CHUNK; |
| 30 stream_.next_out = reinterpret_cast<Bytef*>(&buffer_[last_size]); |
| 31 int ret = deflate(&stream_, flush); |
| 32 assert(ret != Z_STREAM_ERROR); |
| 33 |
| 34 // Shrink the size of the vector. It doesn't alter the capacity. |
| 35 int compressed = kZLIB_CHUNK - stream_.avail_out; |
| 36 buffer_.resize(last_size + compressed); |
| 37 if (!compressed) |
| 38 break; |
| 39 } |
| 40 } |
| 41 |
| 42 void ZCompressor::Write(char* buffer, int size) { |
| 43 WriteInternal(buffer, size, Z_NO_FLUSH); |
| 44 } |
| 45 |
| 46 void ZCompressor::Flush() { |
| 47 WriteInternal(NULL, 0, Z_FINISH); |
| 48 deflateEnd(&stream_); |
| 49 } |
| 50 |
| 51 int ZCompressor::GetCompressedSize() { |
| 52 return buffer_.size(); |
| 53 } |
| 54 |
| 55 char* ZCompressor::GetCompressedData() { |
| 56 return &buffer_[0]; |
| 57 } |
| 58 |
| 59 int ZCompressor::GetRawSize() { |
| 60 // I don't care about this. |
| 61 return 0; |
| 62 } |
| 63 |
| 64 ZDecompressor::ZDecompressor() { |
| 65 stream_.zalloc = Z_NULL; |
| 66 stream_.zfree = Z_NULL; |
| 67 stream_.opaque = Z_NULL; |
| 68 stream_.avail_in = 0; |
| 69 stream_.next_in = Z_NULL; |
| 70 |
| 71 inflateInit(&stream_); |
| 72 } |
| 73 |
| 74 void ZDecompressor::WriteInternal(char* buffer, int size, int flush) { |
| 75 stream_.avail_in = size; |
| 76 stream_.next_in = reinterpret_cast<Bytef*>(buffer); |
| 77 |
| 78 while (true) { |
| 79 int last_size = buffer_.size(); |
| 80 buffer_.resize(last_size + kZLIB_CHUNK); |
| 81 stream_.avail_out = kZLIB_CHUNK; |
| 82 stream_.next_out = reinterpret_cast<Bytef*>(&buffer_[last_size]); |
| 83 int ret = inflate(&stream_, flush); |
| 84 assert(ret != Z_STREAM_ERROR); |
| 85 |
| 86 // Shrink the size of the vector. It doesn't alter the capacity. |
| 87 int decompressed = kZLIB_CHUNK - stream_.avail_out; |
| 88 buffer_.resize(last_size + decompressed); |
| 89 if (!decompressed) |
| 90 break; |
| 91 } |
| 92 } |
| 93 |
| 94 void ZDecompressor::Write(char* buffer, int size) { |
| 95 WriteInternal(buffer, size, Z_NO_FLUSH); |
| 96 } |
| 97 |
| 98 void ZDecompressor::Flush() { |
| 99 inflateEnd(&stream_); |
| 100 } |
| 101 |
| 102 char* ZDecompressor::GetRawData() { |
| 103 return &buffer_[0]; |
| 104 } |
| 105 |
| 106 int ZDecompressor::GetRawSize() { |
| 107 return buffer_.size(); |
| 108 } |
| 109 |
| 110 int ZDecompressor::GetCompressedSize() { |
| 111 // I don't care. |
| 112 } |
| 113 |
| 114 } // namespace remoting |
OLD | NEW |