Coverage Report

Created: 2023-03-26 10:21

/src/woff2/src/woff2_out.cc
Line
Count
Source (jump to first uncovered line)
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
375k
    offset_(0) {}
25
26
3.68M
bool WOFF2StringOut::Write(const void *buf, size_t n) {
27
3.68M
  return Write(buf, offset_, n);
28
3.68M
}
29
30
4.22M
bool WOFF2StringOut::Write(const void *buf, size_t offset, size_t n) {
31
4.22M
  if (offset > max_size_ || n > max_size_ - offset) {
32
0
    return false;
33
0
  }
34
4.22M
  if (offset == buf_->size()) {
35
3.68M
    buf_->append(static_cast<const char*>(buf), n);
36
3.68M
  } else {
37
538k
    if (offset + n > buf_->size()) {
38
0
      buf_->append(offset + n - buf_->size(), 0);
39
0
    }
40
538k
    buf_->replace(offset, n, static_cast<const char*>(buf), n);
41
538k
  }
42
4.22M
  offset_ = std::max(offset_, offset + n);
43
44
4.22M
  return true;
45
4.22M
}
46
47
375k
void WOFF2StringOut::SetMaxSize(size_t max_size) {
48
375k
  max_size_ = max_size;
49
375k
  if (offset_ > max_size_) {
50
0
    offset_ = max_size_;
51
0
  }
52
375k
}
53
54
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
0
}
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
0
  }
67
0
  std::memcpy(buf_ + offset, buf, n);
68
0
  offset_ = std::max(offset_, offset + n);
69
70
0
  return true;
71
0
}
72
73
} // namespace woff2