• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.zip;
17 
18 import com.ohos.hapsigntool.error.ZipException;
19 
20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
22 
23 /**
24  * resolve zip DataDescriptor data
25  * DataDescriptor format:
26  * crc-32                          4 bytes
27  * compressed size                 4 bytes
28  * uncompressed size               4 bytes
29  *
30  * @since 2023/12/02
31  */
32 public class DataDescriptor {
33     /**
34      * DataDescriptor invariable bytes length
35      */
36     public static final int DES_LENGTH = 16;
37 
38     /**
39      * 4 bytes , DataDescriptor signature
40      */
41     public static final int SIGNATURE = 0x08074b50;
42 
43     /**
44      * 4 bytes
45      */
46     private int crc32;
47 
48     /**
49      * 4 bytes
50      */
51     private long compressedSize;
52 
53     /**
54      * 4 bytes
55      */
56     private long unCompressedSize;
57 
58     /**
59      * get Data Descriptor
60      *
61      * @param bytes DataDescriptor bytes
62      * @return DataDescriptor
63      * @throws ZipException read data descriptor exception
64      */
getDataDescriptor(byte[] bytes)65     public static DataDescriptor getDataDescriptor(byte[] bytes) throws ZipException {
66         if (bytes.length != DES_LENGTH) {
67             throw new ZipException("read Data Descriptor failed");
68         }
69         ByteBuffer bf = ByteBuffer.wrap(bytes);
70         bf.order(ByteOrder.LITTLE_ENDIAN);
71         DataDescriptor data = new DataDescriptor();
72         if (bf.getInt() != SIGNATURE) {
73             throw new ZipException("read Data Descriptor failed");
74         }
75         data.setCrc32(bf.getInt());
76         data.setCompressedSize(UnsignedDecimalUtil.getUnsignedInt(bf));
77         data.setUnCompressedSize(UnsignedDecimalUtil.getUnsignedInt(bf));
78         return data;
79     }
80 
81     /**
82      * change DataDescriptor to bytes
83      *
84      * @return bytes
85      */
toBytes()86     public byte[] toBytes() {
87         ByteBuffer bf = ByteBuffer.allocate(DES_LENGTH).order(ByteOrder.LITTLE_ENDIAN);
88         bf.putInt(SIGNATURE);
89         bf.putInt(crc32);
90         UnsignedDecimalUtil.setUnsignedInt(bf, compressedSize);
91         UnsignedDecimalUtil.setUnsignedInt(bf, unCompressedSize);
92         return bf.array();
93     }
94 
getDesLength()95     public static int getDesLength() {
96         return DES_LENGTH;
97     }
98 
getSIGNATURE()99     public static int getSIGNATURE() {
100         return SIGNATURE;
101     }
102 
getCrc32()103     public int getCrc32() {
104         return crc32;
105     }
106 
setCrc32(int crc32)107     public void setCrc32(int crc32) {
108         this.crc32 = crc32;
109     }
110 
getCompressedSize()111     public long getCompressedSize() {
112         return compressedSize;
113     }
114 
setCompressedSize(long compressedSize)115     public void setCompressedSize(long compressedSize) {
116         this.compressedSize = compressedSize;
117     }
118 
getUnCompressedSize()119     public long getUnCompressedSize() {
120         return unCompressedSize;
121     }
122 
setUnCompressedSize(long unCompressedSize)123     public void setUnCompressedSize(long unCompressedSize) {
124         this.unCompressedSize = unCompressedSize;
125     }
126 }
127