Coverage Report

Created: 2025-07-11 06:13

/src/ots/include/ots-memory-stream.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2009-2017 The OTS Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
#ifndef OTS_MEMORY_STREAM_H_
6
#define OTS_MEMORY_STREAM_H_
7
8
#include <cstring>
9
#include <limits>
10
11
#include "opentype-sanitiser.h"
12
13
namespace ots {
14
15
class MemoryStream : public OTSStream {
16
 public:
17
  MemoryStream(void *ptr, size_t length)
18
218
      : ptr_(ptr), length_(length), off_(0) {
19
218
  }
20
21
0
  size_t size() override { return length_; }
22
23
4.12k
  bool WriteRaw(const void *data, size_t length) override {
24
4.12k
    if ((off_ + length > length_) ||
25
4.12k
        (length > std::numeric_limits<size_t>::max() - off_)) {
26
0
      return false;
27
0
    }
28
4.12k
    std::memcpy(static_cast<char*>(ptr_) + off_, data, length);
29
4.12k
    off_ += static_cast<off_t>(length);
30
4.12k
    return true;
31
4.12k
  }
32
33
0
  bool Seek(off_t position) override {
34
0
    if (position < 0) return false;
35
0
    if (static_cast<size_t>(position) > length_) return false;
36
0
    off_ = position;
37
0
    return true;
38
0
  }
39
40
4.12k
  off_t Tell() const override {
41
4.12k
    return off_;
42
4.12k
  }
43
44
 private:
45
  void* const ptr_;
46
  size_t length_;
47
  off_t off_;
48
};
49
50
class ExpandingMemoryStream : public OTSStream {
51
 public:
52
  ExpandingMemoryStream(size_t initial, size_t limit)
53
13.2k
      : length_(initial), limit_(limit), off_(0) {
54
13.2k
    ptr_ = new uint8_t[length_];
55
13.2k
  }
56
57
13.2k
  ~ExpandingMemoryStream() {
58
13.2k
    delete[] static_cast<uint8_t*>(ptr_);
59
13.2k
  }
60
61
0
  void* get() const {
62
0
    return ptr_;
63
0
  }
64
65
43.8k
  size_t size() override { return limit_; }
66
67
11.0M
  bool WriteRaw(const void *data, size_t length) override {
68
11.0M
    if ((off_ + length > length_) ||
69
11.0M
        (length > std::numeric_limits<size_t>::max() - off_)) {
70
377
      if (length_ == limit_)
71
15
        return false;
72
362
      size_t new_length = (length_ + 1) * 2;
73
362
      if (new_length < length_)
74
0
        return false;
75
362
      if (new_length > limit_)
76
18
        new_length = limit_;
77
362
      uint8_t* new_buf = new uint8_t[new_length];
78
362
      std::memcpy(new_buf, ptr_, length_);
79
362
      length_ = new_length;
80
362
      delete[] static_cast<uint8_t*>(ptr_);
81
362
      ptr_ = new_buf;
82
362
      return WriteRaw(data, length);
83
362
    }
84
11.0M
    std::memcpy(static_cast<char*>(ptr_) + off_, data, length);
85
11.0M
    off_ += static_cast<off_t>(length);
86
11.0M
    return true;
87
11.0M
  }
88
89
155k
  bool Seek(off_t position) override {
90
155k
    if (position < 0) return false;
91
155k
    if (static_cast<size_t>(position) > length_) return false;
92
155k
    off_ = position;
93
155k
    return true;
94
155k
  }
95
96
11.7M
  off_t Tell() const override {
97
11.7M
    return off_;
98
11.7M
  }
99
100
 private:
101
  void* ptr_;
102
  size_t length_;
103
  const size_t limit_;
104
  off_t off_;
105
};
106
107
}  // namespace ots
108
109
#endif  // OTS_MEMORY_STREAM_H_