Debian Codegraph @

support/babel/hugo.c

raw symbols SCIP
1 /* hugo.c Treaty of Babel module for hugo 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 hugo
11 #define HOME_PAGE "http://www.generalcoffee.com"
12 #define FORMAT_EXT ".hex"
13 #define NO_METADATA
14 #define NO_COVER
15
16 #include "treaty_builder.h"
17 #include <ctype.h>
18 #include <stdio.h>
19
20 static int number_of_hexadecimals_before_hyphen(char *s, size_t len)
21 {
22 size_t offset = 0;
23
24 while (offset < len && isxdigit(s[offset]))
25 offset++;
26
27 if (offset == len || (offset < len && s[offset] == '-'))
28 return offset;
29
30 return 0;
31 }
32
33 /* We look for the pattern XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX (8-4-4-4-12)
34 * where X is a number or A-F.
35 * One Hugo game, PAXLess, uses lowercase letters. The rest all use uppercase.
36 */
37 static int isUUID(char *s)
38 {
39 if (!(number_of_hexadecimals_before_hyphen(s, 9) == 8))
40 return 0;
41 if (!(number_of_hexadecimals_before_hyphen(s+9, 5) == 4))
42 return 0;
43 if (!(number_of_hexadecimals_before_hyphen(s+14, 5) == 4))
44 return 0;
45 if (!(number_of_hexadecimals_before_hyphen(s+19, 5) == 4))
46 return 0;
47 if (!(number_of_hexadecimals_before_hyphen(s+24, 12) == 12))
48 return 0;
49 return 1;
50 }
51
52 /* The Hugo text obfuscation adds 20 to every character */
53 static char hugo_decode(char c)
54 {
55 int decoded_char = c - 20;
56 if (decoded_char < 0)
57 decoded_char = decoded_char + 256;
58 return (char)decoded_char;
59 }
60
61 static int32 get_story_file_IFID(void *s_file, int32 extent, char *output, int32 output_extent)
62 {
63
64 size_t i,j;
65 char ser[9];
66 char buffer[32];
67 char UUID_candidate[37];
68 char hyphen = '-'+20;
69
70 char *story_file = (char *) s_file;
71
72 if (extent<0x0B) return INVALID_STORY_FILE_RV;
73
74 for(i=0;i<extent-28;i++)
75 {
76 /* First we look for an obfuscated hyphen, '-' + 20 */
77 /* We need to look 8 characters behind and 28 ahead */
78 if (story_file[i] == hyphen && i >= 8 && extent-i >= 28 &&
79 story_file[i+5] == hyphen &&
80 story_file[i+10] == hyphen &&
81 story_file[i+15] == hyphen)
82 {
83 for (j=0;j<36;j++)
84 {
85 UUID_candidate[j] = hugo_decode(story_file[i+j-8]);
86 }
87 if (isUUID(UUID_candidate))
88 {
89 /* Found valid UUID at file offset i - 8 */
90 ASSERT_OUTPUT_SIZE((signed) 37);
91 memcpy(output,UUID_candidate,36);
92 output[36]='\0';
93 return 1;
94 }
95 }
96 }
97
98 /* Found no UUID in file. Construct legacy IFID */
99
100 memcpy(ser, (char *) story_file+0x03, 8);
101 ser[8]=0;
102
103 for(j=0;j<8;j++)
104 if (!isalnum(ser[j])) ser[j]='-';
105
106 sprintf(buffer,"HUGO-%d-%02X-%02X-%s",story_file[0],story_file[1], story_file[2],ser);
107
108 ASSERT_OUTPUT_SIZE((signed) strlen(buffer)+1);
109 strcpy((char *)output,buffer);
110 return 1;
111 }
112
113 static uint32 read_hugo_addx(unsigned char *from)
114 {
115 return ((uint32) from[0])| ((uint32)from[1] << 8);
116 }
117
118 static int32 claim_story_file(void *story_file, int32 exten)
119 {
120 unsigned char *sf=(unsigned char *)story_file;
121 int32 i;
122 int32 scale;
123 uint32 extent=(uint32) exten;
124
125 if (!story_file || extent < 0x28) return INVALID_STORY_FILE_RV;
126
127 /* 39 is the largest version currently accepted by the Hugo interpreter:
128 https://github.com/garglk/garglk/blob/master/terps/hugo/source/hemisc.c#L1310-L1362
129 */
130 if (sf[0] > 39) return INVALID_STORY_FILE_RV;
131
132 if (sf[0]<34) scale=4;
133 else scale=16;
134 for(i=3;i<0x0B;i++) if (sf[i]<0x20 || sf[i]>0x7e) return INVALID_STORY_FILE_RV;
135 for(i=0x0b;i<0x18;i+=2)
136 if (read_hugo_addx(sf+i) * scale > extent) return INVALID_STORY_FILE_RV;
137
138 return VALID_STORY_FILE_RV;
139 }