From ddb7016e69d99bb9355420b49a0ee864e2b7fe15 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 30 Mar 2017 14:01:31 -0400 Subject: Move CFX files into fxcrt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This Cl moves more of the CFX classes defined in the xfa/ directory to the core/fxcrt directory and conditionally builds them. Change-Id: I811e6c6acd31dfbe75b49880be43661f78334013 Reviewed-on: https://pdfium-review.googlesource.com/3372 Reviewed-by: Nicolás Peña Commit-Queue: dsinclair --- core/fxcrt/cfx_checksumcontext.cpp | 151 +++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 core/fxcrt/cfx_checksumcontext.cpp (limited to 'core/fxcrt/cfx_checksumcontext.cpp') diff --git a/core/fxcrt/cfx_checksumcontext.cpp b/core/fxcrt/cfx_checksumcontext.cpp new file mode 100644 index 0000000000..994d18604a --- /dev/null +++ b/core/fxcrt/cfx_checksumcontext.cpp @@ -0,0 +1,151 @@ +// 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 "core/fxcrt/cfx_checksumcontext.h" + +#include "core/fdrm/crypto/fx_crypt.h" +#include "core/fxcrt/xml/cfx_saxreaderhandler.h" +#include "third_party/base/ptr_util.h" + +namespace { + +struct FX_BASE64DATA { + uint32_t data1 : 2; + uint32_t data2 : 6; + uint32_t data3 : 4; + uint32_t data4 : 4; + uint32_t data5 : 6; + uint32_t data6 : 2; + uint32_t data7 : 8; +}; + +const char g_FXBase64EncoderMap[64] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', + 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', +}; + +void Base64EncodePiece(const FX_BASE64DATA& src, int32_t iBytes, char dst[4]) { + dst[0] = g_FXBase64EncoderMap[src.data2]; + uint32_t b = src.data1 << 4; + if (iBytes > 1) { + b |= src.data4; + } + dst[1] = g_FXBase64EncoderMap[b]; + if (iBytes > 1) { + b = src.data3 << 2; + if (iBytes > 2) { + b |= src.data6; + } + dst[2] = g_FXBase64EncoderMap[b]; + if (iBytes > 2) { + dst[3] = g_FXBase64EncoderMap[src.data5]; + } else { + dst[3] = '='; + } + } else { + dst[2] = dst[3] = '='; + } +} + +int32_t Base64EncodeA(const uint8_t* pSrc, int32_t iSrcLen, char* pDst) { + ASSERT(pSrc); + if (iSrcLen < 1) { + return 0; + } + if (!pDst) { + int32_t iDstLen = iSrcLen / 3 * 4; + if ((iSrcLen % 3) != 0) { + iDstLen += 4; + } + return iDstLen; + } + FX_BASE64DATA srcData; + int32_t iBytes = 3; + char* pDstEnd = pDst; + while (iSrcLen > 0) { + if (iSrcLen > 2) { + ((uint8_t*)&srcData)[0] = *pSrc++; + ((uint8_t*)&srcData)[1] = *pSrc++; + ((uint8_t*)&srcData)[2] = *pSrc++; + iSrcLen -= 3; + } else { + *((uint32_t*)&srcData) = 0; + ((uint8_t*)&srcData)[0] = *pSrc++; + if (iSrcLen > 1) { + ((uint8_t*)&srcData)[1] = *pSrc++; + } + iBytes = iSrcLen; + iSrcLen = 0; + } + Base64EncodePiece(srcData, iBytes, pDstEnd); + pDstEnd += 4; + } + return pDstEnd - pDst; +} + +} // namespace + +CFX_ChecksumContext::CFX_ChecksumContext() {} + +CFX_ChecksumContext::~CFX_ChecksumContext() {} + +void CFX_ChecksumContext::StartChecksum() { + FinishChecksum(); + m_pByteContext = pdfium::MakeUnique(); + CRYPT_SHA1Start(m_pByteContext.get()); + m_bsChecksum.clear(); + m_pSAXReader = pdfium::MakeUnique(); +} + +bool CFX_ChecksumContext::UpdateChecksum( + const CFX_RetainPtr& pSrcFile, + FX_FILESIZE offset, + size_t size) { + if (!m_pSAXReader || !pSrcFile) + return false; + + if (size < 1) + size = pSrcFile->GetSize(); + + CFX_SAXReaderHandler handler(this); + m_pSAXReader->SetHandler(&handler); + if (m_pSAXReader->StartParse( + pSrcFile, (uint32_t)offset, (uint32_t)size, + CFX_SaxParseMode_NotSkipSpace | CFX_SaxParseMode_NotConvert_amp | + CFX_SaxParseMode_NotConvert_lt | CFX_SaxParseMode_NotConvert_gt | + CFX_SaxParseMode_NotConvert_sharp) < 0) { + return false; + } + return m_pSAXReader->ContinueParse(nullptr) > 99; +} + +void CFX_ChecksumContext::FinishChecksum() { + m_pSAXReader.reset(); + if (m_pByteContext) { + uint8_t digest[20]; + FXSYS_memset(digest, 0, 20); + CRYPT_SHA1Finish(m_pByteContext.get(), digest); + int32_t nLen = Base64EncodeA(digest, 20, nullptr); + char* pBuffer = m_bsChecksum.GetBuffer(nLen); + Base64EncodeA(digest, 20, pBuffer); + m_bsChecksum.ReleaseBuffer(nLen); + m_pByteContext.reset(); + } +} + +CFX_ByteString CFX_ChecksumContext::GetChecksum() const { + return m_bsChecksum; +} + +void CFX_ChecksumContext::Update(const CFX_ByteStringC& bsText) { + if (!m_pByteContext) + return; + + CRYPT_SHA1Update(m_pByteContext.get(), bsText.raw_str(), bsText.GetLength()); +} -- cgit v1.2.3