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

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

Issue 1549103002: Make more CPDF_Parser method const. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: Created 4 years, 12 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
« no previous file with comments | « core/include/fpdfapi/fpdf_parser.h ('k') | fpdfsdk/include/fsdk_annothandler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "parser_int.h" 7 #include "parser_int.h"
8 8
9 #include <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 return m_ObjectInfo.empty() ? 0 : m_ObjectInfo.rbegin()->first; 129 return m_ObjectInfo.empty() ? 0 : m_ObjectInfo.rbegin()->first;
130 } 130 }
131 131
132 bool CPDF_Parser::IsValidObjectNumber(FX_DWORD objnum) const { 132 bool CPDF_Parser::IsValidObjectNumber(FX_DWORD objnum) const {
133 return !m_ObjectInfo.empty() && objnum <= m_ObjectInfo.rbegin()->first; 133 return !m_ObjectInfo.empty() && objnum <= m_ObjectInfo.rbegin()->first;
134 } 134 }
135 135
136 void CPDF_Parser::SetEncryptDictionary(CPDF_Dictionary* pDict) { 136 void CPDF_Parser::SetEncryptDictionary(CPDF_Dictionary* pDict) {
137 m_pEncryptDict = pDict; 137 m_pEncryptDict = pDict;
138 } 138 }
139
140 FX_FILESIZE CPDF_Parser::GetObjectPositionOrZero(FX_DWORD objnum) const {
141 auto it = m_ObjectInfo.find(objnum);
142 return it != m_ObjectInfo.end() ? it->second.pos : 0;
143 }
144
139 void CPDF_Parser::CloseParser(FX_BOOL bReParse) { 145 void CPDF_Parser::CloseParser(FX_BOOL bReParse) {
140 m_bVersionUpdated = FALSE; 146 m_bVersionUpdated = FALSE;
141 if (!bReParse) { 147 if (!bReParse) {
142 delete m_pDocument; 148 delete m_pDocument;
143 m_pDocument = NULL; 149 m_pDocument = NULL;
144 } 150 }
145 if (m_pTrailer) { 151 if (m_pTrailer) {
146 m_pTrailer->Release(); 152 m_pTrailer->Release();
147 m_pTrailer = NULL; 153 m_pTrailer = NULL;
148 } 154 }
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 m_Syntax.SetEncrypt(pCryptoHandler.release()); 342 m_Syntax.SetEncrypt(pCryptoHandler.release());
337 } 343 }
338 return PDFPARSE_ERROR_SUCCESS; 344 return PDFPARSE_ERROR_SUCCESS;
339 } 345 }
340 void CPDF_Parser::ReleaseEncryptHandler() { 346 void CPDF_Parser::ReleaseEncryptHandler() {
341 m_Syntax.m_pCryptoHandler.reset(); 347 m_Syntax.m_pCryptoHandler.reset();
342 if (!m_bForceUseSecurityHandler) { 348 if (!m_bForceUseSecurityHandler) {
343 m_pSecurityHandler.reset(); 349 m_pSecurityHandler.reset();
344 } 350 }
345 } 351 }
346 FX_FILESIZE CPDF_Parser::GetObjectOffset(FX_DWORD objnum) { 352
353 FX_FILESIZE CPDF_Parser::GetObjectOffset(FX_DWORD objnum) const {
347 if (!IsValidObjectNumber(objnum)) 354 if (!IsValidObjectNumber(objnum))
348 return 0; 355 return 0;
349 356
350 if (m_V5Type[objnum] == 1) 357 if (m_V5Type[objnum] == 1)
351 return m_ObjectInfo[objnum].pos; 358 return GetObjectPositionOrZero(objnum);
352 359
353 if (m_V5Type[objnum] == 2) { 360 if (m_V5Type[objnum] == 2) {
354 FX_FILESIZE pos = m_ObjectInfo[objnum].pos; 361 FX_FILESIZE pos = GetObjectPositionOrZero(objnum);
355 return m_ObjectInfo[pos].pos; 362 return GetObjectPositionOrZero(pos);
356 } 363 }
357 return 0; 364 return 0;
358 } 365 }
359 366
360 FX_BOOL CPDF_Parser::LoadAllCrossRefV4(FX_FILESIZE xrefpos) { 367 FX_BOOL CPDF_Parser::LoadAllCrossRefV4(FX_FILESIZE xrefpos) {
361 if (!LoadCrossRefV4(xrefpos, 0, TRUE)) { 368 if (!LoadCrossRefV4(xrefpos, 0, TRUE)) {
362 return FALSE; 369 return FALSE;
363 } 370 }
364 m_pTrailer = LoadTrailerV4(); 371 m_pTrailer = LoadTrailerV4();
365 if (!m_pTrailer) { 372 if (!m_pTrailer) {
(...skipping 861 matching lines...) Expand 10 before | Expand all | Expand 10 after
1227 const CPDF_Stream* pStream = 1234 const CPDF_Stream* pStream =
1228 ToStream(m_pDocument ? m_pDocument->GetIndirectObject(objnum) : nullptr); 1235 ToStream(m_pDocument ? m_pDocument->GetIndirectObject(objnum) : nullptr);
1229 if (!pStream) 1236 if (!pStream)
1230 return nullptr; 1237 return nullptr;
1231 1238
1232 pStreamAcc = new CPDF_StreamAcc; 1239 pStreamAcc = new CPDF_StreamAcc;
1233 pStreamAcc->LoadAllData(pStream); 1240 pStreamAcc->LoadAllData(pStream);
1234 m_ObjectStreamMap.SetAt((void*)(uintptr_t)objnum, pStreamAcc); 1241 m_ObjectStreamMap.SetAt((void*)(uintptr_t)objnum, pStreamAcc);
1235 return pStreamAcc; 1242 return pStreamAcc;
1236 } 1243 }
1237 FX_FILESIZE CPDF_Parser::GetObjectSize(FX_DWORD objnum) { 1244
1245 FX_FILESIZE CPDF_Parser::GetObjectSize(FX_DWORD objnum) const {
1238 if (!IsValidObjectNumber(objnum)) 1246 if (!IsValidObjectNumber(objnum))
1239 return 0; 1247 return 0;
1240 1248
1241 if (m_V5Type[objnum] == 2) { 1249 if (m_V5Type[objnum] == 2)
1242 objnum = m_ObjectInfo[objnum].pos; 1250 objnum = GetObjectPositionOrZero(objnum);
1243 } 1251
1244 if (m_V5Type[objnum] == 1 || m_V5Type[objnum] == 255) { 1252 if (m_V5Type[objnum] == 1 || m_V5Type[objnum] == 255) {
1245 FX_FILESIZE offset = m_ObjectInfo[objnum].pos; 1253 FX_FILESIZE offset = GetObjectPositionOrZero(objnum);
1246 if (offset == 0) { 1254 if (offset == 0)
1247 return 0; 1255 return 0;
1248 } 1256
1249 void* pResult = FXSYS_bsearch(&offset, m_SortedOffset.GetData(), 1257 FX_FILESIZE* pResult = static_cast<FX_FILESIZE*>(FXSYS_bsearch(
1250 m_SortedOffset.GetSize(), sizeof(FX_FILESIZE), 1258 &offset, m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
1251 CompareFileSize); 1259 sizeof(FX_FILESIZE), CompareFileSize));
1252 if (!pResult) { 1260 if (!pResult)
1253 return 0; 1261 return 0;
1254 } 1262
1255 if ((FX_FILESIZE*)pResult - (FX_FILESIZE*)m_SortedOffset.GetData() == 1263 if (pResult - m_SortedOffset.GetData() == m_SortedOffset.GetSize() - 1)
1256 m_SortedOffset.GetSize() - 1) {
1257 return 0; 1264 return 0;
1258 } 1265
1259 return ((FX_FILESIZE*)pResult)[1] - offset; 1266 return pResult[1] - offset;
1260 } 1267 }
1261 return 0; 1268 return 0;
1262 } 1269 }
1270
1263 void CPDF_Parser::GetIndirectBinary(FX_DWORD objnum, 1271 void CPDF_Parser::GetIndirectBinary(FX_DWORD objnum,
1264 uint8_t*& pBuffer, 1272 uint8_t*& pBuffer,
1265 FX_DWORD& size) { 1273 FX_DWORD& size) {
1266 pBuffer = NULL; 1274 pBuffer = NULL;
1267 size = 0; 1275 size = 0;
1268 if (!IsValidObjectNumber(objnum)) 1276 if (!IsValidObjectNumber(objnum))
1269 return; 1277 return;
1270 1278
1271 if (m_V5Type[objnum] == 2) { 1279 if (m_V5Type[objnum] == 2) {
1272 CPDF_StreamAcc* pObjStream = GetObjectStream(m_ObjectInfo[objnum].pos); 1280 CPDF_StreamAcc* pObjStream = GetObjectStream(m_ObjectInfo[objnum].pos);
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
1688 1696
1689 CPDF_SyntaxParser::CPDF_SyntaxParser() { 1697 CPDF_SyntaxParser::CPDF_SyntaxParser() {
1690 m_pFileAccess = NULL; 1698 m_pFileAccess = NULL;
1691 m_pFileBuf = NULL; 1699 m_pFileBuf = NULL;
1692 m_BufSize = CPDF_ModuleMgr::kFileBufSize; 1700 m_BufSize = CPDF_ModuleMgr::kFileBufSize;
1693 m_pFileBuf = NULL; 1701 m_pFileBuf = NULL;
1694 m_MetadataObjnum = 0; 1702 m_MetadataObjnum = 0;
1695 m_dwWordPos = 0; 1703 m_dwWordPos = 0;
1696 m_bFileStream = FALSE; 1704 m_bFileStream = FALSE;
1697 } 1705 }
1706
1698 CPDF_SyntaxParser::~CPDF_SyntaxParser() { 1707 CPDF_SyntaxParser::~CPDF_SyntaxParser() {
1699 FX_Free(m_pFileBuf); 1708 FX_Free(m_pFileBuf);
1700 } 1709 }
1701 1710
1702 FX_BOOL CPDF_SyntaxParser::GetCharAt(FX_FILESIZE pos, uint8_t& ch) { 1711 FX_BOOL CPDF_SyntaxParser::GetCharAt(FX_FILESIZE pos, uint8_t& ch) {
1703 CFX_AutoRestorer<FX_FILESIZE> save_pos(&m_Pos); 1712 CFX_AutoRestorer<FX_FILESIZE> save_pos(&m_Pos);
1704 m_Pos = pos; 1713 m_Pos = pos;
1705 return GetNextChar(ch); 1714 return GetNextChar(ch);
1706 } 1715 }
1707 1716
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1762 ch = m_pFileBuf[pos - m_BufOffset]; 1771 ch = m_pFileBuf[pos - m_BufOffset];
1763 return TRUE; 1772 return TRUE;
1764 } 1773 }
1765 FX_BOOL CPDF_SyntaxParser::ReadBlock(uint8_t* pBuf, FX_DWORD size) { 1774 FX_BOOL CPDF_SyntaxParser::ReadBlock(uint8_t* pBuf, FX_DWORD size) {
1766 if (!m_pFileAccess->ReadBlock(pBuf, m_Pos + m_HeaderOffset, size)) { 1775 if (!m_pFileAccess->ReadBlock(pBuf, m_Pos + m_HeaderOffset, size)) {
1767 return FALSE; 1776 return FALSE;
1768 } 1777 }
1769 m_Pos += size; 1778 m_Pos += size;
1770 return TRUE; 1779 return TRUE;
1771 } 1780 }
1772 #define MAX_WORD_BUFFER 256 1781
1773 void CPDF_SyntaxParser::GetNextWord() { 1782 void CPDF_SyntaxParser::GetNextWord() {
1774 m_WordSize = 0; 1783 m_WordSize = 0;
1775 m_bIsNumber = TRUE; 1784 m_bIsNumber = TRUE;
1776 uint8_t ch; 1785 uint8_t ch;
1777 if (!GetNextChar(ch)) { 1786 if (!GetNextChar(ch)) {
1778 return; 1787 return;
1779 } 1788 }
1780 while (1) { 1789 while (1) {
1781 while (PDFCharIsWhitespace(ch)) { 1790 while (PDFCharIsWhitespace(ch)) {
1782 if (!GetNextChar(ch)) 1791 if (!GetNextChar(ch))
(...skipping 16 matching lines...) Expand all
1799 if (ch == '/') { 1808 if (ch == '/') {
1800 while (1) { 1809 while (1) {
1801 if (!GetNextChar(ch)) 1810 if (!GetNextChar(ch))
1802 return; 1811 return;
1803 1812
1804 if (!PDFCharIsOther(ch) && !PDFCharIsNumeric(ch)) { 1813 if (!PDFCharIsOther(ch) && !PDFCharIsNumeric(ch)) {
1805 m_Pos--; 1814 m_Pos--;
1806 return; 1815 return;
1807 } 1816 }
1808 1817
1809 if (m_WordSize < MAX_WORD_BUFFER) 1818 if (m_WordSize < sizeof(m_WordBuffer) - 1)
1810 m_WordBuffer[m_WordSize++] = ch; 1819 m_WordBuffer[m_WordSize++] = ch;
1811 } 1820 }
1812 } else if (ch == '<') { 1821 } else if (ch == '<') {
1813 if (!GetNextChar(ch)) 1822 if (!GetNextChar(ch))
1814 return; 1823 return;
1815 if (ch == '<') 1824 if (ch == '<')
1816 m_WordBuffer[m_WordSize++] = ch; 1825 m_WordBuffer[m_WordSize++] = ch;
1817 else 1826 else
1818 m_Pos--; 1827 m_Pos--;
1819 } else if (ch == '>') { 1828 } else if (ch == '>') {
1820 if (!GetNextChar(ch)) 1829 if (!GetNextChar(ch))
1821 return; 1830 return;
1822 if (ch == '>') 1831 if (ch == '>')
1823 m_WordBuffer[m_WordSize++] = ch; 1832 m_WordBuffer[m_WordSize++] = ch;
1824 else 1833 else
1825 m_Pos--; 1834 m_Pos--;
1826 } 1835 }
1827 return; 1836 return;
1828 } 1837 }
1829 1838
1830 while (1) { 1839 while (1) {
1831 if (m_WordSize < MAX_WORD_BUFFER) 1840 if (m_WordSize < sizeof(m_WordBuffer) - 1)
1832 m_WordBuffer[m_WordSize++] = ch; 1841 m_WordBuffer[m_WordSize++] = ch;
1833 1842
1834 if (!PDFCharIsNumeric(ch)) 1843 if (!PDFCharIsNumeric(ch))
1835 m_bIsNumber = FALSE; 1844 m_bIsNumber = FALSE;
1836 if (!GetNextChar(ch)) 1845 if (!GetNextChar(ch))
1837 return; 1846 return;
1838 1847
1839 if (PDFCharIsDelimiter(ch) || PDFCharIsWhitespace(ch)) { 1848 if (PDFCharIsDelimiter(ch) || PDFCharIsWhitespace(ch)) {
1840 m_Pos--; 1849 m_Pos--;
1841 break; 1850 break;
(...skipping 1928 matching lines...) Expand 10 before | Expand all | Expand 10 after
3770 } 3779 }
3771 pObj->Release(); 3780 pObj->Release();
3772 return -1; 3781 return -1;
3773 } 3782 }
3774 pHints->AddSegment(m_Pos, req_size); 3783 pHints->AddSegment(m_Pos, req_size);
3775 return 0; 3784 return 0;
3776 } 3785 }
3777 inline void CPDF_DataAvail::SetStartOffset(FX_FILESIZE dwOffset) { 3786 inline void CPDF_DataAvail::SetStartOffset(FX_FILESIZE dwOffset) {
3778 m_Pos = dwOffset; 3787 m_Pos = dwOffset;
3779 } 3788 }
3780 #define MAX_WORD_BUFFER 256 3789
3781 FX_BOOL CPDF_DataAvail::GetNextToken(CFX_ByteString& token) { 3790 FX_BOOL CPDF_DataAvail::GetNextToken(CFX_ByteString& token) {
3782 m_WordSize = 0; 3791 m_WordSize = 0;
3783 uint8_t ch; 3792 uint8_t ch;
3784 if (!GetNextChar(ch)) 3793 if (!GetNextChar(ch))
3785 return FALSE; 3794 return FALSE;
3786 3795
3787 while (1) { 3796 while (1) {
3788 while (PDFCharIsWhitespace(ch)) { 3797 while (PDFCharIsWhitespace(ch)) {
3789 if (!GetNextChar(ch)) 3798 if (!GetNextChar(ch))
3790 return FALSE; 3799 return FALSE;
(...skipping 17 matching lines...) Expand all
3808 if (!GetNextChar(ch)) 3817 if (!GetNextChar(ch))
3809 return FALSE; 3818 return FALSE;
3810 3819
3811 if (!PDFCharIsOther(ch) && !PDFCharIsNumeric(ch)) { 3820 if (!PDFCharIsOther(ch) && !PDFCharIsNumeric(ch)) {
3812 m_Pos--; 3821 m_Pos--;
3813 CFX_ByteString ret(m_WordBuffer, m_WordSize); 3822 CFX_ByteString ret(m_WordBuffer, m_WordSize);
3814 token = ret; 3823 token = ret;
3815 return TRUE; 3824 return TRUE;
3816 } 3825 }
3817 3826
3818 if (m_WordSize < MAX_WORD_BUFFER) 3827 if (m_WordSize < sizeof(m_WordBuffer) - 1)
Wei Li 2016/01/04 20:11:48 Should be "m_WordSize < sizeof(m_WordBuffer)"?
Lei Zhang 2016/01/05 01:27:53 That's because |CPDF_DataAvail::m_WordBuffer| is d
3819 m_WordBuffer[m_WordSize++] = ch; 3828 m_WordBuffer[m_WordSize++] = ch;
3820 } 3829 }
3821 } else if (ch == '<') { 3830 } else if (ch == '<') {
3822 if (!GetNextChar(ch)) 3831 if (!GetNextChar(ch))
3823 return FALSE; 3832 return FALSE;
3824 3833
3825 if (ch == '<') 3834 if (ch == '<')
3826 m_WordBuffer[m_WordSize++] = ch; 3835 m_WordBuffer[m_WordSize++] = ch;
3827 else 3836 else
3828 m_Pos--; 3837 m_Pos--;
3829 } else if (ch == '>') { 3838 } else if (ch == '>') {
3830 if (!GetNextChar(ch)) 3839 if (!GetNextChar(ch))
3831 return FALSE; 3840 return FALSE;
3832 3841
3833 if (ch == '>') 3842 if (ch == '>')
3834 m_WordBuffer[m_WordSize++] = ch; 3843 m_WordBuffer[m_WordSize++] = ch;
3835 else 3844 else
3836 m_Pos--; 3845 m_Pos--;
3837 } 3846 }
3838 3847
3839 CFX_ByteString ret(m_WordBuffer, m_WordSize); 3848 CFX_ByteString ret(m_WordBuffer, m_WordSize);
3840 token = ret; 3849 token = ret;
3841 return TRUE; 3850 return TRUE;
3842 } 3851 }
3843 3852
3844 while (1) { 3853 while (1) {
3845 if (m_WordSize < MAX_WORD_BUFFER) 3854 if (m_WordSize < sizeof(m_WordBuffer) - 1)
Wei Li 2016/01/04 20:11:48 Ditto
3846 m_WordBuffer[m_WordSize++] = ch; 3855 m_WordBuffer[m_WordSize++] = ch;
3847 3856
3848 if (!GetNextChar(ch)) 3857 if (!GetNextChar(ch))
3849 return FALSE; 3858 return FALSE;
3850 3859
3851 if (PDFCharIsDelimiter(ch) || PDFCharIsWhitespace(ch)) { 3860 if (PDFCharIsDelimiter(ch) || PDFCharIsWhitespace(ch)) {
3852 m_Pos--; 3861 m_Pos--;
3853 break; 3862 break;
3854 } 3863 }
3855 } 3864 }
(...skipping 1131 matching lines...) Expand 10 before | Expand all | Expand 10 after
4987 if (!m_pLinearizedDict) 4996 if (!m_pLinearizedDict)
4988 return -1; 4997 return -1;
4989 CPDF_Array* pRange = m_pLinearizedDict->GetArray("H"); 4998 CPDF_Array* pRange = m_pLinearizedDict->GetArray("H");
4990 if (!pRange) 4999 if (!pRange)
4991 return -1; 5000 return -1;
4992 CPDF_Object* pStreamLen = pRange->GetElementValue(1); 5001 CPDF_Object* pStreamLen = pRange->GetElementValue(1);
4993 if (!pStreamLen) 5002 if (!pStreamLen)
4994 return -1; 5003 return -1;
4995 return pStreamLen->GetInteger(); 5004 return pStreamLen->GetInteger();
4996 } 5005 }
OLDNEW
« no previous file with comments | « core/include/fpdfapi/fpdf_parser.h ('k') | fpdfsdk/include/fsdk_annothandler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698