forked from pyinstaller/pyinstaller
/
pyi_archive.c
533 lines (465 loc) · 14.2 KB
/
pyi_archive.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* ****************************************************************************
* Copyright (c) 2013-2021, PyInstaller Development Team.
*
* Distributed under the terms of the GNU General Public License (version 2
* or later) with exception for distributing the bootloader.
*
* The full license is in the file COPYING.txt, distributed with this software.
*
* SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
* ****************************************************************************
*/
/*
* Fuctions related to PyInstaller archive embedded in executable.
*/
#ifdef _WIN32
#if BYTE_ORDER == LITTLE_ENDIAN
#if defined(_MSC_VER)
#include <stdlib.h>
#define pyi_be32toh(x) _byteswap_ulong(x)
#elif defined(__GNUC__) || defined(__clang__)
#define pyi_be32toh(x) __builtin_bswap32(x)
#else
#error Unsupported compiler
#endif
#elif BYTE_ORDER == BIG_ENDIAN
#define pyi_be32toh(x) (x)
#else
#error Unsupported byte order
#endif
#else
#ifdef __FreeBSD__
/* freebsd issue #188316 */
#include <arpa/inet.h> /* ntohl */
#else
#include <netinet/in.h> /* ntohl */
#endif
#define pyi_be32toh(x) ntohl(x)
#include <stdlib.h> /* malloc */
#include <string.h> /* strncmp, strcpy, strcat */
#include <sys/stat.h> /* fchmod */
#endif /* ifdef _WIN32 */
#include <stddef.h> /* ptrdiff_t */
#include <stdio.h>
/* PyInstaller headers. */
#include "zlib.h"
#include "pyi_global.h"
#include "pyi_path.h"
#include "pyi_archive.h"
#include "pyi_utils.h"
#include "pyi_python.h"
int pyvers = 0;
/*
* Return pointer to next toc entry.
*/
TOC *
pyi_arch_increment_toc_ptr(const ARCHIVE_STATUS *status, const TOC* ptoc)
{
TOC *result = (TOC*)((char *)ptoc + ptoc->structlen);
if (result < status->tocbuff) {
FATALERROR("Cannot read Table of Contents.\n");
return status->tocend;
}
return result;
}
/*
* Open archive file if needed
*/
static int
pyi_arch_open_fp(ARCHIVE_STATUS *status)
{
if (status->fp == NULL) {
status->fp = pyi_path_fopen(status->archivename, "rb");
if (status->fp == NULL) {
return -1;
}
}
return 0;
}
/*
* Close archive file
* File should close after unused to avoid locking
*/
static void
pyi_arch_close_fp(ARCHIVE_STATUS *status)
{
if (status->fp != NULL) {
pyi_path_fclose(status->fp);
status->fp = NULL;
}
}
/*
* Decompress data in buff, described by ptoc.
* Return in malloc'ed buffer (needs to be freed)
*/
static unsigned char *
decompress(unsigned char * buff, TOC *ptoc)
{
unsigned char *out;
z_stream zstream;
int rc;
out = (unsigned char *)malloc(ptoc->ulen);
if (out == NULL) {
OTHERERROR("Error allocating decompression buffer\n");
return NULL;
}
zstream.zalloc = NULL;
zstream.zfree = NULL;
zstream.opaque = NULL;
zstream.next_in = buff;
zstream.avail_in = ptoc->len;
zstream.next_out = out;
zstream.avail_out = ptoc->ulen;
rc = inflateInit(&zstream);
if (rc >= 0) {
rc = (inflate)(&zstream, Z_FINISH);
if (rc >= 0) {
rc = (inflateEnd)(&zstream);
}
else {
OTHERERROR("Error %d from inflate: %s\n", rc, zstream.msg);
return NULL;
}
}
else {
OTHERERROR("Error %d from inflateInit: %s\n", rc, zstream.msg);
return NULL;
}
return out;
}
/*
* Extract an archive entry.
* Returns pointer to the data (must be freed).
*/
unsigned char *
pyi_arch_extract(ARCHIVE_STATUS *status, TOC *ptoc)
{
unsigned char *data;
unsigned char *tmp;
if (pyi_arch_open_fp(status) != 0) {
OTHERERROR("Cannot open archive file\n");
return NULL;
}
fseek(status->fp, (long)(status->pkgstart + ptoc->pos), SEEK_SET);
data = (unsigned char *)malloc(ptoc->len);
if (data == NULL) {
OTHERERROR("Could not allocate read buffer\n");
return NULL;
}
if (fread(data, ptoc->len, 1, status->fp) < 1) {
OTHERERROR("Could not read from file\n");
free(data);
return NULL;
}
if (ptoc->cflag == '\1') {
tmp = decompress(data, ptoc);
free(data);
data = tmp;
if (data == NULL) {
OTHERERROR("Error decompressing %s\n", ptoc->name);
return NULL;
}
}
pyi_arch_close_fp(status);
return data;
}
/*
* Extract from the archive and copy to the filesystem.
* The path is relative to the directory the archive is in.
*/
int
pyi_arch_extract2fs(ARCHIVE_STATUS *status, TOC *ptoc)
{
FILE *out;
size_t result, len;
unsigned char *data = pyi_arch_extract(status, ptoc);
/* Create tmp dir _MEIPASSxxx. */
if (pyi_create_temp_path(status) == -1) {
return -1;
}
out = pyi_open_target(status->temppath, ptoc->name);
len = ptoc->ulen;
if (out == NULL) {
FATAL_PERROR("fopen", "%s could not be extracted!\n", ptoc->name);
return -1;
}
else {
result = fwrite(data, len, 1, out);
if ((1 != result) && (len > 0)) {
FATAL_PERROR("fwrite", "Failed to write all bytes for %s\n", ptoc->name);
return -1;
}
#ifndef WIN32
fchmod(fileno(out), S_IRUSR | S_IWUSR | S_IXUSR);
#endif
fclose(out);
}
free(data);
return 0;
}
/*
* Perform full back-to-front scan of the file to search for the
* MAGIC pattern of the embedded archive's COOKIE header.
*
* Returns offset within the file if MAGIC pattern is found, 0 otherwise.
*/
static size_t
_pyi_find_cookie_offset(FILE *fp)
{
static const unsigned char MAGIC[] = { 'M', 'E', 'I', 014, 013, 012, 013, 016 };
static const int SEARCH_CHUNK_SIZE = 8192;
unsigned char *buffer = NULL;
size_t start_pos, end_pos;
size_t offset = 0; /* return value */
/* Allocate the read buffer */
buffer = malloc(SEARCH_CHUNK_SIZE);
if (!buffer) {
VS("LOADER: failed to allocate read buffer (%d bytes)!\n", SEARCH_CHUNK_SIZE);
goto cleanup;
}
/* Determine file size */
if (fseek(fp, 0, SEEK_END) < 0) {
VS("LOADER: failed to seek to the end of the file!\n");
goto cleanup;
}
end_pos = ftell(fp);
/* Sanity check */
if (end_pos < sizeof(MAGIC)) {
VS("LOADER: file is too short!\n");
goto cleanup;
}
/* Search the file back to front, in overlapping SEARCH_CHUNK_SIZE
* chunks. */
do {
size_t chunk_size;
start_pos = (end_pos >= SEARCH_CHUNK_SIZE) ? (end_pos - SEARCH_CHUNK_SIZE) : 0;
chunk_size = end_pos - start_pos;
/* Is the remaining chunk large enough to hold the pattern? */
if (chunk_size < sizeof(MAGIC)) {
break;
}
/* Read the chunk */
if (fseek(fp, start_pos, SEEK_SET) < 0) {
VS("LOADER: failed to seek to the offset 0x%zX!\n", start_pos);
goto cleanup;
}
if (fread(buffer, 1, chunk_size, fp) != chunk_size) {
VS("LOADER: failed to read chunk (%zd bytes)!\n", chunk_size);
goto cleanup;
}
/* Scan the chunk */
for (size_t i = chunk_size - sizeof(MAGIC) + 1; i > 0; i--) {
if (memcmp(buffer + i - 1, MAGIC, sizeof(MAGIC)) == 0) {
offset = start_pos + i - 1;
goto cleanup;
}
}
/* Adjust search location for next chunk; ensure proper overlap */
end_pos = start_pos + sizeof(MAGIC) - 1;
} while (start_pos > 0);
cleanup:
free(buffer);
return offset;
}
/*
* Fix the endianess of fields in the TOC entries.
*/
static void
_pyi_arch_fix_toc_endianess(ARCHIVE_STATUS *status)
{
TOC *ptoc = status->tocbuff;
while (ptoc < status->tocend) {
/* Fixup the current entry */
ptoc->structlen = pyi_be32toh(ptoc->structlen);
ptoc->pos = pyi_be32toh(ptoc->pos);
ptoc->len = pyi_be32toh(ptoc->len);
ptoc->ulen = pyi_be32toh(ptoc->ulen);
/* Jump to next entry; with the current entry fixed up, we can
* use pyi_arch_increment_toc_ptr() */
ptoc = pyi_arch_increment_toc_ptr(status, ptoc);
}
}
/*
* Open the archive.
* Sets f_archiveFile, f_pkgstart, f_tocbuff and f_cookie.
*/
int
pyi_arch_open(ARCHIVE_STATUS *status)
{
size_t cookie_pos = 0;
VS("LOADER: archivename is %s\n", status->archivename);
/* Physically open the file */
if (pyi_arch_open_fp(status) != 0) {
VS("LOADER: Cannot open archive: %s\n", status->archivename);
return -1;
}
/* Search for the embedded archive's cookie */
cookie_pos = _pyi_find_cookie_offset(status->fp);
if (cookie_pos == 0) {
VS("LOADER: Cannot find cookie!\n");
return -1;
}
VS("LOADER: Cookie found at offset 0x%zX\n", cookie_pos);
/* Read the cookie */
if (fseek(status->fp, cookie_pos, SEEK_SET) < 0) {
FATAL_PERROR("fseek", "failed to seek to cookie position.");
return -1;
}
if (fread(&status->cookie, sizeof(COOKIE), 1, status->fp) < 1) {
FATAL_PERROR("fread", "failed to read cookie.");
return -1;
}
/* Fix endianess of COOKIE fields */
status->cookie.len = pyi_be32toh(status->cookie.len);
status->cookie.TOC = pyi_be32toh(status->cookie.TOC);
status->cookie.TOClen = pyi_be32toh(status->cookie.TOClen);
status->cookie.pyvers = pyi_be32toh(status->cookie.pyvers);
/* From the cookie position and declared archive size, calculate
* the archive start position */
status->pkgstart = cookie_pos + sizeof(COOKIE) - status->cookie.len;
/* Set the flag that Python library was not loaded yet. */
status->is_pylib_loaded = false;
/* Set the the Python version used. */
pyvers = pyi_arch_get_pyversion(status);
/* Read in in the table of contents */
fseek(status->fp, (long)(status->pkgstart + status->cookie.TOC), SEEK_SET);
status->tocbuff = (TOC *) malloc(status->cookie.TOClen);
if (status->tocbuff == NULL) {
FATAL_PERROR("malloc", "Could not allocate buffer for TOC.");
return -1;
}
if (fread(status->tocbuff, status->cookie.TOClen, 1, status->fp) < 1) {
FATAL_PERROR("fread", "Could not read from file.");
return -1;
}
status->tocend = (TOC *) (((char *)status->tocbuff) + status->cookie.TOClen);
/* Check input file is still ok (should be). */
if (ferror(status->fp)) {
FATALERROR("Error on file\n.");
return -1;
}
/* Fix the endianess of the fields in the TOC entries */
_pyi_arch_fix_toc_endianess(status);
/* Close file handler
* if file not close here it will be close in pyi_arch_status_free */
pyi_arch_close_fp(status);
return 0;
}
/* Setup the archive with python modules and the paths required by rest of
* this module (this always needs to be done).
* Sets f_archivename, f_homepath, f_mainpath
*/
bool
pyi_arch_setup(ARCHIVE_STATUS *status, char const * archivePath)
{
/* Get the archive Path */
if (strlen(archivePath) >= PATH_MAX) {
// Should never come here, since `archivePath` was already processed
// by pyi_path_executable or pyi_path_archivefile.
return false;
}
strcpy(status->archivename, archivePath);
/* Set homepath to where the archive is */
pyi_path_dirname(status->homepath, archivePath);
/*
* Initial value of mainpath is homepath. It might be overriden
* by temppath if it is available.
*/
status->has_temp_directory = false;
strcpy(status->mainpath, status->homepath);
/* Open the archive */
if (pyi_arch_open(status)) {
/* If this is not an archive, we MUST close the file, */
/* otherwise the open file-handle will be reused when */
/* testing the next file. */
pyi_arch_close_fp(status);
return false;
}
return true;
}
/*
* external API for iterating TOCs
*/
TOC *
getFirstTocEntry(ARCHIVE_STATUS *status)
{
return status->tocbuff;
}
TOC *
getNextTocEntry(ARCHIVE_STATUS *status, TOC *entry)
{
TOC *rslt = (TOC*)((char *)entry + entry->structlen);
if (rslt >= status->tocend) {
return NULL;
}
return rslt;
}
/*
* Helpers for embedders.
*/
int
pyi_arch_get_pyversion(ARCHIVE_STATUS *status)
{
return status->cookie.pyvers;
}
/*
* Allocate memory for archive status.
*/
ARCHIVE_STATUS *
pyi_arch_status_new() {
ARCHIVE_STATUS *archive_status;
archive_status = (ARCHIVE_STATUS *) calloc(1, sizeof(ARCHIVE_STATUS));
if (archive_status == NULL) {
FATAL_PERROR("calloc", "Cannot allocate memory for ARCHIVE_STATUS\n");
}
return archive_status;
}
/*
* Free memory allocated for archive status.
*/
void
pyi_arch_status_free(ARCHIVE_STATUS *archive_status)
{
if (archive_status != NULL) {
VS("LOADER: Freeing archive status for %s\n", archive_status->archivename);
/* Free the TOC memory from the archive status first. */
if (archive_status->tocbuff != NULL) {
free(archive_status->tocbuff);
}
/* Close file handler */
pyi_arch_close_fp(archive_status);
free(archive_status);
}
}
/*
* Returns the value of the pyi bootloader option given by optname. Returns
* NULL if the option is not present. Returns an empty string if the option is present,
* but has no associated value.
*
* The string returned is owned by the ARCHIVE_STATUS; the caller is NOT responsible
* for freeing it.
*/
char *
pyi_arch_get_option(const ARCHIVE_STATUS * status, char * optname)
{
/* TODO: option-cache? */
size_t optlen;
TOC *ptoc = status->tocbuff;
optlen = strlen(optname);
for (; ptoc < status->tocend; ptoc = pyi_arch_increment_toc_ptr(status, ptoc)) {
if (ptoc->typcd == ARCHIVE_ITEM_RUNTIME_OPTION) {
if (0 == strncmp(ptoc->name, optname, optlen)) {
if (0 != ptoc->name[optlen]) {
/* Space separates option name from option value, so add 1. */
return ptoc->name + optlen + 1;
}
else {
/* No option value, just return the empty string. */
return ptoc->name + optlen;
}
}
}
}
return NULL;
}