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 "block_head.h"
17 #include "byte_buffer.h"
18 #include "sign_head.h"
19 
20 namespace OHOS {
21 namespace SignatureTools {
22 
GetBlockLen()23 int BlockHead::GetBlockLen()
24 {
25     return BLOCK_LEN;
26 }
27 
GetElfBlockLen()28 int BlockHead::GetElfBlockLen()
29 {
30     return ELF_BLOCK_LEN;
31 }
32 
GetBlockHead(const char type, const char tag, const short length, const int offset)33 std::string BlockHead::GetBlockHead(const char type, const char tag, const short length, const int offset)
34 {
35     std::vector<int8_t> tmpVec;
36     tmpVec.push_back(type);
37     tmpVec.push_back(tag);
38     tmpVec.push_back((length >> BIT_SIZE) & 0xff);
39     tmpVec.push_back(length & 0xff);
40     tmpVec.push_back((offset >> TRIPLE_BIT_SIZE) & 0xff);
41     tmpVec.push_back((offset >> DOUBLE_BIT_SIZE) & 0xff);
42     tmpVec.push_back((offset >> BIT_SIZE) & 0xff);
43     tmpVec.push_back(offset & 0xff);
44 
45     return std::string(tmpVec.begin(), tmpVec.end());
46 }
47 
GetBlockHeadLittleEndian(const char type, const char tag, const int length, const int offset)48 std::vector<int8_t> BlockHead::GetBlockHeadLittleEndian(const char type, const char tag,
49     const int length, const int offset)
50 {
51     ByteBuffer bf = ByteBuffer(BlockHead::ELF_BLOCK_LEN);
52     bf.PutByte(type);
53     bf.PutByte(tag);
54     bf.PutByte(0);
55     bf.PutByte(0);
56     bf.PutInt32(length);
57     bf.PutInt32(offset);
58     int8_t ret[BlockHead::ELF_BLOCK_LEN] = {0};
59     bf.GetData(0, ret, BlockHead::ELF_BLOCK_LEN);
60     std::vector<int8_t> byte(ret, ret + BlockHead::ELF_BLOCK_LEN);
61     return byte;
62 }
63 
64 } // namespace SignatureTools
65 } // namespace OHOS
66