-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.c
More file actions
559 lines (433 loc) · 17 KB
/
Main.c
File metadata and controls
559 lines (433 loc) · 17 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Bits_n_Bytes.h"
#include "Tag_List.h"
#include "File_Loading.h"
int main(int argc, char* argv[]) {
//Source_File Variables
FILE * source_file_fp = NULL;
char * source_file_arr = NULL;
int file_size = 0;
unsigned int current_file_pos = 0; // where in the file is in the current Nibble?
int TLV_Block_pos = 0; // where in the file has the TLV Data been printed up until?
//Parsing Variables
unsigned int First_Nibble_In_Byte;
unsigned int * First_Nibble_In_Byte_ptr = &First_Nibble_In_Byte;
unsigned int TagField_Size_Bytes;
unsigned int * TagField_Size_Bytes_ptr = &TagField_Size_Bytes;
unsigned int nibble_flags = 0; // Initialising All Processing Flags 0000 0000
unsigned int * nibble_flags_ptr = &nibble_flags;
int Reading_Status = 0;
int Length = 0; //This is the length of the value field element.
int Length_Field_Size_Bytes = 0;
int Length_Field_Pos = 0;
int* Length_Field_Pos_ptr = &Length_Field_Pos;
int Invalid_Data_Found = 0;
int Resized_Array_Complete = 0;
unsigned int y = 0;
int ReadyforNextNibble = 0;
char input_nibble_cleaned;
char input_nibble_str[2];
int Depth = 0;
//Buffers
char* Temp_Buffer = NULL;
TLV_Block* Head_TLV_Block = NULL;
TLV_Block* Active_TLV_Block = NULL;
TLV_Block* Active_Parent_TLV_Block = NULL;
TLV_Block* Previous_TLV_Block = NULL;
char * Output = NULL;
char * Temp;
//Verify Input File and Establish it's size in Nibbles.
source_file_fp = verify_inputs(argc, argv);
if (source_file_fp == NULL) {
printf("Exiting...\n");
return 0;
}
else {
file_size = get_file_size(source_file_fp); //Null Terminating Character required.
}
//Create and write the file content to an array, close the file.
source_file_arr = malloc((file_size + 1) * sizeof(char));
if (source_file_arr == NULL) {
printf("NAH BRO\n");
return 0;
}
fread(source_file_arr, sizeof(char), file_size, source_file_fp);
source_file_arr[file_size] = '\0';
printf("Input:\n %s\n\n", source_file_arr);
printf("Beginning Tag Processing...\n\n");
TagField_Size_Bytes = 1;
Reading_Status = 1;
Set_Bit(Processing_Tag, nibble_flags_ptr);
Set_Bit(Processing_First_Nibble, nibble_flags_ptr);
for (current_file_pos = 0; source_file_arr[current_file_pos] != '\0'; current_file_pos++) {
if (current_file_pos != 0) {
if (Is_Bit_Set(Processing_First_Nibble, nibble_flags_ptr)) {
UnSet_Bit(Processing_First_Nibble, nibble_flags_ptr);
}
else {
Set_Bit(Processing_First_Nibble, nibble_flags_ptr);
}
}
printf("Reading Status Pre_Nibble Read\n");
Debug_ReadingStatus(nibble_flags_ptr, Reading_Status);
input_nibble_cleaned = Clean_Input((source_file_arr[current_file_pos]));
input_nibble_str[0] = input_nibble_cleaned;
input_nibble_str[1] = '\0';
printf("INPUT CHARACTER IS CURRENTLY: %c\n", input_nibble_cleaned);
printf("IS IT FIRST NIBBLE? %d\n\n", Is_Bit_Set(Processing_First_Nibble, nibble_flags_ptr));
//Invalid Data Check
if ((input_nibble_cleaned == '#') || (Invalid_Data_Found == 1)) {
Reading_Status = 6;
Invalid_Data_Found = 1;
}
ReadyforNextNibble = 0;
while (ReadyforNextNibble == 0) {
switch (Reading_Status) {
case(1):
//Setting up Tag Buffer.
if ((Temp_Buffer == NULL)) {
Temp_Buffer = malloc(((sizeof * Temp_Buffer) * ((TagField_Size_Bytes * 2) + 1)));
if (Temp_Buffer != NULL) {
*Temp_Buffer = '\0';
}
else {
printf("Failed to allocate Memory!\n");
printf("Exiting like a champ...\n");
return 0;
}
}
else {
//If B5 and B4 of the first Byte of the Tag suggest a 2 Byte Tag, we have to expand the tag buffer.
if (Is_Bit_Set(Processing_Subsequent_Field_FirstNibFlag, nibble_flags_ptr) && Is_Bit_Set(Processing_Subsequent_Field, nibble_flags_ptr)) {
printf("We need to Realloc the Tag Buffer");
Temp = realloc(Temp_Buffer, ((sizeof(char) * TagField_Size_Bytes * 2) + 1));
if (Temp != NULL) {
Temp_Buffer = Temp;
Temp = NULL;
}
else {
printf("Failed Reallocation of Memory.\n");
printf("Exiting...\n");
return 0;
}
}
else {
printf("No Realloc required Buffer");
}
//If the Input Data is not invalid, then pass the character to be Processed.
}
Tag_Processing(input_nibble_cleaned, nibble_flags_ptr, TagField_Size_Bytes_ptr);
printf("\nReading Status After Tag Processing\n");
Debug_ReadingStatus(nibble_flags_ptr, Reading_Status);
strcat(Temp_Buffer, input_nibble_str);
ReadyforNextNibble = 1;
break;
case(2):
if (Temp_Buffer != NULL) {
//Are there any existing TLV Blocks?
if (Active_TLV_Block == NULL) {
//This is the first TLV Block, and therefore is the head of the linked list. (Head_TLV_Block).
Active_TLV_Block = Create_New_TLV_Block(Depth, Head_TLV_Block);
Head_TLV_Block = Active_TLV_Block;
Active_TLV_Block->Head = NULL;
}
else {
//Are there any previous TLV Blocks?
if (Previous_TLV_Block == NULL) {
//The Active TLV Block is the First TLV Block, there is nothing to inherit characteristics from.
Previous_TLV_Block = Active_TLV_Block;
//Active_TLV_Block = Create_New_TLV_Block(Depth, Head_TLV_Block);
}
}
if ((Active_TLV_Block != NULL) && (Previous_TLV_Block != NULL)) {
if (Active_TLV_Block != Previous_TLV_Block) {
Previous_TLV_Block = Active_TLV_Block;
Active_TLV_Block = NULL;
}
//An Active TLV Block exists, and a previous TLV Block. there could be characterisitcs that have to be inherited from the previous block.
//Checking for constructed object Nesting/Inheritance.
//Is the Active_TLV_Block within the constructed template of the previous TLV Block?
if ((Previous_TLV_Block->Constructed == 1)) {
if (current_file_pos <= Previous_TLV_Block->FilePos_at_EndofBlock) {
//The Active TLV_Block is within the Constructed Object range of the previous block. Therefore has the previous TLV Block as it's Parent.
Depth = Depth + 1;
Active_Parent_TLV_Block = Previous_TLV_Block;
Active_TLV_Block = Create_New_TLV_Block(Depth, Head_TLV_Block);
Previous_TLV_Block->Child = Active_TLV_Block;
Active_TLV_Block->Parent = Active_Parent_TLV_Block;
}
else {
//The Active TLV_Block is outside the range of the previous constructed TLV_Block. Are there other constructed objects that it is a part of?
Depth = Active_Parent_TLV_Block->Depth;
while (current_file_pos > Active_Parent_TLV_Block->FilePos_at_EndofBlock) {
if (Active_Parent_TLV_Block->Parent != NULL) {
//If another parent exists, and the Parent to the Active block has not yet been found, move to the next parent.
Active_Parent_TLV_Block = Active_Parent_TLV_Block->Parent;
//Is the Active TLV Block within the constructed range of this object?
if (current_file_pos <= Active_Parent_TLV_Block->FilePos_at_EndofBlock) {
//if it is, then this parent is a constructed data object that contains the active TLV Block
Active_TLV_Block->Parent = Active_Parent_TLV_Block;
}
}
else {
//There is no more parents to move to, this TLV_Block must be on the same level as the Head TLV Block (0th level constructed data object).
Active_Parent_TLV_Block = NULL;
Depth = 0;
Head_TLV_Block->Next = Active_TLV_Block;
Active_TLV_Block->Previous = Head_TLV_Block;
Head_TLV_Block = Active_TLV_Block;
break;
}
}
}
}
else {
//The Previous TLV_Block was a Primitive Object, are we within any existing constructed/parent objects?
if (Active_Parent_TLV_Block != NULL) {
if (current_file_pos <= Active_Parent_TLV_Block->FilePos_at_EndofBlock) {
//These two TLV Blocks share the same Parent TLV_Block.
Active_TLV_Block = Create_New_TLV_Block(Depth, Head_TLV_Block);
Active_TLV_Block->Parent = Active_Parent_TLV_Block;
Previous_TLV_Block->Next = Active_TLV_Block;
Active_TLV_Block->Previous = Previous_TLV_Block;
}
else {
//we're outside of this Parent TLV_Block. Are there other parent TLVBlocks/Constructed Data objects to inherit from?
while (Active_Parent_TLV_Block->Parent != NULL) {
Active_Parent_TLV_Block = Active_Parent_TLV_Block->Parent;
if (current_file_pos > Active_Parent_TLV_Block->FilePos_at_EndofBlock) {
//we're outside of the range of this constructed object, move to the next parent block
Active_Parent_TLV_Block = Active_Parent_TLV_Block->Parent;
}
else {
//we'ere inside the range of this constructed object, inherit the properties of this Active_Parent_Block.
Active_TLV_Block->Parent = Active_Parent_TLV_Block;
//Does this parent block have any existing children?
if (Active_Parent_TLV_Block->Child == NULL) {
Active_Parent_TLV_Block->Child = Active_TLV_Block;
break;
}
else {
//if it does have a child, then find the next->Null of that child, and insert the Active_Block to indicate a shared constructed level.
while ((Active_Parent_TLV_Block->Child)->Next != NULL) {
Active_TLV_Block->Previous = (Active_Parent_TLV_Block->Child)->Next;
}
Active_TLV_Block->Previous = (Active_Parent_TLV_Block->Child)->Next;
(Active_Parent_TLV_Block->Child)->Next = Active_TLV_Block;
break;
}
}
}
//if no parent has been found, this is a header level primiive
if (Active_Parent_TLV_Block->Parent == NULL) {
}
}
}
//The Active_Parent_TLV_Block is NULL, this Active_TLV_Block is not contained by any Constructed Data objects, must be a Headblock.
else {
Active_TLV_Block->Next = Create_New_TLV_Block(Depth, Head_TLV_Block);
Active_TLV_Block = Active_TLV_Block->Next;
Active_TLV_Block->Previous = Previous_TLV_Block;
Head_TLV_Block = Active_TLV_Block;
}
}
}
else {
printf("Narda\n.");
}
}
else{
printf("Temp Buffer is equal to NULL!\n");
printf("Exiting...\n");
return 0;
}
//Writing Tag into TLV_Block newly constructed Active_TLV_Block:
Active_TLV_Block->Tag = malloc(sizeof(Temp_Buffer));
if ((Active_TLV_Block->Tag) != NULL) {
strcpy((Active_TLV_Block->Tag), Temp_Buffer);
free(Temp_Buffer);
Temp_Buffer = NULL;
}
//Determining Corresponding Tag Definition and writing it to Active_TLV_BLock:
Find_Tag_Def(&(Active_TLV_Block->Tag_Def), Active_TLV_Block->Tag, TagList);
//Writing Constructed Data Object information to Active Tag_Block:
Active_TLV_Block->Constructed = Is_Bit_Set(Processing_Constructed_Data_Object, nibble_flags_ptr);
Set_Bit(Processing_LengthField, nibble_flags_ptr);
Reading_Status = 3;
ReadyforNextNibble = 0;
//Need to amend the function above, to pass pointer to Length value. All LengthField Processing should be done within the function.
break;
//Determining Length_Field_Size
case(3):
Length_Field_Size_Bytes = LengthField_Processing(input_nibble_str, Length_Field_Size_Bytes, nibble_flags_ptr);
if (Is_Bit_Set(Processing_Length, nibble_flags_ptr)) {
ReadyforNextNibble = 0;
Reading_Status = 4;
}
else {
//the first nibble of the Length Field is 8 or higher, indicating a length field coded over multiple bytes. Move to the next Nibble to determine how much.
Length_Field_Pos++;
ReadyforNextNibble = 1;
Reading_Status = 3;
}
break;
case(4):
//Length Field Processing Complete. Realloc the Output_String_Replace_me Buffer to a size where Length Can be Written.
if (Temp_Buffer == NULL){
Temp_Buffer = malloc(((Length_Field_Size_Bytes * 2) + 1) * sizeof(char));
if (Temp_Buffer != NULL) {
*Temp_Buffer = '\0';
}
else {
printf("Failed to alloc Memory.\n");
printf("Exiting...\n");
return 0;
}
}
//Determining Size of the Length Value.
else {
if (Length_Field_Pos < (Length_Field_Size_Bytes * 2)) {
strcat(Temp_Buffer, input_nibble_str);
Length_Field_Pos++;
ReadyforNextNibble = 1;
}
else {
if (Length_Field_Pos == (Length_Field_Size_Bytes * 2)) {
if (Temp_Buffer != NULL) {
Active_TLV_Block->Length = malloc((sizeof(char) * (strlen(Temp_Buffer) + 1)));
if (Active_TLV_Block->Length != NULL) {
strcpy(Active_TLV_Block->Length, Temp_Buffer);
Length = Length_Processing(Temp_Buffer, nibble_flags_ptr, Length_Field_Size_Bytes);
Active_TLV_Block->FilePos_at_EndofBlock = (current_file_pos + Length) - 1;
if (Is_Bit_Set(Processing_Constructed_Data_Object, nibble_flags_ptr)) {
UnSet_Bit(Processing_Length, nibble_flags_ptr);
Set_Bit(Processing_Tag, nibble_flags_ptr);
Reading_Status = 1;
Length_Field_Pos = 0;
Length_Field_Size_Bytes = 0;
Length = 0;
ReadyforNextNibble = 0;
}
else {
UnSet_Bit(Processing_Length, nibble_flags_ptr);
Set_Bit(Processing_Value, nibble_flags_ptr);
Reading_Status = 5;
ReadyforNextNibble = 0;
}
Length_Field_Pos = 0;
free(Temp_Buffer);
Temp_Buffer = NULL;
}
else {
printf("Failed to allocate for Value writing after Length written.\n");
printf("Exiting...\n");
return 0;
}
}
else {
printf("Failed to realloc memory for Temp_Buffer\n");
printf("Exiting...\n");
return 0;
}
}
}
}
break;
case(5):
//Writing the Value data, using the Length Value determined.
if (Temp_Buffer == NULL) {
Temp_Buffer = malloc(sizeof(char) * (Length + 1));
if (Temp_Buffer != NULL) {
*Temp_Buffer = '\0';
}
else {
printf("Failed to allocate for Value writing after Length written.\n");
printf("Exiting...\n");
return 0;
}
}
//Length_Field_Pos variable is now used to track the position of the current value Nibble being read.
if (Length_Field_Pos < (Length)) {
strcat(Temp_Buffer, input_nibble_str);
Length_Field_Pos++;
ReadyforNextNibble = 1;
if (Length_Field_Pos == (Length)) {
UnSet_Bit(Processing_Value, nibble_flags_ptr);
Active_TLV_Block->Value = malloc(sizeof(char) * (Length + 1));
//Writing to Active_TLV_Block
if (Active_TLV_Block->Value != NULL) {
*(Active_TLV_Block->Value) = '\0';
strcat(Active_TLV_Block->Value, Temp_Buffer);
free(Temp_Buffer);
Temp_Buffer = NULL;
}
else {
printf("Failed to Allocate ACTIVE_TLV_BLOCK->VALUE.\n");
return 0;
}
Debug_ReadingStatus(nibble_flags_ptr, Reading_Status);
//Need to add in Constructed Data Processing flags here.
Length_Field_Pos = 0;
Length_Field_Size_Bytes = 0;
Length = 0;
}
}
else {
}
break;
case(6):
if (y == 0) {
if (Output == NULL) {
Output = malloc((strlen(source_file_arr) + 1) * sizeof(char));
}
else {
free(Output);
Output = NULL;
Output = malloc((strlen(source_file_arr) + 1) * sizeof(char));
}
for (y = 0; y < current_file_pos; y++) {
Output[y] = source_file_arr[y];
}
Output[y] = '\0';
strcat(Output, input_nibble_str);
}
else {
if (input_nibble_cleaned == '#') {
strcat(Output, "#");
}
else {
strcat(Output, input_nibble_str);
printf("%s", Output);
}
}
ReadyforNextNibble = 1;
break;
default:
printf("Unhandled State.\n");
break;
}
}
printf("Determining Reading Status After Nibble Read...\n");
Reading_Status = Determine_Reading_Status(nibble_flags_ptr, Invalid_Data_Found);
printf("Reading_Status End of Nibble Read : \n");
Debug_ReadingStatus(nibble_flags_ptr, Reading_Status);
printf("Moving to Next Nibble...\n\n\n");
}
printf("OUTPUTS:\n\n");
if (Invalid_Data_Found == 1) {
printf("Invalid Data Found, Invalid characters marked as ""#""\n\n");
printf("%s\n\n", Output);
printf("Exiting...");
}
else {
Output[0] = '\0';
Output = Print_Output(Active_TLV_Block, Output);
printf("%s", Output);
}
free(source_file_arr);
fclose(source_file_fp);
printf("Exiting...");
return 0;
}