Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 const CPDF_String* pString = AsString(); |
|
Lei Zhang
2015/10/21 16:55:56
Maybe do AsString()->m_String here, instead of twi
dsinclair
2015/10/21 17:08:05
Done.
Good catch, missed the m_String duplication
| |
| 69 ((CPDF_String*)this)->m_String.GetLength()); | 69 return CFX_ByteStringC((const uint8_t*)pString->m_String, |
| 70 pString->m_String.GetLength()); | |
| 71 } | |
| 70 case PDFOBJ_NAME: | 72 case PDFOBJ_NAME: |
| 71 return CFX_ByteStringC((const uint8_t*)((CPDF_Name*)this)->m_Name, | 73 return CFX_ByteStringC((const uint8_t*)((CPDF_Name*)this)->m_Name, |
| 72 ((CPDF_Name*)this)->m_Name.GetLength()); | 74 ((CPDF_Name*)this)->m_Name.GetLength()); |
| 73 case PDFOBJ_REFERENCE: { | 75 case PDFOBJ_REFERENCE: { |
| 74 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; | 76 CPDF_Reference* pRef = (CPDF_Reference*)(void*)this; |
| 75 if (pRef->m_pObjList == NULL) { | 77 if (pRef->m_pObjList == NULL) { |
| 76 break; | 78 break; |
| 77 } | 79 } |
| 78 CPDF_Object* pObj = | 80 CPDF_Object* pObj = |
| 79 pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum()); | 81 pRef->m_pObjList->GetIndirectObject(pRef->GetRefObjNum()); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 void CPDF_Object::SetString(const CFX_ByteString& str) { | 169 void CPDF_Object::SetString(const CFX_ByteString& str) { |
| 168 ASSERT(this != NULL); | 170 ASSERT(this != NULL); |
| 169 switch (m_Type) { | 171 switch (m_Type) { |
| 170 case PDFOBJ_BOOLEAN: | 172 case PDFOBJ_BOOLEAN: |
| 171 AsBoolean()->m_bValue = (str == FX_BSTRC("true")); | 173 AsBoolean()->m_bValue = (str == FX_BSTRC("true")); |
| 172 return; | 174 return; |
| 173 case PDFOBJ_NUMBER: | 175 case PDFOBJ_NUMBER: |
| 174 AsNumber()->SetString(str); | 176 AsNumber()->SetString(str); |
| 175 return; | 177 return; |
| 176 case PDFOBJ_STRING: | 178 case PDFOBJ_STRING: |
| 177 ((CPDF_String*)this)->m_String = str; | 179 AsString()->m_String = str; |
|
Tom Sepez
2015/10/21 17:12:50
nit: pity we don't have a SetString method as for
dsinclair
2015/10/21 17:40:01
https://code.google.com/p/pdfium/issues/detail?id=
| |
| 178 return; | 180 return; |
| 179 case PDFOBJ_NAME: | 181 case PDFOBJ_NAME: |
| 180 ((CPDF_Name*)this)->m_Name = str; | 182 ((CPDF_Name*)this)->m_Name = str; |
| 181 return; | 183 return; |
| 182 } | 184 } |
| 183 ASSERT(FALSE); | 185 ASSERT(FALSE); |
| 184 } | 186 } |
| 185 int CPDF_Object::GetDirectType() const { | 187 int CPDF_Object::GetDirectType() const { |
| 186 if (m_Type != PDFOBJ_REFERENCE) { | 188 if (m_Type != PDFOBJ_REFERENCE) { |
| 187 return m_Type; | 189 return m_Type; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 204 return IsIdentical(pOther->GetDirect()); | 206 return IsIdentical(pOther->GetDirect()); |
| 205 } | 207 } |
| 206 return FALSE; | 208 return FALSE; |
| 207 } | 209 } |
| 208 switch (m_Type) { | 210 switch (m_Type) { |
| 209 case PDFOBJ_BOOLEAN: | 211 case PDFOBJ_BOOLEAN: |
| 210 return AsBoolean()->Identical(pOther->AsBoolean()); | 212 return AsBoolean()->Identical(pOther->AsBoolean()); |
| 211 case PDFOBJ_NUMBER: | 213 case PDFOBJ_NUMBER: |
| 212 return AsNumber()->Identical(pOther->AsNumber()); | 214 return AsNumber()->Identical(pOther->AsNumber()); |
| 213 case PDFOBJ_STRING: | 215 case PDFOBJ_STRING: |
| 214 return (((CPDF_String*)this)->Identical((CPDF_String*)pOther)); | 216 return AsString()->Identical(pOther->AsString()); |
|
Tom Sepez
2015/10/21 17:12:50
Ok, I guess we checked for null above.
| |
| 215 case PDFOBJ_NAME: | 217 case PDFOBJ_NAME: |
| 216 return (((CPDF_Name*)this)->Identical((CPDF_Name*)pOther)); | 218 return (((CPDF_Name*)this)->Identical((CPDF_Name*)pOther)); |
| 217 case PDFOBJ_ARRAY: | 219 case PDFOBJ_ARRAY: |
| 218 return (((CPDF_Array*)this)->Identical((CPDF_Array*)pOther)); | 220 return (((CPDF_Array*)this)->Identical((CPDF_Array*)pOther)); |
| 219 case PDFOBJ_DICTIONARY: | 221 case PDFOBJ_DICTIONARY: |
| 220 return AsDictionary()->Identical(pOther->AsDictionary()); | 222 return AsDictionary()->Identical(pOther->AsDictionary()); |
| 221 case PDFOBJ_NULL: | 223 case PDFOBJ_NULL: |
| 222 return TRUE; | 224 return TRUE; |
| 223 case PDFOBJ_STREAM: | 225 case PDFOBJ_STREAM: |
| 224 return (((CPDF_Stream*)this)->Identical((CPDF_Stream*)pOther)); | 226 return (((CPDF_Stream*)this)->Identical((CPDF_Stream*)pOther)); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 244 CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, | 246 CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, |
| 245 CFX_MapPtrToPtr* visited) const { | 247 CFX_MapPtrToPtr* visited) const { |
| 246 switch (m_Type) { | 248 switch (m_Type) { |
| 247 case PDFOBJ_BOOLEAN: | 249 case PDFOBJ_BOOLEAN: |
| 248 return new CPDF_Boolean(AsBoolean()->m_bValue); | 250 return new CPDF_Boolean(AsBoolean()->m_bValue); |
| 249 case PDFOBJ_NUMBER: { | 251 case PDFOBJ_NUMBER: { |
| 250 const CPDF_Number* pThis = AsNumber(); | 252 const CPDF_Number* pThis = AsNumber(); |
| 251 return new CPDF_Number(pThis->m_bInteger ? pThis->m_Integer | 253 return new CPDF_Number(pThis->m_bInteger ? pThis->m_Integer |
| 252 : pThis->m_Float); | 254 : pThis->m_Float); |
| 253 } | 255 } |
| 254 case PDFOBJ_STRING: | 256 case PDFOBJ_STRING: { |
| 255 return new CPDF_String(((CPDF_String*)this)->m_String, | 257 const CPDF_String* pString = AsString(); |
| 256 ((CPDF_String*)this)->IsHex()); | 258 return new CPDF_String(pString->m_String, pString->IsHex()); |
| 259 } | |
| 257 case PDFOBJ_NAME: | 260 case PDFOBJ_NAME: |
| 258 return new CPDF_Name(((CPDF_Name*)this)->m_Name); | 261 return new CPDF_Name(((CPDF_Name*)this)->m_Name); |
| 259 case PDFOBJ_ARRAY: { | 262 case PDFOBJ_ARRAY: { |
| 260 CPDF_Array* pCopy = new CPDF_Array(); | 263 CPDF_Array* pCopy = new CPDF_Array(); |
| 261 CPDF_Array* pThis = (CPDF_Array*)this; | 264 CPDF_Array* pThis = (CPDF_Array*)this; |
| 262 int n = pThis->GetCount(); | 265 int n = pThis->GetCount(); |
| 263 for (int i = 0; i < n; i++) { | 266 for (int i = 0; i < n; i++) { |
| 264 CPDF_Object* value = (CPDF_Object*)pThis->m_Objects.GetAt(i); | 267 CPDF_Object* value = (CPDF_Object*)pThis->m_Objects.GetAt(i); |
| 265 pCopy->m_Objects.Add(value->CloneInternal(bDirect, visited)); | 268 pCopy->m_Objects.Add(value->CloneInternal(bDirect, visited)); |
| 266 } | 269 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307 } | 310 } |
| 308 return NULL; | 311 return NULL; |
| 309 } | 312 } |
| 310 CPDF_Object* CPDF_Object::CloneRef(CPDF_IndirectObjects* pDoc) const { | 313 CPDF_Object* CPDF_Object::CloneRef(CPDF_IndirectObjects* pDoc) const { |
| 311 if (m_ObjNum) { | 314 if (m_ObjNum) { |
| 312 return new CPDF_Reference(pDoc, m_ObjNum); | 315 return new CPDF_Reference(pDoc, m_ObjNum); |
| 313 } | 316 } |
| 314 return Clone(); | 317 return Clone(); |
| 315 } | 318 } |
| 316 CFX_WideString CPDF_Object::GetUnicodeText(CFX_CharMap* pCharMap) const { | 319 CFX_WideString CPDF_Object::GetUnicodeText(CFX_CharMap* pCharMap) const { |
| 317 if (m_Type == PDFOBJ_STRING) { | 320 if (const CPDF_String* pString = AsString()) |
| 318 return PDF_DecodeText(((CPDF_String*)this)->m_String, pCharMap); | 321 return PDF_DecodeText(pString->m_String, pCharMap); |
| 319 } | 322 |
| 320 if (m_Type == PDFOBJ_STREAM) { | 323 if (m_Type == PDFOBJ_STREAM) { |
| 321 CPDF_StreamAcc stream; | 324 CPDF_StreamAcc stream; |
| 322 stream.LoadAllData((CPDF_Stream*)this, FALSE); | 325 stream.LoadAllData((CPDF_Stream*)this, FALSE); |
| 323 CFX_WideString result = | 326 CFX_WideString result = |
| 324 PDF_DecodeText(stream.GetData(), stream.GetSize(), pCharMap); | 327 PDF_DecodeText(stream.GetData(), stream.GetSize(), pCharMap); |
| 325 return result; | 328 return result; |
| 326 } | 329 } |
| 327 if (m_Type == PDFOBJ_NAME) { | 330 if (m_Type == PDFOBJ_NAME) { |
| 328 return PDF_DecodeText(((CPDF_Name*)this)->m_Name, pCharMap); | 331 return PDF_DecodeText(((CPDF_Name*)this)->m_Name, pCharMap); |
| 329 } | 332 } |
| 330 return CFX_WideString(); | 333 return CFX_WideString(); |
| 331 } | 334 } |
| 332 void CPDF_Object::SetUnicodeText(const FX_WCHAR* pUnicodes, int len) { | 335 void CPDF_Object::SetUnicodeText(const FX_WCHAR* pUnicodes, int len) { |
| 333 if (m_Type == PDFOBJ_STRING) { | 336 if (CPDF_String* pString = AsString()) { |
| 334 ((CPDF_String*)this)->m_String = PDF_EncodeText(pUnicodes, len); | 337 pString->m_String = PDF_EncodeText(pUnicodes, len); |
| 338 | |
|
Lei Zhang
2015/10/21 16:55:56
no blank line
dsinclair
2015/10/21 17:08:05
Done.
| |
| 335 } else if (m_Type == PDFOBJ_STREAM) { | 339 } else if (m_Type == PDFOBJ_STREAM) { |
| 336 CFX_ByteString result = PDF_EncodeText(pUnicodes, len); | 340 CFX_ByteString result = PDF_EncodeText(pUnicodes, len); |
| 337 ((CPDF_Stream*)this) | 341 ((CPDF_Stream*)this) |
| 338 ->SetData((uint8_t*)result.c_str(), result.GetLength(), FALSE, FALSE); | 342 ->SetData((uint8_t*)result.c_str(), result.GetLength(), FALSE, FALSE); |
| 339 } | 343 } |
| 340 } | 344 } |
| 341 | 345 |
| 342 CPDF_Boolean* CPDF_Object::AsBoolean() { | 346 CPDF_Boolean* CPDF_Object::AsBoolean() { |
| 343 return IsBoolean() ? static_cast<CPDF_Boolean*>(this) : nullptr; | 347 return IsBoolean() ? static_cast<CPDF_Boolean*>(this) : nullptr; |
| 344 } | 348 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 356 } | 360 } |
| 357 | 361 |
| 358 CPDF_Number* CPDF_Object::AsNumber() { | 362 CPDF_Number* CPDF_Object::AsNumber() { |
| 359 return IsNumber() ? static_cast<CPDF_Number*>(this) : nullptr; | 363 return IsNumber() ? static_cast<CPDF_Number*>(this) : nullptr; |
| 360 } | 364 } |
| 361 | 365 |
| 362 const CPDF_Number* CPDF_Object::AsNumber() const { | 366 const CPDF_Number* CPDF_Object::AsNumber() const { |
| 363 return IsNumber() ? static_cast<const CPDF_Number*>(this) : nullptr; | 367 return IsNumber() ? static_cast<const CPDF_Number*>(this) : nullptr; |
| 364 } | 368 } |
| 365 | 369 |
| 370 CPDF_String* CPDF_Object::AsString() { | |
| 371 return IsString() ? static_cast<CPDF_String*>(this) : nullptr; | |
| 372 } | |
| 373 | |
| 374 const CPDF_String* CPDF_Object::AsString() const { | |
| 375 return IsString() ? static_cast<const CPDF_String*>(this) : nullptr; | |
| 376 } | |
| 377 | |
| 366 CPDF_Number::CPDF_Number(int value) | 378 CPDF_Number::CPDF_Number(int value) |
| 367 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(TRUE), m_Integer(value) {} | 379 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(TRUE), m_Integer(value) {} |
| 368 | 380 |
| 369 CPDF_Number::CPDF_Number(FX_FLOAT value) | 381 CPDF_Number::CPDF_Number(FX_FLOAT value) |
| 370 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(FALSE), m_Float(value) {} | 382 : CPDF_Object(PDFOBJ_NUMBER), m_bInteger(FALSE), m_Float(value) {} |
| 371 | 383 |
| 372 CPDF_Number::CPDF_Number(const CFX_ByteStringC& str) | 384 CPDF_Number::CPDF_Number(const CFX_ByteStringC& str) |
| 373 : CPDF_Object(PDFOBJ_NUMBER) { | 385 : CPDF_Object(PDFOBJ_NUMBER) { |
| 374 FX_atonum(str, m_bInteger, &m_Integer); | 386 FX_atonum(str, m_bInteger, &m_Integer); |
| 375 } | 387 } |
| (...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1220 } | 1232 } |
| 1221 pObj->m_ObjNum = objnum; | 1233 pObj->m_ObjNum = objnum; |
| 1222 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj); | 1234 m_IndirectObjs.SetAt((void*)(uintptr_t)objnum, pObj); |
| 1223 if (m_LastObjNum < objnum) { | 1235 if (m_LastObjNum < objnum) { |
| 1224 m_LastObjNum = objnum; | 1236 m_LastObjNum = objnum; |
| 1225 } | 1237 } |
| 1226 } | 1238 } |
| 1227 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const { | 1239 FX_DWORD CPDF_IndirectObjects::GetLastObjNum() const { |
| 1228 return m_LastObjNum; | 1240 return m_LastObjNum; |
| 1229 } | 1241 } |
| OLD | NEW |