This repository was archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdar-extract_psx.c
More file actions
124 lines (101 loc) · 2.88 KB
/
Copy pathdar-extract_psx.c
File metadata and controls
124 lines (101 loc) · 2.88 KB
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
/*
Copyright (C) 2019 Missingno_force a.k.a. Missingmew
See LICENSE for details.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "strcode/strcode.h"
#include "stage-dictionary.h"
#define RETPRINT(x) { sprintf (retstring, "%s", x); break; }
#define BREAK(x) { printf(x); return 1; }
int writeFile( FILE *input, int length, FILE *output ) {
unsigned char dataBuffer[1024];
unsigned int bytesLeft = length;
while(bytesLeft) {
unsigned int wantedRead;
if(bytesLeft >= sizeof(dataBuffer))
wantedRead = sizeof(dataBuffer);
else
wantedRead = bytesLeft;
unsigned int haveRead = fread(dataBuffer, 1, wantedRead, input);
if(haveRead != wantedRead) {
printf("haveRead != wantedRead: %d != %d\n", haveRead, wantedRead);
perror("This broke");
return 0;
}
unsigned int haveWrite = fwrite(dataBuffer, 1, haveRead, output);
if(haveWrite != haveRead) {
printf("haveWrite != haveRead: %d != %d\n", haveWrite, haveRead);
return 0;
}
bytesLeft -= haveRead;
}
return 1;
}
typedef struct {
uint32_t hash;
uint32_t size;
}__attribute__((packed)) fileentry;
dicentry *commondic = NULL, *stagedic = NULL;
int numcommondic = 0, numstagedic = 0, numdicentries = 0;
unsigned int matchhash( uint32_t hash, uint32_t extension, dicentry *dictionary, int numentries ) {
int i;
for( i = 0; i < numentries; i++ ) {
if((dictionary[i].hash == hash) && (dictionary[i].extension == extension)) return i;
}
return 0xFFFFFFFF;
}
char *getNameFromDetails(uint16_t hash, uint16_t extension) {
char *retstring = malloc(32);
unsigned int result;
result = matchhash(hash, extension, commondic, numcommondic);
if(result != 0xFFFFFFFF) sprintf(retstring, "%s", commondic[result].name);
else {
sprintf( retstring, "%04x.%c", hash, extension );
}
return retstring;
}
int main( int argc, char **argv ) {
char execpath[4096];
char *namestring = NULL;
unsigned int darsize;
fileentry thing;
uint16_t type, hash;
if( argc < 2 ) {
printf("Not enough args!\nUse: %s DAR-file [dictionary]\n", argv[0]);
return 1;
}
if( argc == 3 ) {
if(strlen(argv[0]) > 4096) BREAK("Path to executable exceeds 4096 chars!\n")
strcpy(execpath, argv[0]);
*strrchr(execpath, '/') = 0;
numcommondic = loaddic(&commondic, execpath, argv[2], DIC_HASH_SINGLE_EXT, StrCode16);
}
FILE *f, *o;
if( !(f = fopen( argv[1], "rb" ))) {
printf("Couldnt open file %s\n", argv[1]);
return 1;
}
fseek(f, 0, SEEK_END);
darsize = ftell(f);
fseek(f, 0, SEEK_SET);
while(ftell(f)<darsize) {
fread(&thing, 8, 1, f);
type = (thing.hash >> 16) & 0xFF;
hash = thing.hash & 0xFFFF;
namestring = getNameFromDetails(hash, type);
if( !(o = fopen( namestring, "wb" ))) {
printf("Couldnt open file %s\n", namestring);
return 1;
}
writeFile(f, thing.size, o);
fclose(o);
free(namestring);
}
free(commondic);
printf("Done.\n");
fclose(f);
return 0;
}