Debian Codegraph @

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