-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_struct.c
281 lines (237 loc) · 7.62 KB
/
example_struct.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* example_struct.c
**
** Copyright (C) 2019 Meadhbh S. Hamrick
** Released under a BSD License; see license.txt for details.
**
** This file implements a simple parser which uses the text lexxer to fill in
** values in a sample c data structure.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "textlex.h"
typedef struct {
unsigned char secret[20];
unsigned char salt[8];
unsigned int iterations;
} tTestStruct;
unsigned char * input_fixtures [] = {
"@m{\"iterations\"=12 \"salt\"=\"01234567\" \"secret\"=(8a 4d 21 92 03 82 3c f8 6a 05 c5 ea 0c 40 be f4 c8 02 76 33)}",
"@m\n{\n \"iterations\"=12\n \"salt\"=\"01234567\"\n \"secret\"=(8a 4d 21 92 03 82 3c f8 6a 05 c5 ea 0c 40 be f4 c8 02 76 33)\n}",
"@m\n{\n \"iterations\"=12\n \"salt\"=(37 36 35 34 33 32 31 30)\n \"secret\"=(8a 4d 21 92 03 82 3c f8 6a 05 c5 ea 0c 40 be f4 c8 02 76 33)\n}",
(unsigned char *) NULL
};
tTestStruct output_fixtures [] = {
{ {0x8a, 0x4d, 0x21, 0x92, 0x03, 0x82, 0x3c, 0xf8, 0x6a, 0x05, 0xc5, 0xea, 0x0c, 0x40, 0xbe, 0xf4, 0xc8, 0x02, 0x76, 0x33}, "01234567", 12},
{ {0x8a, 0x4d, 0x21, 0x92, 0x03, 0x82, 0x3c, 0xf8, 0x6a, 0x05, 0xc5, 0xea, 0x0c, 0x40, 0xbe, 0xf4, 0xc8, 0x02, 0x76, 0x33}, "01234567", 12},
{ {0x8a, 0x4d, 0x21, 0x92, 0x03, 0x82, 0x3c, 0xf8, 0x6a, 0x05, 0xc5, 0xea, 0x0c, 0x40, 0xbe, 0xf4, 0xc8, 0x02, 0x76, 0x33}, "76543210", 12}
};
void hexdump( unsigned char * data, unsigned int length ) {
unsigned int index;
unsigned char buffer[16];
for( index = 0; index < length; index++ ) {
if( 0 == (index % 16) ) {
printf( "%04X: ", index );
}
printf( "%02X ", ( (unsigned int) data[ index ] ) & 0xFF );
if( ( data[ index ] >= 32 ) && ( data[ index ] < 127 ) ) {
buffer[ index % 16 ] = data[ index ];
} else {
buffer[ index % 16 ] = '.';
}
if( 15 == (index % 16) ) {
printf( "[%16.16s]\n", buffer );
}
}
if( 0 != (index % 16) ) {
for( ; 0 != ( index % 16 ); index++ ) {
printf( " " );
buffer[ index % 16 ] = ' ';
}
printf( "[%16.16s]\n", buffer );
}
}
static unsigned char * token_name( unsigned int token ) {
static unsigned char * tokens [] = {
"END",
"COMMENT",
"ANNOTATION",
"LITERAL",
"INTEGER",
"FLOAT",
"HEX",
"STRING",
"BASE64",
"ARRAY_OPEN",
"ARRAY_CLOSE",
"MAP_OPEN",
"MAP_CLOSE",
"EQUALS",
"--UNDEFINED--"
};
return( ( token < TEXTLEX_C_TOKENS ) ? tokens[ token ] : tokens[ TEXTLEX_C_TOKENS ] );
}
unsigned int hex_to_int( char c ) {
static char *hex = "00112233445566778899AaBbCcDdEeFf";
unsigned int index = 0;
for( ; index < 32; index++ ) {
if( c == hex[ index ] ) {
break;
}
}
if( 32 == index ) {
index = 0;
} else {
index = index / 2;
}
return( index );
}
void hex_to_bytes( void * destination, unsigned char * source, unsigned int count ) {
unsigned int index = 0;
unsigned int half_index = 0;
unsigned int accumulator = 0;
unsigned char * bar = (unsigned char *) destination;
for( ; index < count; index++ ) {
if( 0 == index % 2 ) {
accumulator = hex_to_int( source[ index ] ) * 16;
half_index = index / 2;
} else {
accumulator += hex_to_int( source[ index ] );
bar[ half_index ] = accumulator;
}
}
}
unsigned int parse_this( unsigned char *input, tTestStruct *output ) {
tTextLexContext context;
unsigned char * buffer = NULL;
unsigned int buffer_size = 80;
unsigned int error = 0;
#define PARSE_S_START 0
#define PARSE_S_INMAP 1
#define PARSE_S_EEQUALS 2
#define PARSE_S_EVALUE 3
#define PARSE_S_END 4
unsigned int state = PARSE_S_START;
unsigned char key[ 20 ];
tTextLexErr overflow_handler( tTextLexContext *ncontext ) {
unsigned int error = TEXTLEX_E_NOERR;
unsigned int new_size = ncontext->size * 2;
unsigned char * new_buffer = realloc( buffer, new_size );
if( NULL == new_buffer ) {
error = TEXTLEX_E_ERROR;
} else {
ncontext->buffer = new_buffer;
ncontext->size = new_size;
}
return( error );
}
tTextLexErr token_handler( tTextLexContext * ncontext, tTextLexCount token ) {
switch( state ) {
case PARSE_S_START:
if( TEXTLEX_T_MAP_OPEN == token ) {
state = PARSE_S_INMAP;
}
break;
case PARSE_S_INMAP:
if( TEXTLEX_T_MAP_CLOSE == token ) {
state = PARSE_S_END;
} else if( TEXTLEX_T_STRING == token ) {
memcpy( key, ncontext->buffer, ncontext->index );
key[ ncontext->index ] = '\0';
state = PARSE_S_EEQUALS;
}
break;
case PARSE_S_EEQUALS:
if( TEXTLEX_T_EQUALS == token ) {
state = PARSE_S_EVALUE;
}
break;
case PARSE_S_EVALUE:
switch( token ) {
case TEXTLEX_T_STRING:
if( ! strcmp( "iterations", key ) ) {
output->iterations = (unsigned int) atoi( ncontext->buffer );
} else if( ! strcmp( "salt", key ) ) {
strncpy( output->salt, ncontext->buffer, ( 8 <= ncontext->index ) ? ncontext->index : 8 );
} else if( ! strcmp( "secret", key ) ) {
strncpy( output->secret, ncontext->buffer, ( 20 <= ncontext->index ) ? ncontext->index : 20);
}
break;
case TEXTLEX_T_INTEGER:
if( ! strcmp( "iterations", key ) ) {
output->iterations = (unsigned int) atoi( ncontext->buffer );
}
break;
case TEXTLEX_T_HEX:
if( ! strcmp( "iterations", key ) ) {
output->iterations = (unsigned int) strtol( ncontext->buffer, NULL, 16 );
} else if( ! strcmp( "salt", key ) ) {
hex_to_bytes( output->salt, ncontext->buffer, ncontext->index );
} else if( ! strcmp( "secret", key ) ) {
hex_to_bytes( output->secret, ncontext->buffer, ncontext->index );
}
break;
}
state = PARSE_S_INMAP;
break;
case PARSE_S_END:
break;
}
return( TEXTLEX_E_NOERR );
}
do {
if( NULL == ( buffer = malloc( buffer_size ) ) ) {
error = 1;
break;
}
if( TEXTLEX_E_NOERR != ( error = textlex_init( & context, buffer, buffer_size ) ) ) {
error = ( error << 3 ) + 2;
break;
}
context.token = token_handler;
context.overflow = overflow_handler;
if( TEXTLEX_E_NOERR != ( error = textlex_update( & context, input, strlen( input ) ) ) ) {
error = ( error << 3 ) + 3;
break;
}
if( TEXTLEX_E_NOERR != ( error = textlex_final( & context ) ) ) {
error = ( error << 3 ) + 4;
break;
}
} while( 0 );
if( NULL != buffer ) {
free( buffer );
}
return(error);
}
int main() {
tTestStruct test_struct;
unsigned int index = 0;
unsigned int error = 0;
unsigned char *current_input = input_fixtures[ index ];
tTestStruct *current_output = & output_fixtures[ index ];
while( (unsigned char *) NULL != current_input ) {
printf( "fixture %d: %s\n", index, current_input );
memset( &test_struct, 0, sizeof( tTestStruct ) );
error = parse_this( current_input, & test_struct );
if( 0 != error ) {
printf( "Error %d on test fixture %d\n", error, index );
} else {
printf( "Expected\n" );
hexdump( (unsigned char *) current_output, sizeof( tTestStruct ) );
printf( "Actual\n" );
hexdump( (unsigned char *) & test_struct, sizeof( tTestStruct ) );
if( ! memcmp( (void *) & test_struct, (void *) current_output, sizeof( tTestStruct ) ) ) {
printf( "Success!\n" );
}else {
printf( "Fail!\n" );
break;
}
printf( "\n" );
}
index++;
current_input = input_fixtures[ index ];
current_output = & output_fixtures[ index ];
}
return( 0 );
}