blob: 5e89ed655b70e5d83da4df2588704689898b52e6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.com5595af12012-12-21 15:36:33 +00007
reed@google.comdde09562011-05-23 12:21:05 +00008#include "SkReader32.h"
reed@google.comfd0ffcf2011-06-21 15:43:11 +00009#include "SkString.h"
commit-bot@chromium.org19382422014-01-14 20:51:26 +000010#include "SkWriter32.h"
reed@google.comfd0ffcf2011-06-21 15:43:11 +000011
12/*
13 * Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4
14 */
reed@google.comdde09562011-05-23 12:21:05 +000015
16const char* SkReader32::readString(size_t* outLen) {
reed@google.comfd0ffcf2011-06-21 15:43:11 +000017 size_t len = this->readInt();
18 const void* ptr = this->peek();
reed@google.comdde09562011-05-23 12:21:05 +000019
commit-bot@chromium.org19382422014-01-14 20:51:26 +000020 // skip over the string + '\0' and then pad to a multiple of 4
reed@google.comfd0ffcf2011-06-21 15:43:11 +000021 size_t alignedSize = SkAlign4(len + 1);
reed@google.comdde09562011-05-23 12:21:05 +000022 this->skip(alignedSize);
23
24 if (outLen) {
25 *outLen = len;
26 }
27 return (const char*)ptr;
28}
29
reed@google.comfd0ffcf2011-06-21 15:43:11 +000030size_t SkReader32::readIntoString(SkString* copy) {
31 size_t len;
32 const char* ptr = this->readString(&len);
33 if (copy) {
34 copy->set(ptr, len);
35 }
36 return len;
37}
38
reed@google.comdde09562011-05-23 12:21:05 +000039void SkWriter32::writeString(const char str[], size_t len) {
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +000040 if (NULL == str) {
mtklein@google.com0038c122013-08-15 21:01:32 +000041 str = "";
42 len = 0;
commit-bot@chromium.org47fa1362013-08-09 16:03:05 +000043 }
reed@google.comdde09562011-05-23 12:21:05 +000044 if ((long)len < 0) {
reed@google.comdde09562011-05-23 12:21:05 +000045 len = strlen(str);
46 }
reed@google.comfd0ffcf2011-06-21 15:43:11 +000047 this->write32(len);
reed@google.comdde09562011-05-23 12:21:05 +000048 // add 1 since we also write a terminating 0
reed@google.comfd0ffcf2011-06-21 15:43:11 +000049 size_t alignedLen = SkAlign4(len + 1);
50 char* ptr = (char*)this->reserve(alignedLen);
scroggo@google.comdd394882012-07-24 20:47:55 +000051 {
52 // Write the terminating 0 and fill in the rest with zeroes
53 uint32_t* padding = (uint32_t*)(ptr + (alignedLen - 4));
54 *padding = 0;
scroggo@google.come9617eb2012-07-23 13:44:10 +000055 }
scroggo@google.comdd394882012-07-24 20:47:55 +000056 // Copy the string itself.
57 memcpy(ptr, str, len);
reed@google.comdde09562011-05-23 12:21:05 +000058}
59
60size_t SkWriter32::WriteStringSize(const char* str, size_t len) {
61 if ((long)len < 0) {
62 SkASSERT(str);
63 len = strlen(str);
64 }
reed@google.comfd0ffcf2011-06-21 15:43:11 +000065 const size_t lenBytes = 4; // we use 4 bytes to record the length
reed@google.comdde09562011-05-23 12:21:05 +000066 // add 1 since we also write a terminating 0
67 return SkAlign4(lenBytes + len + 1);
68}