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