Debian Codegraph @

support/babel/alan.c

raw symbols SCIP
1 /* alan.c Treaty of Babel module for ALAN files
2 * 2006 By L. Ross Raszewski
3 *
4 * This file depends on treaty_builder.h
5 *
6 * This file is public domain, but note that any changes to this file
7 * may render it noncompliant with the Treaty of Babel
8 */
9
10 #define FORMAT alan
11 #define HOME_PAGE "http://www.alanif.se/"
12 #define FORMAT_EXT ".acd,.a3c"
13 #define NO_METADATA
14 #define NO_COVER
15
16 #include "treaty_builder.h"
17 #include <ctype.h>
18 #include <stdio.h>
19 #include <stdbool.h>
20
21
22 typedef unsigned char byte;
23
24 static uint32 read_alan_int_at(byte *from)
25 {
26 return ((uint32) from[3])| ((uint32)from[2] << 8) |
27 ((uint32) from[1]<<16)| ((uint32)from[0] << 24);
28 }
29
30 static bool magic_word_found(byte *story_file) {
31 if(story_file == NULL)
32 return false;
33 return memcmp(story_file,"ALAN",4) == 0;
34 }
35
36 static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)
37 {
38 if (story_file || extent)
39 { }
40
41 if (!magic_word_found(story_file))
42 {
43 ASSERT_OUTPUT_SIZE(5);
44 strcpy(output,"ALAN-");
45 return INCOMPLETE_REPLY_RV;
46 }
47 else
48 {
49 /*
50 Alan v3 stores IFIDs in the story file in a format that might differ between versions.
51 So just scan for "UUID://".
52 Alan files seem to have lower-case hex in the "UUID://" string,
53 which is not what we want, but we accept it for historic
54 reasons. We therefore don't call find_uuid_ifid_marker().
55 */
56 int32 i, j, k;
57
58 for(i=0;i<extent;i++) if (memcmp((char *)story_file+i,"UUID://",7)==0) break;
59 if (i<extent) /* Found explicit IFID */
60 {
61 for(j=i+7;j<extent && ((char *)story_file)[j]!='/';j++);
62 if (j<extent)
63 {
64 i+=7;
65 ASSERT_OUTPUT_SIZE(j-i);
66 for(k=0;k<j-i;k++)
67 {
68 output[k]=toupper(((char *)story_file)[i+k]);
69 }
70 output[j-i]=0;
71 return VALID_STORY_FILE_RV;
72 }
73 }
74 ASSERT_OUTPUT_SIZE(5);
75 strcpy(output,"ALAN-");
76 return INCOMPLETE_REPLY_RV;
77 }
78 }
79
80
81 static bool crc_is_correct(byte *story_file, int32 size_in_awords) {
82 /* Size of AcodeHeader is 50 Awords = 200 bytes */
83 int32 calculated_crc = 0;
84 int32 crc_in_file = read_alan_int_at(story_file+46*4);
85
86 for (int i=50*4;i<(size_in_awords*4);i++)
87 calculated_crc+=story_file[i];
88
89 /* Some Alan 3 games seem to have added 284 to their internal checksum.
90 We allow this error.
91 (It would be more conservative to only allow this error for a few games:
92 A Very Hairy Fish-Mess, The Ngah Angah School of Forbidden Wisdom,
93 Room 206, IN-D-I-GO SOUL, The Christmas Party. But we're not going
94 to be that fussy.) */
95 return (calculated_crc == crc_in_file || calculated_crc + 284 == crc_in_file);
96 }
97
98
99 /*
100 The claim algorithm for Alan files is:
101 * For Alan 3, check for the magic word
102 * load the file length in blocks
103 * check that the file length is correct
104 * For alan 2, each word between byte address 24 and 81 is a
105 word address within the file, so check that they're all within
106 the file
107 * Locate the checksum and verify that it is correct
108 */
109 static int32 claim_story_file(void *story_file, int32 extent_in_bytes)
110 {
111 byte *sf = (byte *) story_file;
112 uint32 size_in_awords, i;
113
114 if (extent_in_bytes < 160)
115 /* File is shorter than any Alan file header */
116 return INVALID_STORY_FILE_RV;
117
118 size_in_awords=read_alan_int_at(sf+4);
119
120 if (!magic_word_found(sf))
121 { /* Identify Alan 2.x */
122 uint32 crc = 0;
123
124 if (size_in_awords > extent_in_bytes/4) return INVALID_STORY_FILE_RV;
125 for (i=24;i<81;i+=4)
126 if (read_alan_int_at(sf+i) > extent_in_bytes/4) return INVALID_STORY_FILE_RV;
127 for (i=160;i<(size_in_awords*4);i++)
128 crc+=sf[i];
129 if (crc!=read_alan_int_at(sf+152)) return INVALID_STORY_FILE_RV;
130 return VALID_STORY_FILE_RV;
131 }
132 else
133 { /* Identify Alan 3 */
134 size_in_awords=read_alan_int_at(sf+3*4); /* hdr.size @ 3 */
135
136 if (!crc_is_correct(sf, size_in_awords))
137 {
138 switch (read_alan_int_at(sf+46*4))
139 {
140 case 1427594: /* Enter The Dark */
141 case 8683866: /* Waldo’s Pie */
142 return VALID_STORY_FILE_RV;
143 default:
144 return INVALID_STORY_FILE_RV;
145 }
146 }
147 return VALID_STORY_FILE_RV;
148 }
149 }