Merge branch 'master' into gtk3
[projects/chimara/chimara.git] / babel / agt.c
1 /* agt.c  Treaty of Babel module for AGX-encapsulated AGT files\r
2  * 2006 By L. Ross Raszewski\r
3  *\r
4  * This file depends on treaty_builder.h\r
5  *\r
6  * This file is public domain, but note that any changes to this file\r
7  * may render it noncompliant with the Treaty of Babel\r
8  */\r
9 \r
10 #define FORMAT agt\r
11 #define HOME_PAGE "http://www.ifarchive.org/indexes/if-archiveXprogrammingXagt"\r
12 #define FORMAT_EXT ".agx"\r
13 #define NO_METADATA\r
14 #define NO_COVER\r
15 \r
16 #include "treaty_builder.h"\r
17 #include <ctype.h>\r
18 #include <stdio.h>\r
19 \r
20 \r
21 static char AGX_MAGIC[4] = { 0x58, 0xC7, 0xC1, 0x51 };\r
22 \r
23 /* Helper functions to unencode integers from AGT source */\r
24 static int32 read_agt_short(unsigned char *sf)\r
25 {\r
26  return sf[0] | (int32) sf[1]<<8;\r
27 }\r
28 static int32 read_agt_int(unsigned char *sf)\r
29 {\r
30  return (read_agt_short(sf+2) << 16) | read_agt_short(sf);\r
31 \r
32 }\r
33 \r
34 static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)\r
35 {\r
36  int32 l, game_version, game_sig;\r
37  unsigned char *sf=(unsigned char *)story_file;\r
38 \r
39  /* Read the position of the game desciption block */\r
40  l=read_agt_int(sf+32);\r
41  if (extent<l+6) return INVALID_STORY_FILE_RV;\r
42  game_version = read_agt_short(sf+l);\r
43  game_sig=read_agt_int(sf+l+2);\r
44  ASSERT_OUTPUT_SIZE(19);\r
45  sprintf(output,"AGT-%05d-%08X",game_version,game_sig);\r
46  return 1;\r
47 }\r
48 \r
49 /* The claim algorithm for AGT is to check for the magic word\r
50    defined above\r
51 */\r
52 static int32 claim_story_file(void *story_file, int32 extent)\r
53 {\r
54 \r
55 \r
56  if (extent<36 || memcmp(story_file,AGX_MAGIC,4)) return INVALID_STORY_FILE_RV;\r
57  return VALID_STORY_FILE_RV;\r
58 \r
59 }\r