-
Notifications
You must be signed in to change notification settings - Fork 13
/
savefile.c
317 lines (251 loc) · 8.21 KB
/
savefile.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// $Id: savefile.c,v 1.6 2003/10/20 16:05:06 iain Exp $
#include "git.h"
static void writeWord (git_sint32 word)
{
git_uint8 buffer [4];
write32 (buffer, word);
glk_put_buffer ((char *) buffer, 4);
}
static git_uint32 readWord (strid_t file)
{
git_uint8 buffer [4];
glk_get_buffer_stream (file, (char *) buffer, 4);
return (git_uint32) read32 (buffer);
}
static int sort_heap_summary(const void *p1, const void *p2)
{
glui32 v1 = *((const glui32 *)p1);
glui32 v2 = *((const glui32 *)p2);
if (v1 < v2)
return -1;
if (v1 > v2)
return 1;
return 0;
}
git_sint32 restoreFromFile (git_sint32 * base, git_sint32 id,
git_uint32 protectPos, git_uint32 protectSize)
{
git_uint32 protectEnd = protectPos + protectSize;
git_uint32 i;
strid_t file;
glui32 fileSize, fileStart;
int gotIdent = 0;
int gotMemory = 0;
int gotStack = 0;
int gotHeap = 0;
// Find out what stream they want to use, and make sure it's valid.
file = git_find_stream_by_id (id);
if (file == 0)
return 1;
// Read IFF header.
if (readWord (file) != readtag("FORM"))
return 1; // Not an IFF file.
fileSize = readWord (file);
fileStart = glk_stream_get_position (file);
if (readWord (file) != readtag("IFZS"))
return 1; // Not a Quetzal file.
// Discard the current heap.
heap_clear();
// Read all the chunks.
while (glk_stream_get_position(file) < fileStart + fileSize)
{
git_uint32 chunkType, chunkSize;
chunkType = readWord (file);
chunkSize = readWord (file);
if (chunkType == readtag("IFhd"))
{
if (gotIdent)
return 1;
gotIdent = 1;
if (chunkSize != 128)
return 1;
for (i = 0 ; i < 128 ; ++i)
{
glui32 c = glk_get_char_stream (file);
if (gInitMem [i] != c)
return 1;
}
}
else if (chunkType == readtag("Stks"))
{
if (gotStack)
return 1;
gotStack = 1;
if (chunkSize & 3)
return 1;
gStackPointer = base;
for ( ; chunkSize > 0 ; chunkSize -= 4)
*gStackPointer++ = readWord(file);
}
else if (chunkType == readtag("CMem"))
{
git_uint32 bytesRead = 0;
if (gotMemory)
return 1;
gotMemory = 1;
if (resizeMemory (readWord(file), 1))
fatalError ("Can't resize memory map");
bytesRead = 4;
i = gRamStart;
while (i < gExtStart && bytesRead < chunkSize)
{
int mult = 0;
char c = (char) glk_get_char_stream(file);
++bytesRead;
if (c == 0)
{
mult = (unsigned char) glk_get_char_stream(file);
++bytesRead;
}
for (++mult ; mult > 0 ; --mult, ++i)
if (i >= protectEnd || i < protectPos)
gMem [i] = gInitMem [i] ^ c;
}
while (i < gEndMem && bytesRead < chunkSize)
{
int mult = 0;
char c = (char) glk_get_char_stream(file);
++bytesRead;
if (c == 0)
{
mult = (unsigned char) glk_get_char_stream(file);
++bytesRead;
}
for (++mult ; mult > 0 ; --mult, ++i)
if (i >= protectEnd || i < protectPos)
gMem [i] = c;
}
while (i < gExtStart)
if (i >= protectEnd || i < protectPos)
gMem [i] = gInitMem [i], ++i;
while (i < gEndMem)
if (i >= protectEnd || i < protectPos)
gMem [i] = 0, ++i;
if (bytesRead != chunkSize)
return 1; // Too much data!
if (chunkSize & 1)
glk_get_char_stream (file);
}
else if (chunkType == readtag("MAll"))
{
glui32 heapSize = 0;
glui32 * heap = 0;
if (gotHeap)
return 1;
gotHeap = 1;
if (chunkSize & 3)
return 1;
if (chunkSize > 0)
{
heap = malloc (chunkSize);
heapSize = chunkSize / 4;
for (i = 0 ; i < heapSize ; ++i)
heap[i] = readWord(file);
/* The summary might have come from any interpreter, so it could
be out of order. We'll sort it. */
qsort(heap+2, (heapSize-2)/2, 8, &sort_heap_summary);
if (heap_apply_summary (heapSize, heap))
fatalError ("Couldn't apply heap summary");
free (heap);
}
}
else
{
// Unknown chunk type -- just skip it.
glk_stream_set_position (file, (chunkSize + 1) & ~1, seekmode_Current);
}
}
// Make sure we have all the chunks we need.
if (!gotIdent)
fatalError ("No ident chunk in save file");
if (!gotStack)
fatalError ("No stack chunk in save file");
if (!gotMemory)
fatalError ("No memory chunk in save file");
// If we reach this point, we restored successfully.
return 0;
}
git_sint32 saveToFile (git_sint32 * base, git_sint32 * sp, git_sint32 id)
{
git_uint32 n, zeroCount;
glui32 fileSize, fileSizePos;
glui32 memSize, memSizePos;
glui32 heapSize;
glui32* heap;
strid_t file, oldFile;
// Find out what stream they want to use, and make sure it's valid.
file = git_find_stream_by_id (id);
if (file == 0)
return 1;
// Get the state of the heap.
if (heap_get_summary (&heapSize, &heap))
fatalError ("Couldn't get heap summary");
// Make the given stream the default.
oldFile = glk_stream_get_current ();
glk_stream_set_current (file);
// Write Quetzal header.
glk_put_string ("FORM");
fileSizePos = glk_stream_get_position (file);
writeWord (0);
glk_put_string ("IFZS");
// Header chunk.
glk_put_string ("IFhd");
writeWord (128);
glk_put_buffer ((char *) gInitMem, 128);
// Stack chunk.
glk_put_string ("Stks");
writeWord ((sp - base) * 4);
for (n = 0 ; n < (git_uint32) (sp - base) ; ++n)
writeWord (base [n]);
// Heap chunk.
if (heap != 0)
{
glk_put_string ("MAll");
writeWord (heapSize * 4);
for (n = 0 ; n < heapSize ; ++n)
writeWord (heap [n]);
free(heap);
}
// Memory chunk.
glk_put_string ("CMem");
memSizePos = glk_stream_get_position (file);
writeWord (0);
writeWord (gEndMem);
for (zeroCount = 0, n = gRamStart ; n < gEndMem ; ++n)
{
unsigned char romC = (n < gExtStart) ? gInitMem[n] : 0;
unsigned char c = ((git_uint32) romC) ^ ((git_uint32) gMem[n]);
if (c == 0)
++zeroCount;
else
{
for ( ; zeroCount > 256 ; zeroCount -= 256)
{
glk_put_char (0);
glk_put_char (0xff);
}
if (zeroCount > 0)
{
glk_put_char (0);
glk_put_char ((char) (zeroCount - 1));
zeroCount = 0;
}
glk_put_char (c);
}
}
// Note: we don't bother writing out any remaining zeroes,
// because the memory is padded out with zeroes on restore.
memSize = glk_stream_get_position (file) - memSizePos - 4;
if (memSize & 1)
glk_put_char (0);
// Back up and fill in the lengths.
fileSize = glk_stream_get_position (file) - fileSizePos - 4;
glk_stream_set_position (file, fileSizePos, seekmode_Start);
writeWord (fileSize);
glk_stream_set_position (file, memSizePos, seekmode_Start);
writeWord (memSize);
// Restore the previous default stream.
glk_stream_set_current (oldFile);
// And we're done.
return 0;
}