epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 5595af1 | 2012-12-21 15:36:33 +0000 | [diff] [blame] | 7 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 8 | #include "SkReader32.h" |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame] | 9 | #include "SkString.h" |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 10 | #include "SkWriter32.h" |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4 |
| 14 | */ |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 15 | |
| 16 | const char* SkReader32::readString(size_t* outLen) { |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 17 | size_t len = this->readU32(); |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame] | 18 | const void* ptr = this->peek(); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 19 | |
commit-bot@chromium.org | 1938242 | 2014-01-14 20:51:26 +0000 | [diff] [blame] | 20 | // skip over the string + '\0' and then pad to a multiple of 4 |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame] | 21 | size_t alignedSize = SkAlign4(len + 1); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 22 | this->skip(alignedSize); |
| 23 | |
| 24 | if (outLen) { |
| 25 | *outLen = len; |
| 26 | } |
| 27 | return (const char*)ptr; |
| 28 | } |
| 29 | |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame] | 30 | size_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.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 39 | void SkWriter32::writeString(const char str[], size_t len) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | if (nullptr == str) { |
mtklein@google.com | 0038c12 | 2013-08-15 21:01:32 +0000 | [diff] [blame] | 41 | str = ""; |
| 42 | len = 0; |
commit-bot@chromium.org | 47fa136 | 2013-08-09 16:03:05 +0000 | [diff] [blame] | 43 | } |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 44 | if ((long)len < 0) { |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 45 | len = strlen(str); |
| 46 | } |
commit-bot@chromium.org | 536ac5e | 2014-02-11 22:38:51 +0000 | [diff] [blame] | 47 | |
| 48 | // [ 4 byte len ] [ str ... ] [1 - 4 \0s] |
| 49 | uint32_t* ptr = this->reservePad(sizeof(uint32_t) + len + 1); |
commit-bot@chromium.org | f117781 | 2014-04-23 19:19:44 +0000 | [diff] [blame] | 50 | *ptr = SkToU32(len); |
commit-bot@chromium.org | 536ac5e | 2014-02-11 22:38:51 +0000 | [diff] [blame] | 51 | char* chars = (char*)(ptr + 1); |
| 52 | memcpy(chars, str, len); |
| 53 | chars[len] = '\0'; |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | size_t SkWriter32::WriteStringSize(const char* str, size_t len) { |
| 57 | if ((long)len < 0) { |
| 58 | SkASSERT(str); |
| 59 | len = strlen(str); |
| 60 | } |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame] | 61 | const size_t lenBytes = 4; // we use 4 bytes to record the length |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 62 | // add 1 since we also write a terminating 0 |
| 63 | return SkAlign4(lenBytes + len + 1); |
| 64 | } |
commit-bot@chromium.org | 046f1f6 | 2014-02-11 10:17:02 +0000 | [diff] [blame] | 65 | |
| 66 | void SkWriter32::growToAtLeast(size_t size) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 67 | const bool wasExternal = (fExternal != nullptr) && (fData == fExternal); |
commit-bot@chromium.org | a87b21c | 2014-02-11 18:22:04 +0000 | [diff] [blame] | 68 | |
commit-bot@chromium.org | ca21a00 | 2014-02-13 18:35:54 +0000 | [diff] [blame] | 69 | fCapacity = 4096 + SkTMax(size, fCapacity + (fCapacity / 2)); |
| 70 | fInternal.realloc(fCapacity); |
| 71 | fData = fInternal.get(); |
| 72 | |
commit-bot@chromium.org | 046f1f6 | 2014-02-11 10:17:02 +0000 | [diff] [blame] | 73 | if (wasExternal) { |
| 74 | // we were external, so copy in the data |
| 75 | memcpy(fData, fExternal, fUsed); |
| 76 | } |
commit-bot@chromium.org | c30dcb9 | 2014-03-12 17:04:28 +0000 | [diff] [blame] | 77 | } |
| 78 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 79 | sk_sp<SkData> SkWriter32::snapshotAsData() const { |
| 80 | return SkData::MakeWithCopy(fData, fUsed); |
commit-bot@chromium.org | 046f1f6 | 2014-02-11 10:17:02 +0000 | [diff] [blame] | 81 | } |