| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
| 6 | |
| 7 #include "xfa/src/fxfa/app/xfa_checksum.h" | |
| 8 | |
| 9 #include "core/include/fdrm/fx_crypt.h" | |
| 10 #include "xfa/src/fgas/crt/fgas_algorithm.h" | |
| 11 | |
| 12 CXFA_SAXReaderHandler::CXFA_SAXReaderHandler(CXFA_ChecksumContext* pContext) | |
| 13 : m_pContext(pContext) { | |
| 14 FXSYS_assert(m_pContext); | |
| 15 } | |
| 16 CXFA_SAXReaderHandler::~CXFA_SAXReaderHandler() {} | |
| 17 void* CXFA_SAXReaderHandler::OnTagEnter(const CFX_ByteStringC& bsTagName, | |
| 18 FX_SAXNODE eType, | |
| 19 FX_DWORD dwStartPos) { | |
| 20 UpdateChecksum(TRUE); | |
| 21 if (eType != FX_SAXNODE_Tag && eType != FX_SAXNODE_Instruction) { | |
| 22 return NULL; | |
| 23 } | |
| 24 m_SAXContext.m_eNode = eType; | |
| 25 CFX_ByteTextBuf& textBuf = m_SAXContext.m_TextBuf; | |
| 26 textBuf << "<"; | |
| 27 if (eType == FX_SAXNODE_Instruction) { | |
| 28 textBuf << "?"; | |
| 29 } | |
| 30 textBuf << bsTagName; | |
| 31 m_SAXContext.m_bsTagName = bsTagName; | |
| 32 return &m_SAXContext; | |
| 33 } | |
| 34 void CXFA_SAXReaderHandler::OnTagAttribute(void* pTag, | |
| 35 const CFX_ByteStringC& bsAttri, | |
| 36 const CFX_ByteStringC& bsValue) { | |
| 37 if (pTag == NULL) { | |
| 38 return; | |
| 39 } | |
| 40 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; | |
| 41 textBuf << " " << bsAttri << "=\"" << bsValue << "\""; | |
| 42 } | |
| 43 void CXFA_SAXReaderHandler::OnTagBreak(void* pTag) { | |
| 44 if (pTag == NULL) { | |
| 45 return; | |
| 46 } | |
| 47 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; | |
| 48 textBuf << ">"; | |
| 49 UpdateChecksum(FALSE); | |
| 50 } | |
| 51 void CXFA_SAXReaderHandler::OnTagData(void* pTag, | |
| 52 FX_SAXNODE eType, | |
| 53 const CFX_ByteStringC& bsData, | |
| 54 FX_DWORD dwStartPos) { | |
| 55 if (pTag == NULL) { | |
| 56 return; | |
| 57 } | |
| 58 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; | |
| 59 if (eType == FX_SAXNODE_CharData) { | |
| 60 textBuf << "<![CDATA["; | |
| 61 } | |
| 62 textBuf << bsData; | |
| 63 if (eType == FX_SAXNODE_CharData) { | |
| 64 textBuf << "]]>"; | |
| 65 } | |
| 66 } | |
| 67 void CXFA_SAXReaderHandler::OnTagClose(void* pTag, FX_DWORD dwEndPos) { | |
| 68 if (pTag == NULL) { | |
| 69 return; | |
| 70 } | |
| 71 CXFA_SAXContext* pSAXContext = (CXFA_SAXContext*)pTag; | |
| 72 CFX_ByteTextBuf& textBuf = pSAXContext->m_TextBuf; | |
| 73 if (pSAXContext->m_eNode == FX_SAXNODE_Instruction) { | |
| 74 textBuf << "?>"; | |
| 75 } else if (pSAXContext->m_eNode == FX_SAXNODE_Tag) { | |
| 76 textBuf << "></" << pSAXContext->m_bsTagName << ">"; | |
| 77 } | |
| 78 UpdateChecksum(FALSE); | |
| 79 } | |
| 80 void CXFA_SAXReaderHandler::OnTagEnd(void* pTag, | |
| 81 const CFX_ByteStringC& bsTagName, | |
| 82 FX_DWORD dwEndPos) { | |
| 83 if (pTag == NULL) { | |
| 84 return; | |
| 85 } | |
| 86 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; | |
| 87 textBuf << "</" << bsTagName << ">"; | |
| 88 UpdateChecksum(FALSE); | |
| 89 } | |
| 90 void CXFA_SAXReaderHandler::OnTargetData(void* pTag, | |
| 91 FX_SAXNODE eType, | |
| 92 const CFX_ByteStringC& bsData, | |
| 93 FX_DWORD dwStartPos) { | |
| 94 if (pTag == NULL && eType != FX_SAXNODE_Comment) { | |
| 95 return; | |
| 96 } | |
| 97 if (eType == FX_SAXNODE_Comment) { | |
| 98 CFX_ByteTextBuf& textBuf = m_SAXContext.m_TextBuf; | |
| 99 textBuf << "<!--" << bsData << "-->"; | |
| 100 UpdateChecksum(FALSE); | |
| 101 } else { | |
| 102 CFX_ByteTextBuf& textBuf = ((CXFA_SAXContext*)pTag)->m_TextBuf; | |
| 103 textBuf << " " << bsData; | |
| 104 } | |
| 105 } | |
| 106 void CXFA_SAXReaderHandler::UpdateChecksum(FX_BOOL bCheckSpace) { | |
| 107 int32_t iLength = m_SAXContext.m_TextBuf.GetLength(); | |
| 108 if (iLength < 1) { | |
| 109 return; | |
| 110 } | |
| 111 uint8_t* pBuffer = m_SAXContext.m_TextBuf.GetBuffer(); | |
| 112 FX_BOOL bUpdata = TRUE; | |
| 113 if (bCheckSpace) { | |
| 114 bUpdata = FALSE; | |
| 115 for (int32_t i = 0; i < iLength; i++) { | |
| 116 bUpdata = (pBuffer[i] > 0x20); | |
| 117 if (bUpdata) { | |
| 118 break; | |
| 119 } | |
| 120 } | |
| 121 } | |
| 122 if (bUpdata) { | |
| 123 m_pContext->Update(CFX_ByteStringC(pBuffer, iLength)); | |
| 124 } | |
| 125 m_SAXContext.m_TextBuf.Clear(); | |
| 126 } | |
| 127 IXFA_ChecksumContext* XFA_Checksum_Create() { | |
| 128 return new CXFA_ChecksumContext; | |
| 129 } | |
| 130 CXFA_ChecksumContext::CXFA_ChecksumContext() | |
| 131 : m_pSAXReader(NULL), m_pByteContext(NULL) {} | |
| 132 CXFA_ChecksumContext::~CXFA_ChecksumContext() { | |
| 133 FinishChecksum(); | |
| 134 } | |
| 135 FX_BOOL CXFA_ChecksumContext::StartChecksum() { | |
| 136 FinishChecksum(); | |
| 137 m_pByteContext = FX_Alloc(uint8_t, 128); | |
| 138 CRYPT_SHA1Start(m_pByteContext); | |
| 139 m_bsChecksum.Empty(); | |
| 140 m_pSAXReader = FX_SAXReader_Create(); | |
| 141 return m_pSAXReader != NULL; | |
| 142 } | |
| 143 FX_BOOL CXFA_ChecksumContext::UpdateChecksum(IFX_FileRead* pSrcFile, | |
| 144 FX_FILESIZE offset, | |
| 145 size_t size) { | |
| 146 if (m_pSAXReader == NULL) { | |
| 147 return FALSE; | |
| 148 } | |
| 149 if (pSrcFile == NULL) { | |
| 150 return FALSE; | |
| 151 } | |
| 152 if (size < 1) { | |
| 153 size = pSrcFile->GetSize(); | |
| 154 } | |
| 155 CXFA_SAXReaderHandler handler(this); | |
| 156 m_pSAXReader->SetHandler(&handler); | |
| 157 if (m_pSAXReader->StartParse( | |
| 158 pSrcFile, (FX_DWORD)offset, (FX_DWORD)size, | |
| 159 FX_SAXPARSEMODE_NotSkipSpace | FX_SAXPARSEMODE_NotConvert_amp | | |
| 160 FX_SAXPARSEMODE_NotConvert_lt | FX_SAXPARSEMODE_NotConvert_gt | | |
| 161 FX_SAXPARSEMODE_NotConvert_sharp) < 0) { | |
| 162 return FALSE; | |
| 163 } | |
| 164 return m_pSAXReader->ContinueParse(NULL) > 99; | |
| 165 } | |
| 166 void CXFA_ChecksumContext::FinishChecksum() { | |
| 167 if (m_pSAXReader) { | |
| 168 m_pSAXReader->Release(); | |
| 169 m_pSAXReader = NULL; | |
| 170 } | |
| 171 if (m_pByteContext) { | |
| 172 uint8_t digest[20]; | |
| 173 FXSYS_memset(digest, 0, 20); | |
| 174 CRYPT_SHA1Finish(m_pByteContext, digest); | |
| 175 int32_t nLen = FX_Base64EncodeA(digest, 20, NULL); | |
| 176 FX_CHAR* pBuffer = m_bsChecksum.GetBuffer(nLen); | |
| 177 FX_Base64EncodeA(digest, 20, pBuffer); | |
| 178 m_bsChecksum.ReleaseBuffer(nLen); | |
| 179 FX_Free(m_pByteContext); | |
| 180 m_pByteContext = NULL; | |
| 181 } | |
| 182 } | |
| 183 void CXFA_ChecksumContext::GetChecksum(CFX_ByteString& bsChecksum) { | |
| 184 bsChecksum = m_bsChecksum; | |
| 185 } | |
| 186 void CXFA_ChecksumContext::Update(const CFX_ByteStringC& bsText) { | |
| 187 if (m_pByteContext) { | |
| 188 CRYPT_SHA1Update(m_pByteContext, bsText.GetPtr(), bsText.GetLength()); | |
| 189 } | |
| 190 } | |
| OLD | NEW |