1 |
/* |
2 |
* GXemul: $Id: dreamcast_scramble.c,v 1.4 2007/06/19 03:52:07 debug Exp $ |
3 |
* |
4 |
* COMMENT: Dreamcast emulation (helper module) |
5 |
* |
6 |
* This is from Marcus Comstedt's Dreamcast development files |
7 |
* (http://mc.pp.se/dc/files/scramble.c). Public Domain, according to |
8 |
* Marcus software page. |
9 |
* |
10 |
* The only modification done to make it fit into the emulator is that |
11 |
* it is executed internally from src/file.c. |
12 |
*/ |
13 |
|
14 |
/* #define STAND_ALONE */ |
15 |
|
16 |
|
17 |
#include <stdio.h> |
18 |
#include <stdlib.h> |
19 |
|
20 |
#define MAXCHUNK (2048*1024) |
21 |
|
22 |
static unsigned int seed; |
23 |
|
24 |
void my_srand(unsigned int n) |
25 |
{ |
26 |
seed = n & 0xffff; |
27 |
} |
28 |
|
29 |
unsigned int my_rand(void) |
30 |
{ |
31 |
seed = (seed * 2109 + 9273) & 0x7fff; |
32 |
return (seed + 0xc000) & 0xffff; |
33 |
} |
34 |
|
35 |
void load(FILE *fh, unsigned char *ptr, unsigned long sz) |
36 |
{ |
37 |
if(fread(ptr, 1, sz, fh) != sz) |
38 |
{ |
39 |
fprintf(stderr, "Read error!\n"); |
40 |
exit(1); |
41 |
} |
42 |
} |
43 |
|
44 |
void load_chunk(FILE *fh, unsigned char *ptr, unsigned long sz) |
45 |
{ |
46 |
static int idx[MAXCHUNK/32]; |
47 |
int i; |
48 |
|
49 |
/* Convert chunk size to number of slices */ |
50 |
sz /= 32; |
51 |
|
52 |
/* Initialize index table with unity, |
53 |
so that each slice gets loaded exactly once */ |
54 |
for(i = 0; i < (int)sz; i++) |
55 |
idx[i] = i; |
56 |
|
57 |
for(i = sz-1; i >= 0; --i) |
58 |
{ |
59 |
/* Select a replacement index */ |
60 |
int x = (my_rand() * i) >> 16; |
61 |
|
62 |
/* Swap */ |
63 |
int tmp = idx[i]; |
64 |
idx[i] = idx[x]; |
65 |
idx[x] = tmp; |
66 |
|
67 |
/* Load resulting slice */ |
68 |
load(fh, ptr+32*idx[i], 32); |
69 |
} |
70 |
} |
71 |
|
72 |
void load_file(FILE *fh, unsigned char *ptr, unsigned long filesz) |
73 |
{ |
74 |
unsigned long chunksz; |
75 |
|
76 |
my_srand(filesz); |
77 |
|
78 |
/* Descramble 2 meg blocks for as long as possible, then |
79 |
gradually reduce the window down to 32 bytes (1 slice) */ |
80 |
for(chunksz = MAXCHUNK; chunksz >= 32; chunksz >>= 1) |
81 |
while(filesz >= chunksz) |
82 |
{ |
83 |
load_chunk(fh, ptr, chunksz); |
84 |
filesz -= chunksz; |
85 |
ptr += chunksz; |
86 |
} |
87 |
|
88 |
/* Load final incomplete slice */ |
89 |
if(filesz) |
90 |
load(fh, ptr, filesz); |
91 |
} |
92 |
|
93 |
void read_file(char *filename, unsigned char **ptr, unsigned long *sz) |
94 |
{ |
95 |
FILE *fh = fopen(filename, "rb"); |
96 |
if(fh == NULL) |
97 |
{ |
98 |
fprintf(stderr, "Can't open \"%s\".\n", filename); |
99 |
exit(1); |
100 |
} |
101 |
if(fseek(fh, 0, SEEK_END)<0) |
102 |
{ |
103 |
fprintf(stderr, "Seek error.\n"); |
104 |
exit(1); |
105 |
} |
106 |
*sz = ftell(fh); |
107 |
*ptr = malloc(*sz); |
108 |
if( *ptr == NULL ) |
109 |
{ |
110 |
fprintf(stderr, "Out of memory.\n"); |
111 |
exit(1); |
112 |
} |
113 |
if(fseek(fh, 0, SEEK_SET)<0) |
114 |
{ |
115 |
fprintf(stderr, "Seek error.\n"); |
116 |
exit(1); |
117 |
} |
118 |
load_file(fh, *ptr, *sz); |
119 |
fclose(fh); |
120 |
} |
121 |
|
122 |
void save(FILE *fh, unsigned char *ptr, unsigned long sz) |
123 |
{ |
124 |
if(fwrite(ptr, 1, sz, fh) != sz) |
125 |
{ |
126 |
fprintf(stderr, "Write error!\n"); |
127 |
exit(1); |
128 |
} |
129 |
} |
130 |
|
131 |
void save_chunk(FILE *fh, unsigned char *ptr, unsigned long sz) |
132 |
{ |
133 |
static int idx[MAXCHUNK/32]; |
134 |
int i; |
135 |
|
136 |
/* Convert chunk size to number of slices */ |
137 |
sz /= 32; |
138 |
|
139 |
/* Initialize index table with unity, |
140 |
so that each slice gets saved exactly once */ |
141 |
for(i = 0; i < (int)sz; i++) |
142 |
idx[i] = i; |
143 |
|
144 |
for(i = sz-1; i >= 0; --i) |
145 |
{ |
146 |
/* Select a replacement index */ |
147 |
int x = (my_rand() * i) >> 16; |
148 |
|
149 |
/* Swap */ |
150 |
int tmp = idx[i]; |
151 |
idx[i] = idx[x]; |
152 |
idx[x] = tmp; |
153 |
|
154 |
/* Save resulting slice */ |
155 |
save(fh, ptr+32*idx[i], 32); |
156 |
} |
157 |
} |
158 |
|
159 |
void save_file(FILE *fh, unsigned char *ptr, unsigned long filesz) |
160 |
{ |
161 |
unsigned long chunksz; |
162 |
|
163 |
my_srand(filesz); |
164 |
|
165 |
/* Descramble 2 meg blocks for as long as possible, then |
166 |
gradually reduce the window down to 32 bytes (1 slice) */ |
167 |
for(chunksz = MAXCHUNK; chunksz >= 32; chunksz >>= 1) |
168 |
while(filesz >= chunksz) |
169 |
{ |
170 |
save_chunk(fh, ptr, chunksz); |
171 |
filesz -= chunksz; |
172 |
ptr += chunksz; |
173 |
} |
174 |
|
175 |
/* Save final incomplete slice */ |
176 |
if(filesz) |
177 |
save(fh, ptr, filesz); |
178 |
} |
179 |
|
180 |
void write_file(char *filename, unsigned char *ptr, unsigned long sz) |
181 |
{ |
182 |
FILE *fh = fopen(filename, "wb"); |
183 |
if(fh == NULL) |
184 |
{ |
185 |
fprintf(stderr, "Can't open \"%s\".\n", filename); |
186 |
exit(1); |
187 |
} |
188 |
save_file(fh, ptr, sz); |
189 |
fclose(fh); |
190 |
} |
191 |
|
192 |
void dreamcast_descramble(char *src, char *dst) |
193 |
{ |
194 |
unsigned char *ptr = NULL; |
195 |
unsigned long sz = 0; |
196 |
FILE *fh; |
197 |
|
198 |
read_file(src, &ptr, &sz); |
199 |
|
200 |
fh = fopen(dst, "wb"); |
201 |
if(fh == NULL) |
202 |
{ |
203 |
fprintf(stderr, "Can't open \"%s\".\n", dst); |
204 |
exit(1); |
205 |
} |
206 |
if( fwrite(ptr, 1, sz, fh) != sz ) |
207 |
{ |
208 |
fprintf(stderr, "Write error.\n"); |
209 |
exit(1); |
210 |
} |
211 |
fclose(fh); |
212 |
free(ptr); |
213 |
} |
214 |
|
215 |
void scramble(char *src, char *dst) |
216 |
{ |
217 |
unsigned char *ptr = NULL; |
218 |
unsigned long sz = 0; |
219 |
FILE *fh; |
220 |
|
221 |
fh = fopen(src, "rb"); |
222 |
if(fh == NULL) |
223 |
{ |
224 |
fprintf(stderr, "Can't open \"%s\".\n", src); |
225 |
exit(1); |
226 |
} |
227 |
if(fseek(fh, 0, SEEK_END)<0) |
228 |
{ |
229 |
fprintf(stderr, "Seek error.\n"); |
230 |
exit(1); |
231 |
} |
232 |
sz = ftell(fh); |
233 |
ptr = malloc(sz); |
234 |
if( ptr == NULL ) |
235 |
{ |
236 |
fprintf(stderr, "Out of memory.\n"); |
237 |
exit(1); |
238 |
} |
239 |
if(fseek(fh, 0, SEEK_SET)<0) |
240 |
{ |
241 |
fprintf(stderr, "Seek error.\n"); |
242 |
exit(1); |
243 |
} |
244 |
if( fread(ptr, 1, sz, fh) != sz ) |
245 |
{ |
246 |
fprintf(stderr, "Read error.\n"); |
247 |
exit(1); |
248 |
} |
249 |
fclose(fh); |
250 |
|
251 |
write_file(dst, ptr, sz); |
252 |
|
253 |
free(ptr); |
254 |
} |
255 |
|
256 |
|
257 |
#ifdef STAND_ALONE |
258 |
|
259 |
int main(int argc, char *argv[]) |
260 |
{ |
261 |
int opt = 0; |
262 |
|
263 |
if(argc > 1 && !strcmp(argv[1], "-d")) |
264 |
opt ++; |
265 |
|
266 |
if(argc != 3+opt) |
267 |
{ |
268 |
fprintf(stderr, "Usage: %s [-d] from to\n", argv[0]); |
269 |
exit(1); |
270 |
} |
271 |
|
272 |
if(opt) |
273 |
dreamcast_descramble(argv[2], argv[3]); |
274 |
else |
275 |
scramble(argv[1], argv[2]); |
276 |
|
277 |
return 0; |
278 |
} |
279 |
|
280 |
#endif |