-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjar.cpp
183 lines (170 loc) · 5.59 KB
/
jar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Copyright 2010 by Benjamin J. Land (a.k.a. BenLand100)
*
* This file is part of SJVM the Simple Java Virtual Machine.
*
* SJVM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SJVM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SJVM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Jar.h"
#include "Inflate.h"
#include "ClassLoader.h"
#include <iostream>
#include <fstream>
#include <map>
#include <cstring>
//#define DEBUG_OUTPUT
#ifdef DEBUG_OUTPUT
#define debug(v) std::cout << v;
#else
#define debug(v)
#endif
JarFile::JarFile(char* fpath) : path(fpath) {
open();
}
JarFile::~JarFile() {
for (std::map<std::string, FileHeader*>::iterator iter = files.begin(); iter != files.end(); iter++) {
FileHeader *info = iter->second;
if (info->fname_len > 0) delete [] info->file_name;
delete info;
}
files.clear();
}
std::string JarFile::getPath() {
return path;
}
int JarFile::open() {
std::ifstream file(path.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
if (!file.is_open())
return -1;
int size = file.tellg();
unsigned char *data = new unsigned char[size];
file.seekg(0, std::ios::beg);
file.read((char*) data, size);
file.close();
if (*(unsigned int*) data != 0x04034b50) {
return -1;
delete[] data;
}
readCentralDirectory(data, size);
delete[] data;
return 0;
}
void JarFile::readCentralDirectory(unsigned char * data, long len) {
unsigned char * tmp = data;
unsigned long int * tmp2 = (unsigned long int *) tmp;
len -= 4;
while ((*tmp2) != 0x02014b50 && len) {
tmp++;
tmp2 = (unsigned long int *) tmp;
len--;
}
int size;
do {
FileHeader fhdr;
size = readFileHeader(tmp, &fhdr);
if (size) {
if (files.find(fhdr.file_name) == files.end()) {
FileHeader *pfhdr = new FileHeader;
*pfhdr = fhdr;
files[fhdr.file_name] = pfhdr;
} else {
if (fhdr.fname_len > 0) delete [] fhdr.file_name;
}
}
tmp += size;
} while (size != 0);
}
int JarFile::readFileHeader(unsigned char * data, FileHeader * hdr) {
#define U1(buf,o) buf[o]
#define U2(buf,o) (buf[o+1] << 8) | buf[o]
#define U3(buf,o) (buf[o+2] << 16) | (buf[o+1] << 8) | buf[o]
#define U4(buf,o) (buf[o+3] << 24) | (buf[o+2] << 16) | (buf[o+1] << 8) | buf[o]
unsigned char * origdata = data;
std::string m_stData, st_temp;
unsigned int sig = U4(data, 0);
if (sig != 0x02014b50) return 0;
hdr->bitflags = U2(data, 8);
hdr->comp_method = U2(data, 10);
hdr->comp_size = U4(data, 20);
hdr->uncompr_size = U4(data, 24);
hdr->fname_len = U2(data, 28);
int extralen = U2(data, 30);
int commentlen = U2(data, 32);
hdr->relative_offset = U4(data, 42);
data += 46;
if (hdr->fname_len > 0) {
char *fn;
fn = new char[hdr->fname_len + 1];
strncpy(fn, (char*) data, hdr->fname_len);
fn[hdr->fname_len] = '\0';
hdr->file_name = fn;
}
return (data + hdr->fname_len + commentlen + extralen - origdata);
}
int JarFile::request(const char* name, unsigned char *&data) {
std::string sname(name);
sname.append(".class");
if (files.find(sname) == files.end()) {
data = 0;
return -1;
}
FileHeader *header = files[sname];
int offset = header->relative_offset;
int cprSize = header->comp_size;
if (cprSize == 0) {
data = new unsigned char[4]; //Give it something
return 0; //essentially a success
}
int ucprSize = header->uncompr_size;
debug(name << ' ' << offset << ' ' << cprSize << ' ' << ucprSize << '\n');
unsigned char *buffer = new unsigned char[cprSize];
std::ifstream file(path.c_str(), std::ios::in | std::ios::binary);
if (!file.is_open()) {
data = 0;
return -1;
}
debug("grabbing data");
unsigned short int extralen, namelen;
file.seekg(header->relative_offset + 26, std::ios::beg);
file.read((char*) & namelen, 2);
file.read((char*) & extralen, 2);
debug(' ' << extralen << ' ' << namelen << '\n');
file.seekg(namelen + extralen, std::ios::cur);
file.read((char*) buffer, cprSize);
file.close();
switch (header->comp_method) {
case COMPRESSION_STORED:
data = buffer;
return 0;
case COMPRESSION_DEFLATED:
data = new unsigned char[ucprSize];
if (inflate_oneshot(buffer, cprSize, data, ucprSize) == 0) {
delete [] buffer;
return 0;
}
case COMPRESSION_SHRUNK:
case COMPRESSION_REDUCED1:
case COMPRESSION_REDUCED2:
case COMPRESSION_REDUCED3:
case COMPRESSION_REDUCED4:
case COMPRESSION_IMPLODED:
case COMPRESSION_TOKENIZED:
default:
delete [] buffer;
return -1;
}
}
void JarFile::release(unsigned char *data) {
delete[] data;
}