-
Notifications
You must be signed in to change notification settings - Fork 29
/
ps2-packer.c
767 lines (643 loc) · 19.3 KB
/
ps2-packer.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*
* PS2-Packer
* Copyright (C) 2004-2005 Nicolas "Pixel" Noble
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <stdarg.h>
#include "common.h"
#ifndef PS2_PACKER_LITE
#include "dlopen.h"
#endif
#if defined(_WIN32) || defined(__CYGWIN__)
#define SUFFIX ".dll"
#elif defined (__APPLE__)
#define SUFFIX ".dylib"
#else
#define SUFFIX ".so"
#endif
#ifdef _WIN32
#define snprintf(buffer, size, args...) sprintf(buffer, args)
#endif
#ifdef __MINGW32__
#include <windows.h>
#endif
/* These global variables should contain the data about the loaded stub */
u8 * stub_section;
u32 * stub_data;
u32 stub_pc; /* Starting point */
u32 stub_base; /* Loading address */
u32 stub_size; /* Size of the stub section */
u32 stub_zero; /* Number of bytes to zero-pad at the end of the section */
u32 stub_signature; /* packer signature located in the stub */
u8 alternative = 0; /* boolean for alternative loading method */
u32 alignment = 0x10;
/*
This is the pointer in our output elf file.
*/
u32 data_pointer;
struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"base", 1, NULL, 'b'},
#ifndef PS2_PACKER_LITE
{"packer", 1, NULL, 'p'},
{"stub", 1, NULL, 's'},
#endif
{"align", 1, NULL, 'a'},
{"verbose", 1, NULL, 'v'},
{0, 0, NULL, 0 },
};
void printe(char * fmt, ...) {
va_list list;
va_start(list, fmt);
vfprintf(stderr, fmt, list);
va_end(list);
exit(-1);
}
int verbose = 0;
void printv(char * fmt, ...) {
va_list list;
if (!verbose)
return;
va_start(list, fmt);
vfprintf(stdout, fmt, list);
va_end(list);
}
#ifndef PS2_PACKER_LITE
typedef int (*pack_section_t)(const u8 * source, u8 ** dest, u32 source_size);
pack_section_t ppack_section;
typedef u32 (*signature_t)();
signature_t signature;
#endif
u8 bigendian = 0;
u32 ELF_MAGIC = 0x464c457f;
#define PT_LOAD 1
#define PF_X 1
#define PF_W 2
#define PF_R 4
#define SWAP16(x) { if (bigendian) x = (x >> 8) | (x << 8); }
#define SWAP32(x) { if (bigendian) x = (x >> 24) | ((x >> 8) & 0x0000ff00) | ((x << 8) & 0x00ff0000) | (x << 24); }
/* The primary ELF header. */
typedef struct {
u8 ident[16]; /* The first 4 bytes are the ELF magic */
u16 type; /* == 2, EXEC (executable file) */
u16 machine; /* == 8, MIPS r3000 */
u32 version; /* == 1, default ELF value */
u32 entry; /* program starting point */
u32 phoff; /* program header offset in the file */
u32 shoff; /* section header offset in the file, unused for us, so == 0 */
u32 flags; /* flags, unused for us. */
u16 ehsize; /* this header size ( == 52 ) */
u16 phentsize; /* size of a program header ( == 32 ) */
u16 phnum; /* number of program headers */
u16 shentsize; /* size of a section header, unused here */
u16 shnum; /* number of section headers, unused here */
u16 shstrndx; /* section index of the string table */
} elf_header_t;
#define SWAP_ELF_HEADER(x) { \
SWAP16(x.type); \
SWAP16(x.machine); \
SWAP32(x.version); \
SWAP32(x.entry); \
SWAP32(x.phoff); \
SWAP32(x.shoff); \
SWAP32(x.flags); \
SWAP16(x.ehsize); \
SWAP16(x.phentsize); \
SWAP16(x.phnum); \
SWAP16(x.shentsize); \
SWAP16(x.shnum); \
SWAP16(x.shstrndx); \
}
typedef struct {
u32 type; /* == 1, PT_LOAD (that is, this section will get loaded */
u32 offset; /* offset in file, on a 4096 bytes boundary */
u32 vaddr; /* virtual address where this section is loaded */
u32 paddr; /* physical address where this section is loaded */
u32 filesz; /* size of that section in the file */
u32 memsz; /* size of that section in memory (rest is zero filled) */
u32 flags; /* PF_X | PF_W | PF_R, that is executable, writable, readable */
u32 align; /* == 0x1000 that is 4096 bytes */
} elf_pheader_t;
#define SWAP_ELF_PHEADER(x) { \
SWAP32(x.type); \
SWAP32(x.offset); \
SWAP32(x.vaddr); \
SWAP32(x.paddr); \
SWAP32(x.filesz); \
SWAP32(x.memsz); \
SWAP32(x.flags); \
SWAP32(x.align); \
}
typedef struct {
u32 entryAddr;
u32 numSections;
} packed_Header;
#define SWAP_PACKED_HEADER(x) { \
SWAP32(x.entryAddr); \
SWAP32(x.numSections); \
}
typedef struct {
u32 originalSize;
u32 zeroByteSize;
u32 virtualAddr;
u32 compressedSize;
} packed_SectionHeader;
#define SWAP_PACKED_SECTION_HEADER(x) { \
SWAP32(x.compressedSize); \
SWAP32(x.originalSize); \
SWAP32(x.zeroByteSize); \
SWAP32(x.virtualAddr); \
}
void sanity_checks() {
u32 t1 = 0x12345678;
u8 * t2 = (u8 *) &t1;
if (t2[0] == 0x12) {
bigendian = 1;
SWAP32(ELF_MAGIC);
} else if (t2[0] != 0x78) {
printe("Error computing machine's endianess!\n");
}
if (sizeof(u8) != 1) {
printv("Error: sizeof(u8) != 1\n");
}
if (sizeof(u16) != 2) {
printv("Error: sizeof(u16) != 2\n");
}
if (sizeof(u32) != 4) {
printv("Error: sizeof(u32) != 4\n");
}
}
void show_banner() {
printf(
"PS2-Packer v" VERSION " (C) 2004-2005 Nicolas \"Pixel\" Noble\n"
"This is free software with ABSOLUTELY NO WARRANTY.\n"
"\n"
);
}
void show_usage() {
printf(
"Usage: ps2-packer [-h] [-v] [-a X] [-b X] "
#ifndef PS2_PACKER_LITE
"[-p X] [-s X] "
#endif
"<in_elf> <out_elf>\n"
" -h show this help.\n"
" -v verbose mode.\n"
" -b base sets the loading base of the compressed data. When activated\n"
" it will activate the alternative packing way.\n"
#ifndef PS2_PACKER_LITE
" -p packer sets a packer name. lzma by default.\n"
" -s stub sets another uncruncher stub. stub/lzma-1d00-stub by default,\n"
" or stub/lzma-0088-stub when using alternative packing.\n"
#endif
" -a align sets section alignment. 16 by default. Any value accepted.\n"
);
}
/* The default ELF ident field */
u8 ident[] = {
0x7F, 0x45, 0x4C, 0x46, 0x01, 0x01, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void remove_section_zeroes(u8 * section, u32 * section_size, u32 * zeroes) {
u32 removed = 0;
while (!section[*section_size - 1 - removed]) {
removed++;
if (*section_size == removed)
break;
}
#if 0
u32 whole_size = *section_size + *zeroes;
u32 realign = 0;
if (whole_size & (alignment - 1)) {
realign = whole_size + alignment;
realign &= -alignment;
realign = whole_size - realign;
removed -= realign;
if (removed < 0)
removed = 0;
}
#endif
printv("Removing %i zeroes to section...\n", removed);
*section_size -= removed;
*zeroes += removed;
}
void realign_data_pointer() {
if (data_pointer & (alignment - 1)) {
data_pointer += alignment;
data_pointer &= -alignment;
}
}
#ifdef PS2_PACKER_LITE
extern u8 builtin_stub[];
#endif
/* Loads the stub file in memory, filling up the global variables */
void load_stub(
#ifndef PS2_PACKER_LITE
FILE * stub
#endif
) {
u8 * loadbuf, * pdata;
int i;
elf_header_t *eh = 0;
elf_pheader_t *eph = 0;
int loaded = 0;
#ifndef PS2_PACKER_LITE
int size;
fseek(stub, 0, SEEK_END);
size = ftell(stub);
fseek(stub, 0, SEEK_SET);
loadbuf = (u8 *) malloc(size);
if (fread(loadbuf, 1, size, stub) != size) {
printe("fread error\n");
}
#else
loadbuf = builtin_stub;
#endif
eh = (elf_header_t *)loadbuf;
SWAP_ELF_HEADER((*eh));
if (memcmp(eh->ident, &ELF_MAGIC, sizeof(ELF_MAGIC)) != 0) {
printe("This ain't no ELF file\n");
}
stub_pc = eh->entry;
printv("Stub PC = %08X\n", stub_pc);
/* Parsing the interesting program headers */
eph = (elf_pheader_t *)(loadbuf + eh->phoff);
for (i = 0; i < eh->phnum; i++) {
SWAP_ELF_PHEADER(eph[i]);
if ((eph[i].type != PT_LOAD) || (eph[i].filesz == 0))
continue;
pdata = (loadbuf + eph[i].offset);
stub_size = eph[i].filesz;
stub_section = (u8 *) malloc(eph[i].filesz);
memcpy(stub_section, pdata, eph[i].filesz);
stub_base = eph[i].vaddr;
stub_zero = eph[i].memsz - eph[i].filesz;
loaded = 1;
break;
}
if (!loaded)
printe("Unable to load stub file.\n");
stub_data = (u32 *) (stub_section + stub_pc - stub_base - (2+5)*4); //get the address of Signature within the stub.
stub_signature = stub_data[0];
SWAP32(stub_signature);
remove_section_zeroes(stub_section, &stub_size, &stub_zero);
printv("Loaded stub: %08X bytes (with %08X zeroes) based at %08X\n", stub_size, stub_zero, stub_base);
#ifndef PS2_PACKER_LITE
free(loadbuf);
#endif
}
/* Write out the basic elf structures, that is, the ELF header,
the first program section, and the stub. Updates the global pointer
data_pointer in order to let the packing routine to know where to put
its compressed data */
void prepare_out(FILE * out, u32 base) {
elf_header_t eh;
elf_pheader_t eph;
int i;
for (i = 0; i < 16; i++) {
eh.ident[i] = ident[i];
}
eh.type = 2;
eh.machine = 8;
eh.version = 1;
eh.phoff = sizeof(eh);
eh.shoff = 0;
eh.flags = 0;
eh.ehsize = sizeof(eh);
eh.phentsize = sizeof(elf_pheader_t);
eh.phnum = alternative ? 2 : 1;
eh.shoff = 0;
eh.shnum = 0;
eh.shentsize = 0;
eh.entry = stub_pc;
eh.shstrndx = 0;
SWAP_ELF_HEADER(eh);
if (fwrite(&eh, 1, sizeof(eh), out) != sizeof(eh)) {
printe("Error writing elf header\n");
}
if (!alternative) {
data_pointer = sizeof(eh) + sizeof(eph);
realign_data_pointer();
return;
}
data_pointer = sizeof(eh) + 2 * sizeof(eph);
realign_data_pointer();
eph.type = PT_LOAD;
eph.flags = PF_R | PF_X;
eph.offset = data_pointer;
eph.vaddr = stub_base;
eph.paddr = stub_base;
eph.filesz = stub_size;
eph.memsz = stub_size + stub_zero;
eph.align = alignment;
SWAP_ELF_PHEADER(eph);
if (fwrite(&eph, 1, sizeof(eph), out) != sizeof(eph)) {
printe("Error writing stub program header\n");
}
fseek(out, data_pointer, SEEK_SET);
SWAP32(base);
stub_data[1] = base;
if (fwrite(stub_section, 1, stub_size, out) != stub_size) {
printe("Error writing stub\n");
}
data_pointer += stub_size;
realign_data_pointer();
printv("Actual pointer in file = %08X\n", data_pointer);
}
/* Will produce the second program section of the elf, by packing all the
program headers of the input file */
void packing(FILE * out, FILE * in, u32 base) {
u8 * loadbuf, * pdata, * packed = 0;
int size, packed_size = 0;
u32 section_size;
int i;
elf_header_t *eh = 0;
elf_pheader_t *eph = 0, weph;
packed_Header ph;
packed_SectionHeader psh;
char zero = 0;
/* preparing the output program header for that section */
weph.type = PT_LOAD;
weph.flags = PF_R | PF_W;
if (!alternative)
weph.flags |= PF_X;
weph.offset = data_pointer;
if (alternative) {
weph.vaddr = base;
weph.paddr = base;
}
weph.align = alignment;
fseek(in, 0, SEEK_END);
size = ftell(in);
fseek(in, 0, SEEK_SET);
loadbuf = (u8 *) malloc(size);
if (fread(loadbuf, 1, size, in) != size) {
printe("fread error\n");
}
eh = (elf_header_t *)loadbuf;
SWAP_ELF_HEADER((*eh));
if (memcmp(eh->ident, &ELF_MAGIC, sizeof(ELF_MAGIC)) != 0) {
printe("This ain't no ELF file\n");
}
ph.entryAddr = eh->entry;
ph.numSections = 0;
printv("ELF PC = %08X\n", ph.entryAddr);
eph = (elf_pheader_t *)(loadbuf + eh->phoff);
/* counting and swapping the program headers of the input file */
for (i = 0; i < eh->phnum; i++) {
SWAP_ELF_PHEADER(eph[i]);
if ((eph[i].type != PT_LOAD) || (eph[i].filesz == 0))
continue;
ph.numSections++;
}
weph.filesz = 0;
fseek(out, data_pointer, SEEK_SET);
SWAP_PACKED_HEADER(ph);
if (fwrite(&ph, 1, sizeof(ph), out) != sizeof(ph))
printe("Error writing packed header\n");
weph.filesz += sizeof(ph);
/* looping on the program headers to pack them */
for (i = 0; i < eh->phnum; i++) {
if ((eph[i].type != PT_LOAD) || (eph[i].filesz == 0))
continue;
pdata = (loadbuf + eph[i].offset);
section_size = eph[i].filesz;
psh.originalSize = section_size;
psh.virtualAddr = eph[i].vaddr;
psh.zeroByteSize = eph[i].memsz - eph[i].filesz;
remove_section_zeroes(pdata, §ion_size, &psh.zeroByteSize);
printv("Loaded section: %08X bytes (with %08X zeroes) based at %08X\n", psh.originalSize, psh.zeroByteSize, psh.virtualAddr);
#ifndef PS2_PACKER_LITE
packed_size = ppack_section(pdata, &packed, section_size);
#else
packed_size = pack_section(pdata, &packed, section_size);
#endif
psh.compressedSize = packed_size;
printv("Section packed, from %u to %u bytes, ratio = %5.2f%%\n", section_size, packed_size, 100.0 * (int) (section_size - packed_size) / section_size);
SWAP_PACKED_SECTION_HEADER(psh);
if (fwrite(&psh, 1, sizeof(psh), out) != sizeof(psh))
printe("Error writing packed section header.\n");
weph.filesz += sizeof(psh);
if (fwrite(packed, 1, packed_size, out) != packed_size)
printe("Error writing packed section.\n");
weph.filesz += packed_size;
while (weph.filesz & 3) {
weph.filesz++;
fwrite(&zero, 1, 1, out);
}
free(packed);
}
if (!alternative) {
/* Padd data so they are a multiple of alignment. */
if (weph.filesz & (alignment - 1)) {
weph.filesz += alignment;
weph.filesz &= -alignment;
}
fseek(out, data_pointer + weph.filesz, SEEK_SET);
base = stub_base - weph.filesz;
printv("Final base address: %08X\n", base);
weph.vaddr = base;
weph.paddr = base;
SWAP32(base);
stub_data[1] = base;
printv("Writing stub.\n");
if (fwrite(stub_section, 1, stub_size, out) != stub_size) {
printe("Error writing stub\n");
}
weph.filesz += stub_size;
}
data_pointer += weph.filesz;
printv("All data written, writing program header.\n");
weph.memsz = weph.filesz + stub_zero;
if (alternative)
fseek(out, sizeof(*eh) + sizeof(*eph), SEEK_SET);
else
fseek(out, sizeof(*eh), SEEK_SET);
SWAP_ELF_PHEADER(weph);
if (fwrite(&weph, 1, sizeof(weph), out) != sizeof(weph))
printe("Error writing packed program header.\n");
free(loadbuf);
}
#ifndef BUFSIZ
#define BUFSIZ 1024
#endif
int file_exists(const char * fname) {
FILE * f;
if (!(f = fopen(fname, "rb")))
return 0;
fclose(f);
return 1;
}
int main(int argc, char ** argv) {
char c;
u32 base = 0;
#ifndef PS2_PACKER_LITE
char buffer[BUFSIZ + 1];
char * packer_name = 0;
char * stub_name = 0;
char * packer_dll = 0;
#endif
char * in_name;
char * out_name;
#ifndef PS2_PACKER_LITE
void * packer_module = 0;
FILE * stub_file;
#endif
FILE * in, * out;
u32 size_in, size_out;
char * pwd;
char pwd_buf[BUFSIZ + 1];
setbuf(stdout, NULL);
sanity_checks();
show_banner();
strncpy(pwd_buf, argv[0], BUFSIZ);
if ((pwd = strrchr(argv[0], '/'))) {
*pwd = 0;
} else if ((pwd = strrchr(argv[0], '\\'))) {
*pwd = 0;
}
pwd = argv[0];
#ifdef __MINGW32__
if (strcmp(pwd_buf, argv[0]) == 0) {
if (GetModuleFileName(NULL, pwd_buf, BUFSIZ)) {
if ((pwd = strrchr(pwd_buf, '\\')))
*pwd = 0;
pwd = pwd_buf;
}
}
#endif
while ((c = getopt_long(argc, argv, "b:a:"
#ifndef PS2_PACKER_LITE
"p:s:"
#endif
"hv", long_options, NULL)) != EOF) {
switch (c) {
case 'b':
base = strtoul(optarg, NULL, 0);
alternative = 1;
break;
case 'a':
alignment = strtoul(optarg, NULL, 0);
break;
#ifndef PS2_PACKER_LITE
case 'p':
packer_name = strdup(optarg);
break;
case 's':
stub_name = strdup(optarg);
break;
#endif
case 'v':
verbose = 1;
break;
case 'h':
show_usage();
exit(0);
default:
printf("Unknown option %c\n\n", c);
show_usage();
exit(EINVAL);
}
}
if (alternative)
printv("Using alternative packing method.\n");
if ((argc - optind) != 2) {
printe("%i files specified, I need exactly 2.\n\n", argc - optind);
show_usage();
exit(EINVAL);
}
in_name = argv[optind++];
out_name = argv[optind++];
if (!(in = fopen(in_name, "rb"))) {
printe("Unable to open input file %s\n", in_name);
}
if (!(out = fopen(out_name, "wb"))) {
printe("Unable to open output file %s\n", out_name);
}
#ifndef PS2_PACKER_LITE
if (!packer_name) {
packer_name = "lzma";
}
if (!stub_name) {
if (!base) {
snprintf(buffer, BUFSIZ, "stub/%s-1d00-stub", packer_name);
} else {
snprintf(buffer, BUFSIZ, "stub/%s-0088-stub", packer_name);
}
stub_name = strdup(buffer);
}
if (!file_exists(stub_name)) {
snprintf(buffer, BUFSIZ, PREFIX "/share/ps2-packer/%s", stub_name);
if (!file_exists(buffer)) {
snprintf(buffer, BUFSIZ, "%s/../share/ps2-packer/%s", pwd, stub_name);
if (!file_exists(buffer)) {
snprintf(buffer, BUFSIZ, "%s/%s", pwd, stub_name);
}
}
free(stub_name);
stub_name = strdup(buffer);
}
if (!(stub_file = fopen(stub_name, "rb"))) {
printe("Unable to open stub file %s\n", stub_name);
}
snprintf(buffer, BUFSIZ, PREFIX "/share/ps2-packer/module/%s-packer" SUFFIX, packer_name);
if (!file_exists(buffer)) {
snprintf(buffer, BUFSIZ, "%s/../share/ps2-packer/module/%s-packer" SUFFIX, pwd, packer_name);
if (!file_exists(buffer)) {
snprintf(buffer, BUFSIZ, "%s/%s-packer" SUFFIX, pwd, packer_name);
if (!file_exists(buffer))
snprintf(buffer, BUFSIZ, "./%s-packer" SUFFIX, packer_name);
}
}
packer_dll = strdup(buffer);
#endif
printf("Compressing %s...\n", in_name);
#ifndef PS2_PACKER_LITE
printv("Loading stub file %s.\n", stub_name);
load_stub(stub_file);
fclose(stub_file);
#else
printv("Loading stub file.\n");
load_stub();
#endif
#ifndef PS2_PACKER_LITE
printv("Opening packer %s.\n", packer_dll);
packer_module = open_module(packer_dll);
ppack_section = get_symbol(packer_module, "pack_section");
signature = get_symbol(packer_module, "signature");
if (signature() != stub_signature) {
printe("Packer's signature and stub's signature are not matching.\n");
}
#endif
printv("Preparing output elf file.\n");
prepare_out(out, base);
printv("Packing.\n");
packing(out, in, base);
printv("Done!\n");
fseek(in, 0, SEEK_END);
size_in = ftell(in);
size_out = data_pointer;
printf("File compressed, from %u to %u bytes, ratio = %5.2f%%\n", size_in, size_out, 100.0 * (size_in - size_out) / size_in);
fclose(out);
fclose(in);
return 0;
}