| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#define FORMAT alan |
| 11 |
#define HOME_PAGE "http://www.alanif.se/" |
| 12 |
#define FORMAT_EXT ".acd,.a3c" |
| 13 |
#define NO_METADATA |
| 14 |
#define NO_COVER |
| 15 |
|
| 16 |
#include "treaty_builder.h" |
| 17 |
#include <ctype.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <stdbool.h> |
| 20 |
|
| 21 |
|
| 22 |
typedef unsigned char byte; |
| 23 |
|
| 24 |
static uint32 read_alan_int_at(byte *from) |
| 25 |
{ |
| 26 |
return ((uint32) from[3])| ((uint32)from[2] << 8) | |
| 27 |
((uint32) from[1]<<16)| ((uint32)from[0] << 24); |
| 28 |
} |
| 29 |
|
| 30 |
static bool magic_word_found(byte *story_file) { |
| 31 |
if(story_file == NULL) |
| 32 |
return false; |
| 33 |
return memcmp(story_file,"ALAN",4) == 0; |
| 34 |
} |
| 35 |
|
| 36 |
static int32 get_story_file_IFID(void *story_file, int32 extent, char *output, int32 output_extent) |
| 37 |
{ |
| 38 |
if (story_file || extent) |
| 39 |
{ } |
| 40 |
|
| 41 |
if (!magic_word_found(story_file)) |
| 42 |
{ |
| 43 |
ASSERT_OUTPUT_SIZE(5); |
| 44 |
strcpy(output,"ALAN-"); |
| 45 |
return INCOMPLETE_REPLY_RV; |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
int32 i, j, k; |
| 57 |
|
| 58 |
for(i=0;i<extent;i++) if (memcmp((char *)story_file+i,"UUID://",7)==0) break; |
| 59 |
if (i<extent) |
| 60 |
{ |
| 61 |
for(j=i+7;j<extent && ((char *)story_file)[j]!='/';j++); |
| 62 |
if (j<extent) |
| 63 |
{ |
| 64 |
i+=7; |
| 65 |
ASSERT_OUTPUT_SIZE(j-i); |
| 66 |
for(k=0;k<j-i;k++) |
| 67 |
{ |
| 68 |
output[k]=toupper(((char *)story_file)[i+k]); |
| 69 |
} |
| 70 |
output[j-i]=0; |
| 71 |
return VALID_STORY_FILE_RV; |
| 72 |
} |
| 73 |
} |
| 74 |
ASSERT_OUTPUT_SIZE(5); |
| 75 |
strcpy(output,"ALAN-"); |
| 76 |
return INCOMPLETE_REPLY_RV; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
static bool crc_is_correct(byte *story_file, int32 size_in_awords) { |
| 82 |
|
| 83 |
int32 calculated_crc = 0; |
| 84 |
int32 crc_in_file = read_alan_int_at(story_file+46*4); |
| 85 |
|
| 86 |
for (int i=50*4;i<(size_in_awords*4);i++) |
| 87 |
calculated_crc+=story_file[i]; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
return (calculated_crc == crc_in_file || calculated_crc + 284 == crc_in_file); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static int32 claim_story_file(void *story_file, int32 extent_in_bytes) |
| 110 |
{ |
| 111 |
byte *sf = (byte *) story_file; |
| 112 |
uint32 size_in_awords, i; |
| 113 |
|
| 114 |
if (extent_in_bytes < 160) |
| 115 |
|
| 116 |
return INVALID_STORY_FILE_RV; |
| 117 |
|
| 118 |
size_in_awords=read_alan_int_at(sf+4); |
| 119 |
|
| 120 |
if (!magic_word_found(sf)) |
| 121 |
{ |
| 122 |
uint32 crc = 0; |
| 123 |
|
| 124 |
if (size_in_awords > extent_in_bytes/4) return INVALID_STORY_FILE_RV; |
| 125 |
for (i=24;i<81;i+=4) |
| 126 |
if (read_alan_int_at(sf+i) > extent_in_bytes/4) return INVALID_STORY_FILE_RV; |
| 127 |
for (i=160;i<(size_in_awords*4);i++) |
| 128 |
crc+=sf[i]; |
| 129 |
if (crc!=read_alan_int_at(sf+152)) return INVALID_STORY_FILE_RV; |
| 130 |
return VALID_STORY_FILE_RV; |
| 131 |
} |
| 132 |
else |
| 133 |
{ |
| 134 |
size_in_awords=read_alan_int_at(sf+3*4); |
| 135 |
|
| 136 |
if (!crc_is_correct(sf, size_in_awords)) |
| 137 |
{ |
| 138 |
switch (read_alan_int_at(sf+46*4)) |
| 139 |
{ |
| 140 |
case 1427594: |
| 141 |
case 8683866: */ |
| 142 |
return VALID_STORY_FILE_RV; |
| 143 |
default: |
| 144 |
return INVALID_STORY_FILE_RV; |
| 145 |
} |
| 146 |
} |
| 147 |
return VALID_STORY_FILE_RV; |
| 148 |
} |
| 149 |
} |