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 "xfa/fxfa/xfa_checksum.h" | 7 #include "xfa/fxfa/xfa_checksum.h" |
8 | 8 |
9 #include "core/fdrm/crypto/fx_crypt.h" | 9 #include "core/fdrm/crypto/fx_crypt.h" |
10 | 10 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 93 |
94 CXFA_SAXReaderHandler::CXFA_SAXReaderHandler(CXFA_ChecksumContext* pContext) | 94 CXFA_SAXReaderHandler::CXFA_SAXReaderHandler(CXFA_ChecksumContext* pContext) |
95 : m_pContext(pContext) { | 95 : m_pContext(pContext) { |
96 ASSERT(m_pContext); | 96 ASSERT(m_pContext); |
97 } | 97 } |
98 CXFA_SAXReaderHandler::~CXFA_SAXReaderHandler() {} | 98 CXFA_SAXReaderHandler::~CXFA_SAXReaderHandler() {} |
99 CXFA_SAXContext* CXFA_SAXReaderHandler::OnTagEnter( | 99 CXFA_SAXContext* CXFA_SAXReaderHandler::OnTagEnter( |
100 const CFX_ByteStringC& bsTagName, | 100 const CFX_ByteStringC& bsTagName, |
101 CFX_SAXItem::Type eType, | 101 CFX_SAXItem::Type eType, |
102 uint32_t dwStartPos) { | 102 uint32_t dwStartPos) { |
103 UpdateChecksum(TRUE); | 103 UpdateChecksum(true); |
104 if (eType != CFX_SAXItem::Type::Tag && | 104 if (eType != CFX_SAXItem::Type::Tag && |
105 eType != CFX_SAXItem::Type::Instruction) { | 105 eType != CFX_SAXItem::Type::Instruction) { |
106 return nullptr; | 106 return nullptr; |
107 } | 107 } |
108 m_SAXContext.m_eNode = eType; | 108 m_SAXContext.m_eNode = eType; |
109 CFX_ByteTextBuf& textBuf = m_SAXContext.m_TextBuf; | 109 CFX_ByteTextBuf& textBuf = m_SAXContext.m_TextBuf; |
110 textBuf << "<"; | 110 textBuf << "<"; |
111 if (eType == CFX_SAXItem::Type::Instruction) { | 111 if (eType == CFX_SAXItem::Type::Instruction) { |
112 textBuf << "?"; | 112 textBuf << "?"; |
113 } | 113 } |
114 textBuf << bsTagName; | 114 textBuf << bsTagName; |
115 m_SAXContext.m_bsTagName = bsTagName; | 115 m_SAXContext.m_bsTagName = bsTagName; |
116 return &m_SAXContext; | 116 return &m_SAXContext; |
117 } | 117 } |
118 | 118 |
119 void CXFA_SAXReaderHandler::OnTagAttribute(CXFA_SAXContext* pTag, | 119 void CXFA_SAXReaderHandler::OnTagAttribute(CXFA_SAXContext* pTag, |
120 const CFX_ByteStringC& bsAttri, | 120 const CFX_ByteStringC& bsAttri, |
121 const CFX_ByteStringC& bsValue) { | 121 const CFX_ByteStringC& bsValue) { |
122 if (!pTag) | 122 if (!pTag) |
123 return; | 123 return; |
124 | 124 |
125 pTag->m_TextBuf << " " << bsAttri << "=\"" << bsValue << "\""; | 125 pTag->m_TextBuf << " " << bsAttri << "=\"" << bsValue << "\""; |
126 } | 126 } |
127 | 127 |
128 void CXFA_SAXReaderHandler::OnTagBreak(CXFA_SAXContext* pTag) { | 128 void CXFA_SAXReaderHandler::OnTagBreak(CXFA_SAXContext* pTag) { |
129 if (!pTag) | 129 if (!pTag) |
130 return; | 130 return; |
131 | 131 |
132 pTag->m_TextBuf << ">"; | 132 pTag->m_TextBuf << ">"; |
133 UpdateChecksum(FALSE); | 133 UpdateChecksum(false); |
134 } | 134 } |
135 | 135 |
136 void CXFA_SAXReaderHandler::OnTagData(CXFA_SAXContext* pTag, | 136 void CXFA_SAXReaderHandler::OnTagData(CXFA_SAXContext* pTag, |
137 CFX_SAXItem::Type eType, | 137 CFX_SAXItem::Type eType, |
138 const CFX_ByteStringC& bsData, | 138 const CFX_ByteStringC& bsData, |
139 uint32_t dwStartPos) { | 139 uint32_t dwStartPos) { |
140 if (!pTag) | 140 if (!pTag) |
141 return; | 141 return; |
142 | 142 |
143 CFX_ByteTextBuf& textBuf = pTag->m_TextBuf; | 143 CFX_ByteTextBuf& textBuf = pTag->m_TextBuf; |
144 if (eType == CFX_SAXItem::Type::CharData) | 144 if (eType == CFX_SAXItem::Type::CharData) |
145 textBuf << "<![CDATA["; | 145 textBuf << "<![CDATA["; |
146 | 146 |
147 textBuf << bsData; | 147 textBuf << bsData; |
148 if (eType == CFX_SAXItem::Type::CharData) | 148 if (eType == CFX_SAXItem::Type::CharData) |
149 textBuf << "]]>"; | 149 textBuf << "]]>"; |
150 } | 150 } |
151 | 151 |
152 void CXFA_SAXReaderHandler::OnTagClose(CXFA_SAXContext* pTag, | 152 void CXFA_SAXReaderHandler::OnTagClose(CXFA_SAXContext* pTag, |
153 uint32_t dwEndPos) { | 153 uint32_t dwEndPos) { |
154 if (!pTag) | 154 if (!pTag) |
155 return; | 155 return; |
156 | 156 |
157 CFX_ByteTextBuf& textBuf = pTag->m_TextBuf; | 157 CFX_ByteTextBuf& textBuf = pTag->m_TextBuf; |
158 if (pTag->m_eNode == CFX_SAXItem::Type::Instruction) | 158 if (pTag->m_eNode == CFX_SAXItem::Type::Instruction) |
159 textBuf << "?>"; | 159 textBuf << "?>"; |
160 else if (pTag->m_eNode == CFX_SAXItem::Type::Tag) | 160 else if (pTag->m_eNode == CFX_SAXItem::Type::Tag) |
161 textBuf << "></" << pTag->m_bsTagName.AsStringC() << ">"; | 161 textBuf << "></" << pTag->m_bsTagName.AsStringC() << ">"; |
162 | 162 |
163 UpdateChecksum(FALSE); | 163 UpdateChecksum(false); |
164 } | 164 } |
165 | 165 |
166 void CXFA_SAXReaderHandler::OnTagEnd(CXFA_SAXContext* pTag, | 166 void CXFA_SAXReaderHandler::OnTagEnd(CXFA_SAXContext* pTag, |
167 const CFX_ByteStringC& bsTagName, | 167 const CFX_ByteStringC& bsTagName, |
168 uint32_t dwEndPos) { | 168 uint32_t dwEndPos) { |
169 if (!pTag) | 169 if (!pTag) |
170 return; | 170 return; |
171 | 171 |
172 pTag->m_TextBuf << "</" << bsTagName << ">"; | 172 pTag->m_TextBuf << "</" << bsTagName << ">"; |
173 UpdateChecksum(FALSE); | 173 UpdateChecksum(false); |
174 } | 174 } |
175 | 175 |
176 void CXFA_SAXReaderHandler::OnTargetData(CXFA_SAXContext* pTag, | 176 void CXFA_SAXReaderHandler::OnTargetData(CXFA_SAXContext* pTag, |
177 CFX_SAXItem::Type eType, | 177 CFX_SAXItem::Type eType, |
178 const CFX_ByteStringC& bsData, | 178 const CFX_ByteStringC& bsData, |
179 uint32_t dwStartPos) { | 179 uint32_t dwStartPos) { |
180 if (!pTag && eType != CFX_SAXItem::Type::Comment) | 180 if (!pTag && eType != CFX_SAXItem::Type::Comment) |
181 return; | 181 return; |
182 | 182 |
183 if (eType == CFX_SAXItem::Type::Comment) { | 183 if (eType == CFX_SAXItem::Type::Comment) { |
184 m_SAXContext.m_TextBuf << "<!--" << bsData << "-->"; | 184 m_SAXContext.m_TextBuf << "<!--" << bsData << "-->"; |
185 UpdateChecksum(FALSE); | 185 UpdateChecksum(false); |
186 } else { | 186 } else { |
187 pTag->m_TextBuf << " " << bsData; | 187 pTag->m_TextBuf << " " << bsData; |
188 } | 188 } |
189 } | 189 } |
190 | 190 |
191 void CXFA_SAXReaderHandler::UpdateChecksum(FX_BOOL bCheckSpace) { | 191 void CXFA_SAXReaderHandler::UpdateChecksum(bool bCheckSpace) { |
192 int32_t iLength = m_SAXContext.m_TextBuf.GetLength(); | 192 int32_t iLength = m_SAXContext.m_TextBuf.GetLength(); |
193 if (iLength < 1) { | 193 if (iLength < 1) { |
194 return; | 194 return; |
195 } | 195 } |
196 uint8_t* pBuffer = m_SAXContext.m_TextBuf.GetBuffer(); | 196 uint8_t* pBuffer = m_SAXContext.m_TextBuf.GetBuffer(); |
197 FX_BOOL bUpdata = TRUE; | 197 bool bUpdata = true; |
198 if (bCheckSpace) { | 198 if (bCheckSpace) { |
199 bUpdata = FALSE; | 199 bUpdata = false; |
200 for (int32_t i = 0; i < iLength; i++) { | 200 for (int32_t i = 0; i < iLength; i++) { |
201 bUpdata = (pBuffer[i] > 0x20); | 201 bUpdata = (pBuffer[i] > 0x20); |
202 if (bUpdata) { | 202 if (bUpdata) { |
203 break; | 203 break; |
204 } | 204 } |
205 } | 205 } |
206 } | 206 } |
207 if (bUpdata) { | 207 if (bUpdata) { |
208 m_pContext->Update(CFX_ByteStringC(pBuffer, iLength)); | 208 m_pContext->Update(CFX_ByteStringC(pBuffer, iLength)); |
209 } | 209 } |
210 m_SAXContext.m_TextBuf.Clear(); | 210 m_SAXContext.m_TextBuf.Clear(); |
211 } | 211 } |
212 | 212 |
213 CXFA_ChecksumContext::CXFA_ChecksumContext() | 213 CXFA_ChecksumContext::CXFA_ChecksumContext() |
214 : m_pSAXReader(nullptr), m_pByteContext(nullptr) {} | 214 : m_pSAXReader(nullptr), m_pByteContext(nullptr) {} |
215 | 215 |
216 CXFA_ChecksumContext::~CXFA_ChecksumContext() { | 216 CXFA_ChecksumContext::~CXFA_ChecksumContext() { |
217 FinishChecksum(); | 217 FinishChecksum(); |
218 } | 218 } |
219 | 219 |
220 void CXFA_ChecksumContext::StartChecksum() { | 220 void CXFA_ChecksumContext::StartChecksum() { |
221 FinishChecksum(); | 221 FinishChecksum(); |
222 m_pByteContext = FX_Alloc(uint8_t, 128); | 222 m_pByteContext = FX_Alloc(uint8_t, 128); |
223 CRYPT_SHA1Start(m_pByteContext); | 223 CRYPT_SHA1Start(m_pByteContext); |
224 m_bsChecksum.clear(); | 224 m_bsChecksum.clear(); |
225 m_pSAXReader = new CFX_SAXReader; | 225 m_pSAXReader = new CFX_SAXReader; |
226 } | 226 } |
227 | 227 |
228 FX_BOOL CXFA_ChecksumContext::UpdateChecksum(IFX_SeekableReadStream* pSrcFile, | 228 bool CXFA_ChecksumContext::UpdateChecksum(IFX_SeekableReadStream* pSrcFile, |
229 FX_FILESIZE offset, | 229 FX_FILESIZE offset, |
230 size_t size) { | 230 size_t size) { |
231 if (!m_pSAXReader || !pSrcFile) | 231 if (!m_pSAXReader || !pSrcFile) |
232 return FALSE; | 232 return false; |
233 if (size < 1) | 233 if (size < 1) |
234 size = pSrcFile->GetSize(); | 234 size = pSrcFile->GetSize(); |
235 | 235 |
236 CXFA_SAXReaderHandler handler(this); | 236 CXFA_SAXReaderHandler handler(this); |
237 m_pSAXReader->SetHandler(&handler); | 237 m_pSAXReader->SetHandler(&handler); |
238 if (m_pSAXReader->StartParse( | 238 if (m_pSAXReader->StartParse( |
239 pSrcFile, (uint32_t)offset, (uint32_t)size, | 239 pSrcFile, (uint32_t)offset, (uint32_t)size, |
240 CFX_SaxParseMode_NotSkipSpace | CFX_SaxParseMode_NotConvert_amp | | 240 CFX_SaxParseMode_NotSkipSpace | CFX_SaxParseMode_NotConvert_amp | |
241 CFX_SaxParseMode_NotConvert_lt | CFX_SaxParseMode_NotConvert_gt | | 241 CFX_SaxParseMode_NotConvert_lt | CFX_SaxParseMode_NotConvert_gt | |
242 CFX_SaxParseMode_NotConvert_sharp) < 0) { | 242 CFX_SaxParseMode_NotConvert_sharp) < 0) { |
243 return FALSE; | 243 return false; |
244 } | 244 } |
245 return m_pSAXReader->ContinueParse(nullptr) > 99; | 245 return m_pSAXReader->ContinueParse(nullptr) > 99; |
246 } | 246 } |
247 | 247 |
248 void CXFA_ChecksumContext::FinishChecksum() { | 248 void CXFA_ChecksumContext::FinishChecksum() { |
249 delete m_pSAXReader; | 249 delete m_pSAXReader; |
250 m_pSAXReader = nullptr; | 250 m_pSAXReader = nullptr; |
251 if (m_pByteContext) { | 251 if (m_pByteContext) { |
252 uint8_t digest[20]; | 252 uint8_t digest[20]; |
253 FXSYS_memset(digest, 0, 20); | 253 FXSYS_memset(digest, 0, 20); |
254 CRYPT_SHA1Finish(m_pByteContext, digest); | 254 CRYPT_SHA1Finish(m_pByteContext, digest); |
255 int32_t nLen = Base64EncodeA(digest, 20, nullptr); | 255 int32_t nLen = Base64EncodeA(digest, 20, nullptr); |
256 FX_CHAR* pBuffer = m_bsChecksum.GetBuffer(nLen); | 256 FX_CHAR* pBuffer = m_bsChecksum.GetBuffer(nLen); |
257 Base64EncodeA(digest, 20, pBuffer); | 257 Base64EncodeA(digest, 20, pBuffer); |
258 m_bsChecksum.ReleaseBuffer(nLen); | 258 m_bsChecksum.ReleaseBuffer(nLen); |
259 FX_Free(m_pByteContext); | 259 FX_Free(m_pByteContext); |
260 m_pByteContext = nullptr; | 260 m_pByteContext = nullptr; |
261 } | 261 } |
262 } | 262 } |
263 | 263 |
264 CFX_ByteString CXFA_ChecksumContext::GetChecksum() const { | 264 CFX_ByteString CXFA_ChecksumContext::GetChecksum() const { |
265 return m_bsChecksum; | 265 return m_bsChecksum; |
266 } | 266 } |
267 | 267 |
268 void CXFA_ChecksumContext::Update(const CFX_ByteStringC& bsText) { | 268 void CXFA_ChecksumContext::Update(const CFX_ByteStringC& bsText) { |
269 if (m_pByteContext) { | 269 if (m_pByteContext) { |
270 CRYPT_SHA1Update(m_pByteContext, bsText.raw_str(), bsText.GetLength()); | 270 CRYPT_SHA1Update(m_pByteContext, bsText.raw_str(), bsText.GetLength()); |
271 } | 271 } |
272 } | 272 } |
OLD | NEW |