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

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

Issue 1360103002: Get rid of gotos in CPDF_SyntaxParser and FlateUncompress(). (Closed) Base URL: https://pdfium.googlesource.com/pdfium@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 <set> 7 #include <set>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "../../../../third_party/base/nonstd_unique_ptr.h"
11 #include "../../../include/fpdfapi/fpdf_module.h" 12 #include "../../../include/fpdfapi/fpdf_module.h"
12 #include "../../../include/fpdfapi/fpdf_page.h" 13 #include "../../../include/fpdfapi/fpdf_page.h"
13 #include "../../../include/fpdfapi/fpdf_parser.h" 14 #include "../../../include/fpdfapi/fpdf_parser.h"
14 #include "../../../include/fxcrt/fx_safe_types.h" 15 #include "../../../include/fxcrt/fx_safe_types.h"
15 #include "../fpdf_page/pageint.h" 16 #include "../fpdf_page/pageint.h"
16 17
17 FX_BOOL IsSignatureDict(const CPDF_Dictionary* pDict) { 18 FX_BOOL IsSignatureDict(const CPDF_Dictionary* pDict) {
18 CPDF_Object* pType = pDict->GetElementValue(FX_BSTRC("Type")); 19 CPDF_Object* pType = pDict->GetElementValue(FX_BSTRC("Type"));
19 if (!pType) { 20 if (!pType) {
20 pType = pDict->GetElementValue(FX_BSTRC("FT")); 21 pType = pDict->GetElementValue(FX_BSTRC("FT"));
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 } 478 }
478 } 479 }
479 m_V5Type.SetAtGrow(objnum, 1); 480 m_V5Type.SetAtGrow(objnum, 1);
480 } 481 }
481 } 482 }
482 } 483 }
483 FX_Free(pBuf); 484 FX_Free(pBuf);
484 m_Syntax.RestorePos(SavedPos + count * recordsize); 485 m_Syntax.RestorePos(SavedPos + count * recordsize);
485 return TRUE; 486 return TRUE;
486 } 487 }
488
487 FX_BOOL CPDF_Parser::LoadCrossRefV4(FX_FILESIZE pos, 489 FX_BOOL CPDF_Parser::LoadCrossRefV4(FX_FILESIZE pos,
488 FX_FILESIZE streampos, 490 FX_FILESIZE streampos,
489 FX_BOOL bSkip, 491 FX_BOOL bSkip,
490 FX_BOOL bFirst) { 492 FX_BOOL bFirst) {
491 m_Syntax.RestorePos(pos); 493 m_Syntax.RestorePos(pos);
492 if (m_Syntax.GetKeyword() != FX_BSTRC("xref")) { 494 if (m_Syntax.GetKeyword() != FX_BSTRC("xref")) {
493 return FALSE; 495 return FALSE;
494 } 496 }
495 void* pResult = 497 void* pResult =
496 FXSYS_bsearch(&pos, m_SortedOffset.GetData(), m_SortedOffset.GetSize(), 498 FXSYS_bsearch(&pos, m_SortedOffset.GetData(), m_SortedOffset.GetSize(),
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 } 587 }
586 if (bFirstBlock) { 588 if (bFirstBlock) {
587 bFirstBlock = FALSE; 589 bFirstBlock = FALSE;
588 } 590 }
589 } 591 }
590 } 592 }
591 FX_Free(pBuf); 593 FX_Free(pBuf);
592 } 594 }
593 m_Syntax.RestorePos(SavedPos + count * recordsize); 595 m_Syntax.RestorePos(SavedPos + count * recordsize);
594 } 596 }
595 if (streampos) 597 if (streampos && !LoadCrossRefV5(streampos, streampos, FALSE))
Tom Sepez 2015/09/23 21:48:41 nit: maybe return !streampos || LoadCrossRefV5(..)
Lei Zhang 2015/09/25 09:33:48 Done.
596 if (!LoadCrossRefV5(streampos, streampos, FALSE)) { 598 return FALSE;
597 return FALSE;
598 }
599 return TRUE; 599 return TRUE;
600 } 600 }
601
601 FX_BOOL CPDF_Parser::LoadAllCrossRefV5(FX_FILESIZE xrefpos) { 602 FX_BOOL CPDF_Parser::LoadAllCrossRefV5(FX_FILESIZE xrefpos) {
602 if (!LoadCrossRefV5(xrefpos, xrefpos, TRUE)) { 603 if (!LoadCrossRefV5(xrefpos, xrefpos, TRUE)) {
603 return FALSE; 604 return FALSE;
604 } 605 }
605 while (xrefpos) 606 while (xrefpos)
606 if (!LoadCrossRefV5(xrefpos, xrefpos, FALSE)) { 607 if (!LoadCrossRefV5(xrefpos, xrefpos, FALSE)) {
607 return FALSE; 608 return FALSE;
608 } 609 }
609 m_ObjectStreamMap.InitHashTable(101, FALSE); 610 m_ObjectStreamMap.InitHashTable(101, FALSE);
610 m_bXRefStream = TRUE; 611 m_bXRefStream = TRUE;
(...skipping 2082 matching lines...) Expand 10 before | Expand all | Expand 10 after
2693 } else { 2694 } else {
2694 offset = byte == tag_data[taglen - 1] ? taglen - 2 : taglen - 1; 2695 offset = byte == tag_data[taglen - 1] ? taglen - 2 : taglen - 1;
2695 pos--; 2696 pos--;
2696 } 2697 }
2697 if (pos < 0) { 2698 if (pos < 0) {
2698 return FALSE; 2699 return FALSE;
2699 } 2700 }
2700 } 2701 }
2701 return FALSE; 2702 return FALSE;
2702 } 2703 }
2704
2703 struct _SearchTagRecord { 2705 struct _SearchTagRecord {
2704 const uint8_t* m_pTag; 2706 const uint8_t* m_pTag;
2705 FX_DWORD m_Len; 2707 FX_DWORD m_Len;
2706 FX_DWORD m_Offset; 2708 FX_DWORD m_Offset;
2707 }; 2709 };
2710
2708 int32_t CPDF_SyntaxParser::SearchMultiWord(const CFX_ByteStringC& tags, 2711 int32_t CPDF_SyntaxParser::SearchMultiWord(const CFX_ByteStringC& tags,
2709 FX_BOOL bWholeWord, 2712 FX_BOOL bWholeWord,
2710 FX_FILESIZE limit) { 2713 FX_FILESIZE limit) {
2711 int32_t ntags = 1, i; 2714 int32_t ntags = 1;
2712 for (i = 0; i < tags.GetLength(); i++) 2715 for (int i = 0; i < tags.GetLength(); i++) {
2713 if (tags[i] == 0) { 2716 if (tags[i] == 0) {
2714 ntags++; 2717 ntags++;
2715 } 2718 }
2716 _SearchTagRecord* pPatterns = FX_Alloc(_SearchTagRecord, ntags); 2719 }
2717 FX_DWORD start = 0, itag = 0, max_len = 0; 2720 std::vector<_SearchTagRecord> patterns(ntags);
2718 for (i = 0; i <= tags.GetLength(); i++) { 2721 FX_DWORD start = 0;
2722 FX_DWORD itag = 0;
2723 FX_DWORD max_len = 0;
2724 for (int i = 0; i <= tags.GetLength(); i++) {
2719 if (tags[i] == 0) { 2725 if (tags[i] == 0) {
2720 FX_DWORD len = i - start; 2726 FX_DWORD len = i - start;
2721 if (len > max_len) { 2727 if (len > max_len) {
2722 max_len = len; 2728 max_len = len;
2723 } 2729 }
2724 pPatterns[itag].m_pTag = tags.GetPtr() + start; 2730 patterns[itag].m_pTag = tags.GetPtr() + start;
2725 pPatterns[itag].m_Len = len; 2731 patterns[itag].m_Len = len;
2726 pPatterns[itag].m_Offset = 0; 2732 patterns[itag].m_Offset = 0;
2727 start = i + 1; 2733 start = i + 1;
2728 itag++; 2734 itag++;
2729 } 2735 }
2730 } 2736 }
2731 FX_FILESIZE pos = m_Pos;
2732 uint8_t byte; 2737 uint8_t byte;
2733 GetCharAt(pos++, byte); 2738 GetCharAt(m_Pos, byte);
2734 int32_t found = -1; 2739 for (FX_FILESIZE pos = m_Pos + 1;; ++pos) {
2735 while (1) { 2740 for (int i = 0; i < ntags; i++) {
2736 for (i = 0; i < ntags; i++) { 2741 if (patterns[i].m_pTag[patterns[i].m_Offset] == byte) {
2737 if (pPatterns[i].m_pTag[pPatterns[i].m_Offset] == byte) { 2742 patterns[i].m_Offset++;
2738 pPatterns[i].m_Offset++; 2743 if (patterns[i].m_Offset == patterns[i].m_Len) {
2739 if (pPatterns[i].m_Offset == pPatterns[i].m_Len) {
2740 if (!bWholeWord || 2744 if (!bWholeWord ||
2741 IsWholeWord(pos - pPatterns[i].m_Len, limit, pPatterns[i].m_pTag, 2745 IsWholeWord(pos - patterns[i].m_Len, limit, patterns[i].m_pTag,
2742 pPatterns[i].m_Len)) { 2746 patterns[i].m_Len)) {
2743 found = i; 2747 return i;
2744 goto end;
2745 } else { 2748 } else {
2746 if (pPatterns[i].m_pTag[0] == byte) { 2749 if (patterns[i].m_pTag[0] == byte) {
2747 pPatterns[i].m_Offset = 1; 2750 patterns[i].m_Offset = 1;
2748 } else { 2751 } else {
2749 pPatterns[i].m_Offset = 0; 2752 patterns[i].m_Offset = 0;
2750 } 2753 }
2751 } 2754 }
2752 } 2755 }
2753 } else { 2756 } else {
2754 if (pPatterns[i].m_pTag[0] == byte) { 2757 if (patterns[i].m_pTag[0] == byte) {
2755 pPatterns[i].m_Offset = 1; 2758 patterns[i].m_Offset = 1;
2756 } else { 2759 } else {
2757 pPatterns[i].m_Offset = 0; 2760 patterns[i].m_Offset = 0;
2758 } 2761 }
2759 } 2762 }
2760 } 2763 }
2761 if (limit && pos >= m_Pos + limit) { 2764
2762 goto end; 2765 if ((limit && pos >= m_Pos + limit) || !GetCharAt(pos, byte)) {
Tom Sepez 2015/09/23 21:48:41 I would like to have the first half of this || use
Lei Zhang 2015/09/25 09:33:48 Done.
2766 return -1;
2763 } 2767 }
2764 if (!GetCharAt(pos, byte)) {
2765 goto end;
2766 }
2767 pos++;
2768 } 2768 }
2769 end: 2769 return -1;
Tom Sepez 2015/09/23 21:48:41 notreached? Only way out of your loop by return? w
Lei Zhang 2015/09/25 09:33:48 Acknowledged.
2770 FX_Free(pPatterns);
2771 return found;
2772 } 2770 }
2771
2773 FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag, 2772 FX_FILESIZE CPDF_SyntaxParser::FindTag(const CFX_ByteStringC& tag,
2774 FX_FILESIZE limit) { 2773 FX_FILESIZE limit) {
2775 int32_t taglen = tag.GetLength(); 2774 int32_t taglen = tag.GetLength();
2776 int32_t match = 0; 2775 int32_t match = 0;
2777 limit += m_Pos; 2776 limit += m_Pos;
2778 FX_FILESIZE startpos = m_Pos; 2777 FX_FILESIZE startpos = m_Pos;
2779 while (1) { 2778 while (1) {
2780 uint8_t ch; 2779 uint8_t ch;
2781 if (!GetNextChar(ch)) { 2780 if (!GetNextChar(ch)) {
2782 return -1; 2781 return -1;
(...skipping 24 matching lines...) Expand all
2807 break; 2806 break;
2808 } 2807 }
2809 } 2808 }
2810 } 2809 }
2811 2810
2812 class CPDF_DataAvail final : public IPDF_DataAvail { 2811 class CPDF_DataAvail final : public IPDF_DataAvail {
2813 public: 2812 public:
2814 CPDF_DataAvail(IFX_FileAvail* pFileAvail, IFX_FileRead* pFileRead); 2813 CPDF_DataAvail(IFX_FileAvail* pFileAvail, IFX_FileRead* pFileRead);
2815 ~CPDF_DataAvail() override; 2814 ~CPDF_DataAvail() override;
2816 2815
2817 virtual FX_BOOL IsDocAvail(IFX_DownloadHints* pHints) override; 2816 FX_BOOL IsDocAvail(IFX_DownloadHints* pHints) override;
2818 2817
2819 virtual void SetDocument(CPDF_Document* pDoc) override; 2818 void SetDocument(CPDF_Document* pDoc) override;
2820 2819
2821 virtual FX_BOOL IsPageAvail(int iPage, IFX_DownloadHints* pHints) override; 2820 FX_BOOL IsPageAvail(int iPage, IFX_DownloadHints* pHints) override;
2822 2821
2823 virtual int32_t IsFormAvail(IFX_DownloadHints* pHints) override; 2822 int32_t IsFormAvail(IFX_DownloadHints* pHints) override;
2824 2823
2825 virtual int32_t IsLinearizedPDF() override; 2824 int32_t IsLinearizedPDF() override;
2826 2825
2827 virtual FX_BOOL IsLinearized() override { return m_bLinearized; } 2826 FX_BOOL IsLinearized() override { return m_bLinearized; }
2828 2827
2829 virtual void GetLinearizedMainXRefInfo(FX_FILESIZE* pPos, 2828 void GetLinearizedMainXRefInfo(FX_FILESIZE* pPos, FX_DWORD* pSize) override;
2830 FX_DWORD* pSize) override;
2831 2829
2832 protected: 2830 protected:
2833 static const int kMaxDataAvailRecursionDepth = 64; 2831 static const int kMaxDataAvailRecursionDepth = 64;
2834 static int s_CurrentDataAvailRecursionDepth; 2832 static int s_CurrentDataAvailRecursionDepth;
2835 2833
2836 FX_DWORD GetObjectSize(FX_DWORD objnum, FX_FILESIZE& offset); 2834 FX_DWORD GetObjectSize(FX_DWORD objnum, FX_FILESIZE& offset);
2837 FX_BOOL IsObjectsAvail(CFX_PtrArray& obj_array, 2835 FX_BOOL IsObjectsAvail(CFX_PtrArray& obj_array,
2838 FX_BOOL bParsePage, 2836 FX_BOOL bParsePage,
2839 IFX_DownloadHints* pHints, 2837 IFX_DownloadHints* pHints,
2840 CFX_PtrArray& ret_array); 2838 CFX_PtrArray& ret_array);
(...skipping 1842 matching lines...) Expand 10 before | Expand all | Expand 10 after
4683 return FALSE; 4681 return FALSE;
4684 } 4682 }
4685 CPDF_PageNode::~CPDF_PageNode() { 4683 CPDF_PageNode::~CPDF_PageNode() {
4686 int32_t iSize = m_childNode.GetSize(); 4684 int32_t iSize = m_childNode.GetSize();
4687 for (int32_t i = 0; i < iSize; ++i) { 4685 for (int32_t i = 0; i < iSize; ++i) {
4688 CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i]; 4686 CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i];
4689 delete pNode; 4687 delete pNode;
4690 } 4688 }
4691 m_childNode.RemoveAll(); 4689 m_childNode.RemoveAll();
4692 } 4690 }
OLDNEW
« no previous file with comments | « no previous file | core/src/fxcodec/codec/fx_codec_flate.cpp » ('j') | core/src/fxcodec/codec/fx_codec_flate.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698