Merge branch 'master' into gtk3
[projects/chimara/chimara.git] / babel / alan.c
1 /* alan.c  Treaty of Babel module for ALAN 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 alan\r
11 #define HOME_PAGE "http://www.alanif.se/"\r
12 #define FORMAT_EXT ".acd"\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 static int32 read_alan_int(unsigned char *from)\r
21 {\r
22  return ((unsigned long int) from[3])| ((unsigned long int)from[2] << 8) |\r
23        ((unsigned long int) from[1]<<16)| ((unsigned long int)from[0] << 24);\r
24 }\r
25 static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)\r
26 {\r
27 \r
28  if (story_file || extent) { }\r
29  ASSERT_OUTPUT_SIZE(6);\r
30  strcpy(output,"ALAN-");\r
31  return INCOMPLETE_REPLY_RV;\r
32 }\r
33 /*\r
34   The claim algorithm for Alan files is:\r
35    * For Alan 3, check for the magic word\r
36    * load the file length in blocks\r
37    * check that the file length is correct\r
38    * For alan 2, each word between byte address 24 and 81 is a\r
39       word address within the file, so check that they're all within\r
40       the file\r
41    * Locate the checksum and verify that it is correct\r
42 */\r
43 static int32 claim_story_file(void *story_file, int32 extent)\r
44 {\r
45  unsigned char *sf = (unsigned char *) story_file;\r
46  int32 bf, i, crc=0;\r
47  if (extent < 160) return INVALID_STORY_FILE_RV;\r
48  if (memcmp(sf,"ALAN",4))\r
49  { /* Identify Alan 2.x */\r
50  bf=read_alan_int(sf+4);\r
51  if (bf > extent/4) return INVALID_STORY_FILE_RV;\r
52  for (i=24;i<81;i+=4)\r
53  if (read_alan_int(sf+i) > extent/4) return INVALID_STORY_FILE_RV;\r
54  for (i=160;i<(bf*4);i++)\r
55  crc+=sf[i];\r
56  if (crc!=read_alan_int(sf+152)) return INVALID_STORY_FILE_RV;\r
57  return VALID_STORY_FILE_RV;\r
58  }\r
59  else\r
60  { /* Identify Alan 3 */\r
61    bf=read_alan_int(sf+12);\r
62    if (bf > (extent/4)) return INVALID_STORY_FILE_RV;\r
63    for (i=184;i<(bf*4);i++)\r
64     crc+=sf[i];\r
65  if (crc!=read_alan_int(sf+176)) return INVALID_STORY_FILE_RV;\r
66 \r
67  }\r
68  return INVALID_STORY_FILE_RV;\r
69 }\r