Zoltan Szabadka | 6a53033 | 2015-04-23 14:29:01 +0200 | [diff] [blame] | 1 | // Copyright 2009 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | // Convience routines to make Brotli I/O classes from some memory containers and |
| 16 | // files. |
| 17 | |
| 18 | #include "./streams.h" |
| 19 | |
| 20 | #include <assert.h> |
Zoltan Szabadka | 7de70db | 2015-08-11 11:09:04 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
Zoltan Szabadka | 6a53033 | 2015-04-23 14:29:01 +0200 | [diff] [blame] | 22 | #include <string.h> |
| 23 | |
| 24 | namespace brotli { |
| 25 | |
| 26 | BrotliMemOut::BrotliMemOut(void* buf, int len) |
| 27 | : buf_(buf), |
| 28 | len_(len), |
| 29 | pos_(0) {} |
| 30 | |
| 31 | void BrotliMemOut::Reset(void* buf, int len) { |
| 32 | buf_ = buf; |
| 33 | len_ = len; |
| 34 | pos_ = 0; |
| 35 | } |
| 36 | |
| 37 | // Brotli output routine: copy n bytes to the output buffer. |
| 38 | bool BrotliMemOut::Write(const void *buf, size_t n) { |
| 39 | if (n + pos_ > len_) |
| 40 | return false; |
| 41 | char* p = reinterpret_cast<char*>(buf_) + pos_; |
| 42 | memcpy(p, buf, n); |
| 43 | pos_ += n; |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | BrotliStringOut::BrotliStringOut(std::string* buf, int max_size) |
| 48 | : buf_(buf), |
| 49 | max_size_(max_size) { |
| 50 | assert(buf->empty()); |
| 51 | } |
| 52 | |
| 53 | void BrotliStringOut::Reset(std::string* buf, int max_size) { |
| 54 | buf_ = buf; |
| 55 | max_size_ = max_size; |
| 56 | } |
| 57 | |
| 58 | // Brotli output routine: add n bytes to a string. |
| 59 | bool BrotliStringOut::Write(const void *buf, size_t n) { |
| 60 | if (buf_->size() + n > max_size_) |
| 61 | return false; |
| 62 | buf_->append(static_cast<const char*>(buf), n); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | BrotliMemIn::BrotliMemIn(const void* buf, int len) |
| 67 | : buf_(buf), |
| 68 | len_(len), |
| 69 | pos_(0) {} |
| 70 | |
| 71 | void BrotliMemIn::Reset(const void* buf, int len) { |
| 72 | buf_ = buf; |
| 73 | len_ = len; |
| 74 | pos_ = 0; |
| 75 | } |
| 76 | |
| 77 | // Brotli input routine: read the next chunk of memory. |
| 78 | const void* BrotliMemIn::Read(size_t n, size_t* output) { |
| 79 | if (pos_ == len_) { |
| 80 | return NULL; |
| 81 | } |
| 82 | if (n > len_ - pos_) |
| 83 | n = len_ - pos_; |
| 84 | const char* p = reinterpret_cast<const char*>(buf_) + pos_; |
| 85 | pos_ += n; |
| 86 | *output = n; |
| 87 | return p; |
| 88 | } |
| 89 | |
| 90 | BrotliFileIn::BrotliFileIn(FILE* f, size_t max_read_size) |
| 91 | : f_(f), |
| 92 | buf_(malloc(max_read_size)), |
| 93 | buf_size_(max_read_size) {} |
| 94 | |
| 95 | BrotliFileIn::~BrotliFileIn() { |
| 96 | if (buf_) free(buf_); |
| 97 | } |
| 98 | |
| 99 | const void* BrotliFileIn::Read(size_t n, size_t* bytes_read) { |
| 100 | if (buf_ == NULL) { |
| 101 | *bytes_read = 0; |
| 102 | return NULL; |
| 103 | } |
| 104 | if (n > buf_size_) { |
| 105 | n = buf_size_; |
| 106 | } else if (n == 0) { |
| 107 | return feof(f_) ? NULL : buf_; |
| 108 | } |
| 109 | *bytes_read = fread(buf_, 1, n, f_); |
| 110 | if (*bytes_read == 0) { |
| 111 | return NULL; |
| 112 | } else { |
| 113 | return buf_; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | BrotliFileOut::BrotliFileOut(FILE* f) : f_(f) {} |
| 118 | |
| 119 | bool BrotliFileOut::Write(const void* buf, size_t n) { |
| 120 | if (fwrite(buf, n, 1, f_) != 1) { |
| 121 | return false; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | } // namespace brotli |