Revert "FX Bool considered harmful, part 3"
[pdfium.git] / core / src / fpdfapi / fpdf_parser / fpdf_parser_parser.cpp
index 3caef06..854be75 100644 (file)
@@ -68,7 +68,7 @@ void CPDF_Parser::SetEncryptDictionary(CPDF_Dictionary* pDict)
 void CPDF_Parser::CloseParser(FX_BOOL bReParse)
 {
     m_bVersionUpdated = FALSE;
-    if (m_pDocument && !bReParse) {
+    if (!bReParse) {
         delete m_pDocument;
         m_pDocument = NULL;
     }
@@ -309,11 +309,9 @@ FX_DWORD CPDF_Parser::SetEncryptHandler()
 }
 void CPDF_Parser::ReleaseEncryptHandler()
 {
-    if (m_Syntax.m_pCryptoHandler) {
-        delete m_Syntax.m_pCryptoHandler;
-        m_Syntax.m_pCryptoHandler = NULL;
-    }
-    if (m_pSecurityHandler && !m_bForceUseSecurityHandler) {
+    delete m_Syntax.m_pCryptoHandler;
+    m_Syntax.m_pCryptoHandler = NULL;
+    if (!m_bForceUseSecurityHandler) {
         delete m_pSecurityHandler;
         m_pSecurityHandler = NULL;
     }
@@ -1482,7 +1480,7 @@ FX_BOOL CPDF_Parser::IsOwner()
 void CPDF_Parser::SetSecurityHandler(CPDF_SecurityHandler* pSecurityHandler, FX_BOOL bForced)
 {
     ASSERT(m_pSecurityHandler == NULL);
-    if (m_pSecurityHandler && !m_bForceUseSecurityHandler) {
+    if (!m_bForceUseSecurityHandler) {
         delete m_pSecurityHandler;
         m_pSecurityHandler = NULL;
     }
@@ -1686,7 +1684,7 @@ CPDF_SyntaxParser::CPDF_SyntaxParser()
     m_pFileAccess = NULL;
     m_pCryptoHandler = NULL;
     m_pFileBuf = NULL;
-    m_BufSize = CPDF_ModuleMgr::Get()->m_FileBufSize;
+    m_BufSize = CPDF_ModuleMgr::kFileBufSize;
     m_pFileBuf = NULL;
     m_MetadataObjnum = 0;
     m_dwWordPos = 0;
@@ -1995,21 +1993,16 @@ CFX_ByteString CPDF_SyntaxParser::ReadHexString()
 void CPDF_SyntaxParser::ToNextLine()
 {
     uint8_t ch;
-    while (1) {
-        if (!GetNextChar(ch)) {
-            return;
-        }
+    while (GetNextChar(ch)) {
         if (ch == '\n') {
-            return;
+            break;
         }
         if (ch == '\r') {
             GetNextChar(ch);
-            if (ch == '\n') {
-                return;
-            } else {
-                m_Pos --;
-                return;
+            if (ch != '\n') {
+                --m_Pos;
             }
+            break;
         }
     }
 }
@@ -2064,7 +2057,6 @@ CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjects* pObjList, FX_DWO
     FX_BOOL bTypeOnly = pContext && (pContext->m_Flags & PDFPARSE_TYPEONLY);
     FX_BOOL bIsNumber;
     CFX_ByteString word = GetNextWord(bIsNumber);
-    CPDF_Object* pRet = NULL;
     if (word.GetLength() == 0) {
         if (bTypeOnly) {
             return (CPDF_Object*)PDFOBJ_INVALID;
@@ -2081,38 +2073,26 @@ CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjects* pObjList, FX_DWO
                 if (bTypeOnly) {
                     return (CPDF_Object*)PDFOBJ_REFERENCE;
                 }
-                pRet = CPDF_Reference::Create(pObjList, objnum);
-                return pRet;
-            } else {
-                m_Pos = SavedPos;
-                if (bTypeOnly) {
-                    return (CPDF_Object*)PDFOBJ_NUMBER;
-                }
-                pRet = CPDF_Number::Create(word);
-                return pRet;
-            }
-        } else {
-            m_Pos = SavedPos;
-            if (bTypeOnly) {
-                return (CPDF_Object*)PDFOBJ_NUMBER;
+                return new CPDF_Reference(pObjList, objnum);
             }
-            pRet = CPDF_Number::Create(word);
-            return pRet;
         }
+        m_Pos = SavedPos;
+        if (bTypeOnly) {
+            return (CPDF_Object*)PDFOBJ_NUMBER;
+        }
+        return CPDF_Number::Create(word);
     }
     if (word == FX_BSTRC("true") || word == FX_BSTRC("false")) {
         if (bTypeOnly) {
             return (CPDF_Object*)PDFOBJ_BOOLEAN;
         }
-        pRet = CPDF_Boolean::Create(word == FX_BSTRC("true"));
-        return pRet;
+        return CPDF_Boolean::Create(word == FX_BSTRC("true"));
     }
     if (word == FX_BSTRC("null")) {
         if (bTypeOnly) {
             return (CPDF_Object*)PDFOBJ_NULL;
         }
-        pRet = CPDF_Null::Create();
-        return pRet;
+        return CPDF_Null::Create();
     }
     if (word == FX_BSTRC("(")) {
         if (bTypeOnly) {
@@ -2122,8 +2102,7 @@ CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjects* pObjList, FX_DWO
         if (m_pCryptoHandler && bDecrypt) {
             m_pCryptoHandler->Decrypt(objnum, gennum, str);
         }
-        pRet = CPDF_String::Create(str, FALSE);
-        return pRet;
+        return CPDF_String::Create(str, FALSE);
     }
     if (word == FX_BSTRC("<")) {
         if (bTypeOnly) {
@@ -2133,8 +2112,7 @@ CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjects* pObjList, FX_DWO
         if (m_pCryptoHandler && bDecrypt) {
             m_pCryptoHandler->Decrypt(objnum, gennum, str);
         }
-        pRet = CPDF_String::Create(str, TRUE);
-        return pRet;
+        return CPDF_String::Create(str, TRUE);
     }
     if (word == FX_BSTRC("[")) {
         if (bTypeOnly) {
@@ -2153,8 +2131,8 @@ CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjects* pObjList, FX_DWO
         if (bTypeOnly) {
             return (CPDF_Object*)PDFOBJ_NAME;
         }
-        pRet = CPDF_Name::Create(PDF_NameDecode(CFX_ByteStringC(m_WordBuffer + 1, m_WordSize - 1)));
-        return pRet;
+        return CPDF_Name::Create(
+            PDF_NameDecode(CFX_ByteStringC(m_WordBuffer + 1, m_WordSize - 1)));
     }
     if (word == FX_BSTRC("<<")) {
         if (bTypeOnly) {
@@ -2262,25 +2240,18 @@ CPDF_Object* CPDF_SyntaxParser::GetObjectByStrict(CPDF_IndirectObjects* pObjList
         if (bIsNumber) {
             CFX_ByteString nextword2 = GetNextWord(bIsNumber);
             if (nextword2 == FX_BSTRC("R")) {
-                FX_DWORD objnum = FXSYS_atoi(word);
                 if (bTypeOnly) {
                     return (CPDF_Object*)PDFOBJ_REFERENCE;
                 }
-                return CPDF_Reference::Create(pObjList, objnum);
-            } else {
-                m_Pos = SavedPos;
-                if (bTypeOnly) {
-                    return (CPDF_Object*)PDFOBJ_NUMBER;
-                }
-                return CPDF_Number::Create(word);
-            }
-        } else {
-            m_Pos = SavedPos;
-            if (bTypeOnly) {
-                return (CPDF_Object*)PDFOBJ_NUMBER;
+                FX_DWORD objnum = FXSYS_atoi(word);
+                return new CPDF_Reference(pObjList, objnum);
             }
-            return CPDF_Number::Create(word);
         }
+        m_Pos = SavedPos;
+        if (bTypeOnly) {
+            return (CPDF_Object*)PDFOBJ_NUMBER;
+        }
+        return CPDF_Number::Create(word);
     }
     if (word == FX_BSTRC("true") || word == FX_BSTRC("false")) {
         if (bTypeOnly) {
@@ -2984,12 +2955,8 @@ CPDF_DataAvail::~CPDF_DataAvail()
     if (m_pTrailer) {
         m_pTrailer->Release();
     }
-    if (m_pageMapCheckState) {
-        delete m_pageMapCheckState;
-    }
-    if (m_pagesLoadState) {
-        delete m_pagesLoadState;
-    }
+    delete m_pageMapCheckState;
+    delete m_pagesLoadState;
     int32_t i = 0;
     int32_t iSize = m_arrayAcroforms.GetSize();
     for (i = 0; i < iSize; ++i) {
@@ -3154,21 +3121,20 @@ FX_BOOL CPDF_DataAvail::CheckAcroFormSubObject(IFX_DownloadHints* pHints)
             m_objs_array.RemoveAll();
         }
         return bRet;
-    } else {
-        CFX_PtrArray new_objs_array;
-        FX_BOOL bRet = IsObjectsAvail(m_objs_array, FALSE, pHints, new_objs_array);
-        if (bRet) {
-            int32_t iSize = m_arrayAcroforms.GetSize();
-            for (int32_t i = 0; i < iSize; ++i) {
-                ((CPDF_Object *)m_arrayAcroforms.GetAt(i))->Release();
-            }
-            m_arrayAcroforms.RemoveAll();
-        } else {
-            m_objs_array.RemoveAll();
-            m_objs_array.Append(new_objs_array);
+    }
+    CFX_PtrArray new_objs_array;
+    FX_BOOL bRet = IsObjectsAvail(m_objs_array, FALSE, pHints, new_objs_array);
+    if (bRet) {
+        int32_t iSize = m_arrayAcroforms.GetSize();
+        for (int32_t i = 0; i < iSize; ++i) {
+            ((CPDF_Object *)m_arrayAcroforms.GetAt(i))->Release();
         }
-        return bRet;
+        m_arrayAcroforms.RemoveAll();
+    } else {
+        m_objs_array.RemoveAll();
+        m_objs_array.Append(new_objs_array);
     }
+    return bRet;
 }
 FX_BOOL CPDF_DataAvail::CheckAcroForm(IFX_DownloadHints* pHints)
 {
@@ -3222,16 +3188,14 @@ FX_BOOL CPDF_DataAvail::CheckDocStatus(IFX_DownloadHints *pHints)
         case PDF_DATAAVAIL_PAGETREE:
             if (m_bTotalLoadPageTree) {
                 return CheckPages(pHints);
-            } else {
-                return LoadDocPages(pHints);
             }
+            return LoadDocPages(pHints);
         case PDF_DATAAVAIL_PAGE:
             if (m_bTotalLoadPageTree) {
                 return CheckPage(pHints);
-            } else {
-                m_docStatus = PDF_DATAAVAIL_PAGE_LATERLOAD;
-                return TRUE;
             }
+            m_docStatus = PDF_DATAAVAIL_PAGE_LATERLOAD;
+            return TRUE;
         case PDF_DATAAVAIL_ERROR:
             return LoadAllFile(pHints);
         case PDF_DATAAVAIL_PAGE_LATERLOAD:
@@ -3775,10 +3739,9 @@ FX_BOOL CPDF_DataAvail::CheckEnd(IFX_DownloadHints* pHints)
             SetStartOffset(m_dwXRefOffset);
             m_docStatus = PDF_DATAAVAIL_CROSSREF;
             return TRUE;
-        } else {
-            m_docStatus = PDF_DATAAVAIL_LOADALLFILE;
-            return TRUE;
         }
+        m_docStatus = PDF_DATAAVAIL_LOADALLFILE;
+        return TRUE;
     }
     pHints->AddSegment(req_pos, dwSize);
     return FALSE;
@@ -3813,9 +3776,6 @@ int32_t CPDF_DataAvail::CheckCrossRefStream(IFX_DownloadHints* pHints, FX_FILESI
                 xref_offset = pObj->GetDict()->GetInteger(FX_BSTRC("Prev"));
                 pObj->Release();
                 return 1;
-            } else {
-                pObj->Release();
-                return -1;
             }
         }
         pObj->Release();
@@ -3969,7 +3929,8 @@ FX_BOOL CPDF_DataAvail::CheckAllCrossRefStream(IFX_DownloadHints *pHints)
             m_Pos = xref_offset;
         }
         return TRUE;
-    } else if (nRet == -1) {
+    }
+    if (nRet == -1) {
         m_docStatus = PDF_DATAAVAIL_ERROR;
     }
     return FALSE;
@@ -4072,11 +4033,10 @@ FX_BOOL CPDF_DataAvail::CheckTrailer(IFX_DownloadHints* pHints)
                 }
             }
             return TRUE;
-        } else {
-            m_dwPrevXRefOffset = 0;
-            m_docStatus = PDF_DATAAVAIL_TRAILER_APPEND;
-            pTrailer->Release();
         }
+        m_dwPrevXRefOffset = 0;
+        m_docStatus = PDF_DATAAVAIL_TRAILER_APPEND;
+        pTrailer->Release();
         return TRUE;
     }
     pHints->AddSegment(m_Pos, iTrailerSize);
@@ -4309,9 +4269,8 @@ FX_BOOL CPDF_DataAvail::LoadDocPages(IFX_DownloadHints* pHints)
     if (CheckPageCount(pHints)) {
         m_docStatus = PDF_DATAAVAIL_PAGE;
         return TRUE;
-    } else {
-        m_bTotalLoadPageTree = TRUE;
     }
+    m_bTotalLoadPageTree = TRUE;
     return FALSE;
 }
 FX_BOOL CPDF_DataAvail::LoadPages(IFX_DownloadHints* pHints)
@@ -4377,15 +4336,14 @@ FX_BOOL CPDF_DataAvail::CheckPageAnnots(int32_t iPage, IFX_DownloadHints* pHints
             m_objs_array.RemoveAll();
         }
         return bRet;
-    } else {
-        CFX_PtrArray new_objs_array;
-        FX_BOOL bRet = IsObjectsAvail(m_objs_array, FALSE, pHints, new_objs_array);
-        m_objs_array.RemoveAll();
-        if (!bRet) {
-            m_objs_array.Append(new_objs_array);
-        }
-        return bRet;
     }
+    CFX_PtrArray new_objs_array;
+    FX_BOOL bRet = IsObjectsAvail(m_objs_array, FALSE, pHints, new_objs_array);
+    m_objs_array.RemoveAll();
+    if (!bRet) {
+        m_objs_array.Append(new_objs_array);
+    }
+    return bRet;
 }
 FX_BOOL CPDF_DataAvail::CheckLinearizedFirstPage(int32_t iPage, IFX_DownloadHints* pHints)
 {
@@ -4395,10 +4353,10 @@ FX_BOOL CPDF_DataAvail::CheckLinearizedFirstPage(int32_t iPage, IFX_DownloadHint
         }
         m_bAnnotsLoad = TRUE;
     }
-    if (m_bAnnotsLoad)
-        if (!CheckLinearizedData(pHints)) {
+    if (m_bAnnotsLoad) {
+        if (!CheckLinearizedData(pHints))
             return FALSE;
-        }
+    }
     m_bPageLoadedOK = FALSE;
     return TRUE;
 }
@@ -4553,15 +4511,14 @@ FX_BOOL CPDF_DataAvail::CheckResources(IFX_DownloadHints* pHints)
             m_objs_array.RemoveAll();
         }
         return bRet;
-    } else {
-        CFX_PtrArray new_objs_array;
-        FX_BOOL bRet = IsObjectsAvail(m_objs_array, FALSE, pHints, new_objs_array);
-        m_objs_array.RemoveAll();
-        if (!bRet) {
-            m_objs_array.Append(new_objs_array);
-        }
-        return bRet;
     }
+    CFX_PtrArray new_objs_array;
+    FX_BOOL bRet = IsObjectsAvail(m_objs_array, FALSE, pHints, new_objs_array);
+    m_objs_array.RemoveAll();
+    if (!bRet) {
+        m_objs_array.Append(new_objs_array);
+    }
+    return bRet;
 }
 void CPDF_DataAvail::GetLinearizedMainXRefInfo(FX_FILESIZE *pPos, FX_DWORD *pSize)
 {
@@ -4618,16 +4575,15 @@ FX_BOOL CPDF_SortObjNumArray::Find(FX_DWORD dwObjNum)
 }
 FX_BOOL CPDF_SortObjNumArray::BinarySearch(FX_DWORD value, int32_t &iNext)
 {
-    int32_t iLen = m_number_array.GetSize();
     int32_t iLow = 0;
-    int32_t iHigh = iLen - 1;
-    int32_t iMid = 0;
+    int32_t iHigh = m_number_array.GetSize() - 1;
     while (iLow <= iHigh) {
-        iMid = (iLow + iHigh) / 2;
+        int32_t iMid = (iLow + iHigh) / 2;
         if (m_number_array.GetAt(iMid) == value) {
             iNext = iMid;
             return TRUE;
-        } else if (m_number_array.GetAt(iMid) > value) {
+        }
+        if (m_number_array.GetAt(iMid) > value) {
             iHigh = iMid - 1;
         } else if (m_number_array.GetAt(iMid) < value) {
             iLow = iMid + 1;
@@ -4640,10 +4596,8 @@ CPDF_PageNode::~CPDF_PageNode()
 {
     int32_t iSize = m_childNode.GetSize();
     for (int32_t i = 0; i < iSize; ++i) {
-        CPDF_PageNode *pNode = (CPDF_PageNode*)m_childNode[i];
-        if (pNode) {
-            delete pNode;
-        }
+        CPDF_PageNode* pNode = (CPDF_PageNode*)m_childNode[i];
+        delete pNode;
     }
     m_childNode.RemoveAll();
 }