-
Notifications
You must be signed in to change notification settings - Fork 1
/
Process.cpp
1958 lines (1658 loc) · 68.9 KB
/
Process.cpp
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include <string>
#include <map>
#include "Process.h"
#include "BBHeader.h"
#include "BBHeader.h"
#include "BRHeader.h"
#include "BSFileHeader.h"
#include "VMSBackupTypes.h"
#include "alignment.h"
//============================================================================
#include "wildcards.h"
#include "timevmstounix.h"
//============================================================================
// Temporary Kludge... change this to the desired EOL format
// if required. Albeit VMSWriteFile will need it's ASCII Mode modified if that's
// the case.
const char szEndOfLine[] = {'\r','\n'};
void VMSWriteFile (SBackupParameters* psParameters, unsigned long uiDataLength, uint8_t* cBuffer, FILE** ppCurrentOutputFile, bool* pbLastElementWasLFCR, bool* pbContainsLFCR)
{
unsigned long uiBufferPointer;
bool bLastElementWasLFCR = false;
bool bContainsLFCR = false;
// TODO: This is covering up a bug elsewhere where 0 byte writes are somehow being passed onwards...
if (0 == uiDataLength)
{
return;
}
if (psParameters == NULL)
{
// Assume Binary
fwrite (cBuffer, uiDataLength, sizeof (uint8_t), *ppCurrentOutputFile);
}
else if (psParameters->bExtractModeASCII)
{
// ASCII Mode
for (uiBufferPointer = 0; uiBufferPointer < uiDataLength; uiBufferPointer++)
{
if ((cBuffer[uiBufferPointer] == '\r') && !psParameters->bLFDetected)
{
// Do nothing whilst the EOL is assessed
psParameters->bLFDetected = true;
// Indicate this data package contains an LF/CR entry
bContainsLFCR = true;
bLastElementWasLFCR = (uiBufferPointer+1) == uiDataLength;
}
else if (psParameters->bLFDetected && (cBuffer[uiBufferPointer] == '\n'))
{
// This file already contains standard EOL conventions
fwrite (szEndOfLine, sizeof(szEndOfLine) / sizeof (char), sizeof (char), *ppCurrentOutputFile);
psParameters->bLFDetected = false;
// Indicate this data package contains an LF/CR entry
bContainsLFCR = true;
bLastElementWasLFCR = (uiBufferPointer+1) == uiDataLength;
}
else if (!psParameters->bLFDetected && (cBuffer[uiBufferPointer] == '\n'))
{
// This is not seen as a valid EOL, so normalise it to a PC standard
fwrite (szEndOfLine, sizeof(szEndOfLine) / sizeof (char), sizeof (char), *ppCurrentOutputFile);
psParameters->bLFDetected = false;
// Indicate this data package contains an LF/CR entry
bContainsLFCR = true;
bLastElementWasLFCR = (uiBufferPointer+1) == uiDataLength;
}
else if (psParameters->bLFDetected && (cBuffer[uiBufferPointer] != '\n'))
{
// This is not seen as a valid EOL, so normalise it to a PC standard
fwrite (szEndOfLine, sizeof(szEndOfLine) / sizeof (char), sizeof (char), *ppCurrentOutputFile);
psParameters->bLFDetected = false;
// Indicate this data package contains an LF/CR entry
bContainsLFCR = true;
//! This indicates the *previous* byte was an LF/CR therefore the current
//! element isn't, hence no check to see if the Last Element is an LF/CR
// Output the current byte since it contained non-EOL data
fwrite (&cBuffer[uiBufferPointer] , 1, sizeof (char), *ppCurrentOutputFile);
}
else
{
fwrite (&cBuffer[uiBufferPointer] , 1, sizeof (char), *ppCurrentOutputFile);
psParameters->bLFDetected = false;
}
}
}
else
{
// Binary/Raw Mode
fwrite (cBuffer, uiDataLength, sizeof (uint8_t), *ppCurrentOutputFile);
}
if (pbLastElementWasLFCR != NULL)
{
*pbLastElementWasLFCR = bLastElementWasLFCR;
}
if (pbContainsLFCR != NULL)
{
*pbContainsLFCR = bContainsLFCR;
}
}
void VMSWriteEOL (SBackupParameters* psParameters, FILE** ppCurrentOutputFile, bool bForceEOL)
{
if (psParameters->bExtractModeASCII)
{
// If the last byte was \ '\r' it won't have been written
if (((psParameters->uiFilePointer >= psParameters->uiFileSize) && psParameters->bLFDetected))
{
fwrite (szEndOfLine, sizeof(szEndOfLine) / sizeof (char), sizeof (char), *ppCurrentOutputFile);
psParameters->bLFDetected = false;
}
else if (bForceEOL)
{
fwrite (szEndOfLine, sizeof(szEndOfLine) / sizeof (char), sizeof (char), *ppCurrentOutputFile);
}
}
}
void DecodeTimeAndDate (uint16_t uiRaw[4], uint32_t uiSubSecondResolution)
{
//////////////////////////////////////////////////////////////////////////
// Constants
// Month Lookup Table
const char* cszMonth[] = { "JAN", "FEB", "MAR", "APR",
"MAY", "JUN", "JUL", "AUG",
"SEP", "OCT", "NOV", "DEC" };
//////////////////////////////////////////////////////////////////////////
// Variables
// VMS Raw Time encoded as a 64 Bit Integer
int64_t* uiVMS = (int64_t*) uiRaw;
// Subsecond Time
uint32_t uiTimeSubSeconds = 0;
// Time Structure
time_t sDate;
// Time Stored in GMT
struct tm* sGMTTime;
// Open VMS Time is a 64 Bit value representing 100ns tics since 00:00 November 17, 1858
// (Modified Julian Day Zero)
// http://h71000.www7.hp.com/wizard/wiz_2315.html
// C Time Stamp is a 32 Bit value representing seconds since January 1 1970
if ( (uiRaw[0] == 0) &&
(uiRaw[1] == 0) &&
(uiRaw[2] == 0) &&
(uiRaw[3] == 0) )
{
fprintf (stdout, "<None specified>");
}
else
{
// Convert the VMS Format timestamp to a "UNIX" (i.e., C format) timestamp
*uiVMS = SWAPLONGLONG (*uiVMS);
timevmstounix (uiVMS, &sDate);
// Convert time to GM Time
sGMTTime = gmtime(&sDate);
if (uiSubSecondResolution == 0)
{
// Output the Date/Time to match the Open VMS directory listing
fprintf (stdout, "%i-%s-%i %02i:%02i:%02i", sGMTTime->tm_mday,
cszMonth[sGMTTime->tm_mon],
sGMTTime->tm_year + 1900,
sGMTTime->tm_hour,
sGMTTime->tm_min,
sGMTTime->tm_sec );
}
else
{
// The above algorithm will only have a resolution of seconds, therefore
// extra processing is required in order to get milliseconds
// VMS Time has an LSB of 100ns
// Convert to Hundredths of a Second
uiTimeSubSeconds = (uint32_t) (*uiVMS % (int64_t) 10000000);
uiTimeSubSeconds = uiTimeSubSeconds / (uint32_t) pow ((float) 10, (float) (7-uiSubSecondResolution));
// Output the Date/Time to match the Open VMS directory listing
fprintf (stdout, "%i-%s-%i %02i:%02i:%02i.%02i", sGMTTime->tm_mday,
cszMonth[sGMTTime->tm_mon],
sGMTTime->tm_year + 1900,
sGMTTime->tm_hour,
sGMTTime->tm_min,
sGMTTime->tm_sec,
uiTimeSubSeconds );
}
}
}
void DecodeFileProtection (uint8_t cValue)
{
if ((cValue & 0x1) == 0)
{
fprintf (stdout, "R");
}
if ((cValue & 0x2) == 0)
{
fprintf (stdout, "W");
}
if ((cValue & 0x4) == 0)
{
fprintf (stdout, "E");
}
if ((cValue & 0x8) == 0)
{
fprintf (stdout, "D");
}
}
void DecodeRecordFormat (uint8_t cValue, uint16_t uiSize)
{
switch (cValue & 0x0F)
{
case RECORD_FORMAT_UDF:
{
break;
}
case RECORD_FORMAT_FIX:
{
fprintf (stdout, "Fixed length 512 byte records");
break;
}
case RECORD_FORMAT_VAR:
{
fprintf (stdout, "Variable length");
if (uiSize != 0)
{
fprintf (stdout, ", maximum %i bytes", uiSize);
}
break;
}
case RECORD_FORMAT_VFC:
{
fprintf (stdout, "VFC, 2 byte header");
if (uiSize != 0)
{
fprintf (stdout, ", maximum %i bytes", uiSize);
}
break;
}
case RECORD_FORMAT_STM:
{
fprintf (stdout, "Stream");
if (uiSize != 0)
{
fprintf (stdout, ", maximum %i bytes", uiSize);
}
break;
}
case RECORD_FORMAT_STMLF:
{
fprintf (stdout, "Stream_LF");
if (uiSize != 0)
{
fprintf (stdout, ", maximum %i bytes", uiSize);
}
break;
}
case RECORD_FORMAT_STMCR:
{
if (uiSize != 0)
{
fprintf (stdout, ", maximum %i bytes", uiSize);
}
break;
}
default:
{
break;
}
}
}
void DecodeRecordAttributes (uint8_t cValue, SBackupParameters* psParameters)
{
switch (cValue)
{
case RECORD_ATTRIBUTE_FTN:
{
fprintf (stdout, "None");
break;
}
case RECORD_ATTRIBUTE_CR:
{
break;
}
case RECORD_ATTRIBUTE_CRN:
{
fprintf (stdout, "Carriage return carriage control");
break;
}
case RECORD_ATTRIBUTE_BLK:
{
break;
}
case RECORD_ATTRIBUTE_PRN:
{
fprintf (stdout, "Print file carriage control");
break;
}
default:
{
if (!psParameters->bExtractFirstPass)
{
fprintf (stderr, "WARNING : Unknown attribute %i\n", cValue);
}
break;
}
}
}
void DumpHeader (BBHeader* pHeader, SBackupParameters* psParameters)
{
fprintf (stdout, "Save set: %s\n", pHeader->T_SSNAME ());
// fprintf (stdout, "Written by: \n");
// fprintf (stdout, "UIC: \n");
// fprintf (stdout, "Date: \n");
// fprintf (stdout, "Command: \n");
// fprintf (stdout, "Operating system: %04X\n", pHeader->W_OPSYS ());
// fprintf (stdout, "BACKUP version: \n");
// fprintf (stdout, "CPU ID register: \n");
// fprintf (stdout, "Node name: \n");
// fprintf (stdout, "Written on: \n");
fprintf (stdout, "Block size: %i\n", pHeader->L_BLOCKSIZE ());
// fprintf (stdout, "Group size: \n");
// fprintf (stdout, "Buffer count: \n");
fprintf (stdout, "\n");
}
void DumpBriefFileHeader (BSFileHeader* pcFileHeader, uint32_t uiSubSecondResolution)
{
//////////////////////////////////////////////////////////////////////////
// Variables
// Output the File Name
fprintf (stdout, "%s\n", pcFileHeader->FILENAME ());
// Output the File Size
if (SWAPSHORT (((uint16_t*) pcFileHeader->RECATTR ())[6]) == 0)
{
fprintf (stdout, " Size: %7i/%-7i",
SWAPSHORT (((uint16_t*) pcFileHeader->RECATTR ())[5]) - 1,
SWAPLONG (((uint32_t*) pcFileHeader->FILESIZE ())[0]));
}
else
{
fprintf (stdout, " Size: %7i/%-7i",
SWAPSHORT (((uint16_t*) pcFileHeader->RECATTR ())[5]) - 0,
SWAPLONG (((uint32_t*) pcFileHeader->FILESIZE ())[0]));
}
// Output the Creation Date
fprintf (stdout, " Created: ");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->CREDATE (), uiSubSecondResolution);
fprintf (stdout, "\n");
}
void DumpFullFileHeader (BSFileHeader* pcFileHeader, BRHeader* pcHeader, SBackupParameters* psParameters)
{
// TO BE CLEANED
// Change 0 to 1 to have the output format match a directory /full syntax, otherwise it will
// match a backup set view.
#if 0
//////////////////////////////////////////////////////////////////////////
// Variables
//////////////////////////////////////////////////////////////////////////
// Dump the Full File Header (Directory Format)
// Output the File Name
fprintf (stdout, "%s", pcFileHeader->FILENAME ());
// Output the File Id
fprintf (stdout, " File ID: (%i,%i,%i)\n", SWAPSHORT (((uint16_t*) pcFileHeader->FID ())[0]),
SWAPSHORT (((uint16_t*) pcFileHeader->FID ())[1]),
SWAPSHORT (((uint16_t*) pcFileHeader->FID ())[2])-1);
// Output the File Size
if (SWAPSHORT (((uint16_t*) pcFileHeader->RECATTR ())[6]) == 0)
{
fprintf (stdout, "Size: %12i/%-12i",
SWAPSHORT (((uint16_t*) pcFileHeader->RECATTR ())[5]) - 1,
SWAPLONG (((uint32_t*) pcFileHeader->FILESIZE ())[0]));
}
else
{
fprintf (stdout, "Size: %12i/%-12i",
SWAPSHORT (((uint16_t*) pcFileHeader->RECATTR ())[5]) - 0,
SWAPLONG (((uint32_t*) pcFileHeader->FILESIZE ())[0]));
}
// Output the Owner
fprintf (stdout, "Owner: [%06o,%06o]\n", SWAPSHORT (((uint16_t*) pcFileHeader->UIC ())[1]),
SWAPSHORT (((uint16_t*) pcFileHeader->UIC ())[0]) );
// Output the Creation Date
fprintf (stdout, "Created: ");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->CREDATE (), 2);
fprintf (stdout, "\n");
// Output the Revised Date
fprintf (stdout, "Revised: ");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->REVDATE (), 2);
fprintf (stdout, " (%i)\n", SWAPSHORT (*((uint16_t*) pcFileHeader->REVISION ())));
// Output the Expiry Date
fprintf (stdout, "Expires: ");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->EXPDATE (), 2);
fprintf (stdout, "\n");
// Output the Backup Date
fprintf (stdout, "Backup: ");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->BAKDATE (), 2);
fprintf (stdout, "\n");
// Output File Organisation
//! Not enough data collated to determine other enumerations
fprintf (stdout, "File organization: ");
if (!pcHeader->V_NONSEQUENTIAL ())
{
fprintf (stdout, "Sequential\n");
}
else
{
fprintf (stdout, "????\n");
}
// File Attributes
fprintf (stdout, "File attributes: ");
// File Attributes - Allocation
fprintf (stdout, "Allocation: %i", SWAPLONG (((uint32_t*) pcFileHeader->FILESIZE ())[0]));
// File Attributes - Extend
//! No Idea, always 0
fprintf (stdout, ", Extend: %i", 0);
// File Attributes - Global Buffer Count
//! No Idea, always 0
fprintf (stdout, ", Global Buffer Count: %i", 0);
// File Attributes - , Version Limit
fprintf (stdout, ", Version limit: %i", SWAPSHORT (((uint16_t*) pcFileHeader->VERLIMIT ())[0]));
// File Attributes - ????
//! No Idea, sometimes ", Contiguous-best-try", the check below seems to
//! work for test data
if (SWAPSHORT (((uint16_t*) pcFileHeader->UCHAR ())[0]) == 32)
{
fprintf (stdout, ", Contiguous best try\n");
}
else
{
fprintf (stdout, "\n");
}
// Record Format
fprintf (stdout, " Record format: ");
DecodeRecordFormat (pcFileHeader->RECATTR ()[0], SWAPSHORT (pcFileHeader->RECSIZE ()[0]));
fprintf (stdout, "\n");
// Record Attributes
fprintf (stdout, " Record attributes: ");
DecodeRecordAttributes (pcFileHeader->RECATTR ()[1], psParameters);
fprintf (stdout, "\n");
// Output File Protection
fprintf (stdout, " File protection: ");
fprintf (stdout, "System:");
DecodeFileProtection (pcFileHeader->FPRO ()[0] & 0xF);
fprintf (stdout, ", Owner:");
DecodeFileProtection (pcFileHeader->FPRO ()[0] >> 4);
fprintf (stdout, ", Group:");
DecodeFileProtection (pcFileHeader->FPRO ()[1] & 0xF);
fprintf (stdout, ", World:");
DecodeFileProtection (pcFileHeader->FPRO ()[1] >> 4);
fprintf (stdout, "\n");
// Seperate the File Entries
fprintf (stdout, "\n");
#else
//////////////////////////////////////////////////////////////////////////
// Variables
// Temporary Strings
char* szTempString1 = new char[32];
char* szTempString2 = new char[32];
// First dump the brief header
DumpBriefFileHeader (pcFileHeader, 0);
// Output the Owner
fprintf (stdout, " Owner: [%06o,%06o] ", SWAPSHORT (((uint16_t*) pcFileHeader->UIC ())[1]),
SWAPSHORT (((uint16_t*) pcFileHeader->UIC ())[0]) );
// Output the Revised Date
fprintf (stdout, "Revised: ");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->REVDATE (), 0);
fprintf (stdout, " (%i)\n", SWAPSHORT (*((uint16_t*) pcFileHeader->REVISION ())));
// Output the File Id
//! Note : As a quirk, we actually cut the string if it's too wide
sprintf (szTempString1, "(%i,%i,%i) ",
SWAPSHORT (((uint16_t*) pcFileHeader->FID ())[0]),
SWAPSHORT (((uint16_t*) pcFileHeader->FID ())[1]),
SWAPSHORT (((uint16_t*) pcFileHeader->FID ())[2]) );
strncpy (szTempString2, szTempString1, 14);
szTempString2[14] = '\0';
fprintf (stdout, " File ID: %s", szTempString2);
// Output the Expiry Date
fprintf (stdout, " Expires: ");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->EXPDATE (), 0);
fprintf (stdout, "\n");
// Output the Backup Date
fprintf (stdout, " Backup: ");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->BAKDATE (), 0);
fprintf (stdout, "\n");
// Output File Protection
fprintf (stdout, " File protection: ");
fprintf (stdout, "System:");
DecodeFileProtection (pcFileHeader->FPRO ()[0] & 0xF);
fprintf (stdout, ", Owner:");
DecodeFileProtection (pcFileHeader->FPRO ()[0] >> 4);
fprintf (stdout, ", Group:");
DecodeFileProtection (pcFileHeader->FPRO ()[1] & 0xF);
fprintf (stdout, ", World:");
DecodeFileProtection (pcFileHeader->FPRO ()[1] >> 4);
fprintf (stdout, "\n");
// Output File Organisation
//! Not enough data collated to determine other enumerations
fprintf (stdout, " File organization: ");
if (!pcHeader->V_NONSEQUENTIAL ())
{
fprintf (stdout, "Sequential\n");
}
else
{
fprintf (stdout, "????\n");
}
// File Attributes
fprintf (stdout, " File attributes: ");
// File Attributes - Allocation
fprintf (stdout, "Allocation = %i", SWAPLONG (((uint32_t*) pcFileHeader->FILESIZE ())[0]));
// File Attributes - Extend
//! No Idea, always 0
fprintf (stdout, ", Extend = %i\n", 0);
// File Attributes - Global Buffer Count
//! No Idea, always 0
fprintf (stdout, " Global Buffer Count = %i", 0);
// File Attributes - ????
//! No Idea, sometimes ", Contiguous-best-try", the check below seems to
//! work for test data
if (SWAPSHORT (((uint16_t*) pcFileHeader->UCHAR ())[0]) == 32)
{
fprintf (stdout, ", Contiguous-best-try\n");
}
else
{
fprintf (stdout, "\n");
}
// Record Format
fprintf (stdout, " Record format: ");
DecodeRecordFormat (pcFileHeader->RECATTR ()[0], SWAPSHORT (pcFileHeader->RECSIZE ()[0]));
fprintf (stdout, "\n");
// Record Attributes
fprintf (stdout, " Record attributes: ");
DecodeRecordAttributes (pcFileHeader->RECATTR ()[1], psParameters);
fprintf (stdout, "\n");
// Seperate the File Entries
fprintf (stdout, "\n");
// Remove all allocations
delete (szTempString1);
delete (szTempString2);
#endif
}
void DumpCSVFileHeader (BSFileHeader* pcFileHeader, BRHeader* pcHeader, SBackupParameters* psParameters)
{
//////////////////////////////////////////////////////////////////////////
// Variables
// Output the File Name
fprintf (stdout, "\"%s\",", pcFileHeader->FILENAME ());
// Output the File Size
if (SWAPSHORT (((uint16_t*) pcFileHeader->RECATTR ())[6]) == 0)
{
fprintf (stdout, "%i,%i,",
SWAPSHORT (((uint16_t*) pcFileHeader->RECATTR ())[5]) - 1,
SWAPLONG (((uint32_t*) pcFileHeader->FILESIZE ())[0]));
}
else
{
fprintf (stdout, "%i,%i,",
SWAPSHORT (((uint16_t*) pcFileHeader->RECATTR ())[5]) - 0,
SWAPLONG (((uint32_t*) pcFileHeader->FILESIZE ())[0]));
}
// Output the Creation Date
fprintf (stdout, "\"");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->CREDATE (), 7);
fprintf (stdout, "\"");
fprintf (stdout, ",");
// Output the Owner
fprintf (stdout, "%06o,%06o,", SWAPSHORT (((uint16_t*) pcFileHeader->UIC ())[1]),
SWAPSHORT (((uint16_t*) pcFileHeader->UIC ())[0]) );
// Output the Revised Date
fprintf (stdout, "\"");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->REVDATE (), 7);
fprintf (stdout, "\"");
fprintf (stdout, ",");
fprintf (stdout, "%i,", SWAPSHORT (*((uint16_t*) pcFileHeader->REVISION ())));
// Output the File Id
fprintf (stdout, "%i,%i,%i,", SWAPSHORT (((uint16_t*) pcFileHeader->FID ())[0]),
SWAPSHORT (((uint16_t*) pcFileHeader->FID ())[1]),
SWAPSHORT (((uint16_t*) pcFileHeader->FID ())[2]) );
// Output the Expiry Date
fprintf (stdout, "\"");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->EXPDATE (), 7);
fprintf (stdout, "\"");
fprintf (stdout, ",");
// Output the Backup Date
fprintf (stdout, "\"");
DecodeTimeAndDate ((uint16_t*) pcFileHeader->BAKDATE (), 7);
fprintf (stdout, "\"");
fprintf (stdout, ",");
// Output File Protection
fprintf (stdout, "\"");
DecodeFileProtection (pcFileHeader->FPRO ()[0] & 0xF);
fprintf (stdout, "\"");
fprintf (stdout, ",");
fprintf (stdout, "\"");
DecodeFileProtection (pcFileHeader->FPRO ()[0] >> 4);
fprintf (stdout, "\"");
fprintf (stdout, ",");
fprintf (stdout, "\"");
DecodeFileProtection (pcFileHeader->FPRO ()[1] & 0xF);
fprintf (stdout, "\"");
fprintf (stdout, ",");
fprintf (stdout, "\"");
DecodeFileProtection (pcFileHeader->FPRO ()[1] >> 4);
fprintf (stdout, "\"");
fprintf (stdout, ",");
// Output File Organisation
//! Not enough data collated to determine other enumerations
if (!pcHeader->V_NONSEQUENTIAL ())
{
fprintf (stdout, "Sequential,");
}
else
{
fprintf (stdout, "????,");
}
// File Attributes
// File Attributes - Allocation
fprintf (stdout, "%i,", SWAPLONG (((uint32_t*) pcFileHeader->FILESIZE ())[0]));
// File Attributes - Extend
//! No Idea, always 0
fprintf (stdout, "%i,", 0);
// File Attributes - Global Buffer Count
//! No Idea, always 0
fprintf (stdout, "%i,", 0);
// File Attributes - ????
//! No Idea, sometimes ", Contiguous-best-try", the check below seems to
//! work for test data
if (SWAPSHORT (((uint16_t*) pcFileHeader->UCHAR ())[0]) == 32)
{
fprintf (stdout, "Contiguous-best-try,");
}
else
{
fprintf (stdout, ",");
}
// Record Format
fprintf (stdout, "\"");
DecodeRecordFormat (pcFileHeader->RECATTR ()[0], pcFileHeader->RECSIZE ()[0]);
fprintf (stdout, "\"");
fprintf (stdout, ",");
// Record Attributes
fprintf (stdout, "\"");
DecodeRecordAttributes (pcFileHeader->RECATTR ()[1], psParameters);
fprintf (stdout, "\"");
// Seperate the File Entries
fprintf (stdout, "\n");
}
// It is assumed by the time this routine is called, that SetNewerFile will
// have parsed the entire file list to construct a list of latest versions
bool IsTargetFile (const std::string& kFileName, const char* szTargetExtractMaskVersion, const int iTargetExtractVersion, const FileVersionType& kFileList)
{
//////////////////////////////////////////////////////////////////////////
// Variables
// Extracted Data
std::string kExtractedName = kFileName;
int iExtractedVersion = 0;
bool bTarget = false;
// Extract the File Name and Version Number
if (std::string::npos != kFileName.find(';'))
{
kExtractedName = kExtractedName.substr(0, kExtractedName.find(';'));
iExtractedVersion = strtol(kFileName.data()+kFileName.find(';')+1, NULL, 10);
}
if (kFileList.find(kExtractedName) != kFileList.cend())
{
if (strcmp (szTargetExtractMaskVersion, "*") == 0)
{
// An asterisk indicates all versions
bTarget = true;
}
else
{
// Determine if this version is correct
if (iTargetExtractVersion > 0)
{
// Version must be exact
bTarget = iTargetExtractVersion == iExtractedVersion;
}
else
{
// This is a relative version
// So ;0 means it must be the latest version
// ;-1 means it must be the version prior to the latest
// etc.
bTarget = iExtractedVersion == (kFileList.at(kExtractedName) + iTargetExtractVersion);
}
}
}
return bTarget;
}
void SetNewerFile (const std::string& kFileName, FileVersionType& kFileList) //!SFileLinkedList* psFileList)
{
//////////////////////////////////////////////////////////////////////////
// Variables
// Extracted Data
std::string kExtractedName = kFileName;
int iExtractedVersion = 0;
// Extract the File Name and Version Number
if (std::string::npos != kFileName.find(';'))
{
kExtractedName = kExtractedName.substr(0, kExtractedName.find(';'));
iExtractedVersion = strtol(kFileName.data()+kFileName.find(';')+1, NULL, 10);
}
if (kFileList.find(kExtractedName) == kFileList.cend())
{
// By Default this must be the latest version
kFileList.insert(std::pair<std::string, int>(kExtractedName, iExtractedVersion));
}
else
{
// Determine if this is a later version
if (iExtractedVersion >= kFileList.at(kExtractedName))
{
kFileList[kExtractedName] = iExtractedVersion;
}
}
}
void SetFileMode (const std::string& kFileName, FileFormatType& kTypeList, const bool bASCII)
{
// Check if this is the first entry
if (kTypeList.find(kFileName) == kTypeList.end())
{
// Set the Entry
kTypeList.insert(std::pair<std::string, bool>(kFileName, bASCII));
}
else
{
kTypeList[kFileName] = bASCII;
}
}
bool GetFileMode (const std::string& kFileName, FileFormatType& kTypeList)
{
// Check if the Entry Exists
if (kTypeList.find(kFileName) == kTypeList.end())
{
return false;
}
else
{
return kTypeList[kFileName];
}
}
void ProcessFile (uint8_t* cBuffer, BRHeader* pcHeader, SBackupParameters* psParameters, FILE** ppCurrentOutputFile, std::string& kCurrentOutputFile, FileVersionType& kFileList, FileFormatType& kTypeList)
{
//////////////////////////////////////////////////////////////////////////
// Variables
// File Header
BSFileHeader cFileHeader;
// Modified File Name
char* szModifiedFileName;
// Target File
bool bTargetFile = false;
// Wild Card Match
bool bWildCardMatch = true;
// File Name no mask
char* szFileNameNoMask;
//////////////////////////////////////////////////////////////////////////
// Convert the File Record into a series of streams
cFileHeader.LoadBSFileHeader (cBuffer, pcHeader->W_RSIZE ());
// Allocate and copy the File Name
szFileNameNoMask = new char[strlen((char*) cFileHeader.FILENAME ())+1];
strcpy (szFileNameNoMask, (char*) cFileHeader.FILENAME ());
// Strip the Version Delimiter if required
if (strstr (szFileNameNoMask, ";"))
{
*strstr (szFileNameNoMask, ";") = '\0';
}
// See if this is a file that needs processing
bWildCardMatch = wildcardfit ( psParameters->szExtractMask,
szFileNameNoMask ) == 1;
// Delete the Non-Masked Filename
delete (szFileNameNoMask);
//////////////////////////////////////////////////////////////////////////
// Handle Older Versions
if (bWildCardMatch)
{
// Add the file to the list, in order to generate a linked list of latest file versions only
SetNewerFile ((char*) cFileHeader.FILENAME (), kFileList);
}
//////////////////////////////////////////////////////////////////////////
// Handle Data Extraction if required
if (psParameters->bExtract)
{
// Determine if this is a file we need to extract
if (bWildCardMatch)
{
// Determine if this is the Target File
bTargetFile = IsTargetFile ( (char*) cFileHeader.FILENAME (),
psParameters->szExtractMaskVersion,
psParameters->iExtractVersion,
kFileList );
// Inform the Parser that it is to ignore the VBN if this is not
// the target revision
psParameters->bIgnoreVBN = !bTargetFile;
// If this is a first pass and Smart Scan is enabled
if (psParameters->bExtractModeSmart && psParameters->bExtractFirstPass && bTargetFile)
{
// Default the File Mode to ASCII
psParameters->bExtractModeASCII = true;
psParameters->bExtractModeBinary = false;
SetFileMode ((char*) cFileHeader.FILENAME (), kTypeList, true);
kCurrentOutputFile = (char*) cFileHeader.FILENAME ();
//////////////////////////////////////////////////////////////////////
// DEBUG (ENHANCED)
if (psParameters->bExtractDebugEnhanced)
{
// Output the Chosen Parameters
fprintf (stdout, "*** DEBUG *** ");
fprintf (stdout, "Beginning smart parse for %s\n", (char*) cFileHeader.FILENAME ());
}
//////////////////////////////////////////////////////////////////////
// END DEBUG (ENHANCED)
}
else if (bTargetFile)
{
//////////////////////////////////////////////////////////////////////
// DEBUG (ENHANCED)
if (psParameters->bExtractDebugEnhanced)
{
// Output the Chosen Parameters
fprintf (stdout, "*** DEBUG *** ");
fprintf (stdout, "Beginning parse/extract for %s\n", (char*) cFileHeader.FILENAME ());
}
//////////////////////////////////////////////////////////////////////
// END DEBUG (ENHANCED)
}
// Store the File Size Parameters
psParameters->bLFDetected = false;
psParameters->uiFilePointer = 0;
psParameters->uiFileSize = (SWAPSHORT (((uint16_t*) cFileHeader.RECATTR ())[5]) * 512) - 512 + SWAPSHORT (((uint16_t*) cFileHeader.RECATTR ())[6]);
psParameters->cFormat = cFileHeader.RECATTR ()[0] & 0x0F;
psParameters->uiRemainingRecordLength = 0;
psParameters->uiRemainingStartPos = 0;
// If this is not the first pass (or we're in single pass mode)
if (!psParameters->bExtractFirstPass && bTargetFile)
{
if (psParameters->bExtractModeSmart)
{
psParameters->bExtractModeASCII = GetFileMode ((char*) cFileHeader.FILENAME (), kTypeList);
psParameters->bExtractModeBinary = !psParameters->bExtractModeASCII;
}
//////////////////////////////////////////////////////////////////////
// DEBUG
if (psParameters->bExtractDebug)
{
// Output the Chosen Parameters
fprintf (stdout, "*** DEBUG *** ");
fprintf (stdout, "Using ");
if (psParameters->bExtractModeASCII)
{
fprintf (stdout, "ASCII");
}
else if (psParameters->bExtractModeBinary)
{
fprintf (stdout, "BINARY");