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 * Sign info of file 24 * 25 * @since 2023/09/08 26 */ 27 public class SignedFilePos { 28 /** 29 * file name offset based on start of so info segment 30 */ 31 private int fileNameOffset; 32 33 /** 34 * byte size of file 35 */ 36 private final int fileNameSize; 37 38 /** 39 * sign info offset based on start of so info segment 40 */ 41 private int signInfoOffset; 42 43 /** 44 * byte size of sign info 45 */ 46 private final int signInfoSize; 47 48 /** 49 * Constructor for SignedFilePos 50 * 51 * @param fileNameOffset file name offset based on segment start 52 * @param fileNameSize byte size of file name string 53 * @param signInfoOffset sign info offset based on segment start 54 * @param signInfoSize byte size of sign info 55 */ SignedFilePos(int fileNameOffset, int fileNameSize, int signInfoOffset, int signInfoSize)56 public SignedFilePos(int fileNameOffset, int fileNameSize, int signInfoOffset, int signInfoSize) { 57 this.fileNameOffset = fileNameOffset; 58 this.fileNameSize = fileNameSize; 59 this.signInfoOffset = signInfoOffset; 60 this.signInfoSize = signInfoSize; 61 } 62 getFileNameOffset()63 public int getFileNameOffset() { 64 return fileNameOffset; 65 } 66 getFileNameSize()67 public int getFileNameSize() { 68 return fileNameSize; 69 } 70 getSignInfoOffset()71 public int getSignInfoOffset() { 72 return signInfoOffset; 73 } 74 getSignInfoSize()75 public int getSignInfoSize() { 76 return signInfoSize; 77 } 78 79 /** 80 * increase file name offset 81 * 82 * @param incOffset increase value 83 */ increaseFileNameOffset(int incOffset)84 public void increaseFileNameOffset(int incOffset) { 85 this.fileNameOffset += incOffset; 86 } 87 88 /** 89 * increase sign info offset 90 * 91 * @param incOffset increase value 92 */ increaseSignInfoOffset(int incOffset)93 public void increaseSignInfoOffset(int incOffset) { 94 this.signInfoOffset += incOffset; 95 } 96 97 /** 98 * Constructor for SignedFilePos by byte array 99 * 100 * @param bytes Byte array representation of SignedFilePos 101 * @return a newly created SignedFilePos object 102 */ fromByteArray(byte[] bytes)103 public static SignedFilePos fromByteArray(byte[] bytes) { 104 ByteBuffer bf = ByteBuffer.allocate(bytes.length).order(ByteOrder.LITTLE_ENDIAN); 105 bf.put(bytes); 106 bf.rewind(); 107 int inFileNameOffset = bf.getInt(); 108 int inFileNameSize = bf.getInt(); 109 int inSignInfoOffset = bf.getInt(); 110 int inSignInfoSize = bf.getInt(); 111 return new SignedFilePos(inFileNameOffset, inFileNameSize, inSignInfoOffset, inSignInfoSize); 112 } 113 114 /** 115 * Return a string representation of the object 116 * 117 * @return string representation of the object 118 */ toString()119 public String toString() { 120 return String.format(Locale.ROOT, "SignedFilePos: fileNameOffset, Size[%d, %d], signInfoOffset, Size[%d, %d]", 121 this.fileNameOffset, this.fileNameSize, this.signInfoOffset, this.signInfoSize); 122 } 123 } 124