• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 package com.ohos.hapsigntool.hap.entity;
17 
18 import java.nio.ByteBuffer;
19 import java.nio.ByteOrder;
20 
21 /**
22  * define class of hap signature sub-block head
23  *
24  * @since 2023/11/07
25  */
26 public class BlockHead {
27     /**
28      * bin file sign block length is 8 byte
29      */
30     public static final int BLOCK_LEN = 8;
31 
32     /**
33      * elf file sign block length is 12 byte
34      */
35     public static final int ELF_BLOCK_LEN = 12;
36 
37     private static final int BIT_SIZE = 8;
38 
39     private static final int DOUBLE_BIT_SIZE = 16;
40 
41     private static final int TRIPLE_BIT_SIZE = 24;
42 
43     /**
44      * get bin block length
45      *
46      * @return return bin block length
47      */
getBlockLen()48     public static int getBlockLen() {
49         return BLOCK_LEN;
50     }
51 
52     /**
53      * get elf block length
54      *
55      * @return return elf block length
56      */
getElfBlockLen()57     public static int getElfBlockLen() {
58         return ELF_BLOCK_LEN;
59     }
60 
61     /**
62      * get serialization of file type bin BlockHead
63      *
64      * @param type type of signature block
65      * @param tag tags of signature block
66      * @param length the length of block data
67      * @param offset Byte offset of the data block relative to the start position of the signature block
68      * @return Byte array after serialization of HwBlockHead
69      */
getBlockHead(char type, char tag, short length, int offset)70     public static byte[] getBlockHead(char type, char tag, short length, int offset) {
71         return new byte[] {
72             (byte) (type),
73             (byte) (tag),
74             (byte) ((length >> BIT_SIZE) & 0xff),
75             (byte) (length & 0xff),
76             (byte) ((offset >> TRIPLE_BIT_SIZE) & 0xff),
77             (byte) ((offset >> DOUBLE_BIT_SIZE) & 0xff),
78             (byte) ((offset >> BIT_SIZE) & 0xff),
79             (byte) (offset & 0xff)
80         };
81     }
82 
83     /**
84      * get serialization of file type elf BlockHead
85      *
86      * @param type type of signature block
87      * @param tag tags of signature block
88      * @param length the length of block data
89      * @param offset Byte offset of the data block relative to the start position of the signature block
90      * @return Byte array after serialization of HwBlockHead
91      */
getBlockHeadLittleEndian(char type, char tag, int length, int offset)92     public static byte[] getBlockHeadLittleEndian(char type, char tag, int length, int offset) {
93         ByteBuffer bf = ByteBuffer.allocate(BlockHead.ELF_BLOCK_LEN).order(ByteOrder.LITTLE_ENDIAN);
94         bf.putChar(type);
95         bf.putChar(tag);
96         bf.putInt(length);
97         bf.putInt(offset);
98         return bf.array();
99     }
100 }