1 /* 2 * Copyright (c) 2023-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.codesigning.datastructure; 17 18 import java.nio.ByteBuffer; 19 import java.nio.ByteOrder; 20 import java.util.Locale; 21 22 /** 23 * Extension is an optional field in relative to SignInfo. 24 * It is the base class for all types of extensions, i.e. MerkleTreeExtension. 25 * <p> 26 * Structure: 27 * u32 type: Indicates the type of extension 28 * <p> 29 * u32 size: byte size of extension data 30 * 31 * @since 2023/09/08 32 */ 33 public class Extension { 34 /** 35 * Byte size of Extension base class. 36 */ 37 public static final int EXTENSION_HEADER_SIZE = 8; 38 39 private final int type; 40 41 private final int size; 42 Extension(int type, int size)43 public Extension(int type, int size) { 44 this.type = type; 45 this.size = size; 46 } 47 size()48 public int size() { 49 return EXTENSION_HEADER_SIZE; 50 } 51 isType(int type)52 public boolean isType(int type) { 53 return this.type == type; 54 } 55 56 /** 57 * Converts Extension to a newly created byte array 58 * 59 * @return Byte array representation of Extension 60 */ toByteArray()61 public byte[] toByteArray() { 62 ByteBuffer bf = ByteBuffer.allocate(EXTENSION_HEADER_SIZE).order(ByteOrder.LITTLE_ENDIAN); 63 bf.putInt(this.type); 64 bf.putInt(this.size); 65 return bf.array(); 66 } 67 68 /** 69 * Return a string representation of the object 70 * 71 * @return string representation of the object 72 */ toString()73 public String toString() { 74 return String.format(Locale.ROOT, "Extension: type[%d], size[%d]", this.type, this.size); 75 } 76 } 77