ROSE 0.11.145.147
ModulesJvm.h
1#ifndef ROSE_BinaryAnalysis_Partitioner2_ModulesJvm_H
2#define ROSE_BinaryAnalysis_Partitioner2_ModulesJvm_H
3#include <featureTests.h>
4#ifdef ROSE_ENABLE_BINARY_ANALYSIS
5
6#include <Rose/BinaryAnalysis/Partitioner2/BasicTypes.h>
7#include <Rose/BinaryAnalysis/Partitioner2/Modules.h>
8#include <sageContainer.h>
9
10#include <iostream>
11#include <map>
12
13namespace Rose {
14namespace BinaryAnalysis {
15namespace Partitioner2 {
16
18namespace ModulesJvm {
19
21
22void warn(const std::string &msg);
23
24//-------------------------------------------------------------------------------------
25// The following borrowed largely verbatim (starting with license)
26//
27
28// Copyright (c) 2014-2017 Thomas Fussell
29//
30// Permission is hereby granted, free of charge, to any person obtaining a copy
31// of this software and associated documentation files (the "Software"), to deal
32// in the Software without restriction, including without limitation the rights
33// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34// copies of the Software, and to permit persons to whom the Software is
35// furnished to do so, subject to the following conditions:
36//
37// The above copyright notice and this permission notice shall be included in
38// all copies or substantial portions of the Software.
39//
40// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
43// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, WRISING FROM,
45// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46// THE SOFTWARE
47//
48// @license: http://www.opensource.org/licenses/mit-license.php
49
50
51// Various ZIP archive enums. To completely avoid cross platform compiler alignment and platform endian issues, miniz.c doesn't use structs for any of this stuff.
52enum {
53 // ZIP archive identifiers and record sizes
54 MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50,
55 MZ_ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50,
56 MZ_ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50,
57 MZ_ZIP_LOCAL_DIR_HEADER_SIZE = 30,
58 MZ_ZIP_CENTRAL_DIR_HEADER_SIZE = 46,
59 MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22,
60 // Central directory header record offsets
61 MZ_ZIP_CDH_SIG_OFS = 0,
62 MZ_ZIP_CDH_VERSION_MADE_BY_OFS = 4,
63 MZ_ZIP_CDH_VERSION_NEEDED_OFS = 6,
64 MZ_ZIP_CDH_BIT_FLAG_OFS = 8,
65 MZ_ZIP_CDH_METHOD_OFS = 10,
66 MZ_ZIP_CDH_FILE_TIME_OFS = 12,
67 MZ_ZIP_CDH_FILE_DATE_OFS = 14,
68 MZ_ZIP_CDH_CRC32_OFS = 16,
69 MZ_ZIP_CDH_COMPRESSED_SIZE_OFS = 20,
70 MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24,
71 MZ_ZIP_CDH_FILENAME_LEN_OFS = 28,
72 MZ_ZIP_CDH_EXTRA_LEN_OFS = 30,
73 MZ_ZIP_CDH_COMMENT_LEN_OFS = 32,
74 MZ_ZIP_CDH_DISK_START_OFS = 34,
75 MZ_ZIP_CDH_INTERNAL_ATTR_OFS = 36,
76 MZ_ZIP_CDH_EXTERNAL_ATTR_OFS = 38,
77 MZ_ZIP_CDH_LOCAL_HEADER_OFS = 42,
78 // Local directory header offsets
79 MZ_ZIP_LDH_SIG_OFS = 0,
80 MZ_ZIP_LDH_VERSION_NEEDED_OFS = 4,
81 MZ_ZIP_LDH_BIT_FLAG_OFS = 6,
82 MZ_ZIP_LDH_METHOD_OFS = 8,
83 MZ_ZIP_LDH_FILE_TIME_OFS = 10,
84 MZ_ZIP_LDH_FILE_DATE_OFS = 12,
85 MZ_ZIP_LDH_CRC32_OFS = 14,
86 MZ_ZIP_LDH_COMPRESSED_SIZE_OFS = 18,
87 MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS = 22,
88 MZ_ZIP_LDH_FILENAME_LEN_OFS = 26,
89 MZ_ZIP_LDH_EXTRA_LEN_OFS = 28,
90 // End of central directory offsets
91 MZ_ZIP_ECDH_SIG_OFS = 0,
92 MZ_ZIP_ECDH_NUM_THIS_DISK_OFS = 4,
93 MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS = 6,
94 MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8,
95 MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10,
96 MZ_ZIP_ECDH_CDIR_SIZE_OFS = 12,
97 MZ_ZIP_ECDH_CDIR_OFS_OFS = 16,
98 MZ_ZIP_ECDH_COMMENT_SIZE_OFS = 20
99};
100
101enum {
102 MZ_ZIP_MAX_IO_BUF_SIZE = 64*1024,
103 MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE = 260,
104 MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE = 256
105};
106//-------------------------------------------------------------------------------------
107
108
114struct FileStat {
115
123 explicit FileStat(const uint8_t* buf, size_t &offset, uint32_t fileIndex);
124
126 std::string filename() const;
127
129 size_t compressedSize() const;
130
132 size_t uncompressedSize() const;
133
138 size_t offset() const;
139
141 uint32_t centralHeaderSize() const;
142
143 // delete constructors
144 FileStat() = delete;
145 FileStat& operator=(const FileStat&) = delete;
146 // Needed to copy into an std::vector
147 // FileStat(const FileStat&) = delete;
148
149private:
150
151 uint32_t fileIndex_;
152 uint16_t versionMadeBy_;
153 uint16_t versionNeeded_;
154 uint16_t bitFlag_;
155 uint16_t method_; // compression method
156 // time_t time_; // TODO
157 uint16_t time_;
158 uint16_t date_;
159 uint32_t crc32_;
160 uint64_t compSize_;
161 uint64_t uncompSize_;
162 uint16_t fileNameLength_;
163 uint16_t extraFieldLength_;
164 uint16_t commentLength_;
165 uint16_t diskIndex_;
166 uint16_t internalAttr_;
167 uint32_t externalAttr_;
168 uint32_t localHeaderOfs_;
169 char filename_[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
170 char comment_[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
171};
172
173
174struct Zipper {
175
176 explicit Zipper(SgAsmGenericFile* gf);
177
178 size_t fileSize(const std::string &name) const;
179 size_t offset(const char* file) const;
180
181// TODO: probably need the following?
182// std::map<std::string,uint8_t*> classMap_/classDictionary/classes;
183#if 0
184 const uint8_t* data() const {
185 return buffer_.data();
186 }
187#endif
188
189 // delete constructors
190 Zipper() = delete;
191 Zipper(const Zipper &) = delete;
192 Zipper &operator=(const Zipper&) = delete;
193
194 private:
195 SgAsmGenericFile* gf_;
196 std::vector<uint8_t> buffer_;
197 std::vector<FileStat> files_;
198 std::map<std::string,size_t> offsetMap_;
199
200 // End of central directory (CD) record
201 struct ZipEnd {
202 explicit ZipEnd(const SgFileContentList &buf);
203
204 uint32_t numFiles() const;
205 size_t cdirOffset() const;
206
207 private:
208 uint16_t diskNumber_; // number of this disk
209 uint16_t diskNumberStart_; // number of disk on which CD record starts
210 uint16_t numFiles_; // number of CD entries on this disk
211 uint16_t numFilesTotal_; // number of total CD entries
212 uint32_t sizeCD_; // size of the CD in bytes
213 uint32_t offsetCD_; // offset of the start of the CD
214 uint16_t lenComment_; // length of the comment (followed by the comment)
215
216 // delete constructors
217 ZipEnd() = delete;
218 ZipEnd(const ZipEnd &) = delete;
219 ZipEnd &operator=(const ZipEnd &) = delete;
220
221 }; // ZipEnd
222
223 struct LocalHeader {
224 size_t localHeaderSize() const;
225
226 explicit LocalHeader(const uint8_t* buf, size_t offset);
227
228 private:
229 uint16_t version_;
230 uint16_t bitFlag_;
231 uint16_t method_; // compression method
232 // time_t time_; // TODO
233 uint16_t time_;
234 uint16_t date_;
235 uint32_t crc32_;
236 uint64_t compSize_;
237 uint64_t uncompSize_;
238 uint16_t fileNameLength_;
239 uint16_t extraFieldLength_;
240 char filename_[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
241
242 // delete constructors
243 LocalHeader() = delete;
244 LocalHeader(const LocalHeader&) = delete;
245 LocalHeader& operator=(const LocalHeader&) = delete;
246
247 }; // LocalHeader
248
249}; // Zipper
250
251} // namespace ModulesJvm
252} // namespace Partitioner2
253} // namespace BinaryAnalysis
254} // namespace Rose
255
256#endif // ROSE_ENABLE_BINARY_ANALYSIS
257#endif // ROSE_BinaryAnalysis_Partitioner2_ModulesJvm_H
Base class for binary files.
std::enable_if< std::is_integral< T >::value, T >::type leToHost(const T &x)
Convert a little-endian integer to host order.
Definition ByteOrder.h:44
The ROSE library.
This class represents the contents of a central directory file header in a zip file.
Definition ModulesJvm.h:114
size_t offset() const
Offset (in bytes) of the local headers.
size_t uncompressedSize() const
Uncompressed size of the file (bytes)
uint32_t centralHeaderSize() const
Size (in bytes) of the central directory header.
std::string filename() const
The name of the file.
size_t compressedSize() const
Compressed size of the file (bytes)
FileStat(const uint8_t *buf, size_t &offset, uint32_t fileIndex)
Constructor.