• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 "segment_header.h"
17 
18 namespace OHOS {
19 namespace SignatureTools {
20 
SegmentHeader()21 SegmentHeader::SegmentHeader()
22 {
23 }
24 
SegmentHeader(int32_t type,int32_t segmentSize)25 SegmentHeader::SegmentHeader(int32_t type, int32_t segmentSize) :type(type), segmentOffset(0), segmentSize(segmentSize)
26 {
27 }
28 
SegmentHeader(int32_t type,int32_t segmentOffset,int32_t segmentSize)29 SegmentHeader::SegmentHeader(int32_t type, int32_t segmentOffset,
30                              int32_t segmentSize)
31 {
32     this->type = type;
33     this->segmentOffset = segmentOffset;
34     this->segmentSize = segmentSize;
35 }
36 
GetType()37 int32_t SegmentHeader::GetType()
38 {
39     return type;
40 }
41 
SetSegmentOffset(int32_t offset)42 void SegmentHeader::SetSegmentOffset(int32_t offset)
43 {
44     segmentOffset = offset;
45 }
46 
GetSegmentOffset()47 int32_t SegmentHeader::GetSegmentOffset()
48 {
49     return segmentOffset;
50 }
51 
GetSegmentSize()52 int32_t SegmentHeader::GetSegmentSize()
53 {
54     return segmentSize;
55 }
56 
ToByteArray(std::vector<int8_t> & ret)57 void SegmentHeader::ToByteArray(std::vector<int8_t> &ret)
58 {
59     std::unique_ptr<ByteBuffer> bf = std::make_unique<ByteBuffer>(ByteBuffer(SEGMENT_HEADER_LENGTH));
60     bf->PutInt32(type);
61     bf->PutInt32(segmentOffset);
62     bf->PutInt32(segmentSize);
63     bf->Flip();
64     ret = std::vector<int8_t>(bf->GetBufferPtr(), bf->GetBufferPtr() + bf.get()->GetLimit());
65     return;
66 }
67 
FromByteArray(std::vector<int8_t> bytes)68 std::unique_ptr<SegmentHeader> SegmentHeader::FromByteArray(std::vector<int8_t> bytes)
69 {
70     if (bytes.size() != SEGMENT_HEADER_LENGTH) {
71         PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR, "The flag of SegmentHeader is incorrect.");
72         return std::unique_ptr<SegmentHeader>();
73     }
74     std::unique_ptr<ByteBuffer> bf = std::make_unique<ByteBuffer>(ByteBuffer(SEGMENT_HEADER_LENGTH));
75     bf->PutData(bytes.data(), bytes.size());
76     bf->Flip();
77     int32_t inType = 0;
78     bf->GetInt32(inType);
79     if ((inType != CSB_FSVERITY_INFO_SEG) && (inType != CSB_HAP_META_SEG)
80         && (inType != CSB_NATIVE_LIB_INFO_SEG)) {
81         PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR, "The type of SegmentHeader is incorrect.");
82         return std::unique_ptr<SegmentHeader>();
83     }
84     int32_t inSegmentOffset = 0;
85     bf->GetInt32(inSegmentOffset);
86     // segment offset is always larger than the size of CodeSignBlockHeader
87     int32_t inSegmentSize = 0;
88     bf->GetInt32(inSegmentSize);
89     if (inSegmentSize < 0) {
90         PrintErrorNumberMsg("VERIFY_ERROR", VERIFY_ERROR,
91                             "The segment size of SegmentHeader is incorrect.");
92         return std::unique_ptr<SegmentHeader>();
93     }
94     return std::make_unique<SegmentHeader>(inType, inSegmentOffset, inSegmentSize);
95 }
96 
97 }
98 }