This repository has been archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsar.c
93 lines (76 loc) · 2.52 KB
/
dsar.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
#include "libra/archive.h"
#include <lz4.h>
static void ls(const char* path);
static void decompress(const char* input_path, const char* output_path);
static void print_help();
int main(int argc, char** argv) {
if(argc == 3 && strcmp(argv[1], "list") == 0) {
ls(argv[2]);
} else if(argc == 4 && strcmp(argv[1], "decompress") == 0) {
decompress(argv[2], argv[3]);
} else {
print_help();
return 1;
}
}
static void ls(const char* path) {
RA_Result result;
RA_Archive archive;
if((result = RA_archive_open(&archive, path)) != RA_SUCCESS) {
fprintf(stderr, "Failed to load archive file '%s' (%s).\n", path, result->message);
exit(1);
}
if(!archive.is_dsar_archive) {
fprintf(stderr, "error: Input file in not a dsar archive.\n");
exit(1);
}
printf("Index Decompressed Offset Compressed Offset Size Compressed Size\n");
for(u32 i = 0; i < archive.dsar_block_count; i++) {
printf("%5u %19" PRIx64 " %19" PRIx64 " %08x %08x\n",
i,
archive.dsar_blocks[i].header.decompressed_offset,
archive.dsar_blocks[i].header.compressed_offset,
archive.dsar_blocks[i].header.decompressed_size,
archive.dsar_blocks[i].header.compressed_size);
}
RA_archive_close(&archive);
}
static void decompress(const char* input_path, const char* output_path) {
RA_Result result;
RA_Archive archive;
if((result = RA_archive_open(&archive, input_path)) != RA_SUCCESS) {
fprintf(stderr, "Failed to load archive file '%s' (%s).\n", input_path, result->message);
exit(1);
}
if(!archive.is_dsar_archive) {
fprintf(stderr, "error: Input file in not a dsar archive.\n");
exit(1);
}
s64 size = RA_archive_get_decompressed_size(&archive);
if(size == -1) {
fprintf(stderr, "error: Failed to determine decompressed size.\n");
exit(1);
}
u8* data = RA_malloc(size);
if(data == NULL) {
fprintf(stderr, "error: Failed to allocate memory for decompressed data.\n");
exit(1);
}
if((result = RA_archive_read(&archive, 0, size, data)) != RA_SUCCESS) {
fprintf(stderr, "error: Failed to decompress data (%s).\n", result->message);
exit(1);
}
if((result = RA_file_write(output_path, data, size)) != RA_SUCCESS) {
fprintf(stderr, "error: Failed to write output file '%s' (%s).\n", output_path, result->message);
exit(1);
}
RA_free(data);
RA_archive_close(&archive);
}
static void print_help() {
puts("A utility for working with DSAR archives, such as those used by the PC version of Rift Apart.");
puts("");
puts("Commands:");
puts(" list <input file>");
puts(" decompress <input file> <output file>");
}