-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathencoding.c
More file actions
489 lines (457 loc) · 8.95 KB
/
encoding.c
File metadata and controls
489 lines (457 loc) · 8.95 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*******************************************************************************
*
* dynamic memory routines
*
******************************************************************************/
/*
* memory allocation error (FATAL no return!)
*/
void al_error( int length)
{
fprintf( stderr, "Fatal error: memory allocation failure: %d\n", length);
exit( -1);
}
/*
* allocate buffer memory routine
*/
char *al_buffer( char *text, int length)
{
char *buffer = 0;
if( 0 < length)
{
if( !( buffer = (char*)malloc( length)))
al_error( length);
else
{
/*
* initialize the buffer to zero and copy in text
*/
memset( (void *)buffer, 0, (size_t)length);
strncpy( buffer, text, length);
data.memory += length;
}
}
return( buffer);
}
/*
* deallocate buffer memory routine
*/
void de_buffer( char *buffer, int length)
{
if( 0 < length)
{
free( buffer);
data.memory -= length;
}
return;
}
/*
* allocate a clips data structure
*/
CLIPS *al_clips( int token, unsigned char value, int mask, char *buffer, int length)
{
CLIPS *clips;
if( !(clips = ( CLIPS*)malloc( sizeof( CLIPS))))
al_error( sizeof( CLIPS));
else
{
memset( (void *)clips, 0, (size_t)sizeof( CLIPS));
clips->token = token;
clips->value = value;
clips->mask = mask;
if( 0 < length)
{
clips->length = length;
clips->buffer = al_buffer( buffer, length);
}
data.memory += sizeof( CLIPS);
}
return( clips);
}
/*
* deallocate a clips data structure
*/
void de_clips( CLIPS *clips)
{
if( clips)
{
de_buffer( clips->buffer, clips->length);
free( clips);
data.memory -= sizeof( CLIPS);
}
return;
}
/*
* deallocate a clips linked list
*/
void de_clips_list( CLIPS *clips)
{
CLIPS *clips_next;
/*
* deallocate the clips linked list structure
*/
while( clips)
{
clips_next = clips->next;
de_clips( clips);
clips = clips_next;
}
return;
}
/*
* find the last clips structure in linked list
*/
CLIPS *end_clips( CLIPS *clips)
{
if( clips)
while( clips->next)
clips = clips->next;
return clips;
}
/*
* print a clips structure
*/
void print_clips( CLIPS *clips)
{
printf("token: %s ", tokens[clips->token % TYPEDEF]);
printf("buffer: %s ", clips->buffer);
printf("mask: %x \n",clips->mask);
}
/*
* print a clips linked list
*/
void print_clips_list( CLIPS *clips)
{
/*
* print the clips linked list structure
*/
while( clips)
{
print_clips( clips);
clips = clips->next;
}
return;
}
/*******************************************************************************
*
* lowest level ansi_c audit routines
*
******************************************************************************/
/*
* check for conversion error
*/
int ansi_c_audit_conversion( unsigned char byte, unsigned int integer, char *text, int length)
{
if( byte != integer)
{
fprintf( stderr, "Warning: data conversion error\n");
data.warnings++;
}
return 0;
}
/*
* check for overflow condition
*/
int ansi_c_audit_overflow( unsigned char byte, unsigned int integer, char *text, int length)
{
if( 255 < integer)
{
fprintf( stderr, "Warning: overflow in implicit constant conversion\n");
data.warnings++;
}
return 0;
}
/*
* check for multi character constant
*/
int ansi_c_audit_multi_character( unsigned char byte, unsigned int integer, char *text, int length)
{
switch( text[ 1])
{
case '\\':
switch( text[ 2])
{
case 'n': /* newline */
case 'r': /* cr */
case 't': /* tab */
case 'b': /* backspace */
case '\\':
case '?':
case '\'':
case '"':
case 'x':
case 'X':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
break;
default:
if( text[ 2] != '\'')
{
fprintf( stderr, "Warning: multi-character character constant\n");
data.warnings++;
}
}
}
return 0;
}
int i;
/*
* check the digits for octal
*/
int ansi_c_audit_octal_digits( unsigned char byte, unsigned int integer, char *text, int length)
{
for( i = length - 1; 0 <= i; i--)
{
switch( text[ i])
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case 'l':
case 'L':
case 'u':
case 'U':
break;
default:
fprintf( stderr, "Error: invalid digit \"%c\" in octal constant\n", text[ i]);
data.errors++;
return 1;
}
}
return 0;
}
/*******************************************************************************
*
* highest level ansi_c audit routines
*
******************************************************************************/
/*
* audit the char constant
*/
void ansi_c_audit_char( unsigned char byte, unsigned int integer, char *text, int length)
{
if( ansi_c_audit_conversion( byte, integer, text, length))
return;
if( ansi_c_audit_overflow( byte, integer, text, length))
return;
if( ansi_c_audit_multi_character( byte, integer, text, length))
return;
return;
}
/*
* audit the decimal constant
*/
void ansi_c_audit_decimal( unsigned char byte, unsigned int integer, char *text, int length)
{
if( ansi_c_audit_conversion( byte, integer, text, length))
return;
if( ansi_c_audit_overflow( byte, integer, text, length))
return;
return;
}
/*
* audit the hex constant
*/
void ansi_c_audit_hex( unsigned char byte, unsigned int integer, char *text, int length)
{
if( ansi_c_audit_conversion( byte, integer, text, length))
return;
if( ansi_c_audit_overflow( byte, integer, text, length))
return;
return;
}
/*
* audit the octal constant
*/
void ansi_c_audit_octal( unsigned char byte, unsigned int integer, char *text, int length)
{
if( ansi_c_audit_conversion( byte, integer, text, length))
return;
if( ansi_c_audit_overflow( byte, integer, text, length))
return;
if( ansi_c_audit_octal_digits( byte, integer, text, length))
return;
return;
}
/*
* audit the float constant
*/
void ansi_c_audit_float( unsigned char byte, float f, char *text, int length)
{
int integer;
/*
* check for floating point errors
*/
integer = (int)f;
if( ansi_c_audit_conversion( byte, integer, text, length))
return;
if( ansi_c_audit_overflow( byte, integer, text, length))
return;
return;
}
/*******************************************************************************
*
* yystype routines
*
******************************************************************************/
/*******************************************************************************
*
* scanner encode routines now using CLIPS
*
******************************************************************************/
/*
* encode a constant hex value
*/
CLIPS *constant_hex( char *text, int length)
{
CLIPS *clips;
int x = 0;
sscanf( text, "%x", &x);
clips = al_clips( LITERAL, 0, 0, 0, 0);
//ansi_c_audit_hex( clips->value, x, text, length);
return( clips);
}
/*
* encode a constant octal value
*/
CLIPS *constant_octal( char *text, int length)
{
CLIPS *clips;
int o = 0;
sscanf( text, "%o", &o);
clips = al_clips( LITERAL, 0, 0, 0, 0);
//ansi_c_audit_octal( clips->value, o, text, length);
return( clips);
}
/*
* encode a constant decimal value
*/
CLIPS *constant_decimal( char *text, int length)
{
CLIPS *clips;
int u = 0;
sscanf( text, "%u", &u);
clips = al_clips( LITERAL, 0, 0, 0, 0);
//ansi_c_audit_decimal( clips->lValue, u, text, length);
return( clips);
}
/*
* encode a constant character value
*/
CLIPS *constant_char( char *text, int length)
{
CLIPS *clips;
unsigned int c = 0;
switch( text[ 1])
{
case '\\':
switch( text[ 2])
{
case 'n': /* newline */
c = 0x0a;
break;
case 'r': /* cr */
c = 0x0d;
break;
case 't': /* tab */
c = 0x09;
break;
case 'b': /* backspace */
c = 0x08;
break;
case '\\':
c = 0x5c;
break;
case '?':
c = 0x3f;
break;
case '\'':
c = 0x27;
break;
case '"':
c = 0x22;
break;
case 'x':
case 'X':
text[1] = '0';
sscanf( &text[1], "%x", &c);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
sscanf( &text[2], "%o", &c);
break;
default:
{
int i;
for( i = 2; i < length; i++)
{
if( text[ i] == '\'')
break;
c = text[ i];
}
}
break;
}
break;
default:
c = text[1];
break;
}
clips = al_clips( LITERAL, (unsigned char)c,0, 0, 0);
//ansi_c_audit_char( clips->value, c, text, length);
return( clips);
}
/*
* encode a constant floating point value
*/
CLIPS *constant_float( char *text, int length)
{
CLIPS *clips;
float f = 0;
sscanf( text, "%f", &f);
clips = al_clips( LITERAL, 0, 0, 0, 0);
//ansi_c_audit_float( clips->value, f, text, length);
return( clips);
}
/*
* encode a string literal
*/
CLIPS *string_literal( char *text, int length)
{
CLIPS *clips;
/*
* do not need the beginning " and also remove the ending " but add one for nul (-2 + 1 = -1)
*/
//text[ length - 1] = 0;
clips = al_clips( LITERAL, 0, 0, text, length + 1);
return( clips);
}
/*
* encode an identifier
*/
CLIPS *identifier( char *text, int length)
{
CLIPS *clips;
/*
* but add one for nul
*/
clips = al_clips( IDENTIFIER, 0, 0, text, length + 1);
return( clips);
}