Debian Codegraph @

terps/git/savefile.c

raw symbols SCIP
1 // $Id: savefile.c,v 1.6 2003/10/20 16:05:06 iain Exp $
2
3 #include "git.h"
4
5 static void writeWord (git_sint32 word)
6 {
7 git_uint8 buffer [4];
8 write32 (buffer, word);
9 glk_put_buffer ((char *) buffer, 4);
10 }
11
12 static git_uint32 readWord (strid_t file)
13 {
14 git_uint8 buffer [4];
15 glk_get_buffer_stream (file, (char *) buffer, 4);
16 return (git_uint32) read32 (buffer);
17 }
18
19 static int sort_heap_summary(const void *p1, const void *p2)
20 {
21 glui32 v1 = *((const glui32 *)p1);
22 glui32 v2 = *((const glui32 *)p2);
23
24 if (v1 < v2)
25 return -1;
26 if (v1 > v2)
27 return 1;
28 return 0;
29 }
30
31 git_sint32 restoreFromFile (git_sint32 * base, git_sint32 id,
32 git_uint32 protectPos, git_uint32 protectSize)
33 {
34 git_uint32 protectEnd = protectPos + protectSize;
35 git_uint32 i;
36 strid_t file;
37 glui32 fileSize, fileStart;
38
39 int gotIdent = 0;
40 int gotMemory = 0;
41 int gotStack = 0;
42 int gotHeap = 0;
43
44 // Find out what stream they want to use, and make sure it's valid.
45 file = git_find_stream_by_id (id);
46 if (file == 0)
47 return 1;
48
49 // Read IFF header.
50 if (readWord (file) != readtag("FORM"))
51 return 1; // Not an IFF file.
52
53 fileSize = readWord (file);
54 fileStart = glk_stream_get_position (file);
55
56 if (readWord (file) != readtag("IFZS"))
57 return 1; // Not a Quetzal file.
58
59 // Discard the current heap.
60 heap_clear();
61
62 // Read all the chunks.
63
64 while (glk_stream_get_position(file) < fileStart + fileSize)
65 {
66 git_uint32 chunkType, chunkSize;
67 chunkType = readWord (file);
68 chunkSize = readWord (file);
69
70 if (chunkType == readtag("IFhd"))
71 {
72 if (gotIdent)
73 return 1;
74
75 gotIdent = 1;
76
77 if (chunkSize != 128)
78 return 1;
79
80 for (i = 0 ; i < 128 ; ++i)
81 {
82 glui32 c = glk_get_char_stream (file);
83 if (gInitMem [i] != c)
84 return 1;
85 }
86 }
87 else if (chunkType == readtag("Stks"))
88 {
89 if (gotStack)
90 return 1;
91
92 gotStack = 1;
93
94 if (chunkSize & 3)
95 return 1;
96
97 gStackPointer = base;
98 for ( ; chunkSize > 0 ; chunkSize -= 4)
99 *gStackPointer++ = readWord(file);
100 }
101 else if (chunkType == readtag("CMem"))
102 {
103 git_uint32 bytesRead = 0;
104 if (gotMemory)
105 return 1;
106
107 gotMemory = 1;
108
109 if (resizeMemory (readWord(file), 1))
110 fatalError ("Can't resize memory map");
111
112 bytesRead = 4;
113 i = gRamStart;
114 while (i < gExtStart && bytesRead < chunkSize)
115 {
116 int mult = 0;
117 char c = (char) glk_get_char_stream(file);
118 ++bytesRead;
119
120 if (c == 0)
121 {
122 mult = (unsigned char) glk_get_char_stream(file);
123 ++bytesRead;
124 }
125
126 for (++mult ; mult > 0 ; --mult, ++i)
127 if (i >= protectEnd || i < protectPos)
128 gMem [i] = gInitMem [i] ^ c;
129 }
130
131 while (i < gEndMem && bytesRead < chunkSize)
132 {
133 int mult = 0;
134 char c = (char) glk_get_char_stream(file);
135 ++bytesRead;
136
137 if (c == 0)
138 {
139 mult = (unsigned char) glk_get_char_stream(file);
140 ++bytesRead;
141 }
142
143 for (++mult ; mult > 0 ; --mult, ++i)
144 if (i >= protectEnd || i < protectPos)
145 gMem [i] = c;
146 }
147
148 while (i < gExtStart)
149 if (i >= protectEnd || i < protectPos)
150 gMem [i] = gInitMem [i], ++i;
151
152 while (i < gEndMem)
153 if (i >= protectEnd || i < protectPos)
154 gMem [i] = 0, ++i;
155
156 if (bytesRead != chunkSize)
157 return 1; // Too much data!
158
159 if (chunkSize & 1)
160 glk_get_char_stream (file);
161 }
162 else if (chunkType == readtag("MAll"))
163 {
164 glui32 heapSize = 0;
165 glui32 * heap = 0;
166
167 if (gotHeap)
168 return 1;
169
170 gotHeap = 1;
171
172 if (chunkSize & 3)
173 return 1;
174
175 if (chunkSize > 0)
176 {
177 heap = malloc (chunkSize);
178 heapSize = chunkSize / 4;
179 for (i = 0 ; i < heapSize ; ++i)
180 heap[i] = readWord(file);
181
182 /* The summary might have come from any interpreter, so it could
183 be out of order. We'll sort it. */
184 qsort(heap+2, (heapSize-2)/2, 8, &sort_heap_summary);
185
186 if (heap_apply_summary (heapSize, heap))
187 fatalError ("Couldn't apply heap summary");
188 free (heap);
189 }
190 }
191 else
192 {
193 // Unknown chunk type -- just skip it.
194 glk_stream_set_position (file, (chunkSize + 1) & ~1, seekmode_Current);
195 }
196 }
197
198 // Make sure we have all the chunks we need.
199
200 if (!gotIdent)
201 fatalError ("No ident chunk in save file");
202
203 if (!gotStack)
204 fatalError ("No stack chunk in save file");
205
206 if (!gotMemory)
207 fatalError ("No memory chunk in save file");
208
209 // If we reach this point, we restored successfully.
210
211 return 0;
212 }
213
214 git_sint32 saveToFile (git_sint32 * base, git_sint32 * sp, git_sint32 id)
215 {
216 git_uint32 n, zeroCount;
217 glui32 fileSize, fileSizePos;
218 glui32 memSize, memSizePos;
219 glui32 heapSize;
220 glui32* heap;
221
222 strid_t file, oldFile;
223
224 // Find out what stream they want to use, and make sure it's valid.
225 file = git_find_stream_by_id (id);
226 if (file == 0)
227 return 1;
228
229 // Get the state of the heap.
230 if (heap_get_summary (&heapSize, &heap))
231 fatalError ("Couldn't get heap summary");
232
233 // Make the given stream the default.
234 oldFile = glk_stream_get_current ();
235 glk_stream_set_current (file);
236
237 // Write Quetzal header.
238 glk_put_string ("FORM");
239
240 fileSizePos = glk_stream_get_position (file);
241 writeWord (0);
242
243 glk_put_string ("IFZS");
244
245 // Header chunk.
246 glk_put_string ("IFhd");
247 writeWord (128);
248 glk_put_buffer ((char *) gInitMem, 128);
249
250 // Stack chunk.
251 glk_put_string ("Stks");
252 writeWord ((sp - base) * 4);
253 for (n = 0 ; n < (git_uint32) (sp - base) ; ++n)
254 writeWord (base [n]);
255
256 // Heap chunk.
257 if (heap != 0)
258 {
259 glk_put_string ("MAll");
260 writeWord (heapSize * 4);
261 for (n = 0 ; n < heapSize ; ++n)
262 writeWord (heap [n]);
263 free(heap);
264 }
265
266 // Memory chunk.
267 glk_put_string ("CMem");
268 memSizePos = glk_stream_get_position (file);
269 writeWord (0);
270
271 writeWord (gEndMem);
272 for (zeroCount = 0, n = gRamStart ; n < gEndMem ; ++n)
273 {
274 unsigned char romC = (n < gExtStart) ? gInitMem[n] : 0;
275 unsigned char c = ((git_uint32) romC) ^ ((git_uint32) gMem[n]);
276 if (c == 0)
277 ++zeroCount;
278 else
279 {
280 for ( ; zeroCount > 256 ; zeroCount -= 256)
281 {
282 glk_put_char (0);
283 glk_put_char (0xff);
284 }
285
286 if (zeroCount > 0)
287 {
288 glk_put_char (0);
289 glk_put_char ((char) (zeroCount - 1));
290 zeroCount = 0;
291 }
292
293 glk_put_char (c);
294 }
295 }
296 // Note: we don't bother writing out any remaining zeroes,
297 // because the memory is padded out with zeroes on restore.
298
299 memSize = glk_stream_get_position (file) - memSizePos - 4;
300 if (memSize & 1)
301 glk_put_char (0);
302
303 // Back up and fill in the lengths.
304 fileSize = glk_stream_get_position (file) - fileSizePos - 4;
305
306 glk_stream_set_position (file, fileSizePos, seekmode_Start);
307 writeWord (fileSize);
308
309 glk_stream_set_position (file, memSizePos, seekmode_Start);
310 writeWord (memSize);
311
312 // Restore the previous default stream.
313 glk_stream_set_current (oldFile);
314
315 // And we're done.
316 return 0;
317 }