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

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

Issue 22862002: Write NULL as "" so readString() always returns a non-NULL string. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: len 0 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 | « include/core/SkWriter32.h ('k') | 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 }
255 const void* ptr = this->peek(); 249 const void* ptr = this->peek();
256 250
257 // skip over teh string + '\0' and then pad to a multiple of 4 251 // skip over teh string + '\0' and then pad to a multiple of 4
258 size_t alignedSize = SkAlign4(len + 1); 252 size_t alignedSize = SkAlign4(len + 1);
259 this->skip(alignedSize); 253 this->skip(alignedSize);
260 254
261 if (outLen) { 255 if (outLen) {
262 *outLen = len; 256 *outLen = len;
263 } 257 }
264 return (const char*)ptr; 258 return (const char*)ptr;
265 } 259 }
266 260
267 size_t SkReader32::readIntoString(SkString* copy) { 261 size_t SkReader32::readIntoString(SkString* copy) {
268 size_t len; 262 size_t len;
269 const char* ptr = this->readString(&len); 263 const char* ptr = this->readString(&len);
270 if (copy) { 264 if (copy) {
271 copy->set(ptr, len); 265 copy->set(ptr, len);
272 } 266 }
273 return len; 267 return len;
274 } 268 }
275 269
276 void SkWriter32::writeString(const char str[], size_t len) { 270 void SkWriter32::writeString(const char str[], size_t len) {
277 if (NULL == str) { 271 if (NULL == str) {
278 // We're already requiring len < 0xFFFF, so we can use that to mark NULL . 272 str = "";
279 this->write32(0xFFFF); 273 len = 0;
280 return;
281 } 274 }
282 if ((long)len < 0) { 275 if ((long)len < 0) {
283 len = strlen(str); 276 len = strlen(str);
284 } 277 }
285 this->write32(len); 278 this->write32(len);
286 // add 1 since we also write a terminating 0 279 // add 1 since we also write a terminating 0
287 size_t alignedLen = SkAlign4(len + 1); 280 size_t alignedLen = SkAlign4(len + 1);
288 char* ptr = (char*)this->reserve(alignedLen); 281 char* ptr = (char*)this->reserve(alignedLen);
289 { 282 {
290 // Write the terminating 0 and fill in the rest with zeroes 283 // Write the terminating 0 and fill in the rest with zeroes
291 uint32_t* padding = (uint32_t*)(ptr + (alignedLen - 4)); 284 uint32_t* padding = (uint32_t*)(ptr + (alignedLen - 4));
292 *padding = 0; 285 *padding = 0;
293 } 286 }
294 // Copy the string itself. 287 // Copy the string itself.
295 memcpy(ptr, str, len); 288 memcpy(ptr, str, len);
296 } 289 }
297 290
298 size_t SkWriter32::WriteStringSize(const char* str, size_t len) { 291 size_t SkWriter32::WriteStringSize(const char* str, size_t len) {
299 if ((long)len < 0) { 292 if ((long)len < 0) {
300 SkASSERT(str); 293 SkASSERT(str);
301 len = strlen(str); 294 len = strlen(str);
302 } 295 }
303 const size_t lenBytes = 4; // we use 4 bytes to record the length 296 const size_t lenBytes = 4; // we use 4 bytes to record the length
304 // add 1 since we also write a terminating 0 297 // add 1 since we also write a terminating 0
305 return SkAlign4(lenBytes + len + 1); 298 return SkAlign4(lenBytes + len + 1);
306 } 299 }
OLDNEW
« no previous file with comments | « include/core/SkWriter32.h ('k') | tests/Writer32Test.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698