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

Side by Side Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp

Issue 1410673005: Add type cast definitions for CPDF_Number. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 5 years, 2 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
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "../../../include/fpdfapi/fpdf_parser.h" 7 #include "../../../include/fpdfapi/fpdf_parser.h"
8 #include "../../../include/fxcrt/fx_string.h" 8 #include "../../../include/fxcrt/fx_string.h"
9 9
10 // static 10 // static
(...skipping 10 matching lines...) Expand all
21 case PDFOBJ_STRING: 21 case PDFOBJ_STRING:
22 delete (CPDF_String*)this; 22 delete (CPDF_String*)this;
23 break; 23 break;
24 case PDFOBJ_NAME: 24 case PDFOBJ_NAME:
25 delete (CPDF_Name*)this; 25 delete (CPDF_Name*)this;
26 break; 26 break;
27 case PDFOBJ_ARRAY: 27 case PDFOBJ_ARRAY:
28 delete (CPDF_Array*)this; 28 delete (CPDF_Array*)this;
29 break; 29 break;
30 case PDFOBJ_DICTIONARY: 30 case PDFOBJ_DICTIONARY:
31 delete this->AsDictionary(); 31 delete AsDictionary();
32 break; 32 break;
33 case PDFOBJ_STREAM: 33 case PDFOBJ_STREAM:
34 delete (CPDF_Stream*)this; 34 delete (CPDF_Stream*)this;
35 break; 35 break;
36 default: 36 default:
37 delete this; 37 delete this;
38 } 38 }
39 } 39 }
40 CFX_ByteString CPDF_Object::GetString() const { 40 CFX_ByteString CPDF_Object::GetString() const {
41 switch (m_Type) { 41 switch (m_Type) {
42 case PDFOBJ_BOOLEAN: 42 case PDFOBJ_BOOLEAN:
43 return AsBoolean()->m_bValue ? "true" : "false"; 43 return AsBoolean()->m_bValue ? "true" : "false";
44 case PDFOBJ_NUMBER: 44 case PDFOBJ_NUMBER:
45 return ((CPDF_Number*)this)->GetString(); 45 return AsNumber()->GetString();
46 case PDFOBJ_STRING: 46 case PDFOBJ_STRING:
47 return ((CPDF_String*)this)->m_String; 47 return ((CPDF_String*)this)->m_String;
48 case PDFOBJ_NAME: 48 case PDFOBJ_NAME:
49 return ((CPDF_Name*)this)->m_Name; 49 return ((CPDF_Name*)this)->m_Name;
50 case PDFOBJ_REFERENCE: { 50 case PDFOBJ_REFERENCE: {
51 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; 51 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this;
52 if (pRef->m_pObjList == NULL) { 52 if (pRef->m_pObjList == NULL) {
53 break; 53 break;
54 } 54 }
55 CPDF_Object* pObj = 55 CPDF_Object* pObj =
(...skipping 25 matching lines...) Expand all
81 return CFX_ByteStringC(); 81 return CFX_ByteStringC();
82 } 82 }
83 return pObj->GetConstString(); 83 return pObj->GetConstString();
84 } 84 }
85 } 85 }
86 return CFX_ByteStringC(); 86 return CFX_ByteStringC();
87 } 87 }
88 FX_FLOAT CPDF_Object::GetNumber() const { 88 FX_FLOAT CPDF_Object::GetNumber() const {
89 switch (m_Type) { 89 switch (m_Type) {
90 case PDFOBJ_NUMBER: 90 case PDFOBJ_NUMBER:
91 return ((CPDF_Number*)this)->GetNumber(); 91 return AsNumber()->GetNumber();
92 case PDFOBJ_REFERENCE: { 92 case PDFOBJ_REFERENCE: {
93 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; 93 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this;
94 if (pRef->m_pObjList == NULL) { 94 if (pRef->m_pObjList == NULL) {
95 break; 95 break;
96 } 96 }
97 CPDF_Object* pObj = 97 CPDF_Object* pObj =
98 pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum()); 98 pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum());
99 if (pObj == NULL) { 99 if (pObj == NULL) {
100 return 0; 100 return 0;
101 } 101 }
102 return pObj->GetNumber(); 102 return pObj->GetNumber();
103 } 103 }
104 } 104 }
105 return 0; 105 return 0;
106 } 106 }
107 FX_FLOAT CPDF_Object::GetNumber16() const { 107 FX_FLOAT CPDF_Object::GetNumber16() const {
108 return GetNumber(); 108 return GetNumber();
109 } 109 }
110 int CPDF_Object::GetInteger() const { 110 int CPDF_Object::GetInteger() const {
111 CFX_AutoRestorer<int> restorer(&s_nCurRefDepth); 111 CFX_AutoRestorer<int> restorer(&s_nCurRefDepth);
112 if (++s_nCurRefDepth > OBJECT_REF_MAX_DEPTH) { 112 if (++s_nCurRefDepth > OBJECT_REF_MAX_DEPTH) {
113 return 0; 113 return 0;
114 } 114 }
115 switch (m_Type) { 115 switch (m_Type) {
116 case PDFOBJ_BOOLEAN: 116 case PDFOBJ_BOOLEAN:
117 return this->AsBoolean()->m_bValue; 117 return AsBoolean()->m_bValue;
118 case PDFOBJ_NUMBER: 118 case PDFOBJ_NUMBER:
119 return ((CPDF_Number*)this)->GetInteger(); 119 return AsNumber()->GetInteger();
120 case PDFOBJ_REFERENCE: { 120 case PDFOBJ_REFERENCE: {
121 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; 121 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this;
122 PARSE_CONTEXT context; 122 PARSE_CONTEXT context;
123 FXSYS_memset(&context, 0, sizeof(PARSE_CONTEXT)); 123 FXSYS_memset(&context, 0, sizeof(PARSE_CONTEXT));
124 if (pRef->m_pObjList == NULL) { 124 if (pRef->m_pObjList == NULL) {
125 return 0; 125 return 0;
126 } 126 }
127 CPDF_Object* pObj = 127 CPDF_Object* pObj =
128 pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum(), &context); 128 pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum(), &context);
129 if (pObj == NULL) { 129 if (pObj == NULL) {
130 return 0; 130 return 0;
131 } 131 }
132 return pObj->GetInteger(); 132 return pObj->GetInteger();
133 } 133 }
134 } 134 }
135 return 0; 135 return 0;
136 } 136 }
137 137
138 CPDF_Dictionary* CPDF_Object::GetDict() const { 138 CPDF_Dictionary* CPDF_Object::GetDict() const {
139 switch (m_Type) { 139 switch (m_Type) {
140 case PDFOBJ_DICTIONARY: 140 case PDFOBJ_DICTIONARY:
141 // The method should be made non-const if we want to not be const. 141 // The method should be made non-const if we want to not be const.
142 // See bug #234. 142 // See bug #234.
143 return const_cast<CPDF_Dictionary*>(this->AsDictionary()); 143 return const_cast<CPDF_Dictionary*>(AsDictionary());
144 case PDFOBJ_STREAM: 144 case PDFOBJ_STREAM:
145 return ((CPDF_Stream*)this)->GetDict(); 145 return ((CPDF_Stream*)this)->GetDict();
146 case PDFOBJ_REFERENCE: { 146 case PDFOBJ_REFERENCE: {
147 CPDF_Reference* pRef = (CPDF_Reference*)this; 147 CPDF_Reference* pRef = (CPDF_Reference*)this;
148 CPDF_IndirectObjects* pIndirect = pRef->GetObjList(); 148 CPDF_IndirectObjects* pIndirect = pRef->GetObjList();
149 if (!pIndirect) 149 if (!pIndirect)
150 return nullptr; 150 return nullptr;
151 CPDF_Object* pObj = pIndirect->GetIndirectObject(pRef->GetRefObjNum()); 151 CPDF_Object* pObj = pIndirect->GetIndirectObject(pRef->GetRefObjNum());
152 if (!pObj || (pObj == this)) 152 if (!pObj || (pObj == this))
153 return nullptr; 153 return nullptr;
(...skipping 10 matching lines...) Expand all
164 164
165 return NULL; 165 return NULL;
166 } 166 }
167 void CPDF_Object::SetString(const CFX_ByteString& str) { 167 void CPDF_Object::SetString(const CFX_ByteString& str) {
168 ASSERT(this != NULL); 168 ASSERT(this != NULL);
169 switch (m_Type) { 169 switch (m_Type) {
170 case PDFOBJ_BOOLEAN: 170 case PDFOBJ_BOOLEAN:
171 AsBoolean()->m_bValue = (str == FX_BSTRC("true")); 171 AsBoolean()->m_bValue = (str == FX_BSTRC("true"));
172 return; 172 return;
173 case PDFOBJ_NUMBER: 173 case PDFOBJ_NUMBER:
174 ((CPDF_Number*)this)->SetString(str); 174 AsNumber()->SetString(str);
175 return; 175 return;
176 case PDFOBJ_STRING: 176 case PDFOBJ_STRING:
177 ((CPDF_String*)this)->m_String = str; 177 ((CPDF_String*)this)->m_String = str;
178 return; 178 return;
179 case PDFOBJ_NAME: 179 case PDFOBJ_NAME:
180 ((CPDF_Name*)this)->m_Name = str; 180 ((CPDF_Name*)this)->m_Name = str;
181 return; 181 return;
182 } 182 }
183 ASSERT(FALSE); 183 ASSERT(FALSE);
184 } 184 }
(...skipping 15 matching lines...) Expand all
200 if (m_Type == PDFOBJ_REFERENCE && GetDirect()) { 200 if (m_Type == PDFOBJ_REFERENCE && GetDirect()) {
201 return GetDirect()->IsIdentical(pOther); 201 return GetDirect()->IsIdentical(pOther);
202 } 202 }
203 if (pOther->m_Type == PDFOBJ_REFERENCE) { 203 if (pOther->m_Type == PDFOBJ_REFERENCE) {
204 return IsIdentical(pOther->GetDirect()); 204 return IsIdentical(pOther->GetDirect());
205 } 205 }
206 return FALSE; 206 return FALSE;
207 } 207 }
208 switch (m_Type) { 208 switch (m_Type) {
209 case PDFOBJ_BOOLEAN: 209 case PDFOBJ_BOOLEAN:
210 return this->AsBoolean()->Identical(pOther->AsBoolean()); 210 return AsBoolean()->Identical(pOther->AsBoolean());
211 case PDFOBJ_NUMBER: 211 case PDFOBJ_NUMBER:
212 return (((CPDF_Number*)this)->Identical((CPDF_Number*)pOther)); 212 return AsNumber()->Identical(pOther->AsNumber());
213 case PDFOBJ_STRING: 213 case PDFOBJ_STRING:
214 return (((CPDF_String*)this)->Identical((CPDF_String*)pOther)); 214 return (((CPDF_String*)this)->Identical((CPDF_String*)pOther));
215 case PDFOBJ_NAME: 215 case PDFOBJ_NAME:
216 return (((CPDF_Name*)this)->Identical((CPDF_Name*)pOther)); 216 return (((CPDF_Name*)this)->Identical((CPDF_Name*)pOther));
217 case PDFOBJ_ARRAY: 217 case PDFOBJ_ARRAY:
218 return (((CPDF_Array*)this)->Identical((CPDF_Array*)pOther)); 218 return (((CPDF_Array*)this)->Identical((CPDF_Array*)pOther));
219 case PDFOBJ_DICTIONARY: 219 case PDFOBJ_DICTIONARY:
220 return this->AsDictionary()->Identical(pOther->AsDictionary()); 220 return AsDictionary()->Identical(pOther->AsDictionary());
221 case PDFOBJ_NULL: 221 case PDFOBJ_NULL:
222 return TRUE; 222 return TRUE;
223 case PDFOBJ_STREAM: 223 case PDFOBJ_STREAM:
224 return (((CPDF_Stream*)this)->Identical((CPDF_Stream*)pOther)); 224 return (((CPDF_Stream*)this)->Identical((CPDF_Stream*)pOther));
225 case PDFOBJ_REFERENCE: 225 case PDFOBJ_REFERENCE:
226 return (((CPDF_Reference*)this)->Identical((CPDF_Reference*)pOther)); 226 return (((CPDF_Reference*)this)->Identical((CPDF_Reference*)pOther));
227 } 227 }
228 return FALSE; 228 return FALSE;
229 } 229 }
230 CPDF_Object* CPDF_Object::GetDirect() const { 230 CPDF_Object* CPDF_Object::GetDirect() const {
231 if (m_Type != PDFOBJ_REFERENCE) { 231 if (m_Type != PDFOBJ_REFERENCE) {
232 return (CPDF_Object*)this; 232 return (CPDF_Object*)this;
233 } 233 }
234 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; 234 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this;
235 if (pRef->m_pObjList == NULL) { 235 if (pRef->m_pObjList == NULL) {
236 return NULL; 236 return NULL;
237 } 237 }
238 return pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum()); 238 return pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum());
239 } 239 }
240 CPDF_Object* CPDF_Object::Clone(FX_BOOL bDirect) const { 240 CPDF_Object* CPDF_Object::Clone(FX_BOOL bDirect) const {
241 CFX_MapPtrToPtr visited; 241 CFX_MapPtrToPtr visited;
242 return CloneInternal(bDirect, &visited); 242 return CloneInternal(bDirect, &visited);
243 } 243 }
244 CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, 244 CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect,
245 CFX_MapPtrToPtr* visited) const { 245 CFX_MapPtrToPtr* visited) const {
246 switch (m_Type) { 246 switch (m_Type) {
247 case PDFOBJ_BOOLEAN: 247 case PDFOBJ_BOOLEAN:
248 return new CPDF_Boolean(this->AsBoolean()->m_bValue); 248 return new CPDF_Boolean(AsBoolean()->m_bValue);
249 case PDFOBJ_NUMBER: 249 case PDFOBJ_NUMBER: {
250 if (((CPDF_Number*)this)->m_bInteger) 250 const CPDF_Number* pThis = AsNumber();
251 return new CPDF_Number(((CPDF_Number*)this)->m_Integer); 251 if (pThis->m_bInteger)
Lei Zhang 2015/10/21 14:45:50 Combine into one ctor call? return new CPDF_Numbe
dsinclair 2015/10/21 15:00:11 Done.
252 return new CPDF_Number(((CPDF_Number*)this)->m_Float); 252 return new CPDF_Number(pThis->m_Integer);
253 return new CPDF_Number(pThis->m_Float);
254 }
253 case PDFOBJ_STRING: 255 case PDFOBJ_STRING:
254 return new CPDF_String(((CPDF_String*)this)->m_String, 256 return new CPDF_String(((CPDF_String*)this)->m_String,
255 ((CPDF_String*)this)->IsHex()); 257 ((CPDF_String*)this)->IsHex());
256 case PDFOBJ_NAME: 258 case PDFOBJ_NAME:
257 return new CPDF_Name(((CPDF_Name*)this)->m_Name); 259 return new CPDF_Name(((CPDF_Name*)this)->m_Name);
258 case PDFOBJ_ARRAY: { 260 case PDFOBJ_ARRAY: {
259 CPDF_Array* pCopy = new CPDF_Array(); 261 CPDF_Array* pCopy = new CPDF_Array();
260 CPDF_Array* pThis = (CPDF_Array*)this; 262 CPDF_Array* pThis = (CPDF_Array*)this;
261 int n = pThis->GetCount(); 263 int n = pThis->GetCount();
262 for (int i = 0; i < n; i++) { 264 for (int i = 0; i < n; i++) {
263 CPDF_Object* value = (CPDF_Object*)pThis->m_Objects.GetAt(i); 265 CPDF_Object* value = (CPDF_Object*)pThis->m_Objects.GetAt(i);
264 pCopy->m_Objects.Add(value->CloneInternal(bDirect, visited)); 266 pCopy->m_Objects.Add(value->CloneInternal(bDirect, visited));
265 } 267 }
266 return pCopy; 268 return pCopy;
267 } 269 }
268 case PDFOBJ_DICTIONARY: { 270 case PDFOBJ_DICTIONARY: {
269 CPDF_Dictionary* pCopy = new CPDF_Dictionary(); 271 CPDF_Dictionary* pCopy = new CPDF_Dictionary();
270 const CPDF_Dictionary* pThis = this->AsDictionary(); 272 const CPDF_Dictionary* pThis = AsDictionary();
271 FX_POSITION pos = pThis->m_Map.GetStartPosition(); 273 FX_POSITION pos = pThis->m_Map.GetStartPosition();
272 while (pos) { 274 while (pos) {
273 CFX_ByteString key; 275 CFX_ByteString key;
274 CPDF_Object* value; 276 CPDF_Object* value;
275 pThis->m_Map.GetNextAssoc(pos, key, (void*&)value); 277 pThis->m_Map.GetNextAssoc(pos, key, (void*&)value);
276 pCopy->m_Map.SetAt(key, value->CloneInternal(bDirect, visited)); 278 pCopy->m_Map.SetAt(key, value->CloneInternal(bDirect, visited));
277 } 279 }
278 return pCopy; 280 return pCopy;
279 } 281 }
280 case PDFOBJ_NULL: { 282 case PDFOBJ_NULL: {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 } 349 }
348 350
349 CPDF_Dictionary* CPDF_Object::AsDictionary() { 351 CPDF_Dictionary* CPDF_Object::AsDictionary() {
350 return IsDictionary() ? static_cast<CPDF_Dictionary*>(this) : nullptr; 352 return IsDictionary() ? static_cast<CPDF_Dictionary*>(this) : nullptr;
351 } 353 }
352 354
353 const CPDF_Dictionary* CPDF_Object::AsDictionary() const { 355 const CPDF_Dictionary* CPDF_Object::AsDictionary() const {
354 return IsDictionary() ? static_cast<const CPDF_Dictionary*>(this) : nullptr; 356 return IsDictionary() ? static_cast<const CPDF_Dictionary*>(this) : nullptr;
355 } 357 }
356 358
359 CPDF_Number* CPDF_Object::AsNumber() {
360 return IsNumber() ? static_cast<CPDF_Number*>(this) : nullptr;
361 }
362
363 const CPDF_Number* CPDF_Object::AsNumber() const {
364 return IsNumber() ? static_cast<const CPDF_Number*>(this) : nullptr;
365 }
366
357 CPDF_Number::CPDF_Number(int value) 367 CPDF_Number::CPDF_Number(int value)
358 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(TRUE), m_Integer(value) {} 368 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(TRUE), m_Integer(value) {}
359 369
360 CPDF_Number::CPDF_Number(FX_FLOAT value) 370 CPDF_Number::CPDF_Number(FX_FLOAT value)
361 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(FALSE), m_Float(value) {} 371 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(FALSE), m_Float(value) {}
362 372
363 CPDF_Number::CPDF_Number(const CFX_ByteStringC& str) 373 CPDF_Number::CPDF_Number(const CFX_ByteStringC& str)
364 : CPDF_Object(PDFOBJ_NUMBER) { 374 : CPDF_Object(PDFOBJ_NUMBER) {
365 FX_atonum(str, m_bInteger, &m_Integer); 375 FX_atonum(str, m_bInteger, &m_Integer);
366 } 376 }
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after
1211 } 1221 }
1212 pObj->m_ObjNum = objnum; 1222 pObj->m_ObjNum = objnum;
1213 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj); 1223 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj);
1214 if (m_LastObjNum < objnum) { 1224 if (m_LastObjNum < objnum) {
1215 m_LastObjNum = objnum; 1225 m_LastObjNum = objnum;
1216 } 1226 }
1217 } 1227 }
1218 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const { 1228 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const {
1219 return m_LastObjNum; 1229 return m_LastObjNum;
1220 } 1230 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698