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

Side by Side Diff: src/core/SkSecureReader32.cpp

Issue 23021015: Initial error handling code (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: New SkSecureReadBuffer class Created 7 years, 3 months 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
OLDNEW
(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 ///////////////////////////////////////////////////////////////////////////////
9
10 #include "SkSecureReader32.h"
11 #include "SkString.h"
12
13 SkSecureReader32::SkSecureReader32() : fCurr(NULL), fStop(NULL), fBase(NULL), fE rror(false) {
14 }
15
16 SkSecureReader32::SkSecureReader32(const void* data, size_t size) : fError(false ) {
17 this->setMemory(data, size);
18 }
19
20 void SkSecureReader32::setMemory(const void* data, size_t size) {
21 fError |= (!ptr_align_4(data) || (SkAlign4(size) != size));
22 SkASSERT(!fError);
23
24 fBase = fCurr = (const char*)data;
25 fStop = (const char*)data + size;
26 }
27
28 uint32_t SkSecureReader32::size() const {
29 return SkToU32(fStop - fBase);
30 }
31
32 uint32_t SkSecureReader32::offset() const {
33 return SkToU32(fCurr - fBase);
34 }
35
36 bool SkSecureReader32::eof() const {
37 return fCurr >= fStop;
38 }
39
40 const void* SkSecureReader32::base() const {
41 return fBase;
42 }
43
44 const void* SkSecureReader32::peek() const {
45 return fCurr;
46 }
47
48 bool SkSecureReader32::isAvailable(uint32_t size) const {
49 return fCurr + size <= fStop;
50 }
51
52 bool SkSecureReader32::readBool() {
53 return this->readInt() != 0;
54 }
55
56 int32_t SkSecureReader32::readInt() {
57 int32_t value = 0;
58 size_t inc = sizeof(value);
59 fError |= !ptr_align_4(fCurr) || !this->isAvailable(inc);
60 if (!fError) {
61 value = *(const int32_t*)fCurr;
62 fCurr += inc;
63 }
64 return value;
65 }
66
67 SkScalar SkSecureReader32::readScalar() {
68 SkScalar value = 0;
69 size_t inc = sizeof(value);
70 fError |= !ptr_align_4(fCurr) || !this->isAvailable(inc);
71 if (!fError) {
72 value = *(const SkScalar*)fCurr;
73 fCurr += inc;
74 }
75 return value;
76 }
77
78 const void* SkSecureReader32::skip(size_t size) {
79 size_t inc = SkAlign4(size);
80 fError |= !ptr_align_4(fCurr) || !this->isAvailable(inc);
81 const void* addr = fCurr;
82 if (!fError) {
83 fCurr += inc;
84 }
85 return addr;
86 }
87
88 int32_t SkSecureReader32::readS32() {
89 return this->readInt();
90 }
91
92 uint32_t SkSecureReader32::readU32() {
93 return this->readInt();
94 }
95
96 void SkSecureReader32::readPath(SkPath* path) {
97 size_t size = path->readFromMemory(this->peek());
98 fError |= (SkAlign4(size) != size);
99 if (!fError) {
100 (void)this->skip(size);
101 }
102 }
103
104 void SkSecureReader32::readMatrix(SkMatrix* matrix) {
105 size_t size = matrix->readFromMemory(this->peek());
106 fError |= (SkAlign4(size) != size);
107 if (!fError) {
108 (void)this->skip(size);
109 }
110 }
111
112 void SkSecureReader32::readRegion(SkRegion* rgn) {
113 size_t size = rgn->readFromMemory(this->peek());
114 fError |= (SkAlign4(size) != size);
115 if (!fError) {
116 (void)this->skip(size);
117 }
118 }
119
120 /*
121 * Strings are stored as: length[4-bytes] + string_data + '\0' + pad_to_mul_4
122 */
123
124 const char* SkSecureReader32::readString(size_t* outLen) {
125 size_t len = this->readInt();
126 const void* ptr = this->peek();
127
128 // skip over the string + '\0' and then pad to a multiple of 4
129 size_t alignedSize = SkAlign4(len + 1);
130 this->skip(alignedSize);
131
132 if (outLen) {
133 *outLen = fError ? 0 : len;
134 }
135 return fError ? NULL : (const char*)ptr;
136 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698