Debian Codegraph @

support/babel/adrift.c

raw symbols SCIP
1 /* adrift.c Treaty of Babel module for Adrift files
2 *
3 * PROVISIONAL - Hold for someone else
4 *
5 * This file depends on treaty_builder.h
6 *
7 * This file is public domain, but note that any changes to this file
8 * may render it noncompliant with the Treaty of Babel
9 */
10
11 #define FORMAT adrift
12 #define HOME_PAGE "http://www.adrift.org.uk"
13 #define FORMAT_EXT ".taf"
14 #define NO_COVER
15
16 #include "treaty_builder.h"
17
18 #include <stdio.h>
19 #include <limits.h>
20 #include <stdlib.h>
21
22 #include <strings.h>
23
24 /* VB RNG constants */
25 #define VB_RAND1 0x43FD43FD
26 #define VB_RAND2 0x00C39EC3
27 #define VB_RAND3 0x00FFFFFF
28 #define VB_INIT 0x00A09E86
29 static int32 vbr_state;
30
31 void *my_malloc(int32, char *);
32 int32 ifiction_get_IFID(char *, char *, int32);
33
34 /* Searches case-insensitively for the string str in the story file.
35 The string must be null-terminated. The story file can have nulls
36 anywhere, of course.
37
38 Returns the found position, or -1 for not found.
39 */
40 static int32 find_text_in_file(char *story_file, int32 extent, int32 startat, char *str)
41 {
42 int len = strlen(str);
43 int32 ix;
44
45 if (len == 0) {
46 return -1;
47 }
48
49 for (ix=startat; ix<extent-len; ix++) {
50 if (strncasecmp(story_file+ix, str, len) == 0)
51 return ix;
52 }
53
54 return -1;
55 }
56
57 /*
58 Unobfuscates one byte from a taf file. This should be called on each byte
59 in order, as the ADRIFT obfuscation function is stately.
60
61 The de-obfuscation algorithm works by xoring the byte with the next
62 byte in the sequence produced by the Visual Basic pseudorandom number
63 generator, which is simulated here.
64 */
65 static unsigned char taf_translate (unsigned char c)
66 {
67 int32 r;
68
69 vbr_state = (vbr_state*VB_RAND1+VB_RAND2) & VB_RAND3;
70 r=UCHAR_MAX * (unsigned) vbr_state;
71 r/=((unsigned) VB_RAND3)+1;
72 return r^c;
73 }
74
75 static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)
76 {
77 int32 ificlen;
78 int adv;
79 unsigned char buf[4];
80 unsigned char *sf=(unsigned char *)story_file;
81 vbr_state=VB_INIT;
82
83 ificlen = get_story_file_metadata_extent(story_file, extent);
84 if (ificlen > 0) {
85 void *tempmd = my_malloc(ificlen, "temporary ifiction buffer");
86 int32 res = get_story_file_metadata(story_file, extent, tempmd, ificlen);
87 if (res > 0) {
88 res = ifiction_get_IFID(tempmd, output, output_extent);
89 free(tempmd);
90 if (res >= 0)
91 return res;
92 }
93 else {
94 free(tempmd);
95 /* continue on to fallback IFID code */
96 }
97 }
98
99 if (extent < 12) return INVALID_STORY_FILE_RV;
100
101 buf[3]=0;
102 /* Burn the first 8 bytes of translation */
103 for(adv=0;adv<8;adv++) taf_translate(0);
104 /* Bytes 8-11 contain the Adrift version number in the formay N.NN */
105 buf[0]=taf_translate(sf[8]);
106 taf_translate(0);
107 buf[1]=taf_translate(sf[10]);
108 buf[2]=taf_translate(sf[11]);
109 adv=atoi((char *) buf);
110 ASSERT_OUTPUT_SIZE(12);
111 sprintf(output,"ADRIFT-%03d-",adv);
112 return INCOMPLETE_REPLY_RV;
113
114 }
115
116 static int32 get_story_file_metadata_extent(void *story_file, int32 extent)
117 {
118 int ificpos = find_text_in_file(story_file, extent, 0, "<ifindex");
119 if (ificpos >= 0) {
120 int ificend = find_text_in_file(story_file, extent, ificpos, "</ifindex>");
121 if (ificend >= 0 && ificend > ificpos) {
122 ificend += 10; /* length of closing tag */
123 return (ificend - ificpos);
124 }
125 }
126 return NO_REPLY_RV;
127 }
128
129 static int32 get_story_file_metadata(void *story_file, int32 extent, char *output, int32 output_extent)
130 {
131 int ificpos = find_text_in_file(story_file, extent, 0, "<ifindex");
132 if (ificpos >= 0) {
133 int ificend = find_text_in_file(story_file, extent, ificpos, "</ifindex>");
134 if (ificend >= 0 && ificend > ificpos) {
135 ificend += 10; /* length of closing tag */
136 int32 len = (ificend - ificpos);
137 if (len > output_extent)
138 return INVALID_USAGE_RV;
139 memcpy(output, (char *)story_file+ificpos, ificend-ificpos);
140 return len;
141 }
142 }
143 return NO_REPLY_RV;
144 }
145
146 /* The claim algorithm for ADRIFT is to unobfuscate the first
147 seven bytes, and check for the word "Version".
148 It seems fairly unlikely that the obfuscated form of that
149 word would occur in the wild
150 */
151 static int32 claim_story_file(void *story_file, int32 extent)
152 {
153 unsigned char buf[8];
154 int i;
155 unsigned char *sf=(unsigned char *)story_file;
156 buf[7]=0;
157 vbr_state=VB_INIT;
158 if (extent<12) return INVALID_STORY_FILE_RV;
159 for(i=0;i<7;i++) buf[i]=taf_translate(sf[i]);
160 if (strcmp((char *)buf,"Version")) return INVALID_STORY_FILE_RV;
161 return VALID_STORY_FILE_RV;
162
163 }