• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/fxcrt/cfx_checksumcontext.h"
8 
9 #include "core/fdrm/crypto/fx_crypt.h"
10 #include "core/fxcrt/fx_stream.h"
11 #include "core/fxcrt/xml/cfx_saxreaderhandler.h"
12 #include "third_party/base/ptr_util.h"
13 
14 namespace {
15 
16 struct FX_BASE64DATA {
17   uint32_t data1 : 2;
18   uint32_t data2 : 6;
19   uint32_t data3 : 4;
20   uint32_t data4 : 4;
21   uint32_t data5 : 6;
22   uint32_t data6 : 2;
23   uint32_t data7 : 8;
24 };
25 
26 const char g_FXBase64EncoderMap[64] = {
27     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
28     'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
29     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
30     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
31     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/',
32 };
33 
Base64EncodePiece(const FX_BASE64DATA & src,int32_t iBytes,char dst[4])34 void Base64EncodePiece(const FX_BASE64DATA& src, int32_t iBytes, char dst[4]) {
35   dst[0] = g_FXBase64EncoderMap[src.data2];
36   uint32_t b = src.data1 << 4;
37   if (iBytes > 1) {
38     b |= src.data4;
39   }
40   dst[1] = g_FXBase64EncoderMap[b];
41   if (iBytes > 1) {
42     b = src.data3 << 2;
43     if (iBytes > 2) {
44       b |= src.data6;
45     }
46     dst[2] = g_FXBase64EncoderMap[b];
47     if (iBytes > 2) {
48       dst[3] = g_FXBase64EncoderMap[src.data5];
49     } else {
50       dst[3] = '=';
51     }
52   } else {
53     dst[2] = dst[3] = '=';
54   }
55 }
56 
Base64EncodeA(const uint8_t * pSrc,int32_t iSrcLen,char * pDst)57 int32_t Base64EncodeA(const uint8_t* pSrc, int32_t iSrcLen, char* pDst) {
58   ASSERT(pSrc);
59   if (iSrcLen < 1) {
60     return 0;
61   }
62   if (!pDst) {
63     int32_t iDstLen = iSrcLen / 3 * 4;
64     if ((iSrcLen % 3) != 0) {
65       iDstLen += 4;
66     }
67     return iDstLen;
68   }
69   FX_BASE64DATA srcData;
70   int32_t iBytes = 3;
71   char* pDstEnd = pDst;
72   while (iSrcLen > 0) {
73     if (iSrcLen > 2) {
74       ((uint8_t*)&srcData)[0] = *pSrc++;
75       ((uint8_t*)&srcData)[1] = *pSrc++;
76       ((uint8_t*)&srcData)[2] = *pSrc++;
77       iSrcLen -= 3;
78     } else {
79       *((uint32_t*)&srcData) = 0;
80       ((uint8_t*)&srcData)[0] = *pSrc++;
81       if (iSrcLen > 1) {
82         ((uint8_t*)&srcData)[1] = *pSrc++;
83       }
84       iBytes = iSrcLen;
85       iSrcLen = 0;
86     }
87     Base64EncodePiece(srcData, iBytes, pDstEnd);
88     pDstEnd += 4;
89   }
90   return pDstEnd - pDst;
91 }
92 
93 }  // namespace
94 
CFX_ChecksumContext()95 CFX_ChecksumContext::CFX_ChecksumContext() {}
96 
~CFX_ChecksumContext()97 CFX_ChecksumContext::~CFX_ChecksumContext() {}
98 
StartChecksum()99 void CFX_ChecksumContext::StartChecksum() {
100   FinishChecksum();
101   m_pByteContext = pdfium::MakeUnique<CRYPT_sha1_context>();
102   CRYPT_SHA1Start(m_pByteContext.get());
103   m_bsChecksum.clear();
104   m_pSAXReader = pdfium::MakeUnique<CFX_SAXReader>();
105 }
106 
UpdateChecksum(const RetainPtr<IFX_SeekableReadStream> & pSrcFile,FX_FILESIZE offset,size_t size)107 bool CFX_ChecksumContext::UpdateChecksum(
108     const RetainPtr<IFX_SeekableReadStream>& pSrcFile,
109     FX_FILESIZE offset,
110     size_t size) {
111   if (!m_pSAXReader || !pSrcFile)
112     return false;
113 
114   if (size < 1)
115     size = pSrcFile->GetSize();
116 
117   CFX_SAXReaderHandler handler(this);
118   m_pSAXReader->SetHandler(&handler);
119   if (m_pSAXReader->StartParse(
120           pSrcFile, (uint32_t)offset, (uint32_t)size,
121           CFX_SaxParseMode_NotSkipSpace | CFX_SaxParseMode_NotConvert_amp |
122               CFX_SaxParseMode_NotConvert_lt | CFX_SaxParseMode_NotConvert_gt |
123               CFX_SaxParseMode_NotConvert_sharp) < 0) {
124     return false;
125   }
126   return m_pSAXReader->ContinueParse() > 99;
127 }
128 
FinishChecksum()129 void CFX_ChecksumContext::FinishChecksum() {
130   m_pSAXReader.reset();
131   if (m_pByteContext) {
132     uint8_t digest[20];
133     memset(digest, 0, 20);
134     CRYPT_SHA1Finish(m_pByteContext.get(), digest);
135     int32_t nLen = Base64EncodeA(digest, 20, nullptr);
136     char* pBuffer = m_bsChecksum.GetBuffer(nLen);
137     Base64EncodeA(digest, 20, pBuffer);
138     m_bsChecksum.ReleaseBuffer(nLen);
139     m_pByteContext.reset();
140   }
141 }
142 
GetChecksum() const143 ByteString CFX_ChecksumContext::GetChecksum() const {
144   return m_bsChecksum;
145 }
146 
Update(const ByteStringView & bsText)147 void CFX_ChecksumContext::Update(const ByteStringView& bsText) {
148   if (!m_pByteContext)
149     return;
150 
151   CRYPT_SHA1Update(m_pByteContext.get(), bsText.raw_str(), bsText.GetLength());
152 }
153