| 1 | |
| 2 | /* Extensible array container */ |
| 3 | |
| 4 | typedef struct _sequence { |
| 5 | int size; |
| 6 | int position; |
| 7 | void **array; |
| 8 | } sequence; |
| 9 | |
| 10 | void sequence_init(sequence *t); |
| 11 | void sequence_dispose(sequence *t); |
| 12 | void sequence_push(sequence *t, void *elem); |
| 13 | void *sequence_pop(sequence *t); |
| 14 | |
| 15 | |
| 16 | /* Hash table */ |
| 17 | |
| 18 | typedef struct _table { |
| 19 | sequence keys; |
| 20 | sequence values; |
| 21 | } table; |
| 22 | |
| 23 | void table_init(table *t); |
| 24 | void table_dispose(table *t); |
| 25 | void table_insert(table *t, char *key, void *value); |
| 26 | void *table_find(table *t, char *key); |
| 27 | |
| 28 | |
| 29 | /* Token types */ |
| 30 | |
| 31 | typedef enum _token_type { |
| 32 | ENDFILE, |
| 33 | COMA, |
| 34 | LPARENTHESIS, |
| 35 | RPARENTHESIS, |
| 36 | SEMICOLON, |
| 37 | EQUAL, |
| 38 | QUOTEDSTRING, |
| 39 | NUMBER, |
| 40 | NAME |
| 41 | } token_type; |
| 42 | |
| 43 | |
| 44 | /* State associated with a file being parsed */ |
| 45 | typedef struct _parse_file { |
| 46 | char *name; |
| 47 | FILE * fp; |
| 48 | int lineno; |
| 49 | char *buffer; |
| 50 | token_type type; |
| 51 | int unget; |
| 52 | void (*error) (struct _parse_file *, char *); |
| 53 | } parse_file; |
| 54 | |
| 55 | void ungetToken(parse_file * in); |
| 56 | char *getToken(parse_file *in); |
| 57 | char *getComa(parse_file *in); |
| 58 | char *getLParenthesis(parse_file *in); |
| 59 | char *getRParenthesis(parse_file *in); |
| 60 | char *getSemiColon(parse_file *in); |
| 61 | char *getQuotedString(parse_file *in); |
| 62 | char *getName(parse_file *in); |
| 63 | int getNumber(parse_file *in); |
| 64 | char * getEqual(parse_file *in); |
| 65 | |
| 66 | void skipComment(parse_file * in); |
| 67 | void skipEOL(parse_file * in); |
| 68 | int isalpha(char car); |
| 69 | int isalnum(char car); |
| 70 | |
| 71 | /* Some constants */ |
| 72 | |
| 73 | static const int BUFFER_SIZE = 1024; |
| 74 | |
| 75 | |
| 76 | /* Events data types */ |
| 77 | |
| 78 | typedef enum _data_type { |
| 79 | INT, |
| 80 | UINT, |
| 81 | FLOAT, |
| 82 | STRING, |
| 83 | ENUM, |
| 84 | ARRAY, |
| 85 | SEQUENCE, |
| 86 | STRUCT, |
| 87 | NONE |
| 88 | } data_type; |
| 89 | |
| 90 | |
| 91 | /* Event type descriptors */ |
| 92 | |
| 93 | typedef struct _type_descriptor { |
| 94 | data_type type; |
| 95 | char *fmt; |
| 96 | int size; |
| 97 | sequence labels; // for enumeration |
| 98 | sequence fields; // for structure |
| 99 | struct _type_descriptor *nested_type; // for array and sequence |
| 100 | } type_descriptor; |
| 101 | |
| 102 | |
| 103 | /* Fields within types */ |
| 104 | |
| 105 | typedef struct _field{ |
| 106 | char *name; |
| 107 | char *description; |
| 108 | type_descriptor *type; |
| 109 | } field; |
| 110 | |
| 111 | |
| 112 | /* Events definitions */ |
| 113 | |
| 114 | typedef struct _event { |
| 115 | char *name; |
| 116 | char *description; |
| 117 | type_descriptor *type; |
| 118 | int nested; |
| 119 | } event; |
| 120 | |
| 121 | int getSize(parse_file *in); |
| 122 | unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type, int * nestedStruct); |
| 123 | |
| 124 | void parseEvent(parse_file *in, event *ev, sequence * unnamed_types, table * named_types); |
| 125 | void parseTypeDefinition(parse_file *in, sequence * unnamed_types, table * named_types); |
| 126 | type_descriptor *parseType(parse_file *in, type_descriptor *t, sequence * unnamed_types, table * named_types); |
| 127 | void parseFields(parse_file *in, type_descriptor *t, sequence * unnamed_types, table * named_types); |
| 128 | void checkNamedTypesImplemented(table * namedTypes); |
| 129 | type_descriptor * find_named_type(char *name, table * named_types); |
| 130 | void generateChecksum(char * facName, unsigned long * checksum, sequence * events); |
| 131 | |
| 132 | |
| 133 | |
| 134 | static char *intOutputTypes[] = { |
| 135 | "int8_t", "int16_t", "int32_t", "int64_t", "short int", "int", "long int" }; |
| 136 | |
| 137 | static char *uintOutputTypes[] = { |
| 138 | "uint8_t", "uint16_t", "uint32_t", "uint64_t", "unsigned short int", |
| 139 | "unsigned int", "unsigned long int" }; |
| 140 | |
| 141 | static char *floatOutputTypes[] = { |
| 142 | "undef", "undef", "float", "double", "undef", "float", "double" }; |
| 143 | |
| 144 | |
| 145 | /* Dynamic memory allocation and freeing */ |
| 146 | |
| 147 | void * memAlloc(int size); |
| 148 | char *allocAndCopy(char * str); |
| 149 | char *appendString(char *s, char *suffix); |
| 150 | void freeTypes(sequence *t); |
| 151 | void freeType(type_descriptor * td); |
| 152 | void freeEvents(sequence *t); |
| 153 | void freeNamedType(table * t); |
| 154 | void error_callback(parse_file *in, char *msg); |
| 155 | |
| 156 | |
| 157 | //checksum part |
| 158 | static const unsigned int crctab32[] = |
| 159 | { |
| 160 | #include "crc32.tab" |
| 161 | }; |
| 162 | |
| 163 | static inline unsigned long |
| 164 | partial_crc32_one(unsigned char c, unsigned long crc) |
| 165 | { |
| 166 | return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8); |
| 167 | } |
| 168 | |
| 169 | static inline unsigned long |
| 170 | partial_crc32(const char *s, unsigned long crc) |
| 171 | { |
| 172 | while (*s) |
| 173 | crc = partial_crc32_one(*s++, crc); |
| 174 | return crc; |
| 175 | } |
| 176 | |
| 177 | static inline unsigned long |
| 178 | crc32(const char *s) |
| 179 | { |
| 180 | return partial_crc32(s, 0xffffffff) ^ 0xffffffff; |
| 181 | } |
| 182 | |