1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/ots/include/ots-memory-stream.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +// Copyright (c) 2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef OTS_MEMORY_STREAM_H_ 1.9 +#define OTS_MEMORY_STREAM_H_ 1.10 + 1.11 +#include <cstring> 1.12 +#include <limits> 1.13 + 1.14 +#include "opentype-sanitiser.h" 1.15 + 1.16 +namespace ots { 1.17 + 1.18 +class MemoryStream : public OTSStream { 1.19 + public: 1.20 + MemoryStream(void *ptr, size_t length) 1.21 + : ptr_(ptr), length_(length), off_(0) { 1.22 + } 1.23 + 1.24 + virtual bool WriteRaw(const void *data, size_t length) { 1.25 + if ((off_ + length > length_) || 1.26 + (length > std::numeric_limits<size_t>::max() - off_)) { 1.27 + return false; 1.28 + } 1.29 + std::memcpy(static_cast<char*>(ptr_) + off_, data, length); 1.30 + off_ += length; 1.31 + return true; 1.32 + } 1.33 + 1.34 + virtual bool Seek(off_t position) { 1.35 + if (position < 0) return false; 1.36 + if (static_cast<size_t>(position) > length_) return false; 1.37 + off_ = position; 1.38 + return true; 1.39 + } 1.40 + 1.41 + virtual off_t Tell() const { 1.42 + return off_; 1.43 + } 1.44 + 1.45 + private: 1.46 + void* const ptr_; 1.47 + size_t length_; 1.48 + off_t off_; 1.49 +}; 1.50 + 1.51 +class ExpandingMemoryStream : public OTSStream { 1.52 + public: 1.53 + ExpandingMemoryStream(size_t initial, size_t limit) 1.54 + : length_(initial), limit_(limit), off_(0) { 1.55 + ptr_ = new uint8_t[length_]; 1.56 + } 1.57 + 1.58 + ~ExpandingMemoryStream() { 1.59 + delete[] static_cast<uint8_t*>(ptr_); 1.60 + } 1.61 + 1.62 + void* get() const { 1.63 + return ptr_; 1.64 + } 1.65 + 1.66 + bool WriteRaw(const void *data, size_t length) { 1.67 + if ((off_ + length > length_) || 1.68 + (length > std::numeric_limits<size_t>::max() - off_)) { 1.69 + if (length_ == limit_) 1.70 + return false; 1.71 + size_t new_length = (length_ + 1) * 2; 1.72 + if (new_length < length_) 1.73 + return false; 1.74 + if (new_length > limit_) 1.75 + new_length = limit_; 1.76 + uint8_t* new_buf = new uint8_t[new_length]; 1.77 + std::memcpy(new_buf, ptr_, length_); 1.78 + length_ = new_length; 1.79 + delete[] static_cast<uint8_t*>(ptr_); 1.80 + ptr_ = new_buf; 1.81 + return WriteRaw(data, length); 1.82 + } 1.83 + std::memcpy(static_cast<char*>(ptr_) + off_, data, length); 1.84 + off_ += length; 1.85 + return true; 1.86 + } 1.87 + 1.88 + bool Seek(off_t position) { 1.89 + if (position < 0) return false; 1.90 + if (static_cast<size_t>(position) > length_) return false; 1.91 + off_ = position; 1.92 + return true; 1.93 + } 1.94 + 1.95 + off_t Tell() const { 1.96 + return off_; 1.97 + } 1.98 + 1.99 + private: 1.100 + void* ptr_; 1.101 + size_t length_; 1.102 + const size_t limit_; 1.103 + off_t off_; 1.104 +}; 1.105 + 1.106 +} // namespace ots 1.107 + 1.108 +#endif // OTS_MEMORY_STREAM_H_