reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #ifndef SkReader32_DEFINED |
| 10 | #define SkReader32_DEFINED |
| 11 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 12 | #include "SkData.h" |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 13 | #include "SkMatrix.h" |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 14 | #include "SkPath.h" |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 15 | #include "SkRegion.h" |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 16 | #include "SkRRect.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | #include "SkScalar.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame] | 19 | class SkString; |
| 20 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | class SkReader32 : SkNoncopyable { |
| 22 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 23 | SkReader32() : fCurr(nullptr), fStop(nullptr), fBase(nullptr) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 31 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | fBase = fCurr = (const char*)data; |
| 33 | fStop = (const char*)data + size; |
| 34 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 35 | |
commit-bot@chromium.org | f117781 | 2014-04-23 19:19:44 +0000 | [diff] [blame] | 36 | size_t size() const { return fStop - fBase; } |
| 37 | size_t offset() const { return fCurr - fBase; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | bool eof() const { return fCurr >= fStop; } |
| 39 | const void* base() const { return fBase; } |
| 40 | const void* peek() const { return fCurr; } |
reed@google.com | 5b3d534 | 2011-04-26 17:49:03 +0000 | [diff] [blame] | 41 | |
commit-bot@chromium.org | f117781 | 2014-04-23 19:19:44 +0000 | [diff] [blame] | 42 | size_t available() const { return fStop - fCurr; } |
commit-bot@chromium.org | 1ac99c8 | 2014-04-29 15:35:23 +0000 | [diff] [blame] | 43 | bool isAvailable(size_t size) const { return size <= this->available(); } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 44 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | bool readBool() { return this->readInt() != 0; } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | 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.com | 51c62a6 | 2012-06-12 20:47:53 +0000 | [diff] [blame] | 62 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 82 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 90 | |
reed@google.com | a5adf53 | 2011-09-07 13:52:17 +0000 | [diff] [blame] | 91 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | void read(void* dst, size_t size) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 97 | SkASSERT(0 == size || dst != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | SkASSERT(ptr_align_4(fCurr)); |
| 99 | memcpy(dst, fCurr, size); |
| 100 | fCurr += SkAlign4(size); |
| 101 | SkASSERT(fCurr <= fStop); |
| 102 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 103 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | 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.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 108 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 109 | bool readPath(SkPath* path) { |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 110 | return this->readObjectFromMemory(path); |
djsollen@google.com | 94e75ee | 2012-06-08 18:30:46 +0000 | [diff] [blame] | 111 | } |
| 112 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 113 | bool readMatrix(SkMatrix* matrix) { |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 114 | return this->readObjectFromMemory(matrix); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 115 | } |
| 116 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 117 | bool readRRect(SkRRect* rrect) { |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 118 | return this->readObjectFromMemory(rrect); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 119 | } |
| 120 | |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 121 | bool readRegion(SkRegion* rgn) { |
dandov | 963137b | 2014-08-07 07:49:53 -0700 | [diff] [blame] | 122 | return this->readObjectFromMemory(rgn); |
djsollen@google.com | 2b2ede3 | 2012-04-12 13:24:04 +0000 | [diff] [blame] | 123 | } |
| 124 | |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 125 | /** |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame] | 126 | * Read the length of a string (written by SkWriter32::writeString) into |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 127 | * len (if len is not nullptr) and return the null-ternimated address of the |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame] | 128 | * string within the reader's buffer. |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 129 | */ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 130 | const char* readString(size_t* len = nullptr); |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 131 | |
reed@google.com | fd0ffcf | 2011-06-21 15:43:11 +0000 | [diff] [blame] | 132 | /** |
| 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 | |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 138 | sk_sp<SkData> readData() { |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 139 | uint32_t byteLength = this->readU32(); |
| 140 | if (0 == byteLength) { |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 141 | return SkData::MakeEmpty(); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 142 | } |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 143 | return SkData::MakeWithCopy(this->skip(byteLength), byteLength); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 144 | } |
| 145 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | private: |
commit-bot@chromium.org | 4faa869 | 2013-11-05 15:46:56 +0000 | [diff] [blame] | 147 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | // 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 160 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | #ifdef SK_DEBUG |
reed@google.com | a5adf53 | 2011-09-07 13:52:17 +0000 | [diff] [blame] | 162 | static bool ptr_align_4(const void* ptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 163 | return (((const char*)ptr - (const char*)nullptr) & 3) == 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | } |
| 165 | #endif |
| 166 | }; |
| 167 | |
| 168 | #endif |