toolkit/crashreporter/google-breakpad/src/processor/map_serializers-inl.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) 2010, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29 //
michael@0 30 // map_serializers_inl.h: implementation for serializing std::map and its
michael@0 31 // wrapper classes.
michael@0 32 //
michael@0 33 // See map_serializers.h for documentation.
michael@0 34 //
michael@0 35 // Author: Siyang Xie (lambxsy@google.com)
michael@0 36
michael@0 37 #ifndef PROCESSOR_MAP_SERIALIZERS_INL_H__
michael@0 38 #define PROCESSOR_MAP_SERIALIZERS_INL_H__
michael@0 39
michael@0 40 #include <map>
michael@0 41 #include <string>
michael@0 42
michael@0 43 #include "processor/map_serializers.h"
michael@0 44 #include "processor/simple_serializer.h"
michael@0 45
michael@0 46 #include "processor/address_map-inl.h"
michael@0 47 #include "processor/range_map-inl.h"
michael@0 48 #include "processor/contained_range_map-inl.h"
michael@0 49
michael@0 50 #include "processor/logging.h"
michael@0 51
michael@0 52 namespace google_breakpad {
michael@0 53
michael@0 54 template<typename Key, typename Value>
michael@0 55 size_t StdMapSerializer<Key, Value>::SizeOf(
michael@0 56 const std::map<Key, Value> &m) const {
michael@0 57 size_t size = 0;
michael@0 58 size_t header_size = (1 + m.size()) * sizeof(uint32_t);
michael@0 59 size += header_size;
michael@0 60
michael@0 61 typename std::map<Key, Value>::const_iterator iter;
michael@0 62 for (iter = m.begin(); iter != m.end(); ++iter) {
michael@0 63 size += key_serializer_.SizeOf(iter->first);
michael@0 64 size += value_serializer_.SizeOf(iter->second);
michael@0 65 }
michael@0 66 return size;
michael@0 67 }
michael@0 68
michael@0 69 template<typename Key, typename Value>
michael@0 70 char *StdMapSerializer<Key, Value>::Write(const std::map<Key, Value> &m,
michael@0 71 char *dest) const {
michael@0 72 if (!dest) {
michael@0 73 BPLOG(ERROR) << "StdMapSerializer failed: write to NULL address.";
michael@0 74 return NULL;
michael@0 75 }
michael@0 76 char *start_address = dest;
michael@0 77
michael@0 78 // Write header:
michael@0 79 // Number of nodes.
michael@0 80 dest = SimpleSerializer<uint32_t>::Write(m.size(), dest);
michael@0 81 // Nodes offsets.
michael@0 82 uint32_t *offsets = reinterpret_cast<uint32_t*>(dest);
michael@0 83 dest += sizeof(uint32_t) * m.size();
michael@0 84
michael@0 85 char *key_address = dest;
michael@0 86 dest += sizeof(Key) * m.size();
michael@0 87
michael@0 88 // Traverse map.
michael@0 89 typename std::map<Key, Value>::const_iterator iter;
michael@0 90 int index = 0;
michael@0 91 for (iter = m.begin(); iter != m.end(); ++iter, ++index) {
michael@0 92 offsets[index] = static_cast<uint32_t>(dest - start_address);
michael@0 93 key_address = key_serializer_.Write(iter->first, key_address);
michael@0 94 dest = value_serializer_.Write(iter->second, dest);
michael@0 95 }
michael@0 96 return dest;
michael@0 97 }
michael@0 98
michael@0 99 template<typename Key, typename Value>
michael@0 100 char *StdMapSerializer<Key, Value>::Serialize(
michael@0 101 const std::map<Key, Value> &m, unsigned int *size) const {
michael@0 102 // Compute size of memory to be allocated.
michael@0 103 unsigned int size_to_alloc = SizeOf(m);
michael@0 104 // Allocate memory.
michael@0 105 char *serialized_data = new char[size_to_alloc];
michael@0 106 if (!serialized_data) {
michael@0 107 BPLOG(INFO) << "StdMapSerializer memory allocation failed.";
michael@0 108 if (size) *size = 0;
michael@0 109 return NULL;
michael@0 110 }
michael@0 111 // Write serialized data into memory.
michael@0 112 Write(m, serialized_data);
michael@0 113
michael@0 114 if (size) *size = size_to_alloc;
michael@0 115 return serialized_data;
michael@0 116 }
michael@0 117
michael@0 118 template<typename Address, typename Entry>
michael@0 119 size_t RangeMapSerializer<Address, Entry>::SizeOf(
michael@0 120 const RangeMap<Address, Entry> &m) const {
michael@0 121 size_t size = 0;
michael@0 122 size_t header_size = (1 + m.map_.size()) * sizeof(uint32_t);
michael@0 123 size += header_size;
michael@0 124
michael@0 125 typename std::map<Address, Range>::const_iterator iter;
michael@0 126 for (iter = m.map_.begin(); iter != m.map_.end(); ++iter) {
michael@0 127 // Size of key (high address).
michael@0 128 size += address_serializer_.SizeOf(iter->first);
michael@0 129 // Size of base (low address).
michael@0 130 size += address_serializer_.SizeOf(iter->second.base());
michael@0 131 // Size of entry.
michael@0 132 size += entry_serializer_.SizeOf(iter->second.entry());
michael@0 133 }
michael@0 134 return size;
michael@0 135 }
michael@0 136
michael@0 137 template<typename Address, typename Entry>
michael@0 138 char *RangeMapSerializer<Address, Entry>::Write(
michael@0 139 const RangeMap<Address, Entry> &m, char *dest) const {
michael@0 140 if (!dest) {
michael@0 141 BPLOG(ERROR) << "RangeMapSerializer failed: write to NULL address.";
michael@0 142 return NULL;
michael@0 143 }
michael@0 144 char *start_address = dest;
michael@0 145
michael@0 146 // Write header:
michael@0 147 // Number of nodes.
michael@0 148 dest = SimpleSerializer<uint32_t>::Write(m.map_.size(), dest);
michael@0 149 // Nodes offsets.
michael@0 150 uint32_t *offsets = reinterpret_cast<uint32_t*>(dest);
michael@0 151 dest += sizeof(uint32_t) * m.map_.size();
michael@0 152
michael@0 153 char *key_address = dest;
michael@0 154 dest += sizeof(Address) * m.map_.size();
michael@0 155
michael@0 156 // Traverse map.
michael@0 157 typename std::map<Address, Range>::const_iterator iter;
michael@0 158 int index = 0;
michael@0 159 for (iter = m.map_.begin(); iter != m.map_.end(); ++iter, ++index) {
michael@0 160 offsets[index] = static_cast<uint32_t>(dest - start_address);
michael@0 161 key_address = address_serializer_.Write(iter->first, key_address);
michael@0 162 dest = address_serializer_.Write(iter->second.base(), dest);
michael@0 163 dest = entry_serializer_.Write(iter->second.entry(), dest);
michael@0 164 }
michael@0 165 return dest;
michael@0 166 }
michael@0 167
michael@0 168 template<typename Address, typename Entry>
michael@0 169 char *RangeMapSerializer<Address, Entry>::Serialize(
michael@0 170 const RangeMap<Address, Entry> &m, unsigned int *size) const {
michael@0 171 // Compute size of memory to be allocated.
michael@0 172 unsigned int size_to_alloc = SizeOf(m);
michael@0 173 // Allocate memory.
michael@0 174 char *serialized_data = new char[size_to_alloc];
michael@0 175 if (!serialized_data) {
michael@0 176 BPLOG(INFO) << "RangeMapSerializer memory allocation failed.";
michael@0 177 if (size) *size = 0;
michael@0 178 return NULL;
michael@0 179 }
michael@0 180
michael@0 181 // Write serialized data into memory.
michael@0 182 Write(m, serialized_data);
michael@0 183
michael@0 184 if (size) *size = size_to_alloc;
michael@0 185 return serialized_data;
michael@0 186 }
michael@0 187
michael@0 188
michael@0 189 template<class AddrType, class EntryType>
michael@0 190 size_t ContainedRangeMapSerializer<AddrType, EntryType>::SizeOf(
michael@0 191 const ContainedRangeMap<AddrType, EntryType> *m) const {
michael@0 192 size_t size = 0;
michael@0 193 size_t header_size = addr_serializer_.SizeOf(m->base_)
michael@0 194 + entry_serializer_.SizeOf(m->entry_)
michael@0 195 + sizeof(uint32_t);
michael@0 196 size += header_size;
michael@0 197 // In case m.map_ == NULL, we treat it as an empty map:
michael@0 198 size += sizeof(uint32_t);
michael@0 199 if (m->map_) {
michael@0 200 size += m->map_->size() * sizeof(uint32_t);
michael@0 201 typename Map::const_iterator iter;
michael@0 202 for (iter = m->map_->begin(); iter != m->map_->end(); ++iter) {
michael@0 203 size += addr_serializer_.SizeOf(iter->first);
michael@0 204 // Recursive calculation of size:
michael@0 205 size += SizeOf(iter->second);
michael@0 206 }
michael@0 207 }
michael@0 208 return size;
michael@0 209 }
michael@0 210
michael@0 211 template<class AddrType, class EntryType>
michael@0 212 char *ContainedRangeMapSerializer<AddrType, EntryType>::Write(
michael@0 213 const ContainedRangeMap<AddrType, EntryType> *m, char *dest) const {
michael@0 214 if (!dest) {
michael@0 215 BPLOG(ERROR) << "StdMapSerializer failed: write to NULL address.";
michael@0 216 return NULL;
michael@0 217 }
michael@0 218 dest = addr_serializer_.Write(m->base_, dest);
michael@0 219 dest = SimpleSerializer<uint32_t>::Write(entry_serializer_.SizeOf(m->entry_),
michael@0 220 dest);
michael@0 221 dest = entry_serializer_.Write(m->entry_, dest);
michael@0 222
michael@0 223 // Write map<<AddrType, ContainedRangeMap*>:
michael@0 224 char *map_address = dest;
michael@0 225 if (m->map_ == NULL) {
michael@0 226 dest = SimpleSerializer<uint32_t>::Write(0, dest);
michael@0 227 } else {
michael@0 228 dest = SimpleSerializer<uint32_t>::Write(m->map_->size(), dest);
michael@0 229 uint32_t *offsets = reinterpret_cast<uint32_t*>(dest);
michael@0 230 dest += sizeof(uint32_t) * m->map_->size();
michael@0 231
michael@0 232 char *key_address = dest;
michael@0 233 dest += sizeof(AddrType) * m->map_->size();
michael@0 234
michael@0 235 // Traverse map.
michael@0 236 typename Map::const_iterator iter;
michael@0 237 int index = 0;
michael@0 238 for (iter = m->map_->begin(); iter != m->map_->end(); ++iter, ++index) {
michael@0 239 offsets[index] = static_cast<uint32_t>(dest - map_address);
michael@0 240 key_address = addr_serializer_.Write(iter->first, key_address);
michael@0 241 // Recursively write.
michael@0 242 dest = Write(iter->second, dest);
michael@0 243 }
michael@0 244 }
michael@0 245 return dest;
michael@0 246 }
michael@0 247
michael@0 248 template<class AddrType, class EntryType>
michael@0 249 char *ContainedRangeMapSerializer<AddrType, EntryType>::Serialize(
michael@0 250 const ContainedRangeMap<AddrType, EntryType> *m, unsigned int *size) const {
michael@0 251 unsigned int size_to_alloc = SizeOf(m);
michael@0 252 // Allocating memory.
michael@0 253 char *serialized_data = new char[size_to_alloc];
michael@0 254 if (!serialized_data) {
michael@0 255 BPLOG(INFO) << "ContainedRangeMapSerializer memory allocation failed.";
michael@0 256 if (size) *size = 0;
michael@0 257 return NULL;
michael@0 258 }
michael@0 259 Write(m, serialized_data);
michael@0 260 if (size) *size = size_to_alloc;
michael@0 261 return serialized_data;
michael@0 262 }
michael@0 263
michael@0 264 } // namespace google_breakpad
michael@0 265
michael@0 266 #endif // PROCESSOR_MAP_SERIALIZERS_INL_H__

mercurial