Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: src/core/SkWriter32.cpp

Issue 22359003: allow NULL in writeString/readString (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: style nit Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/Writer32Test.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkWriter32.h" 8 #include "SkWriter32.h"
9 9
10 SkWriter32::SkWriter32(size_t minSize, void* storage, size_t storageSize) { 10 SkWriter32::SkWriter32(size_t minSize, void* storage, size_t storageSize) {
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 239
240 #include "SkReader32.h" 240 #include "SkReader32.h"
241 #include "SkString.h" 241 #include "SkString.h"
242 242
243 /* 243 /*
244 * Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4 244 * Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4
245 */ 245 */
246 246
247 const char* SkReader32::readString(size_t* outLen) { 247 const char* SkReader32::readString(size_t* outLen) {
248 size_t len = this->readInt(); 248 size_t len = this->readInt();
249 if (0xFFFF == len) {
250 if (outLen) {
251 *outLen = 0;
252 }
253 return NULL;
254 }
249 const void* ptr = this->peek(); 255 const void* ptr = this->peek();
250 256
251 // skip over teh string + '\0' and then pad to a multiple of 4 257 // skip over teh string + '\0' and then pad to a multiple of 4
252 size_t alignedSize = SkAlign4(len + 1); 258 size_t alignedSize = SkAlign4(len + 1);
253 this->skip(alignedSize); 259 this->skip(alignedSize);
254 260
255 if (outLen) { 261 if (outLen) {
256 *outLen = len; 262 *outLen = len;
257 } 263 }
258 return (const char*)ptr; 264 return (const char*)ptr;
259 } 265 }
260 266
261 size_t SkReader32::readIntoString(SkString* copy) { 267 size_t SkReader32::readIntoString(SkString* copy) {
262 size_t len; 268 size_t len;
263 const char* ptr = this->readString(&len); 269 const char* ptr = this->readString(&len);
264 if (copy) { 270 if (copy) {
265 copy->set(ptr, len); 271 copy->set(ptr, len);
266 } 272 }
267 return len; 273 return len;
268 } 274 }
269 275
270 void SkWriter32::writeString(const char str[], size_t len) { 276 void SkWriter32::writeString(const char str[], size_t len) {
277 if (NULL == str) {
278 // We're already requiring len < 0xFFFF, so we can use that to mark NULL .
279 this->write32(0xFFFF);
280 return;
281 }
271 if ((long)len < 0) { 282 if ((long)len < 0) {
272 SkASSERT(str);
273 len = strlen(str); 283 len = strlen(str);
274 } 284 }
275 this->write32(len); 285 this->write32(len);
276 // add 1 since we also write a terminating 0 286 // add 1 since we also write a terminating 0
277 size_t alignedLen = SkAlign4(len + 1); 287 size_t alignedLen = SkAlign4(len + 1);
278 char* ptr = (char*)this->reserve(alignedLen); 288 char* ptr = (char*)this->reserve(alignedLen);
279 { 289 {
280 // Write the terminating 0 and fill in the rest with zeroes 290 // Write the terminating 0 and fill in the rest with zeroes
281 uint32_t* padding = (uint32_t*)(ptr + (alignedLen - 4)); 291 uint32_t* padding = (uint32_t*)(ptr + (alignedLen - 4));
282 *padding = 0; 292 *padding = 0;
283 } 293 }
284 // Copy the string itself. 294 // Copy the string itself.
285 memcpy(ptr, str, len); 295 memcpy(ptr, str, len);
286 } 296 }
287 297
288 size_t SkWriter32::WriteStringSize(const char* str, size_t len) { 298 size_t SkWriter32::WriteStringSize(const char* str, size_t len) {
289 if ((long)len < 0) { 299 if ((long)len < 0) {
290 SkASSERT(str); 300 SkASSERT(str);
291 len = strlen(str); 301 len = strlen(str);
292 } 302 }
293 const size_t lenBytes = 4; // we use 4 bytes to record the length 303 const size_t lenBytes = 4; // we use 4 bytes to record the length
294 // add 1 since we also write a terminating 0 304 // add 1 since we also write a terminating 0
295 return SkAlign4(lenBytes + len + 1); 305 return SkAlign4(lenBytes + len + 1);
296 } 306 }
OLDNEW
« no previous file with comments | « no previous file | tests/Writer32Test.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698