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 #ifndef SIGNATRUETOOLS_ZIP_ENTRY_HEADER_H
17 #define SIGNATRUETOOLS_ZIP_ENTRY_HEADER_H
18 
19 #include <string>
20 
21 #include "byte_buffer.h"
22 
23 namespace OHOS {
24 namespace SignatureTools {
25 /**
26  * resolve zip ZipEntryHeader data
27  * end of central dir signature    4 bytes  (0x06054b50)
28  * number of this disk             2 bytes
29  * number of the disk with the
30  * start of the central directory  2 bytes
31  * total number of entries in the
32  * central directory on this disk  2 bytes
33  * total number of entries in
34  * the central directory           2 bytes
35  * size of the central directory   4 bytes
36  * offset of start of central
37  * directory with respect to
38  * the starting disk number        4 bytes
39  * .ZIP file comment length        2 bytes
40  * .ZIP file comment       (variable size)
41  */
42 class ZipEntryHeader {
43 public:
44     /* ZipEntryHeader invariable bytes length */
45     static constexpr int HEADER_LENGTH = 30;
46 
47     /* 4 bytes , entry header signature */
48     static constexpr int SIGNATURE = 0x04034b50;
49 
50     /**
51      * get Zip Entry Header
52      *
53      * @param bytes ZipEntryHeader bytes
54      * @return ZipEntryHeader
55      * @throws ZipException read entry header exception
56      */
57     static ZipEntryHeader* GetZipEntryHeader(const std::string& bytes);
58 
59     void ReadFileName(const std::string& bytes);
60 
61     void ReadExtra(const std::string& bytes);
62 
63     std::string ToBytes();
64 
65     static int GetHeaderLength();
66 
67     static int GetSIGNATURE();
68 
69     short GetVersion();
70 
71     void SetVersion(short version);
72 
73     short GetFlag();
74 
75     void SetFlag(short flag);
76 
77     short GetMethod();
78 
79     void SetMethod(short method);
80 
81     short GetLastTime();
82 
83     void SetLastTime(short lastTime);
84 
85     short GetLastDate();
86 
87     void SetLastDate(short lastDate);
88 
89     int GetCrc32();
90 
91     void SetCrc32(int crc32);
92 
93     uint32_t GetCompressedSize();
94 
95     void SetCompressedSize(uint32_t compressedSize);
96 
97     uint32_t GetUnCompressedSize();
98 
99     void SetUnCompressedSize(uint32_t unCompressedSize);
100 
101     uint16_t GetFileNameLength();
102 
103     void SetFileNameLength(uint16_t fileNameLength);
104 
105     uint16_t GetExtraLength();
106 
107     void SetExtraLength(uint16_t extraLength);
108 
109     std::string GetFileName() const;
110 
111     void SetFileName(const std::string& fileName);
112 
113     std::string GetExtraData() const;
114 
115     void SetExtraData(const std::string& extraData);
116 
117     uint32_t GetLength();
118 
119     void SetLength(uint32_t length);
120 
121 private:
122     /* 2 bytes */
123     short m_version = 0;
124 
125     /* 2 bytes */
126     short m_flag = 0;
127 
128     /* 2 bytes */
129     short m_method = 0;
130 
131     /* 2 bytes */
132     short m_lastTime = 0;
133 
134     /* 2 bytes */
135     short m_lastDate = 0;
136 
137     /* 4 bytes */
138     int m_crc32 = 0;
139 
140     /* 4 bytes */
141     uint32_t m_compressedSize = 0;
142 
143     /* 4 bytes */
144     uint32_t m_unCompressedSize = 0;
145 
146     /* 2 bytes */
147     uint16_t m_fileNameLength = 0;
148 
149     /* 2 bytes */
150     uint16_t m_extraLength = 0;
151 
152     /* n bytes */
153     std::string m_fileName;
154 
155     /* n bytes */
156     std::string m_extraData;
157 
158     uint32_t m_length = 0;
159 };
160 } // namespace SignatureTools
161 } // namespace OHOS
162 #endif // SIGNATRUETOOLS_ZIP_ENTRY_HEADER_H