| 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/include/xfa_checksum.h" | 7 #include "xfa/fxfa/include/xfa_checksum.h" |
| 8 | 8 |
| 9 #include "core/fdrm/crypto/include/fx_crypt.h" | 9 #include "core/fdrm/crypto/include/fx_crypt.h" |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 dst[3] = g_FXBase64EncoderMap[src.data5]; | 47 dst[3] = g_FXBase64EncoderMap[src.data5]; |
| 48 } else { | 48 } else { |
| 49 dst[3] = '='; | 49 dst[3] = '='; |
| 50 } | 50 } |
| 51 } else { | 51 } else { |
| 52 dst[2] = dst[3] = '='; | 52 dst[2] = dst[3] = '='; |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 | 55 |
| 56 int32_t Base64EncodeA(const uint8_t* pSrc, int32_t iSrcLen, FX_CHAR* pDst) { | 56 int32_t Base64EncodeA(const uint8_t* pSrc, int32_t iSrcLen, FX_CHAR* pDst) { |
| 57 ASSERT(pSrc != NULL); | 57 ASSERT(pSrc); |
| 58 if (iSrcLen < 1) { | 58 if (iSrcLen < 1) { |
| 59 return 0; | 59 return 0; |
| 60 } | 60 } |
| 61 if (pDst == NULL) { | 61 if (!pDst) { |
| 62 int32_t iDstLen = iSrcLen / 3 * 4; | 62 int32_t iDstLen = iSrcLen / 3 * 4; |
| 63 if ((iSrcLen % 3) != 0) { | 63 if ((iSrcLen % 3) != 0) { |
| 64 iDstLen += 4; | 64 iDstLen += 4; |
| 65 } | 65 } |
| 66 return iDstLen; | 66 return iDstLen; |
| 67 } | 67 } |
| 68 FX_BASE64DATA srcData; | 68 FX_BASE64DATA srcData; |
| 69 int32_t iBytes = 3; | 69 int32_t iBytes = 3; |
| 70 FX_CHAR* pDstEnd = pDst; | 70 FX_CHAR* pDstEnd = pDst; |
| 71 while (iSrcLen > 0) { | 71 while (iSrcLen > 0) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 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 void* CXFA_SAXReaderHandler::OnTagEnter(const CFX_ByteStringC& bsTagName, | 99 void* CXFA_SAXReaderHandler::OnTagEnter(const CFX_ByteStringC& bsTagName, |
| 100 CFX_SAXItem::Type eType, | 100 CFX_SAXItem::Type eType, |
| 101 uint32_t dwStartPos) { | 101 uint32_t dwStartPos) { |
| 102 UpdateChecksum(TRUE); | 102 UpdateChecksum(TRUE); |
| 103 if (eType != CFX_SAXItem::Type::Tag && | 103 if (eType != CFX_SAXItem::Type::Tag && |
| 104 eType != CFX_SAXItem::Type::Instruction) { | 104 eType != CFX_SAXItem::Type::Instruction) { |
| 105 return NULL; | 105 return nullptr; |
| 106 } | 106 } |
| 107 m_SAXContext.m_eNode = eType; | 107 m_SAXContext.m_eNode = eType; |
| 108 CFX_ByteTextBuf& textBuf = m_SAXContext.m_TextBuf; | 108 CFX_ByteTextBuf& textBuf = m_SAXContext.m_TextBuf; |
| 109 textBuf << "<"; | 109 textBuf << "<"; |
| 110 if (eType == CFX_SAXItem::Type::Instruction) { | 110 if (eType == CFX_SAXItem::Type::Instruction) { |
| 111 textBuf << "?"; | 111 textBuf << "?"; |
| 112 } | 112 } |
| 113 textBuf << bsTagName; | 113 textBuf << bsTagName; |
| 114 m_SAXContext.m_bsTagName = bsTagName; | 114 m_SAXContext.m_bsTagName = bsTagName; |
| 115 return &m_SAXContext; | 115 return &m_SAXContext; |
| 116 } | 116 } |
| 117 void CXFA_SAXReaderHandler::OnTagAttribute(void* pTag, | 117 void CXFA_SAXReaderHandler::OnTagAttribute(void* pTag, |
| 118 const CFX_ByteStringC& bsAttri, | 118 const CFX_ByteStringC& bsAttri, |
| 119 const CFX_ByteStringC& bsValue) { | 119 const CFX_ByteStringC& bsValue) { |
| 120 if (pTag == NULL) { | 120 if (!pTag) |
| 121 return; | 121 return; |
| 122 } | 122 |
| 123 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; | 123 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; |
| 124 textBuf << " " << bsAttri << "=\"" << bsValue << "\""; | 124 textBuf << " " << bsAttri << "=\"" << bsValue << "\""; |
| 125 } | 125 } |
| 126 void CXFA_SAXReaderHandler::OnTagBreak(void* pTag) { | 126 void CXFA_SAXReaderHandler::OnTagBreak(void* pTag) { |
| 127 if (pTag == NULL) { | 127 if (!pTag) |
| 128 return; | 128 return; |
| 129 } | 129 |
| 130 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; | 130 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; |
| 131 textBuf << ">"; | 131 textBuf << ">"; |
| 132 UpdateChecksum(FALSE); | 132 UpdateChecksum(FALSE); |
| 133 } | 133 } |
| 134 void CXFA_SAXReaderHandler::OnTagData(void* pTag, | 134 void CXFA_SAXReaderHandler::OnTagData(void* pTag, |
| 135 CFX_SAXItem::Type eType, | 135 CFX_SAXItem::Type eType, |
| 136 const CFX_ByteStringC& bsData, | 136 const CFX_ByteStringC& bsData, |
| 137 uint32_t dwStartPos) { | 137 uint32_t dwStartPos) { |
| 138 if (pTag == NULL) { | 138 if (!pTag) |
| 139 return; | 139 return; |
| 140 } | 140 |
| 141 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; | 141 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; |
| 142 if (eType == CFX_SAXItem::Type::CharData) { | 142 if (eType == CFX_SAXItem::Type::CharData) { |
| 143 textBuf << "<![CDATA["; | 143 textBuf << "<![CDATA["; |
| 144 } | 144 } |
| 145 textBuf << bsData; | 145 textBuf << bsData; |
| 146 if (eType == CFX_SAXItem::Type::CharData) { | 146 if (eType == CFX_SAXItem::Type::CharData) { |
| 147 textBuf << "]]>"; | 147 textBuf << "]]>"; |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 void CXFA_SAXReaderHandler::OnTagClose(void* pTag, uint32_t dwEndPos) { | 150 void CXFA_SAXReaderHandler::OnTagClose(void* pTag, uint32_t dwEndPos) { |
| 151 if (pTag == NULL) { | 151 if (!pTag) |
| 152 return; | 152 return; |
| 153 } | 153 |
| 154 CXFA_SAXContext* pSAXContext = (CXFA_SAXContext*)pTag; | 154 CXFA_SAXContext* pSAXContext = (CXFA_SAXContext*)pTag; |
| 155 CFX_ByteTextBuf& textBuf = pSAXContext->m_TextBuf; | 155 CFX_ByteTextBuf& textBuf = pSAXContext->m_TextBuf; |
| 156 if (pSAXContext->m_eNode == CFX_SAXItem::Type::Instruction) { | 156 if (pSAXContext->m_eNode == CFX_SAXItem::Type::Instruction) { |
| 157 textBuf << "?>"; | 157 textBuf << "?>"; |
| 158 } else if (pSAXContext->m_eNode == CFX_SAXItem::Type::Tag) { | 158 } else if (pSAXContext->m_eNode == CFX_SAXItem::Type::Tag) { |
| 159 textBuf << "></" << pSAXContext->m_bsTagName.AsStringC() << ">"; | 159 textBuf << "></" << pSAXContext->m_bsTagName.AsStringC() << ">"; |
| 160 } | 160 } |
| 161 UpdateChecksum(FALSE); | 161 UpdateChecksum(FALSE); |
| 162 } | 162 } |
| 163 void CXFA_SAXReaderHandler::OnTagEnd(void* pTag, | 163 void CXFA_SAXReaderHandler::OnTagEnd(void* pTag, |
| 164 const CFX_ByteStringC& bsTagName, | 164 const CFX_ByteStringC& bsTagName, |
| 165 uint32_t dwEndPos) { | 165 uint32_t dwEndPos) { |
| 166 if (pTag == NULL) { | 166 if (!pTag) |
| 167 return; | 167 return; |
| 168 } | 168 |
| 169 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; | 169 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; |
| 170 textBuf << "</" << bsTagName << ">"; | 170 textBuf << "</" << bsTagName << ">"; |
| 171 UpdateChecksum(FALSE); | 171 UpdateChecksum(FALSE); |
| 172 } | 172 } |
| 173 void CXFA_SAXReaderHandler::OnTargetData(void* pTag, | 173 void CXFA_SAXReaderHandler::OnTargetData(void* pTag, |
| 174 CFX_SAXItem::Type eType, | 174 CFX_SAXItem::Type eType, |
| 175 const CFX_ByteStringC& bsData, | 175 const CFX_ByteStringC& bsData, |
| 176 uint32_t dwStartPos) { | 176 uint32_t dwStartPos) { |
| 177 if (pTag == NULL && eType != CFX_SAXItem::Type::Comment) { | 177 if (!pTag && eType != CFX_SAXItem::Type::Comment) |
| 178 return; | 178 return; |
| 179 } | 179 |
| 180 if (eType == CFX_SAXItem::Type::Comment) { | 180 if (eType == CFX_SAXItem::Type::Comment) { |
| 181 CFX_ByteTextBuf& textBuf = m_SAXContext.m_TextBuf; | 181 CFX_ByteTextBuf& textBuf = m_SAXContext.m_TextBuf; |
| 182 textBuf << "<!--" << bsData << "-->"; | 182 textBuf << "<!--" << bsData << "-->"; |
| 183 UpdateChecksum(FALSE); | 183 UpdateChecksum(FALSE); |
| 184 } else { | 184 } else { |
| 185 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; | 185 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; |
| 186 textBuf << " " << bsData; | 186 textBuf << " " << bsData; |
| 187 } | 187 } |
| 188 } | 188 } |
| 189 void CXFA_SAXReaderHandler::UpdateChecksum(FX_BOOL bCheckSpace) { | 189 void CXFA_SAXReaderHandler::UpdateChecksum(FX_BOOL bCheckSpace) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 233 |
| 234 CXFA_SAXReaderHandler handler(this); | 234 CXFA_SAXReaderHandler handler(this); |
| 235 m_pSAXReader->SetHandler(&handler); | 235 m_pSAXReader->SetHandler(&handler); |
| 236 if (m_pSAXReader->StartParse( | 236 if (m_pSAXReader->StartParse( |
| 237 pSrcFile, (uint32_t)offset, (uint32_t)size, | 237 pSrcFile, (uint32_t)offset, (uint32_t)size, |
| 238 CFX_SaxParseMode_NotSkipSpace | CFX_SaxParseMode_NotConvert_amp | | 238 CFX_SaxParseMode_NotSkipSpace | CFX_SaxParseMode_NotConvert_amp | |
| 239 CFX_SaxParseMode_NotConvert_lt | CFX_SaxParseMode_NotConvert_gt | | 239 CFX_SaxParseMode_NotConvert_lt | CFX_SaxParseMode_NotConvert_gt | |
| 240 CFX_SaxParseMode_NotConvert_sharp) < 0) { | 240 CFX_SaxParseMode_NotConvert_sharp) < 0) { |
| 241 return FALSE; | 241 return FALSE; |
| 242 } | 242 } |
| 243 return m_pSAXReader->ContinueParse(NULL) > 99; | 243 return m_pSAXReader->ContinueParse(nullptr) > 99; |
| 244 } | 244 } |
| 245 | 245 |
| 246 void CXFA_ChecksumContext::FinishChecksum() { | 246 void CXFA_ChecksumContext::FinishChecksum() { |
| 247 delete m_pSAXReader; | 247 delete m_pSAXReader; |
| 248 m_pSAXReader = nullptr; | 248 m_pSAXReader = nullptr; |
| 249 if (m_pByteContext) { | 249 if (m_pByteContext) { |
| 250 uint8_t digest[20]; | 250 uint8_t digest[20]; |
| 251 FXSYS_memset(digest, 0, 20); | 251 FXSYS_memset(digest, 0, 20); |
| 252 CRYPT_SHA1Finish(m_pByteContext, digest); | 252 CRYPT_SHA1Finish(m_pByteContext, digest); |
| 253 int32_t nLen = Base64EncodeA(digest, 20, NULL); | 253 int32_t nLen = Base64EncodeA(digest, 20, nullptr); |
| 254 FX_CHAR* pBuffer = m_bsChecksum.GetBuffer(nLen); | 254 FX_CHAR* pBuffer = m_bsChecksum.GetBuffer(nLen); |
| 255 Base64EncodeA(digest, 20, pBuffer); | 255 Base64EncodeA(digest, 20, pBuffer); |
| 256 m_bsChecksum.ReleaseBuffer(nLen); | 256 m_bsChecksum.ReleaseBuffer(nLen); |
| 257 FX_Free(m_pByteContext); | 257 FX_Free(m_pByteContext); |
| 258 m_pByteContext = NULL; | 258 m_pByteContext = nullptr; |
| 259 } | 259 } |
| 260 } | 260 } |
| 261 | 261 |
| 262 CFX_ByteString CXFA_ChecksumContext::GetChecksum() const { | 262 CFX_ByteString CXFA_ChecksumContext::GetChecksum() const { |
| 263 return m_bsChecksum; | 263 return m_bsChecksum; |
| 264 } | 264 } |
| 265 | 265 |
| 266 void CXFA_ChecksumContext::Update(const CFX_ByteStringC& bsText) { | 266 void CXFA_ChecksumContext::Update(const CFX_ByteStringC& bsText) { |
| 267 if (m_pByteContext) { | 267 if (m_pByteContext) { |
| 268 CRYPT_SHA1Update(m_pByteContext, bsText.raw_str(), bsText.GetLength()); | 268 CRYPT_SHA1Update(m_pByteContext, bsText.raw_str(), bsText.GetLength()); |
| 269 } | 269 } |
| 270 } | 270 } |
| OLD | NEW |