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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "glk/alan2/types.h"
#include "glk/alan2/main.h"
#include "glk/alan2/reverse.h"
namespace Glk {
namespace Alan2 {
/*----------------------------------------------------------------------
reversed()
Return the reversed bytes in the Aword
*/
#ifdef _PROTOTYPES_
Aword reversed(Aword w) /* IN - The ACODE word to swap bytes of */
#else
Aword reversed(w)
Aword w; /* IN - The ACODE word to swap bytes of */
#endif
{
Aword s; /* The swapped ACODE word */
char *wp, *sp;
int i;
wp = (char *) &w;
sp = (char *) &s;
for (i = 0; i < sizeof(Aword); i++)
sp[sizeof(Aword)-1 - i] = wp[i];
return s;
}
#ifdef _PROTOTYPES_
void reverse(Aword *w) /* IN - The ACODE word to reverse bytes in */
#else
void reverse(w)
Aword *w; /* IN - The ACODE word to reverse bytes in */
#endif
{
*w = reversed(*w);
}
#ifdef _PROTOTYPES_
static void reverseTable(Aword adr, int len)
#else
static void reverseTable(adr, len)
Aword adr;
int len;
#endif
{
Aword *e = &memory[adr];
int i;
if (adr != 0)
while (!endOfTable(e)) {
for (i = 0; i < len/(int)sizeof(Aword); i++) {
reverse(e);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseStms(Aword adr)
#else
static void reverseStms(adr)
Aword adr;
#endif
{
Aword *e = &memory[adr];
if (adr != 0)
while (TRUE) {
reverse(e);
if (*e == ((Aword)C_STMOP<<28|(Aword)I_RETURN)) break;
e++;
}
}
#ifdef _PROTOTYPES_
static void reverseMsgs(Aword adr)
#else
static void reverseMsgs(adr)
Aword adr;
#endif
{
MsgElem *e = (MsgElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(MsgElem));
while (!endOfTable(e)) {
reverseStms(e->stms);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseWrds(Aword adr)
#else
static void reverseWrds(adr)
Aword adr;
#endif
{
WrdElem *e = (WrdElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(WrdElem));
while (!endOfTable(e)) {
if ((e->_class & (1L<<WRD_SYN)) == 0) { /* Do not do this for synonyms */
reverseTable(e->adjrefs, sizeof(Aword));
reverseTable(e->nounrefs, sizeof(Aword));
}
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseChks(Aword adr)
#else
static void reverseChks(adr)
Aword adr;
#endif
{
ChkElem *e = (ChkElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(ChkElem));
while (!endOfTable(e)) {
reverseStms(e->exp);
reverseStms(e->stms);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseAlts(Aword adr)
#else
static void reverseAlts(adr)
Aword adr;
#endif
{
AltElem *e = (AltElem *)&memory[adr];
if (adr != 0 && !endOfTable(e) && !e->done) {
reverseTable(adr, sizeof(AltElem));
e->done = TRUE;
while (!endOfTable(e)) {
reverseChks(e->checks);
reverseStms(e->action);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseVrbs(Aword adr)
#else
static void reverseVrbs(adr)
Aword adr;
#endif
{
VrbElem *e = (VrbElem *)&memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(VrbElem));
while (!endOfTable(e)) {
reverseAlts(e->alts);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseSteps(Aword adr)
#else
static void reverseSteps(adr)
Aword adr;
#endif
{
StepElem *e = (StepElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(StepElem));
while (!endOfTable(e)) {
reverseStms(e->exp);
reverseStms(e->stm);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseScrs(Aword adr)
#else
static void reverseScrs(adr)
Aword adr;
#endif
{
ScrElem *e = (ScrElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(ScrElem));
while (!endOfTable(e)) {
reverseStms(e->dscr);
reverseSteps(e->steps);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseActs(Aword adr)
#else
static void reverseActs(adr)
Aword adr;
#endif
{
ActElem *e = (ActElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(ActElem));
while (!endOfTable(e)) {
reverseStms(e->nam);
reverseTable(e->atrs, sizeof(AtrElem));
reverseScrs(e->scradr);
reverseVrbs(e->vrbs);
reverseStms(e->dscr);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseObjs(Aword adr, Boolean v2_5)
#else
static void reverseObjs(adr, v2_5)
Aword adr;
Boolean v2_5; /* TRUE if it's a v2.5 format game */
#endif
{
ObjElem *e = (ObjElem *) &memory[adr];
ObjElem25 *e25 = (ObjElem25 *) &memory[adr];
if (v2_5) {
if (adr != 0 && !endOfTable(e25)) {
reverseTable(adr, sizeof(ObjElem25));
while (!endOfTable(e25)) {
reverseTable(e25->atrs, sizeof(AtrElem));
reverseVrbs(e25->vrbs);
reverseStms(e25->dscr1);
reverseStms(e25->dscr2);
e25++;
}
}
} else {
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(ObjElem));
while (!endOfTable(e)) {
reverseTable(e->atrs, sizeof(AtrElem));
reverseVrbs(e->vrbs);
reverseStms(e->art);
reverseStms(e->dscr1);
reverseStms(e->dscr2);
e++;
}
}
}
}
#ifdef _PROTOTYPES_
static void reverseExts(Aword adr)
#else
static void reverseExts(adr)
Aword adr;
#endif
{
ExtElem *e = (ExtElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(ExtElem));
while (!endOfTable(e)) {
if (!e->done) {
reverseChks(e->checks);
reverseStms(e->action);
}
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseLocs(Aword adr)
#else
static void reverseLocs(adr)
Aword adr;
#endif
{
LocElem *e = (LocElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(LocElem));
while (!endOfTable(e)) {
reverseStms(e->nams);
reverseStms(e->dscr);
reverseStms(e->does);
reverseTable(e->atrs, sizeof(AtrElem));
reverseExts(e->exts);
reverseVrbs(e->vrbs);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseClas(Aword adr)
#else
static void reverseClas(adr)
Aword adr;
#endif
{
ClaElem *e = (ClaElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(ClaElem));
while (!endOfTable(e)) {
reverseStms(e->stms);
e++;
}
}
if (adr)
reverse(&((Aword *)e)[1]); /* The verb code is stored after the table */
}
#ifdef _PROTOTYPES_
static void reverseElms(Aword adr)
#else
static void reverseElms(adr)
Aword adr;
#endif
{
ElmElem *e = (ElmElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(ElmElem));
while (!endOfTable(e)) {
if (e->code == EOS) reverseClas(e->next);
else reverseElms(e->next);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseStxs(Aword adr)
#else
static void reverseStxs(adr)
Aword adr;
#endif
{
StxElem *e = (StxElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(StxElem));
while (!endOfTable(e)) {
reverseElms(e->elms);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseEvts(Aword adr)
#else
static void reverseEvts(adr)
Aword adr;
#endif
{
EvtElem *e = (EvtElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(EvtElem));
while (!endOfTable(e)) {
reverseStms(e->code);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseLims(Aword adr)
#else
static void reverseLims(adr)
Aword adr;
#endif
{
LimElem *e = (LimElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(LimElem));
while (!endOfTable(e)) {
reverseStms(e->stms);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseCnts(Aword adr)
#else
static void reverseCnts(adr)
Aword adr;
#endif
{
CntElem *e = (CntElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(CntElem));
while (!endOfTable(e)) {
reverseLims(e->lims);
reverseStms(e->header);
reverseStms(e->empty);
reverseStms(e->nam);
e++;
}
}
}
#ifdef _PROTOTYPES_
static void reverseRuls(Aword adr)
#else
static void reverseRuls(adr)
Aword adr;
#endif
{
RulElem *e = (RulElem *) &memory[adr];
if (adr != 0 && !endOfTable(e)) {
reverseTable(adr, sizeof(RulElem));
while (!endOfTable(e)) {
reverseStms(e->exp);
reverseStms(e->stms);
e++;
}
}
}
/*----------------------------------------------------------------------
reverseHdr()
Reverse the header structure.
*/
#ifdef _PROTOTYPES_
void reverseHdr(AcdHdr *hdr)
#else
void reverseHdr(hdr)
AcdHdr *hdr;
#endif
{
int i;
/* Reverse all words in the header except the first (version marking) */
for (i = 1; i < sizeof(AcdHdr)/sizeof(Aword); i++)
reverse(&((Aword *)hdr)[i]);
}
/*----------------------------------------------------------------------
reverseACD()
Traverse all the data structures and reverse all integers.
Only performed in architectures with reversed byte ordering, which
makes the .ACD files fully compatible across architectures
*/
#ifdef _PROTOTYPES_
void reverseACD(Boolean v2_5)
#else
void reverseACD(v2_5)
Boolean v2_5;
#endif
{
reverseHdr(header);
reverseWrds(header->dict);
reverseTable(header->oatrs, sizeof(AtrElem));
reverseTable(header->latrs, sizeof(AtrElem));
reverseTable(header->aatrs, sizeof(AtrElem));
reverseActs(header->acts);
reverseObjs(header->objs, v2_5);
reverseLocs(header->locs);
reverseStxs(header->stxs);
reverseVrbs(header->vrbs);
reverseEvts(header->evts);
reverseCnts(header->cnts);
reverseRuls(header->ruls);
reverseTable(header->init, sizeof(IniElem));
reverseStms(header->start);
reverseMsgs(header->msgs);
reverseTable(header->scores, sizeof(Aword));
reverseTable(header->freq, sizeof(Aword));
}
} // End of namespace Alan2
} // End of namespace Glk
|