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_serial.h" | 7 #include "../../../include/fpdfapi/fpdf_serial.h" |
8 CFDF_Document::CFDF_Document() : CPDF_IndirectObjects(NULL) | 8 CFDF_Document::CFDF_Document() : CPDF_IndirectObjects(NULL) { |
9 { | 9 m_pRootDict = NULL; |
10 m_pRootDict = NULL; | 10 m_pFile = NULL; |
11 m_pFile = NULL; | 11 m_bOwnFile = FALSE; |
12 m_bOwnFile = FALSE; | |
13 } | 12 } |
14 CFDF_Document::~CFDF_Document() | 13 CFDF_Document::~CFDF_Document() { |
15 { | 14 if (m_bOwnFile && m_pFile) { |
16 if (m_bOwnFile && m_pFile) { | 15 m_pFile->Release(); |
17 m_pFile->Release(); | 16 } |
18 } | |
19 } | 17 } |
20 CFDF_Document* CFDF_Document::CreateNewDoc() | 18 CFDF_Document* CFDF_Document::CreateNewDoc() { |
21 { | 19 CFDF_Document* pDoc = new CFDF_Document; |
22 CFDF_Document* pDoc = new CFDF_Document; | 20 pDoc->m_pRootDict = new CPDF_Dictionary; |
23 pDoc->m_pRootDict = new CPDF_Dictionary; | 21 pDoc->AddIndirectObject(pDoc->m_pRootDict); |
24 pDoc->AddIndirectObject(pDoc->m_pRootDict); | 22 CPDF_Dictionary* pFDFDict = new CPDF_Dictionary; |
25 CPDF_Dictionary* pFDFDict = new CPDF_Dictionary; | 23 pDoc->m_pRootDict->SetAt(FX_BSTRC("FDF"), pFDFDict); |
26 pDoc->m_pRootDict->SetAt(FX_BSTRC("FDF"), pFDFDict); | 24 return pDoc; |
27 return pDoc; | |
28 } | 25 } |
29 CFDF_Document* CFDF_Document::ParseFile(IFX_FileRead *pFile, FX_BOOL bOwnFile) | 26 CFDF_Document* CFDF_Document::ParseFile(IFX_FileRead* pFile, FX_BOOL bOwnFile) { |
30 { | |
31 if (!pFile) { | 27 if (!pFile) { |
32 return NULL; | 28 return NULL; |
33 } | 29 } |
34 CFDF_Document* pDoc = new CFDF_Document; | 30 CFDF_Document* pDoc = new CFDF_Document; |
35 pDoc->ParseStream(pFile, bOwnFile); | 31 pDoc->ParseStream(pFile, bOwnFile); |
36 if (pDoc->m_pRootDict == NULL) { | 32 if (pDoc->m_pRootDict == NULL) { |
37 delete pDoc; | 33 delete pDoc; |
38 return NULL; | 34 return NULL; |
39 } | 35 } |
40 return pDoc; | 36 return pDoc; |
41 } | 37 } |
42 CFDF_Document* CFDF_Document::ParseMemory(const uint8_t* pData, FX_DWORD size) | 38 CFDF_Document* CFDF_Document::ParseMemory(const uint8_t* pData, FX_DWORD size) { |
43 { | 39 return CFDF_Document::ParseFile(FX_CreateMemoryStream((uint8_t*)pData, size), |
44 return CFDF_Document::ParseFile(FX_CreateMemoryStream((uint8_t*)pData, size)
, TRUE); | 40 TRUE); |
45 } | 41 } |
46 void CFDF_Document::ParseStream(IFX_FileRead *pFile, FX_BOOL bOwnFile) | 42 void CFDF_Document::ParseStream(IFX_FileRead* pFile, FX_BOOL bOwnFile) { |
47 { | 43 m_pFile = pFile; |
48 m_pFile = pFile; | 44 m_bOwnFile = bOwnFile; |
49 m_bOwnFile = bOwnFile; | 45 CPDF_SyntaxParser parser; |
50 CPDF_SyntaxParser parser; | 46 parser.InitParser(m_pFile, 0); |
51 parser.InitParser(m_pFile, 0); | 47 while (1) { |
52 while (1) { | 48 FX_BOOL bNumber; |
53 FX_BOOL bNumber; | 49 CFX_ByteString word = parser.GetNextWord(bNumber); |
54 CFX_ByteString word = parser.GetNextWord(bNumber); | 50 if (bNumber) { |
55 if (bNumber) { | 51 FX_DWORD objnum = FXSYS_atoi(word); |
56 FX_DWORD objnum = FXSYS_atoi(word); | 52 word = parser.GetNextWord(bNumber); |
57 word = parser.GetNextWord(bNumber); | 53 if (!bNumber) { |
58 if (!bNumber) { | 54 break; |
59 break; | 55 } |
60 } | 56 word = parser.GetNextWord(bNumber); |
61 word = parser.GetNextWord(bNumber); | 57 if (word != FX_BSTRC("obj")) { |
62 if (word != FX_BSTRC("obj")) { | 58 break; |
63 break; | 59 } |
64 } | 60 CPDF_Object* pObj = parser.GetObject(this, objnum, 0, 0); |
65 CPDF_Object* pObj = parser.GetObject(this, objnum, 0, 0); | 61 if (pObj == NULL) { |
66 if (pObj == NULL) { | 62 break; |
67 break; | 63 } |
68 } | 64 InsertIndirectObject(objnum, pObj); |
69 InsertIndirectObject(objnum, pObj); | 65 word = parser.GetNextWord(bNumber); |
70 word = parser.GetNextWord(bNumber); | 66 if (word != FX_BSTRC("endobj")) { |
71 if (word != FX_BSTRC("endobj")) { | 67 break; |
72 break; | 68 } |
73 } | 69 } else { |
74 } else { | 70 if (word != FX_BSTRC("trailer")) { |
75 if (word != FX_BSTRC("trailer")) { | 71 break; |
76 break; | 72 } |
77 } | 73 CPDF_Dictionary* pMainDict = |
78 CPDF_Dictionary* pMainDict = (CPDF_Dictionary*)parser.GetObject(this
, 0, 0, 0); | 74 (CPDF_Dictionary*)parser.GetObject(this, 0, 0, 0); |
79 if (pMainDict == NULL || pMainDict->GetType() != PDFOBJ_DICTIONARY)
{ | 75 if (pMainDict == NULL || pMainDict->GetType() != PDFOBJ_DICTIONARY) { |
80 break; | 76 break; |
81 } | 77 } |
82 m_pRootDict = pMainDict->GetDict(FX_BSTRC("Root")); | 78 m_pRootDict = pMainDict->GetDict(FX_BSTRC("Root")); |
83 pMainDict->Release(); | 79 pMainDict->Release(); |
84 break; | 80 break; |
85 } | |
86 } | 81 } |
| 82 } |
87 } | 83 } |
88 FX_BOOL CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const | 84 FX_BOOL CFDF_Document::WriteBuf(CFX_ByteTextBuf& buf) const { |
89 { | 85 if (m_pRootDict == NULL) { |
90 if (m_pRootDict == NULL) { | 86 return FALSE; |
91 return FALSE; | 87 } |
| 88 buf << FX_BSTRC("%FDF-1.2\r\n"); |
| 89 FX_POSITION pos = m_IndirectObjs.GetStartPosition(); |
| 90 while (pos) { |
| 91 size_t objnum; |
| 92 CPDF_Object* pObj; |
| 93 m_IndirectObjs.GetNextAssoc(pos, (void*&)objnum, (void*&)pObj); |
| 94 buf << (FX_DWORD)objnum << FX_BSTRC(" 0 obj\r\n") << pObj |
| 95 << FX_BSTRC("\r\nendobj\r\n\r\n"); |
| 96 } |
| 97 buf << FX_BSTRC("trailer\r\n<</Root ") << m_pRootDict->GetObjNum() |
| 98 << FX_BSTRC(" 0 R>>\r\n%%EOF\r\n"); |
| 99 return TRUE; |
| 100 } |
| 101 CFX_WideString CFDF_Document::GetWin32Path() const { |
| 102 CPDF_Dictionary* pDict = |
| 103 m_pRootDict ? m_pRootDict->GetDict(FX_BSTRC("FDF")) : NULL; |
| 104 CPDF_Object* pFileSpec = pDict ? pDict->GetElementValue(FX_BSTRC("F")) : NULL; |
| 105 if (pFileSpec == NULL) { |
| 106 return CFX_WideString(); |
| 107 } |
| 108 if (pFileSpec->GetType() == PDFOBJ_STRING) { |
| 109 return FPDF_FileSpec_GetWin32Path(m_pRootDict->GetDict(FX_BSTRC("FDF"))); |
| 110 } |
| 111 return FPDF_FileSpec_GetWin32Path(pFileSpec); |
| 112 } |
| 113 static CFX_WideString ChangeSlash(const FX_WCHAR* str) { |
| 114 CFX_WideString result; |
| 115 while (*str) { |
| 116 if (*str == '\\') { |
| 117 result += '/'; |
| 118 } else if (*str == '/') { |
| 119 result += '\\'; |
| 120 } else { |
| 121 result += *str; |
92 } | 122 } |
93 buf << FX_BSTRC("%FDF-1.2\r\n"); | 123 str++; |
94 FX_POSITION pos = m_IndirectObjs.GetStartPosition(); | 124 } |
95 while(pos) { | 125 return result; |
96 size_t objnum; | 126 } |
97 CPDF_Object* pObj; | 127 void FPDF_FileSpec_SetWin32Path(CPDF_Object* pFileSpec, |
98 m_IndirectObjs.GetNextAssoc(pos, (void*&)objnum, (void*&)pObj); | 128 const CFX_WideString& filepath) { |
99 buf << (FX_DWORD)objnum << FX_BSTRC(" 0 obj\r\n") << pObj << FX_BSTRC("\
r\nendobj\r\n\r\n"); | 129 CFX_WideString result; |
| 130 if (filepath.GetLength() > 1 && filepath[1] == ':') { |
| 131 result = L"/"; |
| 132 result += filepath[0]; |
| 133 if (filepath[2] != '\\') { |
| 134 result += '/'; |
100 } | 135 } |
101 buf << FX_BSTRC("trailer\r\n<</Root ") << m_pRootDict->GetObjNum() << FX_BST
RC(" 0 R>>\r\n%%EOF\r\n"); | 136 result += ChangeSlash(filepath.c_str() + 2); |
102 return TRUE; | 137 } else if (filepath.GetLength() > 1 && filepath[0] == '\\' && |
| 138 filepath[1] == '\\') { |
| 139 result = ChangeSlash(filepath.c_str() + 1); |
| 140 } else { |
| 141 result = ChangeSlash(filepath.c_str()); |
| 142 } |
| 143 if (pFileSpec->GetType() == PDFOBJ_STRING) { |
| 144 pFileSpec->SetString(CFX_ByteString::FromUnicode(result)); |
| 145 } else if (pFileSpec->GetType() == PDFOBJ_DICTIONARY) { |
| 146 ((CPDF_Dictionary*)pFileSpec) |
| 147 ->SetAtString(FX_BSTRC("F"), CFX_ByteString::FromUnicode(result)); |
| 148 ((CPDF_Dictionary*)pFileSpec) |
| 149 ->SetAtString(FX_BSTRC("UF"), PDF_EncodeText(result)); |
| 150 ((CPDF_Dictionary*)pFileSpec)->RemoveAt(FX_BSTRC("FS")); |
| 151 } |
103 } | 152 } |
104 CFX_WideString CFDF_Document::GetWin32Path() const | 153 CFX_WideString FPDF_FileSpec_GetWin32Path(const CPDF_Object* pFileSpec) { |
105 { | 154 CFX_WideString wsFileName; |
106 CPDF_Dictionary* pDict = m_pRootDict ? m_pRootDict->GetDict(FX_BSTRC("FDF"))
: NULL; | 155 if (pFileSpec->GetType() == PDFOBJ_DICTIONARY) { |
107 CPDF_Object* pFileSpec = pDict ? pDict->GetElementValue(FX_BSTRC("F")) : NUL
L; | 156 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pFileSpec; |
108 if (pFileSpec == NULL) { | 157 wsFileName = pDict->GetUnicodeText(FX_BSTRC("UF")); |
109 return CFX_WideString(); | 158 if (wsFileName.IsEmpty()) { |
| 159 wsFileName = CFX_WideString::FromLocal(pDict->GetString(FX_BSTRC("F"))); |
110 } | 160 } |
111 if (pFileSpec->GetType() == PDFOBJ_STRING) { | 161 if (pDict->GetString(FX_BSTRC("FS")) == FX_BSTRC("URL")) { |
112 return FPDF_FileSpec_GetWin32Path(m_pRootDict->GetDict(FX_BSTRC("FDF")))
; | 162 return wsFileName; |
113 } | 163 } |
114 return FPDF_FileSpec_GetWin32Path(pFileSpec); | 164 if (wsFileName.IsEmpty() && pDict->KeyExist(FX_BSTRC("DOS"))) { |
| 165 wsFileName = CFX_WideString::FromLocal(pDict->GetString(FX_BSTRC("DOS"))); |
| 166 } |
| 167 } else if (!pFileSpec) |
| 168 wsFileName = CFX_WideString(); |
| 169 else { |
| 170 wsFileName = CFX_WideString::FromLocal(pFileSpec->GetString()); |
| 171 } |
| 172 if (wsFileName[0] != '/') { |
| 173 return ChangeSlash(wsFileName.c_str()); |
| 174 } |
| 175 if (wsFileName[2] == '/') { |
| 176 CFX_WideString result; |
| 177 result += wsFileName[1]; |
| 178 result += ':'; |
| 179 result += ChangeSlash(wsFileName.c_str() + 2); |
| 180 return result; |
| 181 } |
| 182 CFX_WideString result; |
| 183 result += '\\'; |
| 184 result += ChangeSlash(wsFileName.c_str()); |
| 185 return result; |
115 } | 186 } |
116 static CFX_WideString ChangeSlash(const FX_WCHAR* str) | |
117 { | |
118 CFX_WideString result; | |
119 while (*str) { | |
120 if (*str == '\\') { | |
121 result += '/'; | |
122 } else if (*str == '/') { | |
123 result += '\\'; | |
124 } else { | |
125 result += *str; | |
126 } | |
127 str ++; | |
128 } | |
129 return result; | |
130 } | |
131 void FPDF_FileSpec_SetWin32Path(CPDF_Object* pFileSpec, const CFX_WideString& fi
lepath) | |
132 { | |
133 CFX_WideString result; | |
134 if (filepath.GetLength() > 1 && filepath[1] == ':') { | |
135 result = L"/"; | |
136 result += filepath[0]; | |
137 if (filepath[2] != '\\') { | |
138 result += '/'; | |
139 } | |
140 result += ChangeSlash(filepath.c_str() + 2); | |
141 } else if (filepath.GetLength() > 1 && filepath[0] == '\\' && filepath[1] ==
'\\') { | |
142 result = ChangeSlash(filepath.c_str() + 1); | |
143 } else { | |
144 result = ChangeSlash(filepath.c_str()); | |
145 } | |
146 if (pFileSpec->GetType() == PDFOBJ_STRING) { | |
147 pFileSpec->SetString(CFX_ByteString::FromUnicode(result)); | |
148 } else if (pFileSpec->GetType() == PDFOBJ_DICTIONARY) { | |
149 ((CPDF_Dictionary*)pFileSpec)->SetAtString(FX_BSTRC("F"), CFX_ByteString
::FromUnicode(result)); | |
150 ((CPDF_Dictionary*)pFileSpec)->SetAtString(FX_BSTRC("UF"), PDF_EncodeTex
t(result)); | |
151 ((CPDF_Dictionary*)pFileSpec)->RemoveAt(FX_BSTRC("FS")); | |
152 } | |
153 } | |
154 CFX_WideString FPDF_FileSpec_GetWin32Path(const CPDF_Object* pFileSpec) | |
155 { | |
156 CFX_WideString wsFileName; | |
157 if (pFileSpec->GetType() == PDFOBJ_DICTIONARY) { | |
158 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pFileSpec; | |
159 wsFileName = pDict->GetUnicodeText(FX_BSTRC("UF")); | |
160 if (wsFileName.IsEmpty()) { | |
161 wsFileName = CFX_WideString::FromLocal(pDict->GetString(FX_BSTRC("F"
))); | |
162 } | |
163 if (pDict->GetString(FX_BSTRC("FS")) == FX_BSTRC("URL")) { | |
164 return wsFileName; | |
165 } | |
166 if (wsFileName.IsEmpty() && pDict->KeyExist(FX_BSTRC("DOS"))) { | |
167 wsFileName = CFX_WideString::FromLocal(pDict->GetString(FX_BSTRC("DO
S"))); | |
168 } | |
169 } | |
170 else if (!pFileSpec) | |
171 wsFileName = CFX_WideString(); | |
172 else { | |
173 wsFileName = CFX_WideString::FromLocal(pFileSpec->GetString()); | |
174 } | |
175 if (wsFileName[0] != '/') { | |
176 return ChangeSlash(wsFileName.c_str()); | |
177 } | |
178 if (wsFileName[2] == '/') { | |
179 CFX_WideString result; | |
180 result += wsFileName[1]; | |
181 result += ':'; | |
182 result += ChangeSlash(wsFileName.c_str() + 2); | |
183 return result; | |
184 } | |
185 CFX_WideString result; | |
186 result += '\\'; | |
187 result += ChangeSlash(wsFileName.c_str()); | |
188 return result; | |
189 } | |
OLD | NEW |