Debian Codegraph @

libs/woff2/src/woff2_out.cc

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