Merge branch 'master' into browser
[projects/chimara/chimara.git] / babel / alan.c
diff --git a/babel/alan.c b/babel/alan.c
new file mode 100644 (file)
index 0000000..fb57dbe
--- /dev/null
@@ -0,0 +1,69 @@
+/* alan.c  Treaty of Babel module for ALAN files\r
+ * 2006 By L. Ross Raszewski\r
+ *\r
+ * This file depends on treaty_builder.h\r
+ *\r
+ * This file is public domain, but note that any changes to this file\r
+ * may render it noncompliant with the Treaty of Babel\r
+ */\r
+\r
+#define FORMAT alan\r
+#define HOME_PAGE "http://www.alanif.se/"\r
+#define FORMAT_EXT ".acd"\r
+#define NO_METADATA\r
+#define NO_COVER\r
+\r
+#include "treaty_builder.h"\r
+#include <ctype.h>\r
+#include <stdio.h>\r
+\r
+static int32 read_alan_int(unsigned char *from)\r
+{\r
+ return ((unsigned long int) from[3])| ((unsigned long int)from[2] << 8) |\r
+       ((unsigned long int) from[1]<<16)| ((unsigned long int)from[0] << 24);\r
+}\r
+static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent)\r
+{\r
+\r
+ if (story_file || extent) { }\r
+ ASSERT_OUTPUT_SIZE(6);\r
+ strcpy(output,"ALAN-");\r
+ return INCOMPLETE_REPLY_RV;\r
+}\r
+/*\r
+  The claim algorithm for Alan files is:\r
+   * For Alan 3, check for the magic word\r
+   * load the file length in blocks\r
+   * check that the file length is correct\r
+   * For alan 2, each word between byte address 24 and 81 is a\r
+      word address within the file, so check that they're all within\r
+      the file\r
+   * Locate the checksum and verify that it is correct\r
+*/\r
+static int32 claim_story_file(void *story_file, int32 extent)\r
+{\r
+ unsigned char *sf = (unsigned char *) story_file;\r
+ int32 bf, i, crc=0;\r
+ if (extent < 160) return INVALID_STORY_FILE_RV;\r
+ if (memcmp(sf,"ALAN",4))\r
+ { /* Identify Alan 2.x */\r
+ bf=read_alan_int(sf+4);\r
+ if (bf > extent/4) return INVALID_STORY_FILE_RV;\r
+ for (i=24;i<81;i+=4)\r
+ if (read_alan_int(sf+i) > extent/4) return INVALID_STORY_FILE_RV;\r
+ for (i=160;i<(bf*4);i++)\r
+ crc+=sf[i];\r
+ if (crc!=read_alan_int(sf+152)) return INVALID_STORY_FILE_RV;\r
+ return VALID_STORY_FILE_RV;\r
+ }\r
+ else\r
+ { /* Identify Alan 3 */\r
+   bf=read_alan_int(sf+12);\r
+   if (bf > (extent/4)) return INVALID_STORY_FILE_RV;\r
+   for (i=184;i<(bf*4);i++)\r
+    crc+=sf[i];\r
+ if (crc!=read_alan_int(sf+176)) return INVALID_STORY_FILE_RV;\r
+\r
+ }\r
+ return INVALID_STORY_FILE_RV;\r
+}\r