-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkunp.cpp
216 lines (182 loc) · 5.81 KB
/
kunp.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
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
/**
* MIT License
* Copyright(C) 2020 Stefano Moioli <smxdev4@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
**/
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include <fstream>
#include <cstdio>
#include <cstdint>
#include <cassert>
#include "mfile.h"
#include "kallisto.h"
#ifdef _WIN32
#define MKDIR(x) mkdir(x)
#else
#define MKDIR(x) mkdir(x, (mode_t)0777)
#endif
class HeraUnpacker {
private:
MFILE *kix = nullptr;
MFILE *kbf = nullptr;
static bool isKixData(uint8_t *data){
kix_node_t *root = (kix_node_t *)(data + sizeof(kix_node_t));
return (root->type == kixNodeType::Directory);
}
static bool isKixFile(MFILE *mf){
size_t headSz = sizeof(kix_hdr_t) + sizeof(kix_node_t);
if(msize(mf) < headSz){
return false;
}
uint8_t *head = mdata(mf, uint8_t);
return isKixData(head);
}
int extract_kix_entry(const char *basedir, kix_node_t *node){
uint8_t *kbf_start = mdata(kbf, uint8_t);
size_t kbf_size = msize(kbf);
if(node->offset + node->size > kbf_size){
std::cerr << util::ssprintf("ERROR: cannot extract! out of boundary (0x%08X >= 0x%08X)!\n", node->offset + node->size, kbf_size);
return -2;
}
// Get the file entry referenced by this index entry
kbf_node_t *data = (kbf_node_t *)(kbf_start + node->offset);
std::string filePath = util::ssprintf("%s/%.32s", basedir, data->name);
std::cout << util::ssprintf("Extracting to '%s'\n", filePath.c_str());
std::ofstream out(filePath, std::ofstream::binary);
if(!out.is_open()){
std::cerr << util::ssprintf("ERROR: cannot open output file '%s' for writing\n", filePath.c_str());
return -3;
}
out.write((char *)data->data, node->size);
off_t written = out.tellp();
if(written != node->size){
std::cerr << util::ssprintf("WARNING: Expected %zu bytes, but written %zu\n", node->size, written);
return -4;
}
return 0;
}
long int parse_kix_block(const char *basedir, kix_hdr_t *hdr){
uintptr_t start = (uintptr_t)hdr;
hdr->print(kix);
std::cout << "-----------------------\n";
start += sizeof(*hdr);
long int parsed = sizeof(*hdr);
for(uint i=0; i<hdr->numRecords; i++){
kix_node_t *node = (kix_node_t *)start;
// nested KIX headers indicate directories
switch(node->type){
case kixNodeType::Directory:
{
start += sizeof(*node);
parsed += sizeof(*node);
node->print(i, kix);
kix_hdr_t *dirent = (kix_hdr_t *)(mdata(kix, uint8_t) + node->offset);
std::string subdir = util::ssprintf("%s/%.32s", basedir, dirent->name);
MKDIR(subdir.c_str());
std::cout << '\n';
parsed += parse_kix_block(subdir.c_str(), dirent);
break;
}
case kixNodeType::File:
{
kix_filenode_t *file = (kix_filenode_t *)start;
start += sizeof(*file);
parsed += sizeof(*file);
// Extract file entry
file->print(i, kix);
extract_kix_entry(basedir, file);
start += file->nameLen;
parsed += file->nameLen;
break;
}
default:
throw util::ssprintf("Invalid block type '%d'", node->type);
}
}
return parsed;
}
public:
HeraUnpacker(const char *kixPath, const char *kbfPath){
this->kix = mopen(kixPath, O_RDONLY);
if(!this->kix){
throw std::invalid_argument(
util::ssprintf("Cannot open KIX file '%s' for reading", kixPath)
);
}
if(!isKixFile(this->kix)){
mclose(this->kix);
this->kix = nullptr;
throw std::invalid_argument(
util::ssprintf("'%s' is not a valid KIX file", kixPath)
);
}
this->kbf = mopen(kbfPath, O_RDONLY);
if(!this->kbf){
throw std::invalid_argument(
util::ssprintf("Cannot open KBF file '%s' for reading", kbfPath)
);
}
}
size_t kix_size(){
return msize(kix);
}
size_t kbf_size(){
return msize(kbf);
}
long int parse_kix_block(const char *basedir){
return parse_kix_block(basedir, mdata(kix, kix_hdr_t));
}
~HeraUnpacker(){
if(this->kix){
mclose(this->kix);
}
if(this->kbf){
mclose(this->kbf);
}
}
};
int main(int argc, char **argv){
std::cout << "Kallisto Index File (KIX) | Kallisto Binary File (KBF) Unpacker\n";
std::cout << "Copyright (C) 2020 Smx\n\n";
if(argc < 3){
std::cerr << util::ssprintf("Usage: %s [file.kix] [file.kbf]\n", argv[0]);
return EXIT_FAILURE;
}
std::filesystem::path cwd = std::filesystem::current_path();
std::filesystem::path kixName = std::filesystem::path(argv[1])
.filename()
.replace_extension("");
std::string targetDirPath = (cwd / kixName).string();
const char *targetDir = targetDirPath.c_str();
MKDIR(targetDir);
int parsed = 0;
try {
HeraUnpacker unp(argv[1], argv[2]);
parsed = unp.parse_kix_block(targetDir);
std::cout << util::ssprintf("Done!, parsed %zu/%zu bytes\n", parsed, unp.kix_size());
return EXIT_SUCCESS;
} catch(const std::exception& e){
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
}