OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 // Output buffer for WOFF2 decompression. |
| 16 |
| 17 #include "./woff2_out.h" |
| 18 |
| 19 namespace woff2 { |
| 20 |
| 21 WOFF2StringOut::WOFF2StringOut(string* buf) |
| 22 : buf_(buf), |
| 23 max_size_(kDefaultMaxSize), |
| 24 offset_(0) {} |
| 25 |
| 26 bool WOFF2StringOut::Write(const void *buf, size_t n) { |
| 27 return Write(buf, offset_, n); |
| 28 } |
| 29 |
| 30 bool WOFF2StringOut::Write(const void *buf, size_t offset, size_t n) { |
| 31 if (offset > max_size_ || n > max_size_ - offset) { |
| 32 return false; |
| 33 } |
| 34 if (offset == buf_->size()) { |
| 35 buf_->append(static_cast<const char*>(buf), n); |
| 36 } else { |
| 37 if (offset + n > buf_->size()) { |
| 38 buf_->append(offset + n - buf_->size(), 0); |
| 39 } |
| 40 buf_->replace(offset, n, static_cast<const char*>(buf), n); |
| 41 } |
| 42 offset_ = std::max(offset_, offset + n); |
| 43 |
| 44 return true; |
| 45 } |
| 46 |
| 47 void WOFF2StringOut::SetMaxSize(size_t max_size) { |
| 48 max_size_ = max_size; |
| 49 if (offset_ > max_size_) { |
| 50 offset_ = max_size_; |
| 51 } |
| 52 } |
| 53 |
| 54 WOFF2MemoryOut::WOFF2MemoryOut(uint8_t* buf, size_t buf_size) |
| 55 : buf_(buf), |
| 56 buf_size_(buf_size), |
| 57 offset_(0) {} |
| 58 |
| 59 bool WOFF2MemoryOut::Write(const void *buf, size_t n) { |
| 60 return Write(buf, offset_, n); |
| 61 } |
| 62 |
| 63 bool WOFF2MemoryOut::Write(const void *buf, size_t offset, size_t n) { |
| 64 if (offset > buf_size_ || n > buf_size_ - offset) { |
| 65 return false; |
| 66 } |
| 67 std::memcpy(buf_ + offset, buf, n); |
| 68 offset_ = std::max(offset_, offset + n); |
| 69 |
| 70 return true; |
| 71 } |
| 72 |
| 73 } // namespace woff2 |
OLD | NEW |