OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/pickle.h" | 5 #include "base/pickle.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include <algorithm> // for max() | 9 #include <algorithm> // for max() |
10 | 10 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 return false; | 146 return false; |
147 *data = read_from; | 147 *data = read_from; |
148 return true; | 148 return true; |
149 } | 149 } |
150 | 150 |
151 // Payload is uint32 aligned. | 151 // Payload is uint32 aligned. |
152 | 152 |
153 Pickle::Pickle() | 153 Pickle::Pickle() |
154 : header_(NULL), | 154 : header_(NULL), |
155 header_size_(sizeof(Header)), | 155 header_size_(sizeof(Header)), |
156 capacity_(0) { | 156 capacity_(0), |
157 write_offset_(0) { | |
157 Resize(kPayloadUnit); | 158 Resize(kPayloadUnit); |
158 header_->payload_size = 0; | 159 header_->payload_size = 0; |
159 } | 160 } |
160 | 161 |
161 Pickle::Pickle(int header_size) | 162 Pickle::Pickle(int header_size) |
162 : header_(NULL), | 163 : header_(NULL), |
163 header_size_(AlignInt(header_size, sizeof(uint32))), | 164 header_size_(AlignInt(header_size, sizeof(uint32))), |
164 capacity_(0) { | 165 capacity_(0), |
166 write_offset_(0) { | |
165 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header)); | 167 DCHECK_GE(static_cast<size_t>(header_size), sizeof(Header)); |
166 DCHECK_LE(header_size, kPayloadUnit); | 168 DCHECK_LE(header_size, kPayloadUnit); |
167 Resize(kPayloadUnit); | 169 Resize(kPayloadUnit); |
168 header_->payload_size = 0; | 170 header_->payload_size = 0; |
169 } | 171 } |
170 | 172 |
171 Pickle::Pickle(const char* data, int data_len) | 173 Pickle::Pickle(const char* data, int data_len) |
172 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))), | 174 : header_(reinterpret_cast<Header*>(const_cast<char*>(data))), |
173 header_size_(0), | 175 header_size_(0), |
174 capacity_(kCapacityReadOnly) { | 176 capacity_(kCapacityReadOnly), |
177 write_offset_(0) { | |
175 if (data_len >= static_cast<int>(sizeof(Header))) | 178 if (data_len >= static_cast<int>(sizeof(Header))) |
176 header_size_ = data_len - header_->payload_size; | 179 header_size_ = data_len - header_->payload_size; |
177 | 180 |
178 if (header_size_ > static_cast<unsigned int>(data_len)) | 181 if (header_size_ > static_cast<unsigned int>(data_len)) |
179 header_size_ = 0; | 182 header_size_ = 0; |
180 | 183 |
181 if (header_size_ != AlignInt(header_size_, sizeof(uint32))) | 184 if (header_size_ != AlignInt(header_size_, sizeof(uint32))) |
182 header_size_ = 0; | 185 header_size_ = 0; |
183 | 186 |
184 // If there is anything wrong with the data, we're not going to use it. | 187 // If there is anything wrong with the data, we're not going to use it. |
185 if (!header_size_) | 188 if (!header_size_) |
186 header_ = NULL; | 189 header_ = NULL; |
187 } | 190 } |
188 | 191 |
189 Pickle::Pickle(const Pickle& other) | 192 Pickle::Pickle(const Pickle& other) |
190 : header_(NULL), | 193 : header_(NULL), |
191 header_size_(other.header_size_), | 194 header_size_(other.header_size_), |
192 capacity_(0) { | 195 capacity_(0), |
193 size_t payload_size = header_size_ + other.header_->payload_size; | 196 write_offset_(other.write_offset_) { |
194 Resize(payload_size); | 197 Resize(other.header_->payload_size); |
195 memcpy(header_, other.header_, payload_size); | 198 memcpy(header_, other.header_, header_size_ + other.header_->payload_size); |
196 } | 199 } |
197 | 200 |
198 Pickle::~Pickle() { | 201 Pickle::~Pickle() { |
199 if (capacity_ != kCapacityReadOnly) | 202 if (capacity_ != kCapacityReadOnly) |
200 free(header_); | 203 free(header_); |
201 } | 204 } |
202 | 205 |
203 Pickle& Pickle::operator=(const Pickle& other) { | 206 Pickle& Pickle::operator=(const Pickle& other) { |
204 if (this == &other) { | 207 if (this == &other) { |
205 NOTREACHED(); | 208 NOTREACHED(); |
206 return *this; | 209 return *this; |
207 } | 210 } |
208 if (capacity_ == kCapacityReadOnly) { | 211 if (capacity_ == kCapacityReadOnly) { |
209 header_ = NULL; | 212 header_ = NULL; |
210 capacity_ = 0; | 213 capacity_ = 0; |
211 } | 214 } |
212 if (header_size_ != other.header_size_) { | 215 if (header_size_ != other.header_size_) { |
213 free(header_); | 216 free(header_); |
214 header_ = NULL; | 217 header_ = NULL; |
215 header_size_ = other.header_size_; | 218 header_size_ = other.header_size_; |
216 } | 219 } |
217 Resize(other.header_size_ + other.header_->payload_size); | 220 Resize(other.header_->payload_size); |
218 memcpy(header_, other.header_, | 221 memcpy(header_, other.header_, |
219 other.header_size_ + other.header_->payload_size); | 222 other.header_size_ + other.header_->payload_size); |
223 write_offset_ = other.write_offset_; | |
220 return *this; | 224 return *this; |
221 } | 225 } |
222 | 226 |
223 bool Pickle::WriteString(const std::string& value) { | 227 bool Pickle::WriteString(const std::string& value) { |
224 if (!WriteInt(static_cast<int>(value.size()))) | 228 if (!WriteInt(static_cast<int>(value.size()))) |
225 return false; | 229 return false; |
226 | 230 |
227 return WriteBytes(value.data(), static_cast<int>(value.size())); | 231 return WriteBytes(value.data(), static_cast<int>(value.size())); |
228 } | 232 } |
229 | 233 |
(...skipping 10 matching lines...) Expand all Loading... | |
240 return false; | 244 return false; |
241 | 245 |
242 return WriteBytes(value.data(), | 246 return WriteBytes(value.data(), |
243 static_cast<int>(value.size()) * sizeof(char16)); | 247 static_cast<int>(value.size()) * sizeof(char16)); |
244 } | 248 } |
245 | 249 |
246 bool Pickle::WriteData(const char* data, int length) { | 250 bool Pickle::WriteData(const char* data, int length) { |
247 return length >= 0 && WriteInt(length) && WriteBytes(data, length); | 251 return length >= 0 && WriteInt(length) && WriteBytes(data, length); |
248 } | 252 } |
249 | 253 |
250 bool Pickle::WriteBytes(const void* data, int data_len) { | 254 inline void Pickle::WriteBytesCommon(const void* data, size_t length) { |
jar (doing other things)
2013/10/24 22:36:48
Note that inline is a "hint" and is traditionally
piman
2013/10/24 23:54:29
Both clang and gcc failed to inline without it.
Wi
| |
251 DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly"; | 255 DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly"; |
256 #ifdef ARCH_CPU_64_BITS | |
257 DCHECK_LE(static_cast<size_t>(length), kuint32max); | |
jar (doing other things)
2013/10/24 22:36:48
nit: length is already of type size_t
Also... if
piman
2013/10/24 23:54:29
Yep, sorry, copy-and-paste from the previous versi
piman
2013/10/25 03:11:41
Done.
| |
258 #endif | |
259 size_t data_len = AlignInt(length, sizeof(uint32)); | |
260 size_t new_size = write_offset_ + data_len; | |
jar (doing other things)
2013/10/24 22:36:48
How are you assured that this is not an overflow?
piman
2013/10/24 23:54:29
The previous code (in BeginWrite) didn't check for
| |
261 if (new_size > capacity_) { | |
262 Resize(std::max(capacity_ * 2, new_size)); | |
263 } | |
252 | 264 |
253 char* dest = BeginWrite(data_len); | 265 char* write = mutable_payload() + write_offset_; |
254 if (!dest) | 266 memcpy(write, data, length); |
255 return false; | 267 memset(write + length, 0, data_len - length); |
268 header_->payload_size = write_offset_ + length; | |
269 write_offset_ = new_size; | |
270 } | |
256 | 271 |
257 memcpy(dest, data, data_len); | 272 bool Pickle::WriteBytes(const void* data, int length) { |
258 | 273 WriteBytesCommon(data, length); |
259 EndWrite(dest, data_len); | |
260 return true; | 274 return true; |
261 } | 275 } |
262 | 276 |
263 void Pickle::Reserve(size_t length) { | 277 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { |
264 // write at a uint32-aligned offset from the beginning of the header | 278 WriteBytesCommon(data, length); |
jar (doing other things)
2013/10/24 22:36:48
For all the template magic... it appears that you
piman
2013/10/24 23:54:29
It's possible to do, but then you need template in
| |
265 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); | |
266 | |
267 size_t new_size = offset + length; | |
268 size_t needed_size = header_size_ + new_size; | |
269 if (needed_size > capacity_) | |
270 Resize(capacity_ * 2 + needed_size); | |
271 } | 279 } |
272 | 280 |
273 char* Pickle::BeginWrite(size_t length) { | 281 template void Pickle::WriteBytesStatic<2>(const void* data); |
274 // write at a uint32-aligned offset from the beginning of the header | 282 template void Pickle::WriteBytesStatic<4>(const void* data); |
275 size_t offset = AlignInt(header_->payload_size, sizeof(uint32)); | 283 template void Pickle::WriteBytesStatic<8>(const void* data); |
276 | 284 |
277 size_t new_size = offset + length; | 285 void Pickle::Reserve(size_t length) { |
278 size_t needed_size = header_size_ + new_size; | 286 size_t data_len = AlignInt(length, sizeof(uint32)); |
279 if (needed_size > capacity_) | 287 size_t new_size = write_offset_ + data_len; |
280 Resize(std::max(capacity_ * 2, needed_size)); | 288 if (new_size > capacity_) |
281 | 289 Resize(capacity_ * 2 + new_size); |
282 #ifdef ARCH_CPU_64_BITS | |
283 DCHECK_LE(length, kuint32max); | |
284 #endif | |
285 | |
286 header_->payload_size = static_cast<uint32>(new_size); | |
287 return mutable_payload() + offset; | |
288 } | |
289 | |
290 void Pickle::EndWrite(char* dest, int length) { | |
291 // Zero-pad to keep tools like valgrind from complaining about uninitialized | |
292 // memory. | |
293 if (length % sizeof(uint32)) | |
294 memset(dest + length, 0, sizeof(uint32) - (length % sizeof(uint32))); | |
295 } | 290 } |
296 | 291 |
297 void Pickle::Resize(size_t new_capacity) { | 292 void Pickle::Resize(size_t new_capacity) { |
298 CHECK_NE(capacity_, kCapacityReadOnly); | 293 CHECK_NE(capacity_, kCapacityReadOnly); |
299 | 294 |
300 new_capacity = AlignInt(new_capacity, kPayloadUnit); | 295 new_capacity = AlignInt(new_capacity, kPayloadUnit); |
301 void* p = realloc(header_, new_capacity); | 296 void* p = realloc(header_, header_size_ + new_capacity); |
302 header_ = reinterpret_cast<Header*>(p); | 297 header_ = reinterpret_cast<Header*>(p); |
303 capacity_ = new_capacity; | 298 capacity_ = new_capacity; |
304 } | 299 } |
305 | 300 |
306 // static | 301 // static |
307 const char* Pickle::FindNext(size_t header_size, | 302 const char* Pickle::FindNext(size_t header_size, |
308 const char* start, | 303 const char* start, |
309 const char* end) { | 304 const char* end) { |
310 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); | 305 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); |
311 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); | 306 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); |
312 | 307 |
313 if (static_cast<size_t>(end - start) < sizeof(Header)) | 308 if (static_cast<size_t>(end - start) < sizeof(Header)) |
314 return NULL; | 309 return NULL; |
315 | 310 |
316 const Header* hdr = reinterpret_cast<const Header*>(start); | 311 const Header* hdr = reinterpret_cast<const Header*>(start); |
317 const char* payload_base = start + header_size; | 312 const char* payload_base = start + header_size; |
318 const char* payload_end = payload_base + hdr->payload_size; | 313 const char* payload_end = payload_base + hdr->payload_size; |
319 if (payload_end < payload_base) | 314 if (payload_end < payload_base) |
320 return NULL; | 315 return NULL; |
321 | 316 |
322 return (payload_end > end) ? NULL : payload_end; | 317 return (payload_end > end) ? NULL : payload_end; |
323 } | 318 } |
OLD | NEW |