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 "zip_entry_header.h"
17 #include "signature_tools_log.h"
18 
19 namespace OHOS {
20 namespace SignatureTools {
GetZipEntryHeader(const std::string& bytes)21 ZipEntryHeader* ZipEntryHeader::GetZipEntryHeader(const std::string& bytes)
22 {
23     ZipEntryHeader* entryHeader = new ZipEntryHeader();
24     ByteBuffer bf(bytes.c_str(), bytes.size());
25 
26     int32_t entryHeaderInt32Value;
27     bf.GetInt32(entryHeaderInt32Value);
28     if (entryHeaderInt32Value != ZipEntryHeader::SIGNATURE) {
29         delete entryHeader;
30         SIGNATURE_TOOLS_LOGE("find zip entry head failed");
31         return nullptr;
32     }
33 
34     int16_t entryHeaderInt16Value;
35     bf.GetInt16(entryHeaderInt16Value);
36     entryHeader->SetVersion(entryHeaderInt16Value);
37 
38     bf.GetInt16(entryHeaderInt16Value);
39     entryHeader->SetFlag(entryHeaderInt16Value);
40 
41     bf.GetInt16(entryHeaderInt16Value);
42     entryHeader->SetMethod(entryHeaderInt16Value);
43 
44     bf.GetInt16(entryHeaderInt16Value);
45     entryHeader->SetLastTime(entryHeaderInt16Value);
46 
47     bf.GetInt16(entryHeaderInt16Value);
48     entryHeader->SetLastDate(entryHeaderInt16Value);
49 
50     bf.GetInt32(entryHeaderInt32Value);
51     entryHeader->SetCrc32(entryHeaderInt32Value);
52 
53     uint32_t entryHeaderUInt32Value;
54     bf.GetUInt32(entryHeaderUInt32Value);
55     entryHeader->SetCompressedSize(entryHeaderUInt32Value);
56 
57     bf.GetUInt32(entryHeaderUInt32Value);
58     entryHeader->SetUnCompressedSize(entryHeaderUInt32Value);
59 
60     uint16_t entryHeaderUInt16Value;
61     bf.GetUInt16(entryHeaderUInt16Value);
62     entryHeader->SetFileNameLength(entryHeaderUInt16Value);
63 
64     bf.GetUInt16(entryHeaderUInt16Value);
65     entryHeader->SetExtraLength(entryHeaderUInt16Value);
66 
67     entryHeader->SetLength(HEADER_LENGTH + entryHeader->GetFileNameLength() + entryHeader->GetExtraLength());
68 
69     return entryHeader;
70 }
71 
ReadFileName(const std::string& bytes)72 void ZipEntryHeader::ReadFileName(const std::string& bytes)
73 {
74     ByteBuffer bf(bytes.c_str(), bytes.size());
75     if (m_fileNameLength > 0) {
76         std::string nameBytes(m_fileNameLength, 0);
77         bf.GetData(&nameBytes[0], m_fileNameLength);
78         m_fileName = nameBytes;
79     }
80 }
81 
ReadExtra(const std::string& bytes)82 void ZipEntryHeader::ReadExtra(const std::string& bytes)
83 {
84     ByteBuffer bf(bytes.c_str(), bytes.size());
85     if (m_extraLength > 0) {
86         std::string extra(m_extraLength, 0);
87         bf.GetData(&extra[0], m_extraLength);
88         m_extraData = extra;
89     }
90 }
91 
ToBytes()92 std::string ZipEntryHeader::ToBytes()
93 {
94     ByteBuffer bf(m_length);
95 
96     bf.PutInt32(SIGNATURE);
97     bf.PutInt16(m_version);
98     bf.PutInt16(m_flag);
99     bf.PutInt16(m_method);
100     bf.PutInt16(m_lastTime);
101     bf.PutInt16(m_lastDate);
102     bf.PutInt32(m_crc32);
103     bf.PutUInt32(m_compressedSize);
104     bf.PutUInt32(m_unCompressedSize);
105     bf.PutUInt16(m_fileNameLength);
106     bf.PutUInt16(m_extraLength);
107 
108     if (m_fileNameLength > 0) {
109         bf.PutData(m_fileName.c_str(), m_fileName.size());
110     }
111     if (m_extraLength > 0) {
112         bf.PutData(m_extraData.c_str(), m_extraData.size());
113     }
114 
115     return bf.ToString();
116 }
117 
GetHeaderLength()118 int ZipEntryHeader::GetHeaderLength()
119 {
120     return HEADER_LENGTH;
121 }
122 
GetSIGNATURE()123 int ZipEntryHeader::GetSIGNATURE()
124 {
125     return SIGNATURE;
126 }
127 
GetVersion()128 short ZipEntryHeader::GetVersion()
129 {
130     return m_version;
131 }
132 
SetVersion(short version)133 void ZipEntryHeader::SetVersion(short version)
134 {
135     m_version = version;
136 }
137 
GetFlag()138 short ZipEntryHeader::GetFlag()
139 {
140     return m_flag;
141 }
142 
SetFlag(short flag)143 void ZipEntryHeader::SetFlag(short flag)
144 {
145     m_flag = flag;
146 }
147 
GetMethod()148 short ZipEntryHeader::GetMethod()
149 {
150     return m_method;
151 }
152 
SetMethod(short method)153 void ZipEntryHeader::SetMethod(short method)
154 {
155     m_method = method;
156 }
157 
GetLastTime()158 short ZipEntryHeader::GetLastTime()
159 {
160     return m_lastTime;
161 }
162 
SetLastTime(short lastTime)163 void ZipEntryHeader::SetLastTime(short lastTime)
164 {
165     m_lastTime = lastTime;
166 }
167 
GetLastDate()168 short ZipEntryHeader::GetLastDate()
169 {
170     return m_lastDate;
171 }
172 
SetLastDate(short lastDate)173 void ZipEntryHeader::SetLastDate(short lastDate)
174 {
175     m_lastDate = lastDate;
176 }
177 
GetCrc32()178 int ZipEntryHeader::GetCrc32()
179 {
180     return m_crc32;
181 }
182 
SetCrc32(int crc32)183 void ZipEntryHeader::SetCrc32(int crc32)
184 {
185     m_crc32 = crc32;
186 }
187 
GetCompressedSize()188 uint32_t ZipEntryHeader::GetCompressedSize()
189 {
190     return m_compressedSize;
191 }
192 
SetCompressedSize(uint32_t compressedSize)193 void ZipEntryHeader::SetCompressedSize(uint32_t compressedSize)
194 {
195     m_compressedSize = compressedSize;
196 }
197 
GetUnCompressedSize()198 uint32_t ZipEntryHeader::GetUnCompressedSize()
199 {
200     return m_unCompressedSize;
201 }
202 
SetUnCompressedSize(uint32_t unCompressedSize)203 void ZipEntryHeader::SetUnCompressedSize(uint32_t unCompressedSize)
204 {
205     m_unCompressedSize = unCompressedSize;
206 }
207 
GetFileNameLength()208 uint16_t ZipEntryHeader::GetFileNameLength()
209 {
210     return m_fileNameLength;
211 }
212 
SetFileNameLength(uint16_t fileNameLength)213 void ZipEntryHeader::SetFileNameLength(uint16_t fileNameLength)
214 {
215     m_fileNameLength = fileNameLength;
216 }
217 
GetExtraLength()218 uint16_t ZipEntryHeader::GetExtraLength()
219 {
220     return m_extraLength;
221 }
222 
SetExtraLength(uint16_t extraLength)223 void ZipEntryHeader::SetExtraLength(uint16_t extraLength)
224 {
225     m_extraLength = extraLength;
226 }
227 
GetFileName() const228 std::string ZipEntryHeader::GetFileName() const
229 {
230     return m_fileName;
231 }
232 
SetFileName(const std::string& fileName)233 void ZipEntryHeader::SetFileName(const std::string& fileName)
234 {
235     m_fileName = fileName;
236 }
237 
GetExtraData() const238 std::string ZipEntryHeader::GetExtraData() const
239 {
240     return m_extraData;
241 }
242 
SetExtraData(const std::string& extraData)243 void ZipEntryHeader::SetExtraData(const std::string& extraData)
244 {
245     m_extraData = extraData;
246 }
247 
GetLength()248 uint32_t ZipEntryHeader::GetLength()
249 {
250     return m_length;
251 }
252 
SetLength(uint32_t length)253 void ZipEntryHeader::SetLength(uint32_t length)
254 {
255     m_length = length;
256 }
257 } // namespace SignatureTools
258 } // namespace OHOS