Debian Codegraph @

support/babel/zcode.c

raw symbols SCIP
1 /* zcode.c Treaty of Babel module for Z-code 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 zcode
11 #define HOME_PAGE "http://www.inform-fiction.org"
12 #define FORMAT_EXT ".z3,.z4,.z5,.z6,.z7,.z8"
13 #define NO_METADATA
14 #define NO_COVER
15 #define CUSTOM_EXTENSION
16 #include "treaty_builder.h"
17 #include "ifiction.h"
18 #include <ctype.h>
19 #include <stdio.h>
20
21 static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)
22 {
23 uint32 i,j;
24 char ser[7];
25 char buffer[32];
26
27
28 if (extent<0x1D) return INVALID_STORY_FILE_RV;
29 memcpy(ser, (char *) story_file+0x12, 6);
30 ser[6]=0;
31 /* Detect vintage story files */
32 if (!(ser[0]=='8' || ser[0]=='9' ||
33 (ser[0]=='0' && ser[1]>='0' && ser[1]<='5')))
34 {
35 i = find_uuid_ifid_marker(story_file, extent, output, output_extent);
36 if (i == VALID_STORY_FILE_RV || i == INVALID_USAGE_RV)
37 return i;
38 }
39 /* Did not find intact IFID. Build one */
40 i=((unsigned char *)story_file)[2] << 8 |((unsigned char *)story_file)[3];
41 for(j=0;j<6;j++)
42 if (!isalnum(ser[j])) ser[j]='-';
43
44 j=((unsigned char *)story_file)[0x1C] << 8 |((unsigned char *)story_file)[0x1D];
45
46 if (strcmp(ser,"000000") && isdigit(ser[0]) && ser[0]!='8')
47 sprintf(buffer,"ZCODE-%d-%s-%04X",i,ser,j);
48 else
49 sprintf(buffer,"ZCODE-%d-%s",i,ser);
50
51 ASSERT_OUTPUT_SIZE((signed) strlen(buffer)+1);
52 strcpy((char *)output,buffer);
53 return 1;
54
55 }
56
57 static uint32 read_zint(unsigned char *sf)
58 {
59 return ((uint32)sf[0] << 8) | ((uint32) sf[1]);
60
61 }
62 static int32 claim_story_file(void *story_file, int32 extent)
63 {
64 unsigned char *sf=(unsigned char *)story_file;
65 uint32 i,j;
66 if (extent<0x3c ||
67 sf[0] < 1 ||
68 sf[0] > 8
69 ) return INVALID_STORY_FILE_RV;
70 for(i=4;i<=14;i+=2)
71 {
72 j=read_zint(sf+i);
73 if (j>(uint32) extent || j < 0x40) return INVALID_STORY_FILE_RV;
74 }
75
76 return VALID_STORY_FILE_RV;
77 }
78 static int32 get_story_file_extension(void *sf, int32 extent, char *out, int32 output_extent)
79 {
80 int v;
81 if (!extent) return INVALID_STORY_FILE_RV;
82 v= ((char *) sf)[0];
83 if (v>9) ASSERT_OUTPUT_SIZE(5);
84 else ASSERT_OUTPUT_SIZE(4);
85 sprintf(out,".z%d",v);
86 return 3+(v>9);
87
88 }