ROSE 2.15.0
Loading...
Searching...
No Matches
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
135 uint16_t compressionMethod() const;
136
141 size_t offset() const;
142
144 uint32_t centralHeaderSize() const;
145
146 // delete constructors
147 FileStat() = delete;
148 FileStat& operator=(const FileStat&) = delete;
149 FileStat(const FileStat&) = default; // Needed to copy into an std::vector
150
151private:
152
153 uint32_t fileIndex_;
154 uint16_t versionMadeBy_;
155 uint16_t versionNeeded_;
156 uint16_t bitFlag_;
157 uint16_t method_; // compression method
158 // time_t time_; // TODO
159 uint16_t time_;
160 uint16_t date_;
161 uint32_t crc32_;
162 uint64_t compSize_;
163 uint64_t uncompSize_;
164 uint16_t fileNameLength_;
165 uint16_t extraFieldLength_;
166 uint16_t commentLength_;
167 uint16_t diskIndex_;
168 uint16_t internalAttr_;
169 uint32_t externalAttr_;
170 uint32_t localHeaderOfs_;
171 char filename_[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
172 char comment_[MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE];
173};
174
175
176struct Zipper {
177 explicit Zipper(SgAsmGenericFile* gf);
178
180 std::string filename() const;
181
183 const std::vector<FileStat> & files() const;
184
185 bool present(const std::string &name) const;
186
188 size_t fileSize(const std::string &name) const;
189
190 size_t offset(const char* file) const;
191
201 unsigned char* decode(const std::string &name, size_t &nbytes);
202
203// TODO: probably need the following?
204// std::map<std::string,uint8_t*> classMap_/classDictionary/classes;
205#if 0
206 const uint8_t* data() const {
207 return buffer_.data();
208 }
209#endif
210
211 // delete constructors
212 Zipper() = delete;
213 Zipper(const Zipper &) = delete;
214 Zipper &operator=(const Zipper&) = delete;
215
216 private:
217 SgAsmGenericFile* gf_;
218 std::vector<uint8_t> buffer_;
219 std::vector<FileStat> files_;
220 std::map<std::string,size_t> offsetMap_;
221
222 // End of central directory (CD) record
223 struct ZipEnd {
224 explicit ZipEnd(const SgFileContentList &buf);
225
226 uint32_t numFiles() const;
227 size_t cdirOffset() const;
228
229 private:
230 uint16_t diskNumber_; // number of this disk
231 uint16_t diskNumberStart_; // number of disk on which CD record starts
232 uint16_t numFiles_; // number of CD entries on this disk
233 uint16_t numFilesTotal_; // number of total CD entries
234 uint32_t sizeCD_; // size of the CD in bytes
235 uint32_t offsetCD_; // offset of the start of the CD
236 uint16_t lenComment_; // length of the comment (followed by the comment)
237
238 // delete constructors
239 ZipEnd() = delete;
240 ZipEnd(const ZipEnd &) = delete;
241 ZipEnd &operator=(const ZipEnd &) = delete;
242
243 }; // ZipEnd
244
245 struct LocalHeader {
246 size_t localHeaderSize() const;
247
248 explicit LocalHeader(const uint8_t* buf, size_t offset);
249
250 private:
251 uint16_t version_;
252 uint16_t bitFlag_;
253 uint16_t method_; // compression method
254 // time_t time_; // TODO
255 uint16_t time_;
256 uint16_t date_;
257 uint32_t crc32_;
258 uint64_t compSize_;
259 uint64_t uncompSize_;
260 uint16_t fileNameLength_;
261 uint16_t extraFieldLength_;
262 char filename_[MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE];
263
264 // delete constructors
265 LocalHeader() = delete;
266 LocalHeader(const LocalHeader&) = delete;
267 LocalHeader& operator=(const LocalHeader&) = delete;
268
269 }; // LocalHeader
270
271}; // Zipper
272
273} // namespace ModulesJvm
274} // namespace Partitioner2
275} // namespace BinaryAnalysis
276} // namespace Rose
277
278#endif // ROSE_ENABLE_BINARY_ANALYSIS
279#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.
uint16_t compressionMethod() const
ZIP compression method used for this file.
std::string filename() const
The file name of the container.
size_t fileSize(const std::string &name) const
Return uncompressed file size for file with the given name.
const std::vector< FileStat > & files() const
Listing of file names in the container.
unsigned char * decode(const std::string &name, size_t &nbytes)
Decode the contents of a file in this zipped container.