OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef OTS_MEMORY_STREAM_H_ | 5 #ifndef OTS_MEMORY_STREAM_H_ |
6 #define OTS_MEMORY_STREAM_H_ | 6 #define OTS_MEMORY_STREAM_H_ |
7 | 7 |
8 #include <cstring> | 8 #include <cstring> |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
11 #include "opentype-sanitiser.h" | 11 #include "opentype-sanitiser.h" |
12 | 12 |
13 namespace ots { | 13 namespace ots { |
14 | 14 |
15 class MemoryStream : public OTSStream { | 15 class MemoryStream : public OTSStream { |
16 public: | 16 public: |
17 MemoryStream(void *ptr, size_t length) | 17 MemoryStream(void *ptr, size_t length) |
18 : ptr_(ptr), length_(length), off_(0) { | 18 : ptr_(ptr), length_(length), off_(0) { |
19 } | 19 } |
20 | 20 |
21 bool WriteRaw(const void *data, size_t length) { | 21 virtual bool WriteRaw(const void *data, size_t length) { |
22 if ((off_ + length > length_) || | 22 if ((off_ + length > length_) || |
23 (length > std::numeric_limits<size_t>::max() - off_)) { | 23 (length > std::numeric_limits<size_t>::max() - off_)) { |
24 return false; | 24 return false; |
25 } | 25 } |
26 std::memcpy(static_cast<char*>(ptr_) + off_, data, length); | 26 std::memcpy(static_cast<char*>(ptr_) + off_, data, length); |
27 off_ += length; | 27 off_ += length; |
28 return true; | 28 return true; |
29 } | 29 } |
30 | 30 |
31 bool Seek(off_t position) { | 31 virtual bool Seek(off_t position) { |
32 if (position < 0) return false; | 32 if (position < 0) return false; |
33 if (static_cast<size_t>(position) > length_) return false; | 33 if (static_cast<size_t>(position) > length_) return false; |
34 off_ = position; | 34 off_ = position; |
35 return true; | 35 return true; |
36 } | 36 } |
37 | 37 |
38 off_t Tell() const { | 38 virtual off_t Tell() const { |
39 return off_; | 39 return off_; |
40 } | 40 } |
41 | 41 |
42 private: | 42 private: |
43 void* const ptr_; | 43 void* const ptr_; |
44 size_t length_; | 44 size_t length_; |
45 off_t off_; | 45 off_t off_; |
46 }; | 46 }; |
47 | 47 |
48 class ExpandingMemoryStream : public OTSStream { | 48 class ExpandingMemoryStream : public OTSStream { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 private: | 96 private: |
97 void* ptr_; | 97 void* ptr_; |
98 size_t length_; | 98 size_t length_; |
99 const size_t limit_; | 99 const size_t limit_; |
100 off_t off_; | 100 off_t off_; |
101 }; | 101 }; |
102 | 102 |
103 } // namespace ots | 103 } // namespace ots |
104 | 104 |
105 #endif // OTS_MEMORY_STREAM_H_ | 105 #endif // OTS_MEMORY_STREAM_H_ |
OLD | NEW |