/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 | 525 | : ptr_(ptr), length_(length), off_(0) { |
19 | 525 | } |
20 | | |
21 | 0 | size_t size() override { return length_; } |
22 | | |
23 | 7.66k | bool WriteRaw(const void *data, size_t length) override { |
24 | 7.66k | if ((off_ + length > length_) || |
25 | 7.66k | (length > std::numeric_limits<size_t>::max() - off_)) { |
26 | 0 | return false; |
27 | 0 | } |
28 | 7.66k | std::memcpy(static_cast<char*>(ptr_) + off_, data, length); |
29 | 7.66k | off_ += length; |
30 | 7.66k | return true; |
31 | 7.66k | } |
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 | 7.66k | off_t Tell() const override { |
41 | 7.66k | return off_; |
42 | 7.66k | } |
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 | 24.3k | : length_(initial), limit_(limit), off_(0) { |
54 | 24.3k | ptr_ = new uint8_t[length_]; |
55 | 24.3k | } |
56 | | |
57 | 24.3k | ~ExpandingMemoryStream() { |
58 | 24.3k | delete[] static_cast<uint8_t*>(ptr_); |
59 | 24.3k | } |
60 | | |
61 | 0 | void* get() const { |
62 | 0 | return ptr_; |
63 | 0 | } |
64 | | |
65 | 107k | size_t size() override { return limit_; } |
66 | | |
67 | 25.0M | bool WriteRaw(const void *data, size_t length) override { |
68 | 25.0M | if ((off_ + length > length_) || |
69 | 25.0M | (length > std::numeric_limits<size_t>::max() - off_)) { |
70 | 386 | if (length_ == limit_) |
71 | 13 | return false; |
72 | 373 | size_t new_length = (length_ + 1) * 2; |
73 | 373 | if (new_length < length_) |
74 | 0 | return false; |
75 | 373 | if (new_length > limit_) |
76 | 18 | new_length = limit_; |
77 | 373 | uint8_t* new_buf = new uint8_t[new_length]; |
78 | 373 | std::memcpy(new_buf, ptr_, length_); |
79 | 373 | length_ = new_length; |
80 | 373 | delete[] static_cast<uint8_t*>(ptr_); |
81 | 373 | ptr_ = new_buf; |
82 | 373 | return WriteRaw(data, length); |
83 | 373 | } |
84 | 25.0M | std::memcpy(static_cast<char*>(ptr_) + off_, data, length); |
85 | 25.0M | off_ += length; |
86 | 25.0M | return true; |
87 | 25.0M | } |
88 | | |
89 | 441k | bool Seek(off_t position) override { |
90 | 441k | if (position < 0) return false; |
91 | 441k | if (static_cast<size_t>(position) > length_) return false; |
92 | 441k | off_ = position; |
93 | 441k | return true; |
94 | 441k | } |
95 | | |
96 | 27.1M | off_t Tell() const override { |
97 | 27.1M | return off_; |
98 | 27.1M | } |
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_ |