OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 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 */ |
| 7 |
| 8 #include "SkBitmap.h" |
| 9 #include "SkErrorInternals.h" |
| 10 #include "SkSecureReadBuffer.h" |
| 11 #include "SkStream.h" |
| 12 #include "SkTypeface.h" |
| 13 |
| 14 SkSecureReadBuffer::SkSecureReadBuffer() : INHERITED() { |
| 15 fMemoryPtr = NULL; |
| 16 |
| 17 fBitmapStorage = NULL; |
| 18 fTFArray = NULL; |
| 19 fTFCount = 0; |
| 20 |
| 21 fFactoryTDArray = NULL; |
| 22 fFactoryArray = NULL; |
| 23 fFactoryCount = 0; |
| 24 fBitmapDecoder = NULL; |
| 25 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
| 26 fDecodedBitmapIndex = -1; |
| 27 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
| 28 |
| 29 setFlags(SkFlattenableReadBuffer::kSecure_Flag); |
| 30 } |
| 31 |
| 32 SkSecureReadBuffer::SkSecureReadBuffer(const void* data, size_t size) : INHERITE
D() { |
| 33 fReader.setMemory(data, size); |
| 34 fMemoryPtr = NULL; |
| 35 |
| 36 fBitmapStorage = NULL; |
| 37 fTFArray = NULL; |
| 38 fTFCount = 0; |
| 39 |
| 40 fFactoryTDArray = NULL; |
| 41 fFactoryArray = NULL; |
| 42 fFactoryCount = 0; |
| 43 fBitmapDecoder = NULL; |
| 44 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
| 45 fDecodedBitmapIndex = -1; |
| 46 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
| 47 |
| 48 setFlags(SkFlattenableReadBuffer::kSecure_Flag); |
| 49 } |
| 50 |
| 51 SkSecureReadBuffer::SkSecureReadBuffer(SkStream* stream) { |
| 52 const size_t length = stream->getLength(); |
| 53 fMemoryPtr = sk_malloc_throw(length); |
| 54 stream->read(fMemoryPtr, length); |
| 55 fReader.setMemory(fMemoryPtr, length); |
| 56 |
| 57 fBitmapStorage = NULL; |
| 58 fTFArray = NULL; |
| 59 fTFCount = 0; |
| 60 |
| 61 fFactoryTDArray = NULL; |
| 62 fFactoryArray = NULL; |
| 63 fFactoryCount = 0; |
| 64 fBitmapDecoder = NULL; |
| 65 #ifdef DEBUG_NON_DETERMINISTIC_ASSERT |
| 66 fDecodedBitmapIndex = -1; |
| 67 #endif // DEBUG_NON_DETERMINISTIC_ASSERT |
| 68 |
| 69 setFlags(SkFlattenableReadBuffer::kSecure_Flag); |
| 70 } |
| 71 |
| 72 SkSecureReadBuffer::~SkSecureReadBuffer() { |
| 73 sk_free(fMemoryPtr); |
| 74 SkSafeUnref(fBitmapStorage); |
| 75 } |
| 76 |
| 77 bool SkSecureReadBuffer::readBool() { |
| 78 return fReader.readBool(); |
| 79 } |
| 80 |
| 81 SkColor SkSecureReadBuffer::readColor() { |
| 82 return fReader.readInt(); |
| 83 } |
| 84 |
| 85 SkFixed SkSecureReadBuffer::readFixed() { |
| 86 return fReader.readS32(); |
| 87 } |
| 88 |
| 89 int32_t SkSecureReadBuffer::readInt() { |
| 90 return fReader.readInt(); |
| 91 } |
| 92 |
| 93 SkScalar SkSecureReadBuffer::readScalar() { |
| 94 return fReader.readScalar(); |
| 95 } |
| 96 |
| 97 uint32_t SkSecureReadBuffer::readUInt() { |
| 98 return fReader.readU32(); |
| 99 } |
| 100 |
| 101 int32_t SkSecureReadBuffer::read32() { |
| 102 return fReader.readInt(); |
| 103 } |
| 104 |
| 105 void SkSecureReadBuffer::readString(SkString* string) { |
| 106 size_t len; |
| 107 const char* strContents = fReader.readString(&len); |
| 108 string->set(strContents, len); |
| 109 } |
| 110 |
| 111 void* SkSecureReadBuffer::readEncodedString(size_t* length, SkPaint::TextEncodin
g encoding) { |
| 112 int32_t encodingType = fReader.readInt(); |
| 113 if (encodingType == encoding) { |
| 114 fReader.setError(); |
| 115 } |
| 116 *length = fReader.readInt(); |
| 117 const void* ptr = fReader.skip(SkAlign4(*length)); |
| 118 void* data = NULL; |
| 119 if (!fReader.getError()) { |
| 120 data = sk_malloc_throw(*length); |
| 121 memcpy(data, ptr, *length); |
| 122 } |
| 123 return data; |
| 124 } |
| 125 |
| 126 void SkSecureReadBuffer::readPoint(SkPoint* point) { |
| 127 point->fX = fReader.readScalar(); |
| 128 point->fY = fReader.readScalar(); |
| 129 } |
| 130 |
| 131 void SkSecureReadBuffer::readMatrix(SkMatrix* matrix) { |
| 132 fReader.readMatrix(matrix); |
| 133 } |
| 134 |
| 135 void SkSecureReadBuffer::readIRect(SkIRect* rect) { |
| 136 memcpy(rect, fReader.skip(sizeof(SkIRect)), sizeof(SkIRect)); |
| 137 } |
| 138 |
| 139 void SkSecureReadBuffer::readRect(SkRect* rect) { |
| 140 memcpy(rect, fReader.skip(sizeof(SkRect)), sizeof(SkRect)); |
| 141 } |
| 142 |
| 143 void SkSecureReadBuffer::readRegion(SkRegion* region) { |
| 144 fReader.readRegion(region); |
| 145 } |
| 146 |
| 147 void SkSecureReadBuffer::readPath(SkPath* path) { |
| 148 fReader.readPath(path); |
| 149 } |
| 150 |
| 151 uint32_t SkSecureReadBuffer::readByteArray(void* value) { |
| 152 const uint32_t length = fReader.readU32(); |
| 153 memcpy(value, fReader.skip(SkAlign4(length)), length); |
| 154 return length; |
| 155 } |
| 156 |
| 157 uint32_t SkSecureReadBuffer::readColorArray(SkColor* colors) { |
| 158 const uint32_t count = fReader.readU32(); |
| 159 const uint32_t byteLength = count * sizeof(SkColor); |
| 160 memcpy(colors, fReader.skip(SkAlign4(byteLength)), byteLength); |
| 161 return count; |
| 162 } |
| 163 |
| 164 uint32_t SkSecureReadBuffer::readIntArray(int32_t* values) { |
| 165 const uint32_t count = fReader.readU32(); |
| 166 const uint32_t byteLength = count * sizeof(int32_t); |
| 167 memcpy(values, fReader.skip(SkAlign4(byteLength)), byteLength); |
| 168 return count; |
| 169 } |
| 170 |
| 171 uint32_t SkSecureReadBuffer::readPointArray(SkPoint* points) { |
| 172 const uint32_t count = fReader.readU32(); |
| 173 const uint32_t byteLength = count * sizeof(SkPoint); |
| 174 memcpy(points, fReader.skip(SkAlign4(byteLength)), byteLength); |
| 175 return count; |
| 176 } |
| 177 |
| 178 uint32_t SkSecureReadBuffer::readScalarArray(SkScalar* values) { |
| 179 const uint32_t count = fReader.readU32(); |
| 180 const uint32_t byteLength = count * sizeof(SkScalar); |
| 181 memcpy(values, fReader.skip(SkAlign4(byteLength)), byteLength); |
| 182 return count; |
| 183 } |
| 184 |
| 185 uint32_t SkSecureReadBuffer::getArrayCount() { |
| 186 return *(uint32_t*)fReader.peek(); |
| 187 } |
| 188 |
| 189 void SkSecureReadBuffer::readBitmap(SkBitmap* bitmap) { |
| 190 const int width = this->readInt(); |
| 191 const int height = this->readInt(); |
| 192 const size_t length = this->readUInt(); |
| 193 // A size of zero means the SkBitmap was simply flattened. |
| 194 if (length != 0) { |
| 195 fReader.setError(); |
| 196 } |
| 197 if (fReader.getError()) { |
| 198 return; |
| 199 } |
| 200 bitmap->unflatten(*this); |
| 201 if ((bitmap->width() != width) || (bitmap->height() != height)) { |
| 202 fReader.setError(); |
| 203 } |
| 204 } |
| 205 |
| 206 SkTypeface* SkSecureReadBuffer::readTypeface() { |
| 207 |
| 208 uint32_t index = fReader.readU32(); |
| 209 if (0 == index || index > (unsigned)fTFCount || fReader.getError()) { |
| 210 if (index) { |
| 211 SkDebugf("====== typeface index %d\n", index); |
| 212 } |
| 213 return NULL; |
| 214 } else { |
| 215 SkASSERT(fTFArray); |
| 216 return fTFArray[index - 1]; |
| 217 } |
| 218 } |
| 219 |
| 220 SkFlattenable* SkSecureReadBuffer::readFlattenable() { |
| 221 SkString string; |
| 222 this->readString(&string); |
| 223 if (fReader.getError()) { |
| 224 return NULL; |
| 225 } |
| 226 SkFlattenable::Factory factory = SkFlattenable::NameToFactory(string.c_str()
); |
| 227 if (NULL == factory) { |
| 228 return NULL; // writer failed to give us the flattenable |
| 229 } |
| 230 |
| 231 // if we get here, factory may still be null, but if that is the case, the |
| 232 // failure was ours, not the writer. |
| 233 SkFlattenable* obj = NULL; |
| 234 uint32_t sizeRecorded = fReader.readU32(); |
| 235 if (factory) { |
| 236 uint32_t offset = fReader.offset(); |
| 237 obj = (*factory)(*this); |
| 238 // check that we read the amount we expected |
| 239 uint32_t sizeRead = fReader.offset() - offset; |
| 240 if (sizeRecorded != sizeRead) { |
| 241 // we could try to fix up the offset... |
| 242 fReader.setError(); |
| 243 delete obj; |
| 244 obj = NULL; |
| 245 } |
| 246 } else { |
| 247 // we must skip the remaining data |
| 248 fReader.skip(sizeRecorded); |
| 249 } |
| 250 return obj; |
| 251 } |
OLD | NEW |