-
Notifications
You must be signed in to change notification settings - Fork 1
/
unpkg.c
132 lines (99 loc) · 2.35 KB
/
unpkg.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
// Copyright 2010 Sven Peter <[email protected]>
// Licensed under the terms of the GNU GPL, version 2
// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
#include "tools.h"
#include "types.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
u8 *pkg = NULL;
static u64 dec_size;
static u32 meta_offset;
static u32 n_sections;
static void unpack_content(const char *name)
{
u8 *tmp;
u8 *decompressed;
u64 offset;
u64 size;
u64 size_real;
tmp = pkg + meta_offset + 0x80 + 0x30 * 2;
offset = be64(tmp);
size = be64(tmp + 8);
size_real = dec_size - 0x80;
if (be32(tmp + 0x2c) == 0x2) {
decompressed = malloc(size_real);
memset(decompressed, 0xaa, size_real);
decompress(pkg + offset, size, decompressed, size_real);
memcpy_to_file(name, decompressed, size_real);
} else {
memcpy_to_file(name, pkg + offset, size);
}
}
static void unpack_info(u32 i)
{
u8 *tmp;
u64 offset;
u64 size;
char path[256];
tmp = pkg + meta_offset + 0x80 + 0x30 * i;
snprintf(path, sizeof path, "info%d", i);
offset = be64(tmp);
size = be64(tmp + 8);
if (size != 0x40)
fail("weird info size: %08x", size);
memcpy_to_file(path, pkg + offset, size);
}
static void unpack_pkg(void)
{
unpack_info(0);
unpack_info(1);
unpack_content("content");
}
static void decrypt_pkg(void)
{
u16 flags;
u16 type;
u32 hdr_len;
struct keylist *k;
flags = be16(pkg + 0x08);
type = be16(pkg + 0x0a);
hdr_len = be64(pkg + 0x10);
dec_size = be64(pkg + 0x18);
if (type != 3)
fail("no .pkg file");
k = keys_get(KEY_PKG);
if (k == NULL)
fail("no key found");
if (sce_decrypt_header(pkg, k) < 0)
fail("header decryption failed");
if (sce_decrypt_data(pkg) < 0)
fail("data decryption failed");
meta_offset = be32(pkg + 0x0c);
n_sections = be32(pkg + meta_offset + 0x60 + 0xc);
if (n_sections != 3)
fail("invalid section count: %d", n_sections);
}
int main(int argc, char *argv[])
{
if (argc == 3) {
pkg = mmap_file(argv[1]);
mkdir(argv[2], 0777);
if (chdir(argv[2]) != 0)
fail("chdir");
decrypt_pkg();
unpack_pkg();
} else if (argc == 4) {
if (strcmp(argv[1], "-s") != 0)
fail("invalid option: %s", argv[1]);
pkg = mmap_file(argv[2]);
decrypt_pkg();
unpack_content(argv[3]);
} else {
fail("usage: unpkg [-s] filename.pkg target");
}
return 0;
}