OLD | NEW |
---|---|
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 "SkReader32.h" | 8 #include "SkReader32.h" |
9 #include "SkString.h" | 9 #include "SkString.h" |
10 #include "SkWriter32.h" | 10 #include "SkWriter32.h" |
(...skipping 26 matching lines...) Expand all Loading... | |
37 } | 37 } |
38 | 38 |
39 void SkWriter32::writeString(const char str[], size_t len) { | 39 void SkWriter32::writeString(const char str[], size_t len) { |
40 if (NULL == str) { | 40 if (NULL == str) { |
41 str = ""; | 41 str = ""; |
42 len = 0; | 42 len = 0; |
43 } | 43 } |
44 if ((long)len < 0) { | 44 if ((long)len < 0) { |
45 len = strlen(str); | 45 len = strlen(str); |
46 } | 46 } |
47 this->write32(len); | 47 |
48 // add 1 since we also write a terminating 0 | 48 // add 1 since we also write a terminating 0 |
mtklein
2014/02/10 22:37:34
Hmm, maybe we can make this clearer? Does this wo
f(malita)
2014/02/11 03:30:56
Ah, reservePad takes care of padding, doesn't it.
| |
49 size_t alignedLen = SkAlign4(len + 1); | 49 size_t alignedLen = SkAlign4(len + 1); |
50 char* ptr = (char*)this->reserve(alignedLen); | 50 // reserve an additional 4 bytes for len storage |
51 uint32_t* lenPtr = this->reserve(alignedLen + sizeof(uint32_t)); | |
52 char* ptr = (char*)(lenPtr + 1); | |
51 { | 53 { |
52 // Write the terminating 0 and fill in the rest with zeroes | 54 // Write the terminating 0 and fill in the rest with zeroes |
53 uint32_t* padding = (uint32_t*)(ptr + (alignedLen - 4)); | 55 uint32_t* padding = (uint32_t*)(ptr + (alignedLen - 4)); |
54 *padding = 0; | 56 *padding = 0; |
55 } | 57 } |
58 | |
59 *lenPtr = len; | |
56 // Copy the string itself. | 60 // Copy the string itself. |
57 memcpy(ptr, str, len); | 61 memcpy(ptr, str, len); |
58 } | 62 } |
59 | 63 |
60 size_t SkWriter32::WriteStringSize(const char* str, size_t len) { | 64 size_t SkWriter32::WriteStringSize(const char* str, size_t len) { |
61 if ((long)len < 0) { | 65 if ((long)len < 0) { |
62 SkASSERT(str); | 66 SkASSERT(str); |
63 len = strlen(str); | 67 len = strlen(str); |
64 } | 68 } |
65 const size_t lenBytes = 4; // we use 4 bytes to record the length | 69 const size_t lenBytes = 4; // we use 4 bytes to record the length |
66 // add 1 since we also write a terminating 0 | 70 // add 1 since we also write a terminating 0 |
67 return SkAlign4(lenBytes + len + 1); | 71 return SkAlign4(lenBytes + len + 1); |
68 } | 72 } |
OLD | NEW |