| OLD | NEW |
| (Empty) |
| 1 /* Copyright 2009 Google Inc. All Rights Reserved. | |
| 2 | |
| 3 Distributed under MIT license. | |
| 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | |
| 5 */ | |
| 6 | |
| 7 // Convience routines to make Brotli I/O classes from some memory containers and | |
| 8 // files. | |
| 9 | |
| 10 #include "./streams.h" | |
| 11 | |
| 12 #include <assert.h> | |
| 13 #include <stdlib.h> | |
| 14 #include <string.h> | |
| 15 | |
| 16 namespace brotli { | |
| 17 | |
| 18 BrotliMemOut::BrotliMemOut(void* buf, size_t len) | |
| 19 : buf_(buf), | |
| 20 len_(len), | |
| 21 pos_(0) {} | |
| 22 | |
| 23 void BrotliMemOut::Reset(void* buf, size_t len) { | |
| 24 buf_ = buf; | |
| 25 len_ = len; | |
| 26 pos_ = 0; | |
| 27 } | |
| 28 | |
| 29 // Brotli output routine: copy n bytes to the output buffer. | |
| 30 bool BrotliMemOut::Write(const void *buf, size_t n) { | |
| 31 if (n + pos_ > len_) | |
| 32 return false; | |
| 33 char* p = reinterpret_cast<char*>(buf_) + pos_; | |
| 34 memcpy(p, buf, n); | |
| 35 pos_ += n; | |
| 36 return true; | |
| 37 } | |
| 38 | |
| 39 BrotliStringOut::BrotliStringOut(std::string* buf, size_t max_size) | |
| 40 : buf_(buf), | |
| 41 max_size_(max_size) { | |
| 42 assert(buf->empty()); | |
| 43 } | |
| 44 | |
| 45 void BrotliStringOut::Reset(std::string* buf, size_t max_size) { | |
| 46 buf_ = buf; | |
| 47 max_size_ = max_size; | |
| 48 } | |
| 49 | |
| 50 // Brotli output routine: add n bytes to a string. | |
| 51 bool BrotliStringOut::Write(const void *buf, size_t n) { | |
| 52 if (buf_->size() + n > max_size_) | |
| 53 return false; | |
| 54 buf_->append(static_cast<const char*>(buf), n); | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 BrotliMemIn::BrotliMemIn(const void* buf, size_t len) | |
| 59 : buf_(buf), | |
| 60 len_(len), | |
| 61 pos_(0) {} | |
| 62 | |
| 63 void BrotliMemIn::Reset(const void* buf, size_t len) { | |
| 64 buf_ = buf; | |
| 65 len_ = len; | |
| 66 pos_ = 0; | |
| 67 } | |
| 68 | |
| 69 // Brotli input routine: read the next chunk of memory. | |
| 70 const void* BrotliMemIn::Read(size_t n, size_t* output) { | |
| 71 if (pos_ == len_) { | |
| 72 return NULL; | |
| 73 } | |
| 74 if (n > len_ - pos_) | |
| 75 n = len_ - pos_; | |
| 76 const char* p = reinterpret_cast<const char*>(buf_) + pos_; | |
| 77 pos_ += n; | |
| 78 *output = n; | |
| 79 return p; | |
| 80 } | |
| 81 | |
| 82 BrotliFileIn::BrotliFileIn(FILE* f, size_t max_read_size) | |
| 83 : f_(f), | |
| 84 buf_(new char[max_read_size]), | |
| 85 buf_size_(max_read_size) { } | |
| 86 | |
| 87 BrotliFileIn::~BrotliFileIn(void) { | |
| 88 delete[] buf_; | |
| 89 } | |
| 90 | |
| 91 const void* BrotliFileIn::Read(size_t n, size_t* bytes_read) { | |
| 92 if (n > buf_size_) { | |
| 93 n = buf_size_; | |
| 94 } else if (n == 0) { | |
| 95 return feof(f_) ? NULL : buf_; | |
| 96 } | |
| 97 *bytes_read = fread(buf_, 1, n, f_); | |
| 98 if (*bytes_read == 0) { | |
| 99 return NULL; | |
| 100 } else { | |
| 101 return buf_; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 BrotliFileOut::BrotliFileOut(FILE* f) : f_(f) {} | |
| 106 | |
| 107 bool BrotliFileOut::Write(const void* buf, size_t n) { | |
| 108 if (fwrite(buf, n, 1, f_) != 1) { | |
| 109 return false; | |
| 110 } | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 } // namespace brotli | |
| OLD | NEW |