Revert "FX Bool considered harmful, part 3"
[pdfium.git] / core / src / fpdfapi / fpdf_parser / fpdf_parser_fdf.cpp
index 22f9aa7..1958fab 100644 (file)
-// Copyright 2014 PDFium Authors. All rights reserved.\r
-// Use of this source code is governed by a BSD-style license that can be\r
-// found in the LICENSE file.\r
\r
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
-\r
-#include "../../../include/fpdfapi/fpdf_serial.h"\r
-CFDF_Document::CFDF_Document() : CPDF_IndirectObjects(NULL)\r
-{\r
-    m_pRootDict = NULL;\r
-    m_pFile = NULL;\r
-    m_bOwnFile = FALSE;\r
-}\r
-CFDF_Document::~CFDF_Document()\r
-{\r
-    if (m_bOwnFile && m_pFile) {\r
-        m_pFile->Release();\r
-    }\r
-}\r
-CFDF_Document* CFDF_Document::CreateNewDoc()\r
-{\r
-    CFDF_Document* pDoc = FX_NEW CFDF_Document;\r
-    pDoc->m_pRootDict = FX_NEW CPDF_Dictionary;\r
-    pDoc->AddIndirectObject(pDoc->m_pRootDict);\r
-    CPDF_Dictionary* pFDFDict = FX_NEW CPDF_Dictionary;\r
-    pDoc->m_pRootDict->SetAt(FX_BSTRC("FDF"), pFDFDict);\r
-    return pDoc;\r
-}\r
-CFDF_Document* CFDF_Document::ParseFile(FX_LPCSTR file_path)\r
-{\r
-    return CFDF_Document::ParseFile(FX_CreateFileRead(file_path), TRUE);\r
-}\r
-CFDF_Document* CFDF_Document::ParseFile(FX_LPCWSTR file_path)\r
-{\r
-    return CFDF_Document::ParseFile(FX_CreateFileRead(file_path), TRUE);\r
-}\r
-CFDF_Document* CFDF_Document::ParseFile(IFX_FileRead *pFile, FX_BOOL bOwnFile)\r
-{\r
-    if (!pFile) {\r
-        return NULL;\r
-    }\r
-    CFDF_Document* pDoc = FX_NEW CFDF_Document;\r
-    pDoc->ParseStream(pFile, bOwnFile);\r
-    if (pDoc->m_pRootDict == NULL) {\r
-        delete pDoc;\r
-        return NULL;\r
-    }\r
-    return pDoc;\r
-}\r
-CFDF_Document* CFDF_Document::ParseMemory(FX_LPCBYTE pData, FX_DWORD size)\r
-{\r
-    return CFDF_Document::ParseFile(FX_CreateMemoryStream((FX_LPBYTE)pData, size), TRUE);\r
-}\r
-void CFDF_Document::ParseStream(IFX_FileRead *pFile, FX_BOOL bOwnFile)\r
-{\r
-    m_pFile = pFile;\r
-    m_bOwnFile = bOwnFile;\r
-    CPDF_SyntaxParser parser;\r
-    parser.InitParser(m_pFile, 0);\r
-    while (1) {\r
-        FX_BOOL bNumber;\r
-        CFX_ByteString word = parser.GetNextWord(bNumber);\r
-        if (bNumber) {\r
-            FX_DWORD objnum = FXSYS_atoi(word);\r
-            word = parser.GetNextWord(bNumber);\r
-            if (!bNumber) {\r
-                break;\r
-            }\r
-            word = parser.GetNextWord(bNumber);\r
-            if (word != FX_BSTRC("obj")) {\r
-                break;\r
-            }\r
-            CPDF_Object* pObj = parser.GetObject(this, objnum, 0, FALSE);\r
-            if (pObj == NULL) {\r
-                break;\r
-            }\r
-            InsertIndirectObject(objnum, pObj);\r
-            word = parser.GetNextWord(bNumber);\r
-            if (word != FX_BSTRC("endobj")) {\r
-                break;\r
-            }\r
-        } else {\r
-            if (word != FX_BSTRC("trailer")) {\r
-                break;\r
-            }\r
-            CPDF_Dictionary* pMainDict = (CPDF_Dictionary*)parser.GetObject(this, 0, 0, 0);\r
-            if (pMainDict == NULL || pMainDict->GetType() != PDFOBJ_DICTIONARY) {\r
-                break;\r
-            }\r
-            m_pRootDict = pMainDict->GetDict(FX_BSTRC("Root"));\r
-            pMainDict->Release();\r
-            break;\r
-        }\r
-    }\r
-}\r
-FX_BOOL CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const\r
-{\r
-    if (m_pRootDict == NULL) {\r
-        return FALSE;\r
-    }\r
-    buf << FX_BSTRC("%FDF-1.2\r\n");\r
-    FX_POSITION pos = m_IndirectObjs.GetStartPosition();\r
-    while(pos) {\r
-        size_t objnum;\r
-        CPDF_Object* pObj;\r
-        m_IndirectObjs.GetNextAssoc(pos, (FX_LPVOID&)objnum, (FX_LPVOID&)pObj);\r
-        buf << (FX_DWORD)objnum << FX_BSTRC(" 0 obj\r\n") << pObj << FX_BSTRC("\r\nendobj\r\n\r\n");\r
-    }\r
-    buf << FX_BSTRC("trailer\r\n<</Root ") << m_pRootDict->GetObjNum() << FX_BSTRC(" 0 R>>\r\n%%EOF\r\n");\r
-    return TRUE;\r
-}\r
-CFX_WideString CFDF_Document::GetWin32Path() const\r
-{\r
-    CPDF_Object* pFileSpec = m_pRootDict->GetDict(FX_BSTRC("FDF"))->GetElementValue(FX_BSTRC("F"));\r
-    if (pFileSpec == NULL) {\r
-        return CFX_WideString();\r
-    }\r
-    if (pFileSpec->GetType() == PDFOBJ_STRING) {\r
-        return FPDF_FileSpec_GetWin32Path(m_pRootDict->GetDict(FX_BSTRC("FDF")));\r
-    }\r
-    return FPDF_FileSpec_GetWin32Path(pFileSpec);\r
-}\r
-FX_BOOL CFDF_Document::WriteFile(FX_LPCSTR file_path) const\r
-{\r
-    IFX_FileWrite *pFile = FX_CreateFileWrite(file_path);\r
-    if (!pFile) {\r
-        return FALSE;\r
-    }\r
-    FX_BOOL bRet = WriteFile(pFile);\r
-    pFile->Release();\r
-    return bRet;\r
-}\r
-FX_BOOL CFDF_Document::WriteFile(FX_LPCWSTR file_path) const\r
-{\r
-    IFX_FileWrite *pFile = FX_CreateFileWrite(file_path);\r
-    if (!pFile) {\r
-        return FALSE;\r
-    }\r
-    FX_BOOL bRet = WriteFile(pFile);\r
-    pFile->Release();\r
-    return bRet;\r
-}\r
-FX_BOOL CFDF_Document::WriteFile(IFX_FileWrite *pFile) const\r
-{\r
-    CFX_ByteTextBuf buf;\r
-    WriteBuf(buf);\r
-    FX_BOOL bRet = pFile->WriteBlock(buf.GetBuffer(), buf.GetSize());\r
-    if (bRet) {\r
-        pFile->Flush();\r
-    }\r
-    return bRet;\r
-}\r
-static CFX_WideString ChangeSlash(FX_LPCWSTR str)\r
-{\r
-    CFX_WideString result;\r
-    while (*str) {\r
-        if (*str == '\\') {\r
-            result += '/';\r
-        } else if (*str == '/') {\r
-            result += '\\';\r
-        } else {\r
-            result += *str;\r
-        }\r
-        str ++;\r
-    }\r
-    return result;\r
-}\r
-void FPDF_FileSpec_SetWin32Path(CPDF_Object* pFileSpec, const CFX_WideString& filepath)\r
-{\r
-    CFX_WideString result;\r
-    if (filepath.GetLength() > 1 && filepath[1] == ':') {\r
-        result = L"/";\r
-        result += filepath[0];\r
-        if (filepath[2] != '\\') {\r
-            result += '/';\r
-        }\r
-        result += ChangeSlash((FX_LPCWSTR)filepath + 2);\r
-    } else if (filepath.GetLength() > 1 && filepath[0] == '\\' && filepath[1] == '\\') {\r
-        result = ChangeSlash((FX_LPCWSTR)filepath + 1);\r
-    } else {\r
-        result = ChangeSlash(filepath);\r
-    }\r
-    if (pFileSpec->GetType() == PDFOBJ_STRING) {\r
-        pFileSpec->SetString(CFX_ByteString::FromUnicode(result));\r
-    } else if (pFileSpec->GetType() == PDFOBJ_DICTIONARY) {\r
-        ((CPDF_Dictionary*)pFileSpec)->SetAtString(FX_BSTRC("F"), CFX_ByteString::FromUnicode(result));\r
-        ((CPDF_Dictionary*)pFileSpec)->SetAtString(FX_BSTRC("UF"), PDF_EncodeText(result));\r
-        ((CPDF_Dictionary*)pFileSpec)->RemoveAt(FX_BSTRC("FS"));\r
-    }\r
-}\r
-CFX_WideString FPDF_FileSpec_GetWin32Path(const CPDF_Object* pFileSpec)\r
-{\r
-    CFX_WideString wsFileName;\r
-    if (pFileSpec->GetType() == PDFOBJ_DICTIONARY) {\r
-        CPDF_Dictionary* pDict = (CPDF_Dictionary*)pFileSpec;\r
-        wsFileName = pDict->GetUnicodeText(FX_BSTRC("UF"));\r
-        if (wsFileName.IsEmpty()) {\r
-            wsFileName = CFX_WideString::FromLocal(pDict->GetString(FX_BSTRC("F")));\r
-        }\r
-        if (pDict->GetString(FX_BSTRC("FS")) == FX_BSTRC("URL")) {\r
-            return wsFileName;\r
-        }\r
-        if (wsFileName.IsEmpty() && pDict->KeyExist(FX_BSTRC("DOS"))) {\r
-            wsFileName = CFX_WideString::FromLocal(pDict->GetString(FX_BSTRC("DOS")));\r
-        }\r
-    } else {\r
-        wsFileName = CFX_WideString::FromLocal(pFileSpec->GetString());\r
-    }\r
-    if (wsFileName[0] != '/') {\r
-        return ChangeSlash(wsFileName);\r
-    }\r
-    if (wsFileName[2] == '/') {\r
-        CFX_WideString result;\r
-        result += wsFileName[1];\r
-        result += ':';\r
-        result += ChangeSlash(((FX_LPCWSTR)wsFileName) + 2);\r
-        return result;\r
-    } else {\r
-        CFX_WideString result;\r
-        result += '\\';\r
-        result += ChangeSlash(wsFileName);\r
-        return result;\r
-    }\r
-}\r
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../../include/fpdfapi/fpdf_serial.h"
+CFDF_Document::CFDF_Document() : CPDF_IndirectObjects(NULL)
+{
+    m_pRootDict = NULL;
+    m_pFile = NULL;
+    m_bOwnFile = FALSE;
+}
+CFDF_Document::~CFDF_Document()
+{
+    if (m_bOwnFile && m_pFile) {
+        m_pFile->Release();
+    }
+}
+CFDF_Document* CFDF_Document::CreateNewDoc()
+{
+    CFDF_Document* pDoc = new CFDF_Document;
+    pDoc->m_pRootDict = new CPDF_Dictionary;
+    pDoc->AddIndirectObject(pDoc->m_pRootDict);
+    CPDF_Dictionary* pFDFDict = new CPDF_Dictionary;
+    pDoc->m_pRootDict->SetAt(FX_BSTRC("FDF"), pFDFDict);
+    return pDoc;
+}
+CFDF_Document* CFDF_Document::ParseFile(IFX_FileRead *pFile, FX_BOOL bOwnFile)
+{
+  if (!pFile) {
+    return NULL;
+  }
+  CFDF_Document* pDoc = new CFDF_Document;
+  pDoc->ParseStream(pFile, bOwnFile);
+  if (pDoc->m_pRootDict == NULL) {
+    delete pDoc;
+    return NULL;
+  }
+  return pDoc;
+}
+CFDF_Document* CFDF_Document::ParseMemory(const uint8_t* pData, FX_DWORD size)
+{
+    return CFDF_Document::ParseFile(FX_CreateMemoryStream((uint8_t*)pData, size), TRUE);
+}
+void CFDF_Document::ParseStream(IFX_FileRead *pFile, FX_BOOL bOwnFile)
+{
+    m_pFile = pFile;
+    m_bOwnFile = bOwnFile;
+    CPDF_SyntaxParser parser;
+    parser.InitParser(m_pFile, 0);
+    while (1) {
+        FX_BOOL bNumber;
+        CFX_ByteString word = parser.GetNextWord(bNumber);
+        if (bNumber) {
+            FX_DWORD objnum = FXSYS_atoi(word);
+            word = parser.GetNextWord(bNumber);
+            if (!bNumber) {
+                break;
+            }
+            word = parser.GetNextWord(bNumber);
+            if (word != FX_BSTRC("obj")) {
+                break;
+            }
+            CPDF_Object* pObj = parser.GetObject(this, objnum, 0, 0);
+            if (pObj == NULL) {
+                break;
+            }
+            InsertIndirectObject(objnum, pObj);
+            word = parser.GetNextWord(bNumber);
+            if (word != FX_BSTRC("endobj")) {
+                break;
+            }
+        } else {
+            if (word != FX_BSTRC("trailer")) {
+                break;
+            }
+            CPDF_Dictionary* pMainDict = (CPDF_Dictionary*)parser.GetObject(this, 0, 0, 0);
+            if (pMainDict == NULL || pMainDict->GetType() != PDFOBJ_DICTIONARY) {
+                break;
+            }
+            m_pRootDict = pMainDict->GetDict(FX_BSTRC("Root"));
+            pMainDict->Release();
+            break;
+        }
+    }
+}
+FX_BOOL CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const
+{
+    if (m_pRootDict == NULL) {
+        return FALSE;
+    }
+    buf << FX_BSTRC("%FDF-1.2\r\n");
+    FX_POSITION pos = m_IndirectObjs.GetStartPosition();
+    while(pos) {
+        size_t objnum;
+        CPDF_Object* pObj;
+        m_IndirectObjs.GetNextAssoc(pos, (void*&)objnum, (void*&)pObj);
+        buf << (FX_DWORD)objnum << FX_BSTRC(" 0 obj\r\n") << pObj << FX_BSTRC("\r\nendobj\r\n\r\n");
+    }
+    buf << FX_BSTRC("trailer\r\n<</Root ") << m_pRootDict->GetObjNum() << FX_BSTRC(" 0 R>>\r\n%%EOF\r\n");
+    return TRUE;
+}
+CFX_WideString CFDF_Document::GetWin32Path() const
+{
+    CPDF_Dictionary* pDict = m_pRootDict ? m_pRootDict->GetDict(FX_BSTRC("FDF")) : NULL;
+    CPDF_Object* pFileSpec = pDict ? pDict->GetElementValue(FX_BSTRC("F")) : NULL;
+    if (pFileSpec == NULL) {
+        return CFX_WideString();
+    }
+    if (pFileSpec->GetType() == PDFOBJ_STRING) {
+        return FPDF_FileSpec_GetWin32Path(m_pRootDict->GetDict(FX_BSTRC("FDF")));
+    }
+    return FPDF_FileSpec_GetWin32Path(pFileSpec);
+}
+static CFX_WideString ChangeSlash(const FX_WCHAR* str)
+{
+    CFX_WideString result;
+    while (*str) {
+        if (*str == '\\') {
+            result += '/';
+        } else if (*str == '/') {
+            result += '\\';
+        } else {
+            result += *str;
+        }
+        str ++;
+    }
+    return result;
+}
+void FPDF_FileSpec_SetWin32Path(CPDF_Object* pFileSpec, const CFX_WideString& filepath)
+{
+    CFX_WideString result;
+    if (filepath.GetLength() > 1 && filepath[1] == ':') {
+        result = L"/";
+        result += filepath[0];
+        if (filepath[2] != '\\') {
+            result += '/';
+        }
+        result += ChangeSlash(filepath.c_str() + 2);
+    } else if (filepath.GetLength() > 1 && filepath[0] == '\\' && filepath[1] == '\\') {
+        result = ChangeSlash(filepath.c_str() + 1);
+    } else {
+        result = ChangeSlash(filepath.c_str());
+    }
+    if (pFileSpec->GetType() == PDFOBJ_STRING) {
+        pFileSpec->SetString(CFX_ByteString::FromUnicode(result));
+    } else if (pFileSpec->GetType() == PDFOBJ_DICTIONARY) {
+        ((CPDF_Dictionary*)pFileSpec)->SetAtString(FX_BSTRC("F"), CFX_ByteString::FromUnicode(result));
+        ((CPDF_Dictionary*)pFileSpec)->SetAtString(FX_BSTRC("UF"), PDF_EncodeText(result));
+        ((CPDF_Dictionary*)pFileSpec)->RemoveAt(FX_BSTRC("FS"));
+    }
+}
+CFX_WideString FPDF_FileSpec_GetWin32Path(const CPDF_Object* pFileSpec)
+{
+    CFX_WideString wsFileName;
+    if (pFileSpec->GetType() == PDFOBJ_DICTIONARY) {
+        CPDF_Dictionary* pDict = (CPDF_Dictionary*)pFileSpec;
+        wsFileName = pDict->GetUnicodeText(FX_BSTRC("UF"));
+        if (wsFileName.IsEmpty()) {
+            wsFileName = CFX_WideString::FromLocal(pDict->GetString(FX_BSTRC("F")));
+        }
+        if (pDict->GetString(FX_BSTRC("FS")) == FX_BSTRC("URL")) {
+            return wsFileName;
+        }
+        if (wsFileName.IsEmpty() && pDict->KeyExist(FX_BSTRC("DOS"))) {
+            wsFileName = CFX_WideString::FromLocal(pDict->GetString(FX_BSTRC("DOS")));
+        }
+    }
+    else if (!pFileSpec)
+        wsFileName = CFX_WideString();
+    else {
+        wsFileName = CFX_WideString::FromLocal(pFileSpec->GetString());
+    }
+    if (wsFileName[0] != '/') {
+        return ChangeSlash(wsFileName.c_str());
+    }
+    if (wsFileName[2] == '/') {
+        CFX_WideString result;
+        result += wsFileName[1];
+        result += ':';
+        result += ChangeSlash(wsFileName.c_str() + 2);
+        return result;
+    }
+    CFX_WideString result;
+    result += '\\';
+    result += ChangeSlash(wsFileName.c_str());
+    return result;
+}