-
Notifications
You must be signed in to change notification settings - Fork 71
/
helper.c
276 lines (233 loc) · 6.68 KB
/
helper.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#define _BSD_SOURCE 1
#define _DEFAULT_SOURCE 1
#include "internal.h"
#include "log.h"
#include "convert_utf/ConvertUTF.h"
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#ifdef _WIN32
#define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
#include <direct.h>
#ifndef PATH_MAX
#define PATH_MAX _MAX_PATH
#endif
#else
#include <limits.h>
#endif
#define VERBOSE 0
#if defined(_MSC_VER)
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif
long int unshield_get_path_max(Unshield* unshield)
{
#ifdef PATH_MAX
return PATH_MAX;
#else
long int path_max = pathconf(unshield->filename_pattern, _PC_PATH_MAX);
if (path_max <= 0)
path_max = 4096;
return path_max;
#endif
}
char *unshield_get_base_directory_name(Unshield *unshield) {
long int path_max = unshield_get_path_max(unshield);
char *p = unshield_get_last_path_separator(unshield->filename_pattern);
char *dirname = malloc(path_max);
if (p) {
strncpy(dirname, unshield->filename_pattern, path_max);
if ((unsigned int) (p - unshield->filename_pattern) > path_max) {
dirname[path_max - 1] = 0;
} else
dirname[(p - unshield->filename_pattern)] = 0;
} else
strcpy(dirname, ".");
return dirname;
}
static char* get_filename(Unshield* unshield, int index, const char* suffix) {
if (unshield && unshield->filename_pattern)
{
long path_max = unshield_get_path_max(unshield);
char* filename = malloc(path_max);
if (filename == NULL) {
unshield_error("Unable to allocate memory.\n");
goto exit;
}
if (snprintf(filename, path_max, unshield->filename_pattern, index, suffix) >= path_max) {
unshield_error("Pathname exceeds system limits.\n");
goto exit;
}
exit:
return filename;
}
return NULL;
}
FILE* unshield_fopen_for_reading(Unshield* unshield, int index, const char* suffix)
{
if (unshield && unshield->filename_pattern)
{
FILE* result = NULL;
char* filename = get_filename(unshield, index, suffix);
char* dirname = unshield_get_base_directory_name(unshield);
const char *q;
struct dirent *dent = NULL;
DIR *sourcedir = NULL;
long int path_max = unshield_get_path_max(unshield);
q=unshield_get_last_path_separator(filename);
if (q)
q++;
else
q=filename;
sourcedir = unshield_opendir(unshield, dirname);
/* Search for the File case independent */
if (sourcedir)
{
for (dent=unshield_readdir(unshield, sourcedir);dent;
dent=unshield_readdir(unshield, sourcedir))
{
if (!(strcasecmp(q, dent->d_name)))
{
/*unshield_trace("Found match %s\n",dent->d_name);*/
break;
}
}
if (dent == NULL)
{
unshield_trace("File %s not found even case insensitive\n",filename);
goto exit;
}
else
if(snprintf(filename, path_max, "%s/%s", dirname, dent->d_name)>=path_max)
{
unshield_error("Pathname exceeds system limits.\n");
goto exit;
}
}
else
unshield_trace("Could not open directory %s error %s\n", dirname, strerror(errno));
#if VERBOSE
unshield_trace("Opening file '%s'", filename);
#endif
result = unshield_fopen(unshield, filename, "rb");
exit:
if (sourcedir)
unshield_closedir(unshield, sourcedir);
free(filename);
free(dirname);
return result;
}
return NULL;
}
long long unshield_fsize(Unshield* unshield, FILE* file)
{
long long result;
long long previous = unshield_ftell(unshield, file);
unshield_fseek(unshield, file, 0L, SEEK_END);
result = unshield_ftell(unshield, file);
unshield_fseek(unshield, file, previous, SEEK_SET);
return result;
}
bool unshield_read_common_header(uint8_t** buffer, CommonHeader* common)
{
uint8_t* p = *buffer;
common->signature = READ_UINT32(p); p += 4;
if (CAB_SIGNATURE != common->signature)
{
unshield_error("Invalid file signature");
if (MSCF_SIGNATURE == common->signature)
unshield_warning("Found Microsoft Cabinet header. Use cabextract (https://www.cabextract.org.uk/) to unpack this file.");
return false;
}
common->version = READ_UINT32(p); p += 4;
common->volume_info = READ_UINT32(p); p += 4;
common->cab_descriptor_offset = READ_UINT32(p); p += 4;
common->cab_descriptor_size = READ_UINT32(p); p += 4;
#if VERBOSE
unshield_trace("Common header: %08x %08x %08x %08x",
common->version,
common->volume_info,
common->cab_descriptor_offset,
common->cab_descriptor_size);
#endif
*buffer = p;
return true;
}
/**
Get pointer at cab descriptor + offset
*/
uint8_t* unshield_header_get_buffer(Header* header, uint32_t offset)
{
if (offset)
return
header->data +
header->common.cab_descriptor_offset +
offset;
else
return NULL;
}
/**
Returns the last path separator in a filesystem path
*/
char *unshield_get_last_path_separator(char *path)
{
char *p = strrchr(path, '/');
#ifdef WIN32
char *pbs = strrchr(path, '\\');
if (NULL != pbs && (NULL == p || pbs > p))
return pbs;
#endif
return p;
}
static int unshield_strlen_utf16(const uint16_t* utf16)
{
const uint16_t* current = utf16;
while (*current++)
;
return current - utf16;
}
static StringBuffer* unshield_add_string_buffer(Header* header)
{
StringBuffer* result = NEW1(StringBuffer);
result->next = header->string_buffer;
return header->string_buffer = result;
}
static const char* unshield_utf16_to_utf8(Header* header, const uint16_t* utf16)
{
StringBuffer* string_buffer = unshield_add_string_buffer(header);
int length = unshield_strlen_utf16(utf16);
int buffer_size = 3 * length + 1;
char* target = string_buffer->string = NEW(char, buffer_size);
ConversionResult result = ConvertUTF16toUTF8(
(const UTF16**)&utf16, utf16 + length + 1,
(UTF8**)&target, (UTF8*)(target + buffer_size), lenientConversion);
if (result != conversionOK)
{
/* fail fast */
abort();
}
return string_buffer->string;
}
const char* unshield_get_utf8_string(Header* header, const void* buffer)
{
if (header->major_version >= 17 && buffer != NULL)
{
return unshield_utf16_to_utf8(header, (const uint16_t*)buffer);
}
else
{
return (const char*)buffer;
}
}
/**
Get string at cab descriptor offset + string offset
*/
const char* unshield_header_get_string(Header* header, uint32_t offset)
{
return unshield_get_utf8_string(header, unshield_header_get_buffer(header, offset));
}