/work/obj-fuzz/dist/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 | 0 | : ptr_(ptr), length_(length), off_(0) { |
19 | 0 | } |
20 | | |
21 | 0 | virtual bool WriteRaw(const void *data, size_t length) { |
22 | 0 | if ((off_ + length > length_) || |
23 | 0 | (length > std::numeric_limits<size_t>::max() - off_)) { |
24 | 0 | return false; |
25 | 0 | } |
26 | 0 | std::memcpy(static_cast<char*>(ptr_) + off_, data, length); |
27 | 0 | off_ += length; |
28 | 0 | return true; |
29 | 0 | } |
30 | | |
31 | 0 | virtual bool Seek(off_t position) { |
32 | 0 | if (position < 0) return false; |
33 | 0 | if (static_cast<size_t>(position) > length_) return false; |
34 | 0 | off_ = position; |
35 | 0 | return true; |
36 | 0 | } |
37 | | |
38 | 0 | virtual off_t Tell() const { |
39 | 0 | return off_; |
40 | 0 | } |
41 | | |
42 | | private: |
43 | | void* const ptr_; |
44 | | size_t length_; |
45 | | off_t off_; |
46 | | }; |
47 | | |
48 | | class ExpandingMemoryStream : public OTSStream { |
49 | | public: |
50 | | ExpandingMemoryStream(size_t initial, size_t limit) |
51 | 0 | : length_(initial), limit_(limit), off_(0) { |
52 | 0 | ptr_ = new uint8_t[length_]; |
53 | 0 | } |
54 | | |
55 | 0 | ~ExpandingMemoryStream() { |
56 | 0 | delete[] static_cast<uint8_t*>(ptr_); |
57 | 0 | } |
58 | | |
59 | 0 | void* get() const { |
60 | 0 | return ptr_; |
61 | 0 | } |
62 | | |
63 | 0 | bool WriteRaw(const void *data, size_t length) { |
64 | 0 | if ((off_ + length > length_) || |
65 | 0 | (length > std::numeric_limits<size_t>::max() - off_)) { |
66 | 0 | if (length_ == limit_) |
67 | 0 | return false; |
68 | 0 | size_t new_length = (length_ + 1) * 2; |
69 | 0 | if (new_length < length_) |
70 | 0 | return false; |
71 | 0 | if (new_length > limit_) |
72 | 0 | new_length = limit_; |
73 | 0 | uint8_t* new_buf = new uint8_t[new_length]; |
74 | 0 | std::memcpy(new_buf, ptr_, length_); |
75 | 0 | length_ = new_length; |
76 | 0 | delete[] static_cast<uint8_t*>(ptr_); |
77 | 0 | ptr_ = new_buf; |
78 | 0 | return WriteRaw(data, length); |
79 | 0 | } |
80 | 0 | std::memcpy(static_cast<char*>(ptr_) + off_, data, length); |
81 | 0 | off_ += length; |
82 | 0 | return true; |
83 | 0 | } |
84 | | |
85 | 0 | bool Seek(off_t position) { |
86 | 0 | if (position < 0) return false; |
87 | 0 | if (static_cast<size_t>(position) > length_) return false; |
88 | 0 | off_ = position; |
89 | 0 | return true; |
90 | 0 | } |
91 | | |
92 | 0 | off_t Tell() const { |
93 | 0 | return off_; |
94 | 0 | } |
95 | | |
96 | | private: |
97 | | void* ptr_; |
98 | | size_t length_; |
99 | | const size_t limit_; |
100 | | off_t off_; |
101 | | }; |
102 | | |
103 | | } // namespace ots |
104 | | |
105 | | #endif // OTS_MEMORY_STREAM_H_ |