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