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_CENTRAL_DIRECTORY_H 17 #define SIGNATRUETOOLS_CENTRAL_DIRECTORY_H 18 19 #include <string> 20 21 #include "byte_buffer.h" 22 23 namespace OHOS { 24 namespace SignatureTools { 25 /** 26 * resolve zip CentralDirectory data 27 * CentralDirectory format for: 28 * central file header signature 4 bytes (0x02014b50) 29 * version made by 2 bytes 30 * version needed to extract 2 bytes 31 * general purpose bit flag 2 bytes 32 * compression method 2 bytes 33 * last mod file time 2 bytes 34 * last mod file date 2 bytes 35 * crc-32 4 bytes 36 * compressed size 4 bytes 37 * uncompressed size 4 bytes 38 * file name length 2 bytes 39 * extra field length 2 bytes 40 * file comment length 2 bytes 41 * disk number start 2 bytes 42 * internal file attributes 2 bytes 43 * external file attributes 4 bytes 44 * relative offset of local header 4 bytes 45 * file name (variable size) 46 * extra field (variable size) 47 * file comment (variable size) 48 */ 49 class CentralDirectory { 50 public: 51 // central directory invariable bytes length 52 static constexpr int CD_LENGTH = 46; 53 54 // 4 bytes, central directory signature 55 static constexpr int SIGNATURE = 0x02014b50; 56 57 static bool GetCentralDirectory(ByteBuffer& bf, CentralDirectory* cd); 58 59 std::string ToBytes(); 60 61 static int GetCdLength(); 62 63 static int GetSIGNATURE(); 64 65 short GetVersion(); 66 67 void SetVersion(short version); 68 69 short GetVersionExtra(); 70 71 void SetVersionExtra(short versionExtra); 72 73 short GetMethod(); 74 75 void SetMethod(short method); 76 77 short GetFlag(); 78 79 void SetFlag(short flag); 80 81 int GetCrc32(); 82 83 void SetCrc32(int crc32); 84 85 short GetLastTime(); 86 87 void SetLastTime(short lastTime); 88 89 short GetLastDate(); 90 91 void SetLastDate(short lastDate); 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 GetExtraLength(); 102 103 void SetExtraLength(uint16_t extraLength); 104 105 uint16_t GetFileNameLength(); 106 107 void SetFileNameLength(uint16_t fileNameLength); 108 109 uint16_t GetDiskNumStart(); 110 111 void SetDiskNumStart(uint16_t diskNumStart); 112 113 uint16_t GetCommentLength(); 114 115 void SetCommentLength(uint16_t commentLength); 116 117 short GetInternalFile(); 118 119 void SetInternalFile(short internalFile); 120 121 int GetExternalFile(); 122 123 void SetExternalFile(int externalFile); 124 125 uint32_t GetOffset(); 126 127 void SetOffset(uint32_t offset); 128 129 std::string GetFileName(); 130 131 void SetFileName(const std::string& fileName); 132 133 std::string GetExtraData() const; 134 135 void SetExtraData(const std::string& extraData); 136 137 std::string GetComment(); 138 139 void SetComment(const std::string& comment); 140 141 uint32_t GetLength(); 142 143 void SetLength(uint32_t length); 144 145 private: 146 static void SetCentralDirectoryValues(ByteBuffer& bf, CentralDirectory* cd); 147 148 /* 2 bytes */ 149 short m_version = 0; 150 151 /* 2 bytes */ 152 short m_versionExtra = 0; 153 154 /* 2 bytes */ 155 short m_flag = 0; 156 157 /* 2 bytes */ 158 short m_method = 0; 159 160 /* 2 bytes */ 161 short m_lastTime = 0; 162 163 /* 2 bytes */ 164 short m_lastDate = 0; 165 166 /* 4 bytes */ 167 int m_crc32 = 0; 168 169 /* 4 bytes */ 170 uint32_t m_compressedSize = 0; 171 172 /* 4 bytes */ 173 uint32_t m_unCompressedSize = 0; 174 175 /* 2 bytes */ 176 uint16_t m_fileNameLength = 0; 177 178 /* 2 bytes */ 179 uint16_t m_extraLength = 0; 180 181 /* 2 bytes */ 182 uint16_t m_commentLength = 0; 183 184 /* 2 bytes */ 185 uint16_t m_diskNumStart = 0; 186 187 /* 2 bytes */ 188 short m_internalFile = 0; 189 190 /* 4 bytes */ 191 int m_externalFile = 0; 192 193 /* 4 bytes */ 194 uint32_t m_offset = 0; 195 196 /* n bytes */ 197 std::string m_fileName; 198 199 /* n bytes */ 200 std::string m_extraData; 201 202 /* n bytes */ 203 std::string m_comment; 204 205 uint32_t m_length = 0; 206 }; 207 } // namespace SignatureTools 208 } // namespace OHOS 209 #endif // SIGNATRUETOOLS_CENTRAL_DIRECTORY_H