Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(793)

Side by Side Diff: src/core/SkBuffer.h

Issue 61913002: Adding error checks to SkRBuffer (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Added doc Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/core/SkFlattenableBuffers.h ('k') | src/core/SkBuffer.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #ifndef SkBuffer_DEFINED 10 #ifndef SkBuffer_DEFINED
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 size_t size() const { return fStop - fData; } 49 size_t size() const { return fStop - fData; }
50 /** Return true if the buffer has read to the end of the data pointer. 50 /** Return true if the buffer has read to the end of the data pointer.
51 Only defined if the length was specified in the constructor or in a call 51 Only defined if the length was specified in the constructor or in a call
52 to reset(). Always returns true if the length was not specified. 52 to reset(). Always returns true if the length was not specified.
53 */ 53 */
54 bool eof() const { return fPos >= fStop; } 54 bool eof() const { return fPos >= fStop; }
55 55
56 /** Read the specified number of bytes from the data pointer. If buffer is n ot 56 /** Read the specified number of bytes from the data pointer. If buffer is n ot
57 null, copy those bytes into buffer. 57 null, copy those bytes into buffer.
58 */ 58 */
59 virtual void read(void* buffer, size_t size) { 59 virtual bool read(void* buffer, size_t size) {
60 if (size) { 60 if (size) {
61 this->readNoSizeCheck(buffer, size); 61 this->readNoSizeCheck(buffer, size);
62 } 62 }
63 return true;
63 } 64 }
64 65
65 const void* skip(size_t size); // return start of skipped data 66 const void* skip(size_t size); // return start of skipped data
66 size_t skipToAlign4(); 67 size_t skipToAlign4();
67 68
68 void* readPtr() { void* ptr; read(&ptr, sizeof(ptr)); return ptr; } 69 bool readPtr(void** ptr) { return read(ptr, sizeof(void*)); }
69 SkScalar readScalar() { SkScalar x; read(&x, 4); return x; } 70 bool readScalar(SkScalar* x) { return read(x, 4); }
70 uint32_t readU32() { uint32_t x; read(&x, 4); return x; } 71 bool readU32(uint32_t* x) { return read(x, 4); }
71 int32_t readS32() { int32_t x; read(&x, 4); return x; } 72 bool readS32(int32_t* x) { return read(x, 4); }
72 uint16_t readU16() { uint16_t x; read(&x, 2); return x; } 73 bool readU16(uint16_t* x) { return read(x, 2); }
73 int16_t readS16() { int16_t x; read(&x, 2); return x; } 74 bool readS16(int16_t* x) { return read(x, 2); }
74 uint8_t readU8() { uint8_t x; read(&x, 1); return x; } 75 bool readU8(uint8_t* x) { return read(x, 1); }
75 bool readBool() { return this->readU8() != 0; } 76 bool readBool(bool* x) {
77 uint8_t u8;
78 if (this->readU8(&u8)) {
79 *x = (u8 != 0);
80 return true;
81 }
82 return false;
83 }
76 84
77 protected: 85 protected:
78 void readNoSizeCheck(void* buffer, size_t size); 86 void readNoSizeCheck(void* buffer, size_t size);
79 87
80 const char* fData; 88 const char* fData;
81 const char* fPos; 89 const char* fPos;
82 const char* fStop; 90 const char* fStop;
83 }; 91 };
84 92
85 /** \class SkRBufferWithSizeCheck 93 /** \class SkRBufferWithSizeCheck
86 94
87 Same as SkRBuffer, except that a size check is performed before the read ope ration and an 95 Same as SkRBuffer, except that a size check is performed before the read ope ration and an
88 error is set if the read operation is attempting to read past the end of the data. 96 error is set if the read operation is attempting to read past the end of the data.
89 */ 97 */
90 class SkRBufferWithSizeCheck : public SkRBuffer { 98 class SkRBufferWithSizeCheck : public SkRBuffer {
91 public: 99 public:
92 SkRBufferWithSizeCheck(const void* data, size_t size) : SkRBuffer(data, size ), fError(false) {} 100 SkRBufferWithSizeCheck(const void* data, size_t size) : SkRBuffer(data, size ), fError(false) {}
93 101
94 /** Read the specified number of bytes from the data pointer. If buffer is n ot 102 /** Read the specified number of bytes from the data pointer. If buffer is n ot
95 null and the number of bytes to read does not overflow this object's dat a, 103 null and the number of bytes to read does not overflow this object's dat a,
96 copy those bytes into buffer. 104 copy those bytes into buffer.
97 */ 105 */
98 virtual void read(void* buffer, size_t size) SK_OVERRIDE; 106 virtual bool read(void* buffer, size_t size) SK_OVERRIDE;
99 107
100 /** Returns whether or not a read operation attempted to read past the end o f the data. 108 /** Returns whether or not a read operation attempted to read past the end o f the data.
101 */ 109 */
102 bool isValid() const { return !fError; } 110 bool isValid() const { return !fError; }
103 private: 111 private:
104 bool fError; 112 bool fError;
105 }; 113 };
106 114
107 /** \class SkWBuffer 115 /** \class SkWBuffer
108 116
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 159
152 private: 160 private:
153 void writeNoSizeCheck(const void* buffer, size_t size); 161 void writeNoSizeCheck(const void* buffer, size_t size);
154 162
155 char* fData; 163 char* fData;
156 char* fPos; 164 char* fPos;
157 char* fStop; 165 char* fStop;
158 }; 166 };
159 167
160 #endif 168 #endif
OLDNEW
« no previous file with comments | « include/core/SkFlattenableBuffers.h ('k') | src/core/SkBuffer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698