2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkReader32.h"
10 #include "SkWriter32.h"
13 * Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4
16 const char* SkReader32::readString(size_t* outLen) {
17 size_t len = this->readInt();
18 const void* ptr = this->peek();
20 // skip over the string + '\0' and then pad to a multiple of 4
21 size_t alignedSize = SkAlign4(len + 1);
22 this->skip(alignedSize);
27 return (const char*)ptr;
30 size_t SkReader32::readIntoString(SkString* copy) {
32 const char* ptr = this->readString(&len);
39 void SkWriter32::writeString(const char str[], size_t len) {
48 // [ 4 byte len ] [ str ... ] [1 - 4 \0s]
49 uint32_t* ptr = this->reservePad(sizeof(uint32_t) + len + 1);
51 char* chars = (char*)(ptr + 1);
52 memcpy(chars, str, len);
56 size_t SkWriter32::WriteStringSize(const char* str, size_t len) {
61 const size_t lenBytes = 4; // we use 4 bytes to record the length
62 // add 1 since we also write a terminating 0
63 return SkAlign4(lenBytes + len + 1);
66 void SkWriter32::growToAtLeast(size_t size) {
67 const bool wasExternal = (fExternal != NULL) && (fData == fExternal);
69 fCapacity = 4096 + SkTMax(size, fCapacity + (fCapacity / 2));
70 fInternal.realloc(fCapacity);
71 fData = fInternal.get();
74 // we were external, so copy in the data
75 memcpy(fData, fExternal, fUsed);