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_ENDOF_CENTRAL_DIRECTORY_H
17 #define SIGNATRUETOOLS_ENDOF_CENTRAL_DIRECTORY_H
18 
19 #include <optional>
20 #include <string>
21 
22 #include "byte_buffer.h"
23 
24 namespace OHOS {
25 namespace SignatureTools {
26 /**
27  * resolve zip EndOfCentralDirectory data
28  * EndOfCentralDirectory format for:
29  * end of central dir signature    4 bytes  (0x06054b50)
30  * number of this disk             2 bytes
31  * number of the disk with the
32  * start of the central directory  2 bytes
33  * total number of entries in the
34  * central directory on this disk  2 bytes
35  * total number of entries in
36  * the central directory           2 bytes
37  * size of the central directory   4 bytes
38  * offset of start of central
39  * directory with respect to
40  * the starting disk number        4 bytes
41  * .ZIP file comment length        2 bytes
42  * .ZIP file comment       (variable size)
43  */
44 class EndOfCentralDirectory {
45 public:
46     /* EndOfCentralDirectory invariable bytes length */
47     static constexpr int EOCD_LENGTH = 22;
48 
49     /* 4 bytes , central directory signature */
50     static constexpr int SIGNATURE = 0x06054b50;
51 
52     /**
53      * init End Of Central Directory, default offset is 0
54      *
55      * @param bytes End Of Central Directory bytes
56      * @return End Of Central Directory
57      */
58     static std::optional<EndOfCentralDirectory*> GetEOCDByBytes(const std::string& bytes);
59 
60     /**
61      * init End Of Central Directory
62      *
63      * @param bytes End Of Central Directory bytes
64      * @param offset offset
65      * @return End Of Central Directory
66      */
67     static std::optional<EndOfCentralDirectory*> GetEOCDByBytes(const std::string& bytes, int offset);
68 
69     /**
70      * change End Of Central Directory to bytes
71      *
72      * @return bytes
73      */
74     std::string ToBytes();
75 
76     static int GetEocdLength();
77 
78     static int GetSIGNATURE();
79 
80     uint16_t GetDiskNum();
81 
82     void SetDiskNum(uint16_t diskNum);
83 
84     uint16_t GetcDStartDiskNum();
85 
86     void SetcDStartDiskNum(uint16_t cDStartDiskNum);
87 
88     uint16_t GetThisDiskCDNum();
89 
90     void SetThisDiskCDNum(uint16_t thisDiskCDNum);
91 
92     uint16_t GetcDTotal();
93 
94     void SetcDTotal(uint16_t cDTotal);
95 
96     uint32_t GetcDSize();
97 
98     void SetcDSize(uint32_t cDSize);
99 
100     uint32_t GetOffset();
101 
102     void SetOffset(uint32_t offset);
103 
104     uint16_t GetCommentLength();
105 
106     void SetCommentLength(uint16_t commentLength);
107 
108     std::string GetComment();
109 
110     void SetComment(const std::string& comment);
111 
112     int GetLength();
113 
114     void SetLength(uint32_t length);
115 
116 private:
117     static void SetEndOfCentralDirectoryValues(ByteBuffer& bf, EndOfCentralDirectory* eocd);
118 
119     /* 2 bytes */
120     uint16_t m_diskNum = 0;
121 
122     /* 2 bytes */
123     uint16_t m_cDStartDiskNum = 0;
124 
125     /* 2 bytes */
126     uint16_t m_thisDiskCDNum = 0;
127 
128     /* 2 bytes */
129     uint16_t m_cDTotal = 0;
130 
131     /* 4 bytes */
132     uint32_t m_cDSize = 0;
133 
134     /* 4bytes */
135     uint32_t m_offset = 0;
136 
137     /* 2 bytes */
138     uint16_t m_commentLength = 0;
139 
140     /* n bytes */
141     std::string m_comment;
142 
143     uint32_t m_length = 0;
144 };
145 } // namespace SignatureTools
146 } // namespace OHOS
147 #endif // SIGNATRUETOOLS_ENDOF_CENTRAL_DIRECTORY_H