1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "glk/frotz/mem.h"
24 #include "glk/frotz/frotz.h"
25 #include "common/memstream.h"
26 #include "common/textconsole.h"
27 
28 namespace Glk {
29 namespace Frotz {
30 
Mem()31 Mem::Mem() : story_fp(nullptr), story_size(0), first_undo(nullptr), last_undo(nullptr),
32 		curr_undo(nullptr), undo_mem(nullptr), zmp(nullptr), pcp(nullptr), prev_zmp(nullptr),
33 		undo_diff(nullptr), undo_count(0), reserve_mem(0) {
34 }
35 
initialize()36 void Mem::initialize() {
37 	initializeStoryFile();
38 	loadGameHeader();
39 	loadMemory();
40 	initializeUndo();
41 
42 	// Read header extension table
43 	hx_table_size = get_header_extension(HX_TABLE_SIZE);
44 	hx_unicode_table = get_header_extension(HX_UNICODE_TABLE);
45 	hx_flags = get_header_extension(HX_FLAGS);
46 }
47 
initializeStoryFile()48 void Mem::initializeStoryFile() {
49 	if (story_fp->size() < 64)
50 		error("This file is too small to be a Z-code file.");
51 }
52 
loadGameHeader()53 void Mem::loadGameHeader() {
54 	// Load header
55 	zmp = (byte *)malloc(64);
56 	story_fp->seek(0);
57 	story_fp->read(zmp, 64);
58 
59 	Common::MemoryReadStream h(zmp, 64);
60 	loadHeader(h);
61 
62 	// Calculate story file size in bytes
63 	if (h_file_size != 0) {
64 		story_size = (long)2 * h_file_size;
65 
66 		if (h_version >= V4)
67 			story_size *= 2;
68 		if (h_version >= V6)
69 			story_size *= 2;
70 	} else {
71 		// Some old games lack the file size entry
72 		story_size = story_fp->size();
73 	}
74 }
75 
loadMemory()76 void Mem::loadMemory() {
77 	// Allocate memory for story data
78 	if ((zmp = (zbyte *)realloc(zmp, story_size)) == nullptr)
79 		error("Out of memory");
80 
81 	// Load story file in chunks of 32KB
82 	uint n = 0x8000;
83 	for (uint size = 64; size < story_size; size += n) {
84 		if (story_size - size < 0x8000)
85 			n = story_size - size;
86 
87 		if (story_fp->read(zmp + size, n) != n)
88 			error("Story file read error");
89 	}
90 }
91 
initializeUndo()92 void Mem::initializeUndo() {
93 	byte *reserved = nullptr;
94 
95 	if (reserve_mem != 0) {
96 		if ((reserved = new byte[reserve_mem]) == nullptr)
97 			return;
98 	}
99 
100 	// Allocate h_dynamic_size bytes for previous dynamic zmp state
101 	// + 1.5 h_dynamic_size for Quetzal diff + 2.
102 	undo_mem = new zbyte[(h_dynamic_size * 5) / 2 + 2];
103 	if (undo_mem != nullptr) {
104 		prev_zmp = undo_mem;
105 		undo_diff = undo_mem + h_dynamic_size;
106 		memcpy(prev_zmp, zmp, h_dynamic_size);
107 	} else {
108 		_undo_slots = 0;
109 	}
110 
111 	if (reserve_mem)
112 		delete[] reserved;
113 }
114 
get_header_extension(int entry)115 zword Mem::get_header_extension(int entry) {
116 	zword addr;
117 	zword val;
118 
119 	if (h_extension_table == 0 || entry > hx_table_size)
120 		return 0;
121 
122 	addr = h_extension_table + 2 * entry;
123 	LOW_WORD(addr, val);
124 
125 	return val;
126 }
127 
set_header_extension(int entry,zword val)128 void Mem::set_header_extension(int entry, zword val) {
129 	zword addr;
130 
131 	if (h_extension_table == 0 || entry > hx_table_size)
132 		return;
133 
134 	addr = h_extension_table + 2 * entry;
135 	SET_WORD(addr, val);
136 }
137 
restart_header(void)138 void Mem::restart_header(void) {
139 	zword screen_x_size;
140 	zword screen_y_size;
141 	zbyte font_x_size;
142 	zbyte font_y_size;
143 
144 	int i;
145 
146 	SET_BYTE(H_CONFIG, h_config);
147 	SET_WORD(H_FLAGS, h_flags);
148 
149 	if (h_version >= V4) {
150 		SET_BYTE(H_INTERPRETER_NUMBER, h_interpreter_number);
151 		SET_BYTE(H_INTERPRETER_VERSION, h_interpreter_version);
152 		SET_BYTE(H_SCREEN_ROWS, h_screen_rows);
153 		SET_BYTE(H_SCREEN_COLS, h_screen_cols);
154 	}
155 
156 	// It's less trouble to use font size 1x1 for V5 games, especially because of
157 	// a bug in the unreleased German version of "Zork 1"
158 
159 	if (h_version != V6) {
160 		screen_x_size = (zword)h_screen_cols;
161 		screen_y_size = (zword)h_screen_rows;
162 		font_x_size = 1;
163 		font_y_size = 1;
164 	} else {
165 		screen_x_size = h_screen_width;
166 		screen_y_size = h_screen_height;
167 		font_x_size = h_font_width;
168 		font_y_size = h_font_height;
169 	}
170 
171 	if (h_version >= V5) {
172 		SET_WORD(H_SCREEN_WIDTH, screen_x_size);
173 		SET_WORD(H_SCREEN_HEIGHT, screen_y_size);
174 		SET_BYTE(H_FONT_HEIGHT, font_y_size);
175 		SET_BYTE(H_FONT_WIDTH, font_x_size);
176 		SET_BYTE(H_DEFAULT_BACKGROUND, h_default_background);
177 		SET_BYTE(H_DEFAULT_FOREGROUND, h_default_foreground);
178 	}
179 
180 	if (h_version == V6)
181 		for (i = 0; i < 8; i++)
182 			storeb((zword)(H_USER_NAME + i), h_user_name[i]);
183 
184 	SET_BYTE(H_STANDARD_HIGH, h_standard_high);
185 	SET_BYTE(H_STANDARD_LOW, h_standard_low);
186 
187 	set_header_extension(HX_FLAGS, hx_flags);
188 	set_header_extension(HX_FORE_COLOUR, hx_fore_colour);
189 	set_header_extension(HX_BACK_COLOUR, hx_back_colour);
190 }
191 
storeb(zword addr,zbyte value)192 void Mem::storeb(zword addr, zbyte value) {
193 	if (addr >= h_dynamic_size)
194 		runtimeError(ERR_STORE_RANGE);
195 
196 	if (addr == H_FLAGS + 1) {
197 		// flags register is modified
198 
199 		h_flags &= ~(SCRIPTING_FLAG | FIXED_FONT_FLAG);
200 		h_flags |= value & (SCRIPTING_FLAG | FIXED_FONT_FLAG);
201 
202 		flagsChanged(value);
203 	}
204 
205 	SET_BYTE(addr, value);
206 }
207 
storew(zword addr,zword value)208 void Mem::storew(zword addr, zword value) {
209 	storeb((zword)(addr + 0), hi(value));
210 	storeb((zword)(addr + 1), lo(value));
211 }
212 
free_undo(int count)213 void Mem::free_undo(int count) {
214 	undo_t *p;
215 
216 	if (count > undo_count)
217 		count = undo_count;
218 	while (count--) {
219 		p = first_undo;
220 		if (curr_undo == first_undo)
221 			curr_undo = curr_undo->next;
222 		first_undo = first_undo->next;
223 		free(p);
224 		undo_count--;
225 	}
226 	if (first_undo)
227 		first_undo->prev = nullptr;
228 	else
229 		last_undo = nullptr;
230 }
231 
reset_memory()232 void Mem::reset_memory() {
233 	story_fp = nullptr;
234 
235 	if (undo_mem) {
236 		free_undo(undo_count);
237 		delete[] undo_mem;
238 	}
239 
240 	undo_mem = nullptr;
241 	undo_count = 0;
242 	free(zmp);
243 	zmp = nullptr;
244 }
245 
mem_diff(zbyte * a,zbyte * b,zword mem_size,zbyte * diff)246 long Mem::mem_diff(zbyte *a, zbyte *b, zword mem_size, zbyte *diff) {
247 	unsigned size = mem_size;
248 	zbyte *p = diff;
249 	unsigned j;
250 	zbyte c = 0;
251 
252 	for (;;) {
253 		for (j = 0; size > 0 && (c = *a++ ^ *b++) == 0; j++)
254 			size--;
255 		if (size == 0) break;
256 		size--;
257 		if (j > 0x8000) {
258 			*p++ = 0;
259 			*p++ = 0xff;
260 			*p++ = 0xff;
261 			j -= 0x8000;
262 		}
263 		if (j > 0) {
264 			*p++ = 0;
265 			j--;
266 			if (j <= 0x7f) {
267 				*p++ = j;
268 			} else {
269 				*p++ = (j & 0x7f) | 0x80;
270 				*p++ = (j & 0x7f80) >> 7;
271 			}
272 		}
273 
274 		*p++ = c;
275 		*(b - 1) ^= c;
276 	}
277 
278 	return p - diff;
279 }
280 
mem_undiff(zbyte * diff,long diff_length,zbyte * dest)281 void Mem::mem_undiff(zbyte *diff, long diff_length, zbyte *dest) {
282 	zbyte c;
283 
284 	while (diff_length) {
285 		c = *diff++;
286 		diff_length--;
287 		if (c == 0) {
288 			unsigned runlen;
289 
290 			if (!diff_length)
291 				return;  // Incomplete run
292 			runlen = *diff++;
293 			diff_length--;
294 			if (runlen & 0x80) {
295 				if (!diff_length)
296 					return; // Incomplete extended run
297 				c = *diff++;
298 				diff_length--;
299 				runlen = (runlen & 0x7f) | (((unsigned)c) << 7);
300 			}
301 
302 			dest += runlen + 1;
303 		} else {
304 			*dest++ ^= c;
305 		}
306 	}
307 }
308 
309 } // End of namespace Frotz
310 } // End of namespace Glk
311