blob: 7e31fb9e23879e64f647955dc8dd9ea642645c29 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#ifndef SkReader32_DEFINED
10#define SkReader32_DEFINED
11
reedf70b5312016-03-04 16:36:20 -080012#include "SkData.h"
djsollen@google.com2b2ede32012-04-12 13:24:04 +000013#include "SkMatrix.h"
djsollen@google.com94e75ee2012-06-08 18:30:46 +000014#include "SkPath.h"
djsollen@google.com2b2ede32012-04-12 13:24:04 +000015#include "SkRegion.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000016#include "SkRRect.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkScalar.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000018
reed@google.comfd0ffcf2011-06-21 15:43:11 +000019class SkString;
20
reed@android.com8a1c16f2008-12-17 15:59:43 +000021class SkReader32 : SkNoncopyable {
22public:
halcanary96fcdcc2015-08-27 07:41:13 -070023 SkReader32() : fCurr(nullptr), fStop(nullptr), fBase(nullptr) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 SkReader32(const void* data, size_t size) {
25 this->setMemory(data, size);
26 }
27
28 void setMemory(const void* data, size_t size) {
29 SkASSERT(ptr_align_4(data));
30 SkASSERT(SkAlign4(size) == size);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 fBase = fCurr = (const char*)data;
33 fStop = (const char*)data + size;
34 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +000036 size_t size() const { return fStop - fBase; }
37 size_t offset() const { return fCurr - fBase; }
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 bool eof() const { return fCurr >= fStop; }
39 const void* base() const { return fBase; }
40 const void* peek() const { return fCurr; }
reed@google.com5b3d5342011-04-26 17:49:03 +000041
commit-bot@chromium.orgf1177812014-04-23 19:19:44 +000042 size_t available() const { return fStop - fCurr; }
commit-bot@chromium.org1ac99c82014-04-29 15:35:23 +000043 bool isAvailable(size_t size) const { return size <= this->available(); }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 void rewind() { fCurr = fBase; }
46
47 void setOffset(size_t offset) {
48 SkASSERT(SkAlign4(offset) == offset);
49 SkASSERT(offset <= this->size());
50 fCurr = fBase + offset;
51 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 bool readBool() { return this->readInt() != 0; }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000054
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 int32_t readInt() {
56 SkASSERT(ptr_align_4(fCurr));
57 int32_t value = *(const int32_t*)fCurr;
58 fCurr += sizeof(value);
59 SkASSERT(fCurr <= fStop);
60 return value;
61 }
reed@google.com51c62a62012-06-12 20:47:53 +000062
63 void* readPtr() {
64 void* ptr;
65 // we presume this "if" is resolved at compile-time
66 if (4 == sizeof(void*)) {
67 ptr = *(void**)fCurr;
68 } else {
69 memcpy(&ptr, fCurr, sizeof(void*));
70 }
71 fCurr += sizeof(void*);
72 return ptr;
73 }
74
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 SkScalar readScalar() {
76 SkASSERT(ptr_align_4(fCurr));
77 SkScalar value = *(const SkScalar*)fCurr;
78 fCurr += sizeof(value);
79 SkASSERT(fCurr <= fStop);
80 return value;
81 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 const void* skip(size_t size) {
84 SkASSERT(ptr_align_4(fCurr));
85 const void* addr = fCurr;
86 fCurr += SkAlign4(size);
87 SkASSERT(fCurr <= fStop);
88 return addr;
89 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000090
reed@google.coma5adf532011-09-07 13:52:17 +000091 template <typename T> const T& skipT() {
92 SkASSERT(SkAlign4(sizeof(T)) == sizeof(T));
93 return *(const T*)this->skip(sizeof(T));
94 }
95
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 void read(void* dst, size_t size) {
halcanary96fcdcc2015-08-27 07:41:13 -070097 SkASSERT(0 == size || dst != nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 SkASSERT(ptr_align_4(fCurr));
99 memcpy(dst, fCurr, size);
100 fCurr += SkAlign4(size);
101 SkASSERT(fCurr <= fStop);
102 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000103
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 uint8_t readU8() { return (uint8_t)this->readInt(); }
105 uint16_t readU16() { return (uint16_t)this->readInt(); }
106 int32_t readS32() { return this->readInt(); }
107 uint32_t readU32() { return this->readInt(); }
reed@google.comdde09562011-05-23 12:21:05 +0000108
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000109 bool readPath(SkPath* path) {
dandov963137b2014-08-07 07:49:53 -0700110 return this->readObjectFromMemory(path);
djsollen@google.com94e75ee2012-06-08 18:30:46 +0000111 }
112
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000113 bool readMatrix(SkMatrix* matrix) {
dandov963137b2014-08-07 07:49:53 -0700114 return this->readObjectFromMemory(matrix);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000115 }
116
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000117 bool readRRect(SkRRect* rrect) {
dandov963137b2014-08-07 07:49:53 -0700118 return this->readObjectFromMemory(rrect);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000119 }
120
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000121 bool readRegion(SkRegion* rgn) {
dandov963137b2014-08-07 07:49:53 -0700122 return this->readObjectFromMemory(rgn);
djsollen@google.com2b2ede32012-04-12 13:24:04 +0000123 }
124
reed@google.comdde09562011-05-23 12:21:05 +0000125 /**
reed@google.comfd0ffcf2011-06-21 15:43:11 +0000126 * Read the length of a string (written by SkWriter32::writeString) into
halcanary96fcdcc2015-08-27 07:41:13 -0700127 * len (if len is not nullptr) and return the null-ternimated address of the
reed@google.comfd0ffcf2011-06-21 15:43:11 +0000128 * string within the reader's buffer.
reed@google.comdde09562011-05-23 12:21:05 +0000129 */
halcanary96fcdcc2015-08-27 07:41:13 -0700130 const char* readString(size_t* len = nullptr);
reed@google.comdde09562011-05-23 12:21:05 +0000131
reed@google.comfd0ffcf2011-06-21 15:43:11 +0000132 /**
133 * Read the string (written by SkWriter32::writeString) and return it in
134 * copy (if copy is not null). Return the length of the string.
135 */
136 size_t readIntoString(SkString* copy);
137
reedfde05112016-03-11 13:02:28 -0800138 sk_sp<SkData> readData() {
reedf70b5312016-03-04 16:36:20 -0800139 uint32_t byteLength = this->readU32();
140 if (0 == byteLength) {
reedfde05112016-03-11 13:02:28 -0800141 return SkData::MakeEmpty();
reedf70b5312016-03-04 16:36:20 -0800142 }
reedfde05112016-03-11 13:02:28 -0800143 return SkData::MakeWithCopy(this->skip(byteLength), byteLength);
reedf70b5312016-03-04 16:36:20 -0800144 }
145
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146private:
commit-bot@chromium.org4faa8692013-11-05 15:46:56 +0000147 template <typename T> bool readObjectFromMemory(T* obj) {
148 size_t size = obj->readFromMemory(this->peek(), this->available());
149 // If readFromMemory() fails (which means that available() was too small), it returns 0
150 bool success = (size > 0) && (size <= this->available()) && (SkAlign4(size) == size);
151 // In case of failure, we want to skip to the end
152 (void)this->skip(success ? size : this->available());
153 return success;
154 }
155
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 // these are always 4-byte aligned
157 const char* fCurr; // current position within buffer
158 const char* fStop; // end of buffer
159 const char* fBase; // beginning of buffer
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000160
reed@android.com8a1c16f2008-12-17 15:59:43 +0000161#ifdef SK_DEBUG
reed@google.coma5adf532011-09-07 13:52:17 +0000162 static bool ptr_align_4(const void* ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -0700163 return (((const char*)ptr - (const char*)nullptr) & 3) == 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000164 }
165#endif
166};
167
168#endif