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

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

Issue 1417933002: Add type cast definitions for CPDF_String. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 5 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
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
11 int CPDF_Object::s_nCurRefDepth = 0; 11 int CPDF_Object::s_nCurRefDepth = 0;
12 12
13 void CPDF_Object::Release() { 13 void CPDF_Object::Release() {
14 if (m_ObjNum) { 14 if (m_ObjNum) {
15 return; 15 return;
16 } 16 }
17 Destroy(); 17 Destroy();
18 } 18 }
19 void CPDF_Object::Destroy() { 19 void CPDF_Object::Destroy() {
20 switch (m_Type) { 20 switch (m_Type) {
21 case PDFOBJ_STRING: 21 case PDFOBJ_STRING:
22 delete (CPDF_String*)this; 22 delete AsString();
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 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 AsNumber()->GetString(); 45 return AsNumber()->GetString();
46 case PDFOBJ_STRING: 46 case PDFOBJ_STRING:
47 return ((CPDF_String*)this)->m_String; 47 return AsString()->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 =
56 pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum()); 56 pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum());
57 if (pObj == NULL) { 57 if (pObj == NULL) {
58 return CFX_ByteString(); 58 return CFX_ByteString();
59 } 59 }
60 return pObj->GetString(); 60 return pObj->GetString();
61 } 61 }
62 } 62 }
63 return CFX_ByteString(); 63 return CFX_ByteString();
64 } 64 }
65 CFX_ByteStringC CPDF_Object::GetConstString() const { 65 CFX_ByteStringC CPDF_Object::GetConstString() const {
66 switch (m_Type) { 66 switch (m_Type) {
67 case PDFOBJ_STRING: 67 case PDFOBJ_STRING: {
68 return CFX_ByteStringC((const uint8_t*)((CPDF_String*)this)->m_String, 68 CFX_ByteString str = AsString()->m_String;
69 ((CPDF_String*)this)->m_String.GetLength()); 69 return CFX_ByteStringC((const uint8_t*)str, str.GetLength());
70 }
70 case PDFOBJ_NAME: 71 case PDFOBJ_NAME:
71 return CFX_ByteStringC((const uint8_t*)((CPDF_Name*)this)->m_Name, 72 return CFX_ByteStringC((const uint8_t*)((CPDF_Name*)this)->m_Name,
72 ((CPDF_Name*)this)->m_Name.GetLength()); 73 ((CPDF_Name*)this)->m_Name.GetLength());
73 case PDFOBJ_REFERENCE: { 74 case PDFOBJ_REFERENCE: {
74 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; 75 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this;
75 if (pRef->m_pObjList == NULL) { 76 if (pRef->m_pObjList == NULL) {
76 break; 77 break;
77 } 78 }
78 CPDF_Object* pObj = 79 CPDF_Object* pObj =
79 pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum()); 80 pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum());
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 void CPDF_Object::SetString(const CFX_ByteString& str) { 168 void CPDF_Object::SetString(const CFX_ByteString& str) {
168 ASSERT(this != NULL); 169 ASSERT(this != NULL);
169 switch (m_Type) { 170 switch (m_Type) {
170 case PDFOBJ_BOOLEAN: 171 case PDFOBJ_BOOLEAN:
171 AsBoolean()->m_bValue = (str == FX_BSTRC("true")); 172 AsBoolean()->m_bValue = (str == FX_BSTRC("true"));
172 return; 173 return;
173 case PDFOBJ_NUMBER: 174 case PDFOBJ_NUMBER:
174 AsNumber()->SetString(str); 175 AsNumber()->SetString(str);
175 return; 176 return;
176 case PDFOBJ_STRING: 177 case PDFOBJ_STRING:
177 ((CPDF_String*)this)->m_String = str; 178 AsString()->m_String = str;
178 return; 179 return;
179 case PDFOBJ_NAME: 180 case PDFOBJ_NAME:
180 ((CPDF_Name*)this)->m_Name = str; 181 ((CPDF_Name*)this)->m_Name = str;
181 return; 182 return;
182 } 183 }
183 ASSERT(FALSE); 184 ASSERT(FALSE);
184 } 185 }
185 int CPDF_Object::GetDirectType() const { 186 int CPDF_Object::GetDirectType() const {
186 if (m_Type != PDFOBJ_REFERENCE) { 187 if (m_Type != PDFOBJ_REFERENCE) {
187 return m_Type; 188 return m_Type;
(...skipping 16 matching lines...) Expand all
204 return IsIdentical(pOther->GetDirect()); 205 return IsIdentical(pOther->GetDirect());
205 } 206 }
206 return FALSE; 207 return FALSE;
207 } 208 }
208 switch (m_Type) { 209 switch (m_Type) {
209 case PDFOBJ_BOOLEAN: 210 case PDFOBJ_BOOLEAN:
210 return AsBoolean()->Identical(pOther->AsBoolean()); 211 return AsBoolean()->Identical(pOther->AsBoolean());
211 case PDFOBJ_NUMBER: 212 case PDFOBJ_NUMBER:
212 return AsNumber()->Identical(pOther->AsNumber()); 213 return AsNumber()->Identical(pOther->AsNumber());
213 case PDFOBJ_STRING: 214 case PDFOBJ_STRING:
214 return (((CPDF_String*)this)->Identical((CPDF_String*)pOther)); 215 return AsString()->Identical(pOther->AsString());
215 case PDFOBJ_NAME: 216 case PDFOBJ_NAME:
216 return (((CPDF_Name*)this)->Identical((CPDF_Name*)pOther)); 217 return (((CPDF_Name*)this)->Identical((CPDF_Name*)pOther));
217 case PDFOBJ_ARRAY: 218 case PDFOBJ_ARRAY:
218 return (((CPDF_Array*)this)->Identical((CPDF_Array*)pOther)); 219 return (((CPDF_Array*)this)->Identical((CPDF_Array*)pOther));
219 case PDFOBJ_DICTIONARY: 220 case PDFOBJ_DICTIONARY:
220 return AsDictionary()->Identical(pOther->AsDictionary()); 221 return AsDictionary()->Identical(pOther->AsDictionary());
221 case PDFOBJ_NULL: 222 case PDFOBJ_NULL:
222 return TRUE; 223 return TRUE;
223 case PDFOBJ_STREAM: 224 case PDFOBJ_STREAM:
224 return (((CPDF_Stream*)this)->Identical((CPDF_Stream*)pOther)); 225 return (((CPDF_Stream*)this)->Identical((CPDF_Stream*)pOther));
(...skipping 19 matching lines...) Expand all
244 CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, 245 CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect,
245 CFX_MapPtrToPtr* visited) const { 246 CFX_MapPtrToPtr* visited) const {
246 switch (m_Type) { 247 switch (m_Type) {
247 case PDFOBJ_BOOLEAN: 248 case PDFOBJ_BOOLEAN:
248 return new CPDF_Boolean(AsBoolean()->m_bValue); 249 return new CPDF_Boolean(AsBoolean()->m_bValue);
249 case PDFOBJ_NUMBER: { 250 case PDFOBJ_NUMBER: {
250 const CPDF_Number* pThis = AsNumber(); 251 const CPDF_Number* pThis = AsNumber();
251 return new CPDF_Number(pThis->m_bInteger ? pThis->m_Integer 252 return new CPDF_Number(pThis->m_bInteger ? pThis->m_Integer
252 : pThis->m_Float); 253 : pThis->m_Float);
253 } 254 }
254 case PDFOBJ_STRING: 255 case PDFOBJ_STRING: {
255 return new CPDF_String(((CPDF_String*)this)->m_String, 256 const CPDF_String* pString = AsString();
256 ((CPDF_String*)this)->IsHex()); 257 return new CPDF_String(pString->m_String, pString->IsHex());
258 }
257 case PDFOBJ_NAME: 259 case PDFOBJ_NAME:
258 return new CPDF_Name(((CPDF_Name*)this)->m_Name); 260 return new CPDF_Name(((CPDF_Name*)this)->m_Name);
259 case PDFOBJ_ARRAY: { 261 case PDFOBJ_ARRAY: {
260 CPDF_Array* pCopy = new CPDF_Array(); 262 CPDF_Array* pCopy = new CPDF_Array();
261 CPDF_Array* pThis = (CPDF_Array*)this; 263 CPDF_Array* pThis = (CPDF_Array*)this;
262 int n = pThis->GetCount(); 264 int n = pThis->GetCount();
263 for (int i = 0; i < n; i++) { 265 for (int i = 0; i < n; i++) {
264 CPDF_Object* value = (CPDF_Object*)pThis->m_Objects.GetAt(i); 266 CPDF_Object* value = (CPDF_Object*)pThis->m_Objects.GetAt(i);
265 pCopy->m_Objects.Add(value->CloneInternal(bDirect, visited)); 267 pCopy->m_Objects.Add(value->CloneInternal(bDirect, visited));
266 } 268 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 } 309 }
308 return NULL; 310 return NULL;
309 } 311 }
310 CPDF_Object* CPDF_Object::CloneRef(CPDF_IndirectObjects* pDoc) const { 312 CPDF_Object* CPDF_Object::CloneRef(CPDF_IndirectObjects* pDoc) const {
311 if (m_ObjNum) { 313 if (m_ObjNum) {
312 return new CPDF_Reference(pDoc, m_ObjNum); 314 return new CPDF_Reference(pDoc, m_ObjNum);
313 } 315 }
314 return Clone(); 316 return Clone();
315 } 317 }
316 CFX_WideString CPDF_Object::GetUnicodeText(CFX_CharMap* pCharMap) const { 318 CFX_WideString CPDF_Object::GetUnicodeText(CFX_CharMap* pCharMap) const {
317 if (m_Type == PDFOBJ_STRING) { 319 if (const CPDF_String* pString = AsString())
318 return PDF_DecodeText(((CPDF_String*)this)->m_String, pCharMap); 320 return PDF_DecodeText(pString->m_String, pCharMap);
319 } 321
320 if (m_Type == PDFOBJ_STREAM) { 322 if (m_Type == PDFOBJ_STREAM) {
321 CPDF_StreamAcc stream; 323 CPDF_StreamAcc stream;
322 stream.LoadAllData((CPDF_Stream*)this, FALSE); 324 stream.LoadAllData((CPDF_Stream*)this, FALSE);
323 CFX_WideString result = 325 CFX_WideString result =
324 PDF_DecodeText(stream.GetData(), stream.GetSize(), pCharMap); 326 PDF_DecodeText(stream.GetData(), stream.GetSize(), pCharMap);
325 return result; 327 return result;
326 } 328 }
327 if (m_Type == PDFOBJ_NAME) { 329 if (m_Type == PDFOBJ_NAME) {
328 return PDF_DecodeText(((CPDF_Name*)this)->m_Name, pCharMap); 330 return PDF_DecodeText(((CPDF_Name*)this)->m_Name, pCharMap);
329 } 331 }
330 return CFX_WideString(); 332 return CFX_WideString();
331 } 333 }
332 void CPDF_Object::SetUnicodeText(const FX_WCHAR* pUnicodes, int len) { 334 void CPDF_Object::SetUnicodeText(const FX_WCHAR* pUnicodes, int len) {
333 if (m_Type == PDFOBJ_STRING) { 335 if (CPDF_String* pString = AsString()) {
334 ((CPDF_String*)this)->m_String = PDF_EncodeText(pUnicodes, len); 336 pString->m_String = PDF_EncodeText(pUnicodes, len);
335 } else if (m_Type == PDFOBJ_STREAM) { 337 } else if (m_Type == PDFOBJ_STREAM) {
336 CFX_ByteString result = PDF_EncodeText(pUnicodes, len); 338 CFX_ByteString result = PDF_EncodeText(pUnicodes, len);
337 ((CPDF_Stream*)this) 339 ((CPDF_Stream*)this)
338 ->SetData((uint8_t*)result.c_str(), result.GetLength(), FALSE, FALSE); 340 ->SetData((uint8_t*)result.c_str(), result.GetLength(), FALSE, FALSE);
339 } 341 }
340 } 342 }
341 343
342 CPDF_Boolean* CPDF_Object::AsBoolean() { 344 CPDF_Boolean* CPDF_Object::AsBoolean() {
343 return IsBoolean() ? static_cast<CPDF_Boolean*>(this) : nullptr; 345 return IsBoolean() ? static_cast<CPDF_Boolean*>(this) : nullptr;
344 } 346 }
(...skipping 11 matching lines...) Expand all
356 } 358 }
357 359
358 CPDF_Number* CPDF_Object::AsNumber() { 360 CPDF_Number* CPDF_Object::AsNumber() {
359 return IsNumber() ? static_cast<CPDF_Number*>(this) : nullptr; 361 return IsNumber() ? static_cast<CPDF_Number*>(this) : nullptr;
360 } 362 }
361 363
362 const CPDF_Number* CPDF_Object::AsNumber() const { 364 const CPDF_Number* CPDF_Object::AsNumber() const {
363 return IsNumber() ? static_cast<const CPDF_Number*>(this) : nullptr; 365 return IsNumber() ? static_cast<const CPDF_Number*>(this) : nullptr;
364 } 366 }
365 367
368 CPDF_String* CPDF_Object::AsString() {
369 return IsString() ? static_cast<CPDF_String*>(this) : nullptr;
370 }
371
372 const CPDF_String* CPDF_Object::AsString() const {
373 return IsString() ? static_cast<const CPDF_String*>(this) : nullptr;
374 }
375
366 CPDF_Number::CPDF_Number(int value) 376 CPDF_Number::CPDF_Number(int value)
367 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(TRUE), m_Integer(value) {} 377 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(TRUE), m_Integer(value) {}
368 378
369 CPDF_Number::CPDF_Number(FX_FLOAT value) 379 CPDF_Number::CPDF_Number(FX_FLOAT value)
370 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(FALSE), m_Float(value) {} 380 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(FALSE), m_Float(value) {}
371 381
372 CPDF_Number::CPDF_Number(const CFX_ByteStringC& str) 382 CPDF_Number::CPDF_Number(const CFX_ByteStringC& str)
373 : CPDF_Object(PDFOBJ_NUMBER) { 383 : CPDF_Object(PDFOBJ_NUMBER) {
374 FX_atonum(str, m_bInteger, &m_Integer); 384 FX_atonum(str, m_bInteger, &m_Integer);
375 } 385 }
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after
1220 } 1230 }
1221 pObj->m_ObjNum = objnum; 1231 pObj->m_ObjNum = objnum;
1222 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj); 1232 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj);
1223 if (m_LastObjNum < objnum) { 1233 if (m_LastObjNum < objnum) {
1224 m_LastObjNum = objnum; 1234 m_LastObjNum = objnum;
1225 } 1235 }
1226 } 1236 }
1227 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const { 1237 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const {
1228 return m_LastObjNum; 1238 return m_LastObjNum;
1229 } 1239 }
OLDNEW
« no previous file with comments | « core/src/fpdfapi/fpdf_parser/fpdf_parser_fdf.cpp ('k') | core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698