michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkReader32.h" michael@0: #include "SkString.h" michael@0: #include "SkWriter32.h" michael@0: michael@0: /* michael@0: * Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4 michael@0: */ michael@0: michael@0: const char* SkReader32::readString(size_t* outLen) { michael@0: size_t len = this->readInt(); michael@0: const void* ptr = this->peek(); michael@0: michael@0: // skip over the string + '\0' and then pad to a multiple of 4 michael@0: size_t alignedSize = SkAlign4(len + 1); michael@0: this->skip(alignedSize); michael@0: michael@0: if (outLen) { michael@0: *outLen = len; michael@0: } michael@0: return (const char*)ptr; michael@0: } michael@0: michael@0: size_t SkReader32::readIntoString(SkString* copy) { michael@0: size_t len; michael@0: const char* ptr = this->readString(&len); michael@0: if (copy) { michael@0: copy->set(ptr, len); michael@0: } michael@0: return len; michael@0: } michael@0: michael@0: void SkWriter32::writeString(const char str[], size_t len) { michael@0: if (NULL == str) { michael@0: str = ""; michael@0: len = 0; michael@0: } michael@0: if ((long)len < 0) { michael@0: len = strlen(str); michael@0: } michael@0: michael@0: // [ 4 byte len ] [ str ... ] [1 - 4 \0s] michael@0: uint32_t* ptr = this->reservePad(sizeof(uint32_t) + len + 1); michael@0: *ptr = len; michael@0: char* chars = (char*)(ptr + 1); michael@0: memcpy(chars, str, len); michael@0: chars[len] = '\0'; michael@0: } michael@0: michael@0: size_t SkWriter32::WriteStringSize(const char* str, size_t len) { michael@0: if ((long)len < 0) { michael@0: SkASSERT(str); michael@0: len = strlen(str); michael@0: } michael@0: const size_t lenBytes = 4; // we use 4 bytes to record the length michael@0: // add 1 since we also write a terminating 0 michael@0: return SkAlign4(lenBytes + len + 1); michael@0: } michael@0: michael@0: void SkWriter32::growToAtLeast(size_t size) { michael@0: const bool wasExternal = (fExternal != NULL) && (fData == fExternal); michael@0: michael@0: fCapacity = 4096 + SkTMax(size, fCapacity + (fCapacity / 2)); michael@0: fInternal.realloc(fCapacity); michael@0: fData = fInternal.get(); michael@0: michael@0: if (wasExternal) { michael@0: // we were external, so copy in the data michael@0: memcpy(fData, fExternal, fUsed); michael@0: } michael@0: // Invalidate the snapshot, we know it is no longer useful. michael@0: fSnapshot.reset(NULL); michael@0: } michael@0: michael@0: SkData* SkWriter32::snapshotAsData() const { michael@0: // get a non const version of this, we are only conceptually const michael@0: SkWriter32& mutable_this = *const_cast(this); michael@0: // we use size change detection to invalidate the cached data michael@0: if ((fSnapshot.get() != NULL) && (fSnapshot->size() != fUsed)) { michael@0: mutable_this.fSnapshot.reset(NULL); michael@0: } michael@0: if (fSnapshot.get() == NULL) { michael@0: uint8_t* buffer = NULL; michael@0: if ((fExternal != NULL) && (fData == fExternal)) { michael@0: // We need to copy to an allocated buffer before returning. michael@0: buffer = (uint8_t*)sk_malloc_throw(fUsed); michael@0: memcpy(buffer, fData, fUsed); michael@0: } else { michael@0: buffer = mutable_this.fInternal.detach(); michael@0: // prepare us to do copy on write, by pretending the data buffer michael@0: // is external and size limited michael@0: mutable_this.fData = buffer; michael@0: mutable_this.fCapacity = fUsed; michael@0: mutable_this.fExternal = buffer; michael@0: } michael@0: mutable_this.fSnapshot.reset(SkData::NewFromMalloc(buffer, fUsed)); michael@0: } michael@0: return SkRef(fSnapshot.get()); // Take an extra ref for the caller. michael@0: }