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  */
7 
8 #ifndef SkReadBuffer_DEFINED
9 #define SkReadBuffer_DEFINED
10 
11 #include "SkColorFilter.h"
12 #include "SkSerialProcs.h"
13 #include "SkDrawLooper.h"
14 #include "SkFont.h"
15 #include "SkImageFilter.h"
16 #include "SkMaskFilterBase.h"
17 #include "SkPaintPriv.h"
18 #include "SkPath.h"
19 #include "SkPathEffect.h"
20 #include "SkPicture.h"
21 #include "SkReader32.h"
22 #include "SkRefCnt.h"
23 #include "SkShaderBase.h"
24 #include "SkWriteBuffer.h"
25 
26 class SkData;
27 class SkImage;
28 
29 #ifndef SK_DISABLE_READBUFFER
30 
31 class SkReadBuffer {
32 public:
33     SkReadBuffer();
34     SkReadBuffer(const void* data, size_t size);
35 
36     enum Version {
37         kTileModeInBlurImageFilter_Version = 56,
38         kTileInfoInSweepGradient_Version   = 57,
39         k2PtConicalNoFlip_Version          = 58,
40         kRemovePictureImageFilterLocalSpace = 59,
41         kRemoveHeaderFlags_Version         = 60,
42         kTwoColorDrawShadow_Version        = 61,
43         kDontNegateImageSize_Version       = 62,
44         kStoreImageBounds_Version          = 63,
45         kRemoveOccluderFromBlurMaskFilter  = 64,
46         kFloat4PaintColor_Version          = 65,
47         kSaveBehind_Version                = 66,
48         kSerializeFonts_Version            = 67,
49         kPaintDoesntSerializeFonts_Version = 68,
50     };
51 
52     /**
53      *  Returns true IFF the version is older than the specified version.
54      */
isVersionLT(Version targetVersion)55     bool isVersionLT(Version targetVersion) const {
56         SkASSERT(targetVersion > 0);
57         return fVersion > 0 && fVersion < targetVersion;
58     }
59 
getVersion()60     uint32_t getVersion() const { return fVersion; }
61 
62     /** This may be called at most once; most clients of SkReadBuffer should not mess with it. */
setVersion(int version)63     void setVersion(int version) {
64         SkASSERT(0 == fVersion || version == fVersion);
65         fVersion = version;
66     }
67 
size()68     size_t size() const { return fReader.size(); }
offset()69     size_t offset() const { return fReader.offset(); }
eof()70     bool eof() { return fReader.eof(); }
71     const void* skip(size_t size);
72     const void* skip(size_t count, size_t size);    // does safe multiply
available()73     size_t available() const { return fReader.available(); }
74 
skipT()75     template <typename T> const T* skipT() {
76         return static_cast<const T*>(this->skip(sizeof(T)));
77     }
skipT(size_t count)78     template <typename T> const T* skipT(size_t count) {
79         return static_cast<const T*>(this->skip(count, sizeof(T)));
80     }
81 
82     // primitives
83     bool readBool();
84     SkColor readColor();
85     int32_t readInt();
86     SkScalar readScalar();
87     uint32_t readUInt();
88     int32_t read32();
89 
read32LE(T max)90     template <typename T> T read32LE(T max) {
91         uint32_t value = this->readUInt();
92         if (!this->validate(value <= static_cast<uint32_t>(max))) {
93             value = 0;
94         }
95         return static_cast<T>(value);
96     }
97 
98     // peek
99     uint8_t peekByte();
100 
101     // strings -- the caller is responsible for freeing the string contents
102     void readString(SkString* string);
103 
104     // common data structures
105     void readColor4f(SkColor4f* color);
106     void readPoint(SkPoint* point);
readPoint()107     SkPoint readPoint() { SkPoint p; this->readPoint(&p); return p; }
108     void readPoint3(SkPoint3* point);
109     void readMatrix(SkMatrix* matrix);
110     void readIRect(SkIRect* rect);
111     void readRect(SkRect* rect);
112     void readRRect(SkRRect* rrect);
113     void readRegion(SkRegion* region);
114 
115     void readPath(SkPath* path);
116 
readPaint(SkPaint * paint,SkFont * font)117     SkReadPaintResult readPaint(SkPaint* paint, SkFont* font) {
118         return SkPaintPriv::Unflatten(paint, *this, font);
119     }
120 
121     SkFlattenable* readFlattenable(SkFlattenable::Type);
readFlattenable()122     template <typename T> sk_sp<T> readFlattenable() {
123         return sk_sp<T>((T*)this->readFlattenable(T::GetFlattenableType()));
124     }
readColorFilter()125     sk_sp<SkColorFilter> readColorFilter() { return this->readFlattenable<SkColorFilter>(); }
readDrawLooper()126     sk_sp<SkDrawLooper> readDrawLooper() { return this->readFlattenable<SkDrawLooper>(); }
readImageFilter()127     sk_sp<SkImageFilter> readImageFilter() { return this->readFlattenable<SkImageFilter>(); }
readMaskFilter()128     sk_sp<SkMaskFilter> readMaskFilter() { return this->readFlattenable<SkMaskFilterBase>(); }
readPathEffect()129     sk_sp<SkPathEffect> readPathEffect() { return this->readFlattenable<SkPathEffect>(); }
readShader()130     sk_sp<SkShader> readShader() { return this->readFlattenable<SkShaderBase>(); }
131 
132     // Reads SkAlign4(bytes), but will only copy bytes into the buffer.
133     bool readPad32(void* buffer, size_t bytes);
134 
135     // binary data and arrays
136     bool readByteArray(void* value, size_t size);
137     bool readColorArray(SkColor* colors, size_t size);
138     bool readColor4fArray(SkColor4f* colors, size_t size);
139     bool readIntArray(int32_t* values, size_t size);
140     bool readPointArray(SkPoint* points, size_t size);
141     bool readScalarArray(SkScalar* values, size_t size);
142 
143     sk_sp<SkData> readByteArrayAsData();
144 
145     // helpers to get info about arrays and binary data
146     uint32_t getArrayCount();
147 
148     // If there is a real error (e.g. data is corrupted) this returns null. If the image cannot
149     // be created (e.g. it was not originally encoded) then this returns an image that doesn't
150     // draw.
151     sk_sp<SkImage> readImage();
152     sk_sp<SkTypeface> readTypeface();
153 
setTypefaceArray(sk_sp<SkTypeface> array[],int count)154     void setTypefaceArray(sk_sp<SkTypeface> array[], int count) {
155         fTFArray = array;
156         fTFCount = count;
157     }
158 
159     /**
160      *  Call this with a pre-loaded array of Factories, in the same order as
161      *  were created/written by the writer. SkPicture uses this.
162      */
setFactoryPlayback(SkFlattenable::Factory array[],int count)163     void setFactoryPlayback(SkFlattenable::Factory array[], int count) {
164         fFactoryArray = array;
165         fFactoryCount = count;
166     }
167 
168     void setDeserialProcs(const SkDeserialProcs& procs);
getDeserialProcs()169     const SkDeserialProcs& getDeserialProcs() const { return fProcs; }
170 
171     /**
172      *  If isValid is false, sets the buffer to be "invalid". Returns true if the buffer
173      *  is still valid.
174      */
validate(bool isValid)175     bool validate(bool isValid) {
176         if (!isValid) {
177             this->setInvalid();
178         }
179         return !fError;
180     }
181 
182     /**
183      * Helper function to do a preflight check before a large allocation or read.
184      * Returns true if there is enough bytes in the buffer to read n elements of T.
185      * If not, the buffer will be "invalid" and false will be returned.
186      */
187     template <typename T>
validateCanReadN(size_t n)188     bool validateCanReadN(size_t n) {
189         return this->validate(n <= (fReader.available() / sizeof(T)));
190     }
191 
isValid()192     bool isValid() const { return !fError; }
validateIndex(int index,int count)193     bool validateIndex(int index, int count) {
194         return this->validate(index >= 0 && index < count);
195     }
196 
197     // Utilities that mark the buffer invalid if the requested value is out-of-range
198 
199     // If the read value is outside of the range, validate(false) is called, and min
200     // is returned, else the value is returned.
201     int32_t checkInt(int min, int max);
202 
checkRange(T min,T max)203     template <typename T> T checkRange(T min, T max) {
204         return static_cast<T>(this->checkInt(static_cast<int32_t>(min),
205                                              static_cast<int32_t>(max)));
206     }
207 
208     SkFilterQuality checkFilterQuality();
209 
210 private:
211     void setInvalid();
212     bool readArray(void* value, size_t size, size_t elementSize);
213     void setMemory(const void*, size_t);
214 
215     SkReader32 fReader;
216 
217     // Only used if we do not have an fFactoryArray.
218     SkTHashMap<uint32_t, SkFlattenable::Factory> fFlattenableDict;
219 
220     int fVersion;
221 
222     sk_sp<SkTypeface>* fTFArray;
223     int                fTFCount;
224 
225     SkFlattenable::Factory* fFactoryArray;
226     int                     fFactoryCount;
227 
228     SkDeserialProcs fProcs;
229 
IsPtrAlign4(const void * ptr)230     static bool IsPtrAlign4(const void* ptr) {
231         return SkIsAlign4((uintptr_t)ptr);
232     }
233 
234     bool fError = false;
235 };
236 
237 #else // #ifndef SK_DISABLE_READBUFFER
238 
239 class SkReadBuffer {
240 public:
SkReadBuffer()241     SkReadBuffer() {}
SkReadBuffer(const void *,size_t)242     SkReadBuffer(const void*, size_t) {}
243 
244     enum Version {
245         kTileModeInBlurImageFilter_Version = 56,
246         kTileInfoInSweepGradient_Version   = 57,
247         k2PtConicalNoFlip_Version          = 58,
248         kRemovePictureImageFilterLocalSpace = 59,
249         kRemoveHeaderFlags_Version         = 60,
250         kTwoColorDrawShadow_Version        = 61,
251         kDontNegateImageSize_Version       = 62,
252         kStoreImageBounds_Version          = 63,
253         kRemoveOccluderFromBlurMaskFilter  = 64,
254         kFloat4PaintColor_Version          = 65,
255         kSaveBehind_Version                = 66,
256         kSerializeFonts_Version            = 67,
257         kPaintDoesntSerializeFonts_Version = 68,
258     };
259 
isVersionLT(Version)260     bool isVersionLT(Version) const { return false; }
getVersion()261     uint32_t getVersion() const { return 0xffffffff; }
setVersion(int)262     void     setVersion(int) {}
263 
size()264     size_t size() const { return 0; }
offset()265     size_t offset() const { return 0; }
eof()266     bool eof() { return true; }
available()267     size_t available() const { return 0; }
268 
skip(size_t)269     const void* skip(size_t)         { return nullptr; }
skip(size_t,size_t)270     const void* skip(size_t, size_t) { return nullptr; }
skipT()271     template <typename T> const T* skipT()       { return nullptr; }
skipT(size_t)272     template <typename T> const T* skipT(size_t) { return nullptr; }
273 
readBool()274     bool     readBool()   { return 0; }
readColor()275     SkColor  readColor()  { return 0; }
readInt()276     int32_t  readInt()    { return 0; }
readScalar()277     SkScalar readScalar() { return 0; }
readUInt()278     uint32_t readUInt()   { return 0; }
read32()279     int32_t  read32()     { return 0; }
280 
read32LE(T max)281     template <typename T> T read32LE(T max) { return max; }
282 
peekByte()283     uint8_t  peekByte()   { return 0; }
284 
readColor4f(SkColor4f * out)285     void readColor4f(SkColor4f* out) { *out = SkColor4f{0,0,0,0}; }
readPoint(SkPoint * out)286     void readPoint  (SkPoint*   out) { *out = SkPoint{0,0};       }
readPoint3(SkPoint3 * out)287     void readPoint3 (SkPoint3*  out) { *out = SkPoint3{0,0,0};    }
readMatrix(SkMatrix * out)288     void readMatrix (SkMatrix*  out) { *out = SkMatrix::I();      }
readIRect(SkIRect * out)289     void readIRect  (SkIRect*   out) { *out = SkIRect{0,0,0,0};   }
readRect(SkRect * out)290     void readRect   (SkRect*    out) { *out = SkRect{0,0,0,0};    }
readRRect(SkRRect * out)291     void readRRect  (SkRRect*   out) { *out = SkRRect();          }
readRegion(SkRegion * out)292     void readRegion (SkRegion*  out) { *out = SkRegion();         }
readString(SkString * out)293     void readString (SkString*  out) { *out = SkString();         }
readPath(SkPath * out)294     void readPath   (SkPath*    out) { *out = SkPath();           }
readPaint(SkPaint * out,SkFont * font)295     SkReadPaintResult readPaint  (SkPaint*   out, SkFont* font) {
296         *out = SkPaint();
297         if (font) {
298             *font = SkFont();
299         }
300         return kFailed_ReadPaint;
301     }
302 
readPoint()303     SkPoint readPoint() { return {0,0}; }
304 
readFlattenable(SkFlattenable::Type)305     SkFlattenable* readFlattenable(SkFlattenable::Type) { return nullptr; }
306 
readFlattenable()307     template <typename T> sk_sp<T> readFlattenable() { return nullptr; }
readColorFilter()308     sk_sp<SkColorFilter> readColorFilter() { return nullptr; }
readDrawLooper()309     sk_sp<SkDrawLooper>  readDrawLooper()  { return nullptr; }
readImageFilter()310     sk_sp<SkImageFilter> readImageFilter() { return nullptr; }
readMaskFilter()311     sk_sp<SkMaskFilter>  readMaskFilter()  { return nullptr; }
readPathEffect()312     sk_sp<SkPathEffect>  readPathEffect()  { return nullptr; }
readShader()313     sk_sp<SkShader>      readShader()      { return nullptr; }
314 
readPad32(void *,size_t)315     bool readPad32       (void*,      size_t) { return false; }
readByteArray(void *,size_t)316     bool readByteArray   (void*,      size_t) { return false; }
readColorArray(SkColor *,size_t)317     bool readColorArray  (SkColor*,   size_t) { return false; }
readColor4fArray(SkColor4f *,size_t)318     bool readColor4fArray(SkColor4f*, size_t) { return false; }
readIntArray(int32_t *,size_t)319     bool readIntArray    (int32_t*,   size_t) { return false; }
readPointArray(SkPoint *,size_t)320     bool readPointArray  (SkPoint*,   size_t) { return false; }
readScalarArray(SkScalar *,size_t)321     bool readScalarArray (SkScalar*,  size_t) { return false; }
322 
readByteArrayAsData()323     sk_sp<SkData> readByteArrayAsData() { return nullptr; }
getArrayCount()324     uint32_t getArrayCount() { return 0; }
325 
readImage()326     sk_sp<SkImage>    readImage()    { return nullptr; }
readTypeface()327     sk_sp<SkTypeface> readTypeface() { return nullptr; }
328 
validate(bool)329     bool validate(bool)                                 { return false; }
validateCanReadN(size_t)330     template <typename T> bool validateCanReadN(size_t) { return false; }
isValid()331     bool isValid() const                                { return false; }
validateIndex(int,int)332     bool validateIndex(int, int)                        { return false; }
333 
checkInt(int min,int)334     int32_t checkInt(int min, int)               { return min; }
checkRange(T min,T)335     template <typename T> T checkRange(T min, T) { return min; }
336 
checkFilterQuality()337     SkFilterQuality checkFilterQuality() { return SkFilterQuality::kNone_SkFilterQuality; }
338 
setTypefaceArray(sk_sp<SkTypeface>[],int)339     void setTypefaceArray(sk_sp<SkTypeface>[], int)        {}
setFactoryPlayback(SkFlattenable::Factory[],int)340     void setFactoryPlayback(SkFlattenable::Factory[], int) {}
setDeserialProcs(const SkDeserialProcs &)341     void setDeserialProcs(const SkDeserialProcs&)          {}
342 
getDeserialProcs()343     const SkDeserialProcs& getDeserialProcs() const {
344         static const SkDeserialProcs procs;
345         return procs;
346     }
347 };
348 
349 #endif // #ifndef SK_DISABLE_READBUFFER
350 
351 #endif // SkReadBuffer_DEFINED
352