gfx/ots/include/ots-memory-stream.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial