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#include "central_directory.h"
17#include "signature_tools_log.h"
18
19namespace OHOS {
20namespace SignatureTools {
21bool CentralDirectory::GetCentralDirectory(ByteBuffer& bf, CentralDirectory* cd)
22{
23    int signatureValue;
24    bf.GetInt32(signatureValue);
25    if (signatureValue != SIGNATURE) {
26        SIGNATURE_TOOLS_LOGE("find zip central directory failed");
27        return false;
28    }
29
30    SetCentralDirectoryValues(bf, cd);
31
32    uint16_t fileNameLength = cd->GetFileNameLength();
33    if (fileNameLength > 0) {
34        std::string readFileName(fileNameLength, 0);
35        bf.GetData(&readFileName[0], fileNameLength);
36        cd->SetFileName(readFileName);
37    }
38    uint16_t extraLength = cd->GetExtraLength();
39    if (extraLength > 0) {
40        std::string extra(extraLength, 0);
41        bf.GetData(&extra[0], extraLength);
42        cd->SetExtraData(extra);
43    }
44    uint16_t commentLength = cd->GetCommentLength();
45    if (commentLength > 0) {
46        std::string readComment(commentLength, 0);
47        bf.GetData(&readComment[0], commentLength);
48        cd->SetComment(readComment);
49    }
50    cd->SetLength(CD_LENGTH + fileNameLength + extraLength + commentLength);
51
52    return true;
53}
54
55void CentralDirectory::SetCentralDirectoryValues(ByteBuffer& bf, CentralDirectory* cd)
56{
57    int16_t centralDirectoryInt16Value;
58    bf.GetInt16(centralDirectoryInt16Value);
59    cd->SetVersion(centralDirectoryInt16Value);
60
61    bf.GetInt16(centralDirectoryInt16Value);
62    cd->SetVersionExtra(centralDirectoryInt16Value);
63
64    bf.GetInt16(centralDirectoryInt16Value);
65    cd->SetFlag(centralDirectoryInt16Value);
66
67    bf.GetInt16(centralDirectoryInt16Value);
68    cd->SetMethod(centralDirectoryInt16Value);
69
70    bf.GetInt16(centralDirectoryInt16Value);
71    cd->SetLastTime(centralDirectoryInt16Value);
72
73    bf.GetInt16(centralDirectoryInt16Value);
74    cd->SetLastDate(centralDirectoryInt16Value);
75
76    int32_t centralDirectoryInt32Value;
77    bf.GetInt32(centralDirectoryInt32Value);
78    cd->SetCrc32(centralDirectoryInt32Value);
79
80    uint32_t centralDirectoryUInt32Value;
81    bf.GetUInt32(centralDirectoryUInt32Value);
82    cd->SetCompressedSize(centralDirectoryUInt32Value);
83
84    bf.GetUInt32(centralDirectoryUInt32Value);
85    cd->SetUnCompressedSize(centralDirectoryUInt32Value);
86
87    uint16_t centralDirectoryUInt16Value;
88    bf.GetUInt16(centralDirectoryUInt16Value);
89    cd->SetFileNameLength(centralDirectoryUInt16Value);
90
91    bf.GetUInt16(centralDirectoryUInt16Value);
92    cd->SetExtraLength(centralDirectoryUInt16Value);
93
94    bf.GetUInt16(centralDirectoryUInt16Value);
95    cd->SetCommentLength(centralDirectoryUInt16Value);
96
97    bf.GetUInt16(centralDirectoryUInt16Value);
98    cd->SetDiskNumStart(centralDirectoryUInt16Value);
99
100    bf.GetInt16(centralDirectoryInt16Value);
101    cd->SetInternalFile(centralDirectoryInt16Value);
102
103    bf.GetInt32(centralDirectoryInt32Value);
104    cd->SetExternalFile(centralDirectoryInt32Value);
105
106    bf.GetUInt32(centralDirectoryUInt32Value);
107    cd->SetOffset(centralDirectoryUInt32Value);
108}
109
110std::string CentralDirectory::ToBytes()
111{
112    ByteBuffer bf(m_length);
113    bf.PutInt32(SIGNATURE);
114    bf.PutInt16(m_version);
115    bf.PutInt16(m_versionExtra);
116    bf.PutInt16(m_flag);
117    bf.PutInt16(m_method);
118    bf.PutInt16(m_lastTime);
119    bf.PutInt16(m_lastDate);
120    bf.PutInt32(m_crc32);
121    bf.PutUInt32(m_compressedSize);
122    bf.PutUInt32(m_unCompressedSize);
123    bf.PutUInt16(m_fileNameLength);
124    bf.PutUInt16(m_extraLength);
125    bf.PutUInt16(m_commentLength);
126    bf.PutUInt16(m_diskNumStart);
127    bf.PutInt16(m_internalFile);
128    bf.PutInt32(m_externalFile);
129    bf.PutUInt32(m_offset);
130
131    if (m_fileNameLength > 0) {
132        bf.PutData(m_fileName.c_str(), m_fileName.size());
133    }
134    if (m_extraLength > 0) {
135        bf.PutData(m_extraData.c_str(), m_extraData.size());
136    }
137    if (m_commentLength > 0) {
138        bf.PutData(m_extraData.c_str(), m_extraData.size());
139    }
140
141    return bf.ToString();
142}
143
144int CentralDirectory::GetCdLength()
145{
146    return CD_LENGTH;
147}
148
149int CentralDirectory::GetSIGNATURE()
150{
151    return SIGNATURE;
152}
153
154short CentralDirectory::GetVersion()
155{
156    return m_version;
157}
158
159void CentralDirectory::SetVersion(short version)
160{
161    m_version = version;
162}
163
164short CentralDirectory::GetVersionExtra()
165{
166    return m_versionExtra;
167}
168
169void CentralDirectory::SetVersionExtra(short versionExtra)
170{
171    m_versionExtra = versionExtra;
172}
173
174short CentralDirectory::GetFlag()
175{
176    return m_flag;
177}
178
179void CentralDirectory::SetFlag(short flag)
180{
181    m_flag = flag;
182}
183
184short CentralDirectory::GetMethod()
185{
186    return m_method;
187}
188
189void CentralDirectory::SetMethod(short method)
190{
191    m_method = method;
192}
193
194short CentralDirectory::GetLastTime()
195{
196    return m_lastTime;
197}
198
199void CentralDirectory::SetLastTime(short lastTime)
200{
201    m_lastTime = lastTime;
202}
203
204short CentralDirectory::GetLastDate()
205{
206    return m_lastDate;
207}
208
209void CentralDirectory::SetLastDate(short lastDate)
210{
211    m_lastDate = lastDate;
212}
213
214int CentralDirectory::GetCrc32()
215{
216    return m_crc32;
217}
218
219void CentralDirectory::SetCrc32(int crc32)
220{
221    m_crc32 = crc32;
222}
223
224uint32_t CentralDirectory::GetCompressedSize()
225{
226    return m_compressedSize;
227}
228
229void CentralDirectory::SetCompressedSize(uint32_t compressedSize)
230{
231    m_compressedSize = compressedSize;
232}
233
234uint32_t CentralDirectory::GetUnCompressedSize()
235{
236    return m_unCompressedSize;
237}
238
239void CentralDirectory::SetUnCompressedSize(uint32_t unCompressedSize)
240{
241    m_unCompressedSize = unCompressedSize;
242}
243
244uint16_t CentralDirectory::GetFileNameLength()
245{
246    return m_fileNameLength;
247}
248
249void CentralDirectory::SetFileNameLength(uint16_t fileNameLength)
250{
251    m_fileNameLength = fileNameLength;
252}
253
254uint16_t CentralDirectory::GetExtraLength()
255{
256    return m_extraLength;
257}
258
259void CentralDirectory::SetExtraLength(uint16_t extraLength)
260{
261    m_extraLength = extraLength;
262}
263
264uint16_t CentralDirectory::GetCommentLength()
265{
266    return m_commentLength;
267}
268
269void CentralDirectory::SetCommentLength(uint16_t commentLength)
270{
271    m_commentLength = commentLength;
272}
273
274uint16_t CentralDirectory::GetDiskNumStart()
275{
276    return m_diskNumStart;
277}
278
279void CentralDirectory::SetDiskNumStart(uint16_t diskNumStart)
280{
281    m_diskNumStart = diskNumStart;
282}
283
284short CentralDirectory::GetInternalFile()
285{
286    return m_internalFile;
287}
288
289void CentralDirectory::SetInternalFile(short internalFile)
290{
291    m_internalFile = internalFile;
292}
293
294int CentralDirectory::GetExternalFile()
295{
296    return m_externalFile;
297}
298
299void CentralDirectory::SetExternalFile(int externalFile)
300{
301    m_externalFile = externalFile;
302}
303
304uint32_t CentralDirectory::GetOffset()
305{
306    return m_offset;
307}
308
309void CentralDirectory::SetOffset(uint32_t offset)
310{
311    m_offset = offset;
312}
313
314std::string CentralDirectory::GetFileName()
315{
316    return m_fileName;
317}
318
319void CentralDirectory::SetFileName(const std::string& fileName)
320{
321    m_fileName = fileName;
322}
323
324std::string CentralDirectory::GetExtraData() const
325{
326    return m_extraData;
327}
328
329void CentralDirectory::SetExtraData(const std::string& extraData)
330{
331    m_extraData = extraData;
332}
333
334std::string CentralDirectory::GetComment()
335{
336    return m_comment;
337}
338
339void CentralDirectory::SetComment(const std::string& comment)
340{
341    m_comment = comment;
342}
343
344uint32_t CentralDirectory::GetLength()
345{
346    return m_length;
347}
348
349void CentralDirectory::SetLength(uint32_t length)
350{
351    m_length = length;
352}
353} // namespace SignatureTools
354} // namespace OHOS