1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "common/hap_byte_buffer_data_source.h"
17 #include "util/hap_verify_openssl_utils.h"
18
19 namespace OHOS {
20 namespace Security {
21 namespace Verify {
HapByteBufferDataSource(HapByteBuffer & hapBuffer)22 HapByteBufferDataSource::HapByteBufferDataSource(HapByteBuffer& hapBuffer)
23 : DataSource(), hapByteBuffer(hapBuffer)
24 {
25 }
26
~HapByteBufferDataSource()27 HapByteBufferDataSource::~HapByteBufferDataSource()
28 {
29 }
30
HasRemaining() const31 bool HapByteBufferDataSource::HasRemaining() const
32 {
33 return hapByteBuffer.HasRemaining();
34 }
35
Remaining() const36 long long HapByteBufferDataSource::Remaining() const
37 {
38 return static_cast<long long>(hapByteBuffer.Remaining());
39 }
40
Reset()41 void HapByteBufferDataSource::Reset()
42 {
43 hapByteBuffer.Clear();
44 }
45
ReadDataAndDigestUpdate(const DigestParameter & digestParam,int32_t chunkSize)46 bool HapByteBufferDataSource::ReadDataAndDigestUpdate(const DigestParameter& digestParam, int32_t chunkSize)
47 {
48 const unsigned char* chunk = reinterpret_cast<const unsigned char*>(hapByteBuffer.GetBufferPtr() +
49 hapByteBuffer.GetPosition());
50 bool res = HapVerifyOpensslUtils::DigestUpdate(digestParam, chunk, chunkSize);
51 if (res) {
52 hapByteBuffer.SetPosition(hapByteBuffer.GetPosition() + chunkSize);
53 }
54 return res;
55 }
56 } // namespace Verify
57 } // namespace Security
58 } // namespace OHOS
59