-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
1055 lines (810 loc) · 37.3 KB
/
index.js
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
let fs = require("fs");
const {table} = require('table');
let defnPage = 2//2//75//81 //2,4, 5, 18, 42
let headerJetVersion = 4
let showDebug = false
var offset = 0
var tempoffset
let wholeDb = {}
let numPages = null
let binary = null
let tablesToPageNums = {}
// -----------------------------------------------------------------------
// load( MS ACCESS FILE NAME )
//
// This kicks things off by loading the database schema into memory
//
// -----------------------------------------------------------------------
exports.load = function(options) {
let dbFileName = options.fileName
var stats = fs.statSync(dbFileName)
var fileSizeInBytes = stats["size"]
numPages = (fileSizeInBytes / 4096) + 1
let useCatalog = options.useCatalog
if (options.usePage) {
defnPage = parseInt(options.usePage)
} else if (!useCatalog) {
defnPage = 2
}
if (options.showDebug) {
showDebug = options.showDebug
}
if (showDebug) {
console.log("Load Access file: " + dbFileName);
console.log("fileSizeInBytes: " + fileSizeInBytes )
console.log("")
console.log("")
}
binary = fs.readFileSync(dbFileName);
//
// do stuff with the functions
//
findDataPages()
getTableDefinitionForPage(defnPage)
populateDataForTableDefinedOnPage(defnPage)
let tableD = this.getTableDataForPage(2)
for (let fg=0; fg<tableD.length;fg++){
tablesToPageNums[ tableD[fg].Name ] = tableD[fg].Id
}
//return wholeDb
let result
if (options.showTable) {
result = table(genData(defnPage))
} else {
result = createData(defnPage)
}
return {loaded: true}
}
exports.getWholeDb = function() {
return wholeDb
}
exports.getTablePages = function(colName) {
return tablesToPageNums
}
exports.getColumns = function(tableName) {
getTableDefinitionForPage(tablesToPageNums[tableName])
return Object.keys(wholeDb.table_pages[tablesToPageNums[tableName]].columnNames)
}
exports.getTables = function() {
let schemaList = []
for (let xx=0;xx<wholeDb.table_pages[2].data.length ; xx++){
if (wholeDb.table_pages[2].data[xx].data) {
if (wholeDb.table_pages[2].data[xx].data.Name) {
schemaList.push(wholeDb.table_pages[2].data[xx].data.Name)
}
}
}
return schemaList
}
exports.getTableData = function(tableName) {
return this.getTableDataForPage(tablesToPageNums[tableName])
}
exports.getTableDataForPage = function(localPageNum) {
let tableData = []
if (!wholeDb.table_pages[localPageNum].data) {
console.log("Populate data for page: " + localPageNum)
getTableDefinitionForPage(localPageNum)
populateDataForTableDefinedOnPage(localPageNum)
console.log("Populate data for page: " + localPageNum + " done.")
}
for (let xx=0;xx<wholeDb.table_pages[localPageNum].data.length ; xx++){
let rowData = {}
if (wholeDb.table_pages[localPageNum].data[xx].data) {
rowData = wholeDb.table_pages[localPageNum].data[xx].data
if (rowData && (Object.keys(rowData).length > 0)) {
tableData.push(rowData)
}
}
}
return tableData
}
// -----------------------------------------------------------------------
//
//
//
//
// -----------------------------------------------------------------------
function byteArrayToLong(/*byte[]*/byteArray) {
var value = 0;
for ( var i = byteArray.length - 1; i >= 0; i--) {
value = (value * 256) + byteArray[i];
}
return value;
};
// -----------------------------------------------------------------------
//
//
//
//
// -----------------------------------------------------------------------
function find(aoffset, length , typeob) {
let value = binary.slice(aoffset, aoffset + length)
if (typeob=="number") {
return byteArrayToLong(value)
} else if (typeob == "littleendian") {
value = [value[2],value[1],value[0]]
return byteArrayToLong(value)
}
return value
}
// -----------------------------------------------------------------------
//
//
//
//
// -----------------------------------------------------------------------
function getVar(params) {
if (params.useJetVersion) {
if (headerJetVersion != params.useJetVersion) {
console.log("Skipping " + params.name)
return null
}
}
let retvalue = find(tempoffset , params.length, params.type)
tempoffset = tempoffset + params.length
if (params.type == "string") {
retvalue = retvalue.toString()
}
return retvalue
}
// -----------------------------------------------------------------------
//
//
//
//
// -----------------------------------------------------------------------
function getColumnType(colType) {
switch(colType) {
case 1:
return "Boolean"
case 2:
return "Integer, 8 bit"
case 3:
return "Integer, 16 bit"
case 4:
return "Integer, 32 bit"
case 5:
return "Fixed Point Number, 64 bit (Money / Currency)"
case 6:
return "Floating Point Number, 32 bit (single)"
case 7:
return "Floating Point Number, 64 bit (double)"
case 8:
return "Date/Time, 64 bit, (stored as double)"
case 9:
return "Binary (up to 255 bytes)"
case 10:
return "Text (up to 255 characters)"
case 11:
return "OLE (long binary)"
case 12:
return "Memo (long Text)"
case 15:
return "GUID (global unique identifier)"
case 16:
return "Fixed Point, 96 bit, stored in 17 bytes"
case 18:
return "Complex field (32 bit integer key)"
break;
default:
return "Unknown"
// code block
}
}
// ---------------------------------------------------------------------------+
//
// +---------------+
// | findDataPages |
// +---------------+
//
//
// Every MS Access database has a set of tables, each of which contain data.
//
// In an MS Access file this data is stored across many pages, in pages known
// as data pages. What this "findDataPages" method does is to go
// through all the pages in the MS Access file and find out which
// pages belong to which tables. We can tell this as there is a pointer from
// each data page to the table definition page, as shown below (****)
//
//
// +--------------------------------------------------------------------------+
// | Jet4 Data Page Definition |
// +------+---------+---------------------------------------------------------+
// | data | length | name | description |
// +------+---------+---------------------------------------------------------+
// | 0x01 | 1 byte | page_type | 0x01 indicates a data page. |
// | | 1 byte | unknown | |
// | | 2 bytes | free_space | Free space in this page |
// | | 4 bytes | tdef_pg | Page pointer to table definition (****) |
// | | 4 bytes | unknown | |
// | | 2 bytes | num_rows | number of records on this page |
// +--------------------------------------------------------------------------+
//
// Once we have the pages read in then we store them like so in the global
// "wholeDb" variable:
//
// wholeDb
// {
// table_pages
// {
// 2
// {
// pages
// [
// {
// pagenum: 17,
// recordcount: 43
// },
// {
// pagenum: 122
// recordcount: 7
// }
// ],
//
// -----------------------------------------------------------------------
function findDataPages() {
let listOfTableDefPages = {}
//
// for every page in the MS access database file
//
for ( let currentPage = 0; currentPage < numPages; currentPage++ ){
tempoffset = 4096 * currentPage
//
// if this page is a data page ...
//
let page_type = getVar({length: 1,name: "Page Type",type: "number"})
if (page_type == 0x01) {
getVar({length: 1,name: "Unknown"})
getVar({length: 2,name: "Free Space",type: "number"})
//
// get the page number of the table definition
//
let tdef_pg = getVar({length: 4,name: "tdef_pg",type: "number"})
getVar({length: 4,name: "Unknown"})
let RecordCount = getVar({length: 2,name: "RecordCount",type: "number"})
if (tdef_pg < 2) {
} else if (tdef_pg > 10000) {
} else if (!listOfTableDefPages[tdef_pg]) {
listOfTableDefPages[tdef_pg] = {
data_pages: [{pagenum: currentPage, recordcount: RecordCount}]
}
} else {
listOfTableDefPages[tdef_pg].data_pages.push({pagenum: currentPage, recordcount: RecordCount})
}
}
}
wholeDb.table_pages = listOfTableDefPages
}
// -----------------------------------------------------------------------
//
// +----------------------------+
// | getTableDefinitionForPage |
// +----------------------------+
//
// This reads the table definition and stores it in "wholeDb". The
// originating MS Access structure for table definition page is:
//
//
// +-------------------------------------------------------------------------+
// | Jet4 Table Definition Header
// +------+---------+-------------+------------------------------------------+
// | data | length | name | description |
// +------+---------+-------------+------------------------------------------+
// | 0x02 | 1 bytes | page_type | 0x02 indicate a table defn page |
// | | 1 bytes | unknown | |
// | | 2 bytes | tdef_id | (jet4) Free space on this page minus 8 |
// | 0x00 | 4 bytes | next_pg | Next tdef page pointer (0 if none) |
// +------+---------+-------------+------------------------------------------+
//
//
// +-------------------------------------------------------------------------+
// | Jet4 Table Definition Block (55 bytes) |
// +------+---------+-------------+------------------------------------------+
// | data | length | name | description |
// +------+---------+-------------+------------------------------------------+
// | | 4 bytes | tdef_len | Length of the data for this page |
// | | 4 bytes | unknown | unknown |
// | | 4 bytes | num_rows | Number of records in this table |
// | 0x00 | 4 bytes | autonumber | value for the next value of the |
// | | | | autonumber column, if any. 0 otherwise |
// | 0x01 | 1 byte | autonum_flag| 0x01 makes autonumbers work in access |
// | | 3 bytes | unknown | unknown |
// | 0x00 | 4 bytes | ct_autonum | autonumber value for complex type column(s) |
// | | | | (shared across all columns in the table) |
// | | 8 bytes | unknown | unknown |
// | 0x4e | 1 byte | table_type | 0x4e: user table, 0x53: system table |
// | | 2 bytes | max_cols | Max columns a row will have (deletions) |
// | | 2 bytes | num_var_cols| Number of variable columns in table |
// | | 2 bytes | num_cols | Number of columns in table (repeat) |
// | | 4 bytes | num_idx | Number of logical indexes in table |
// | | 4 bytes | num_real_idx| Number of index entries |
// | | 4 bytes | used_pages | Points to a record containing the |
// | | | | usage bitmask for this table. |
// | | 4 bytes | free_pages | Points to a similar record as above, |
// | | | | listing pages which contain free space. |
// +-------------------------------------------------------------------------+
// | Iterate for the number of num_real_idx (12 bytes per idxs) |
// +-------------------------------------------------------------------------+
// | ...
// +-------------------------------------------------------------------------+
// | Iterate for the number of num_cols (25 bytes per column) |
// +-------------------------------------------------------------------------+
// | | 1 byte | col_type | Column Type (see table below) |
// | | 4 bytes | unknown | matches first unknown definition block |
// | | 2 bytes | col_num | Column Number (includes deleted columns) |
// | | 2 bytes | offset_V | Offset for variable length columns |
// | | 2 bytes | col_num | Column Number |
// | | 2 bytes | misc | prec/scale (1 byte each), or sort order |
// | | | | for textual columns(0x409=General) |
// | | | | or "complexid" for complex columns (4bytes)|
// | | 2 bytes | misc_ext | text sort order version num is 2nd byte |
// | | 1 byte | bitmask | See column flags below |
// | | 1 byte | misc_flags | 0x01 for compressed unicode |
// | 0000 | 4 bytes | ??? | |
// | | 2 bytes | offset_F | Offset for fixed length columns |
// | | 2 bytes | col_len | Length of the column (0 if memo/ole) |
// +-------------------------------------------------------------------------+
// | Iterate for the number of num_cols (n*2 bytes per column) |
// +-------------------------------------------------------------------------+
// | | 2 bytes | col_name_len| len of the name of the column |
// | | n bytes | col_name | Name of the column (UCS-2 format) |
// +-------------------------------------------------------------------------+
//
// -----------------------------------------------------------------------
function getTableDefinitionForPage(pageNum) {
//
// go to the page which defines the table
//
tempoffset = 4096 * pageNum
//
// get the table header
//
wholeDb.table_pages[pageNum].header = {}
let PageSignature = find(tempoffset, 1, "number") // 0x02 means table deinition page
wholeDb.table_pages[pageNum].header.PageSignature = PageSignature
let freeBytes = find(tempoffset + 2, 2, "number")
wholeDb.table_pages[pageNum].header.freeSpaceInThisPageMinus8 = freeBytes
let NextPage = find(tempoffset + 4, 4, "number")
wholeDb.table_pages[pageNum].header.NextPage = NextPage
tempoffset = tempoffset + 8
//
// get the table definition
//
wholeDb.table_pages[pageNum].definition = {}
let TableDefinitionLength = find(tempoffset, 4, "number")
wholeDb.table_pages[pageNum].definition.TableDefinitionLength = TableDefinitionLength
let Numberofrows = find(tempoffset + 8, 4, "number")
wholeDb.table_pages[pageNum].definition.Numberofrows = Numberofrows
tempoffset = tempoffset + 12
let Autonumber = find(tempoffset, 4, "number")
wholeDb.table_pages[pageNum].definition.Autonumber = Autonumber
tempoffset = tempoffset + 4
let AutonumberIncrement = getVar({useJetVersion: 4,length: 4,name: "Autonumber Increment",type: "number"})
wholeDb.table_pages[pageNum].definition.Autonumber = Autonumber
getVar({useJetVersion: 4,length: 4,name: "Complex Autonumber",showas: "number"})
getVar({useJetVersion: 4,length: 4,name: "Unknown"})
getVar({useJetVersion: 4,length: 4,name: "Unknown"})
let Flags = getVar({length: 1,name: "Table Type / Flags?",type: "number",showas: "hex"})
wholeDb.table_pages[pageNum].definition.Flags = Flags
let NextColumnId = getVar({length: 2,name: "Next Column Id",type: "number"})
wholeDb.table_pages[pageNum].definition.NextColumnId = NextColumnId
let VariableColumns = getVar({length: 2,name: "Variable columns",type: "number"})
wholeDb.table_pages[pageNum].definition.VariableColumnsCount = VariableColumns
let colCount = getVar({length: 2,name: "Column Count",type: "number"})
wholeDb.table_pages[pageNum].definition.TotalColumnCount = colCount
let indexCount = getVar({length: 4,name: "Index Count",type: "number"})
wholeDb.table_pages[pageNum].definition.indexCount = indexCount
let RealIndexCount = getVar({length: 4,name: "Real Index Count",type: "number"})
wholeDb.table_pages[pageNum].definition.RealIndexCount = RealIndexCount
let RowPageMapRecord = getVar({length: 1,name: "Row Page Map record",type: "number" })
wholeDb.table_pages[pageNum].definition.RowPageMapRecord = RowPageMapRecord
var RowPageMapPage = getVar({length: 3,name: "Row Page Map Page",type: "number"})
wholeDb.table_pages[pageNum].definition.RowPageMapPage = RowPageMapPage
let FreeSpacePageMapRecord = getVar({length: 1,name: "Free Space Page Map Record",type: "number" })
wholeDb.table_pages[pageNum].definition.FreeSpacePageMapRecord = FreeSpacePageMapRecord
let FreeSpacePageMapPage = getVar({length: 3,name: "Free Space Page Map Page",type: "number"})
wholeDb.table_pages[pageNum].definition.FreeSpacePageMapPage = FreeSpacePageMapPage
//
// skip indexes
//
// for every real index :
//
// Unknown A1 4 bytes ???
// Index Row Count 4 bytes UINT 32 LE Unknown
// Unknown A2 4 bytes ??? Jet 4 only, always 0
//
tempoffset = tempoffset + (12 * RealIndexCount)
//
// get the definition of all the cols
//
let columns = {}
let columnNames = {}
let fixedColsList = []
let colIdList = {}
for (var x=0; x< colCount; x++) {
let newColumn = new Object()
let colType = getVar({length: 1,name: "col Type",type: "number"})
newColumn.colType = colType
newColumn.colTypeText = getColumnType(colType)
//console.log("Col type: " + getColumnType(colType))
getVar({useJetVersion: 4,length: 4,name: "Unknown"})
let ColID = getVar({length: 2,name: "Col ID",type: "number"})
let VariableColumnNumber = getVar({length: 2,name: "Variable Column Number",type: "number"})
let ColumnIndex = getVar({length: 2,name: "Column Index",type: "number"})
getVar({useJetVersion: 4,length: 4,name: "Various" })
let ColFlags = getVar({useJetVersion: 4,length: 1,name: "Col Flags"})
let fixedLength = false
if (ColFlags[0] & 0x01) {
fixedLength = true
fixedColsList.push(x)
}
colIdList[ColID] = x
let canBeNull = false
if (ColFlags[0] & 0x02) {
canBeNull = true
}
let autonumber = false
if (ColFlags[0] & 0x04 ) {
autonumber = true
}
let UnicodeFlag = getVar({useJetVersion: 4,length: 1,name: "Col Flags2"})
getVar({useJetVersion: 4,length: 4,name: "Unknown"})
let FixedOffset = getVar({length: 2,name: "Fixed offset",type: "number"})
let colDataLen = getVar({length: 2,name: "Length",type: "number"})
newColumn.ColID = ColID
newColumn.length = colDataLen
newColumn.FixedOffset = FixedOffset
newColumn.ColumnIndex = ColumnIndex
newColumn.VariableColumnNumber = VariableColumnNumber
newColumn.fixedLength = fixedLength
newColumn.canBeNull = canBeNull
newColumn.autonumber = autonumber
newColumn.ColFlags = "0x" + ":0x" + ColFlags[0].toString(16)
newColumn.UnicodeFlag = "" + UnicodeFlag[0]
//columns[ColID] = newColumn
columns[x] = newColumn
}
console.log(" ")
console.log(" ")
console.log(" ")
wholeDb.table_pages[pageNum].col_defns = {}
for (var x=0; x< colCount; x++) {
let colLen = getVar({length: 2,name: "col length",type: "number"})
let colname = getVar({length: colLen,name: "col name"})
let tttt=toUTF8Array(colname)
//console.log("colname: " + colname)
//console.log("columns[" + x + "]: " + columns[x])
let colNum = x
columns[ colNum ].name = tttt
columnNames[tttt] = colNum
wholeDb.table_pages[pageNum].col_defns[ colNum ] = columns[ colNum ]
}
wholeDb.table_pages[pageNum].columnNames = columnNames
wholeDb.table_pages[pageNum].fixedColsList = fixedColsList
wholeDb.table_pages[pageNum].colIdList = colIdList
}
// -----------------------------------------------------------------------
//
//
//
//
// -----------------------------------------------------------------------
function getColName(pageNum, varIndex) {
return ( (wholeDb.table_pages[pageNum].col_defns[varIndex].name) +
(wholeDb.table_pages[pageNum].col_defns[varIndex].fixedLength?" (fixed)":" (var)") )
//return varIndex
}
// -----------------------------------------------------------------------
//
//
//
//
// -----------------------------------------------------------------------
function toUTF8Array(input) {
let s=""
for (let x=0;x<input.length;x=x+2){
s=s + String.fromCharCode(input[x])
}
return s
}
// -----------------------------------------------------------------------
//
// +------------------------------------+
// | populateDataForTableDefinedOnPage |
// +------------------------------------+
//
//
// +--------------------------------------------------------------------------+
// | Jet4 Data Page Definition |
// +------+---------+---------------------------------------------------------+
// | data | length | name | description |
// +------+---------+---------------------------------------------------------+
// | 0x01 | 1 byte | page_type | 0x01 indicates a data page. |
// | | 1 byte | unknown | |
// | | 2 bytes | free_space | Free space in this page |
// | | 4 bytes | tdef_pg | Page pointer to table definition |
// | | 4 bytes | unknown | |
// | | 2 bytes | num_rows | number of records on this page |
// +--------------------------------------------------------------------------+
// | Iterate for the number of records |
// +--------------------------------------------------------------------------+
// | | 2 bytes | offset_row | The record's location on this page |
// +--------------------------------------------------------------------------+
//
//
// +--------------------------------------------------------------------------+
// | Jet4 Row Definition |
// +------+---------+---------------------------------------------------------+
// | data | length | name | description |
// +------+---------+---------------------------------------------------------+
// | | 2 bytes | num_cols | Number of columns stored on this row. |
// | | n bytes | fixed_cols | Fixed length columns |
// | | n bytes | var_cols | Variable length columns |
// | | 2 bytes | eod | length of data from begining of record |
// | | n bytes | var_table[]| offset from start of row for each var_col |
// | | 2 bytes | var_len | number of variable length columns |
// | | n bytes | null_mask | Null indicator. See notes. |
// +--------------------------------------------------------------------------+
//
// -----------------------------------------------------------------------
function populateDataForTableDefinedOnPage( pageNum ) {
let tableData = [] // list of rows of data
let table_pages = wholeDb.table_pages[ pageNum ] // table definitions
//
// find all the data pages for this table. stored in
// "wholeDb.table_pages.data_pages[ pageNum ]"
//
for ( let dataPageIndex = 0; dataPageIndex < table_pages.data_pages.length; dataPageIndex++ ) {
let dataPageNum = table_pages.data_pages[ dataPageIndex ].pagenum
tempoffset = 4096 * dataPageNum
let DataPageSignature = getVar({length: 1,name: "DataPageSignature",type: "number"})
//
// for each data page first read the header of the data page
//
getVar({ length: 1, name: "Unknown", type: "number"})
let FreeSpace = getVar({length: 2, name: "Free Space", type: "number"})
let tdef_pg = getVar({length: 3,name: "tdef_pg",type: "number"})
let pgr = getVar({length: 1,name: "tdef_pg record",type: "number"})
getVar({length: 4,name: "Unknown",type: "number"})
let RecordCount = getVar({length: 2,name: "RecordCount",type: "number"})
let NullFieldBitmapLength = Math.floor((wholeDb.table_pages[pageNum].TotalColumnCount + 7) / 8)
//
// Since we are given the record count for how many records are stored
// on this data page in "RecordCount" we can go through and find all
// the record positions on this page since we are given the offsets
//
let recordPosOffsetFromStartOfPage = []
let lastEnd = (4096 * dataPageNum) + 4096 - 1
for (let recIndex = 0 ; recIndex < RecordCount; recIndex++)
{
let RawRecordOffset = getVar({length: 2,name: "RecordOffset",type: "number"})
let newRecordMetaData = {
RawRecordOffset: RawRecordOffset
,
valid: true
}
if (RawRecordOffset == 0) {
newRecordMetaData.valid = false
} else if (RawRecordOffset & 0x4000) {
newRecordMetaData.valid = false
newRecordMetaData.overflow = true
newRecordMetaData.RealOffset = RawRecordOffset - 0x4000
} else if (RawRecordOffset & 0x8000) {
newRecordMetaData.deleted = true
newRecordMetaData.valid = false
newRecordMetaData.RealOffset = RawRecordOffset - 0x8000
} else {
newRecordMetaData.RealOffset = RawRecordOffset
}
newRecordMetaData.start = (4096 * dataPageNum) + newRecordMetaData.RealOffset
newRecordMetaData.end = lastEnd
newRecordMetaData.length = (newRecordMetaData.end - newRecordMetaData.start) + 1
lastEnd = newRecordMetaData.start - 1
recordPosOffsetFromStartOfPage.push( newRecordMetaData )
}
//
// Now that we have all the positions of the records within this data page
// stored in "recordPosOffsetFromStartOfPage" we can go through all the records
//
let NumCols = Object.keys(wholeDb.table_pages[pageNum].col_defns).length
let numFixed = wholeDb.table_pages[pageNum].definition.TotalColumnCount -
wholeDb.table_pages[pageNum].definition.VariableColumnsCount
//
// for every record on this data page
//
for (let record_index = 0;record_index < RecordCount; record_index ++) {
tableRecord = {
meta: {
data_page: dataPageNum
}
,
data: {
}
}
tableData.push(tableRecord)
if (recordPosOffsetFromStartOfPage[record_index].valid) {
tempoffset = recordPosOffsetFromStartOfPage[record_index].start
let NumCols = getVar({ length: 2, name: "NumCols", type: "number" })
tableRecord.meta.NumCols = NumCols
for (let yy=0 ;yy < numFixed ; yy++)
{
let coIn = (numFixed - 1) - yy
let fixedColRealIndex = wholeDb.table_pages[pageNum].fixedColsList[coIn]
let fixedColDefn = wholeDb.table_pages[pageNum].col_defns[fixedColRealIndex]
tempoffset = recordPosOffsetFromStartOfPage[record_index].start + fixedColDefn.FixedOffset + 2
let colVal = getVar({
length: fixedColDefn.length,
name: fixedColDefn.name,
type: "number"
})
tableRecord.data[fixedColDefn.name] = colVal
//zzz
}
let NullFieldBitmapLength = Math.floor((wholeDb.table_pages[pageNum].definition.TotalColumnCount + 7) / 8)
tempoffset = (recordPosOffsetFromStartOfPage[record_index].end - NullFieldBitmapLength) + 1
tempoffset = tempoffset - 2
let VarLenCount = getVar({
length: 2,
name: "VarLenCount",
type: "number"
})
let FieldMask = getVar({
length: NullFieldBitmapLength,
name: "FieldMask",
type: "number"
})
let maskedFields = {}
let notNullVarList = []
let notNullVarListFieldOffset = []
for ( let recIndex = 0;
recIndex < wholeDb.table_pages[pageNum].definition.TotalColumnCount;
recIndex ++ )
{
let realColId = wholeDb.table_pages[pageNum].colIdList["" + recIndex]
//let realColId = recIndex
let maskBit = Math.pow(2, recIndex)
if (FieldMask & maskBit) {
maskedFields[getColName(pageNum,realColId)] = ""
if (!wholeDb.table_pages[pageNum].col_defns[realColId].fixedLength) {
let notNullVarName
notNullVarName = wholeDb.table_pages[pageNum].col_defns[realColId].name
notNullVarList.push(notNullVarName)
notNullVarListFieldOffset.push(realColId)
}
} else {
//maskedFields[getColName(pageNum,realColId)] = "null"
let notNullVarName
notNullVarName = wholeDb.table_pages[pageNum].col_defns[realColId].name
notNullVarList.push(notNullVarName)
tableRecord.data[notNullVarName] = null
}
}
tableRecord.meta.__var_count = VarLenCount
//tableRecord.meta._mask = maskedFields
tempoffset = recordPosOffsetFromStartOfPage[record_index].end - NullFieldBitmapLength - 1
let lastOffset = tempoffset
let VariableLengthFieldCount = getVar({length: 2,name: "VariableLengthFieldCount",type: "number"})
let listOfOffsets = []
let listOfOffsetsRaw = []
let endRec = lastOffset
for (let varIndex=0; varIndex < VariableLengthFieldCount;varIndex++){
tempoffset = lastOffset - 2
lastOffset = tempoffset
let VariableLengthFieldOffset = getVar({length: 2,name: "VariableLengthFieldOffset",type: "number"})
listOfOffsetsRaw.push(VariableLengthFieldOffset)
if ((varIndex == 0 ) || (listOfOffsetsRaw[varIndex] != listOfOffsetsRaw[varIndex - 1])) {
listOfOffsets.push({relative_offset: VariableLengthFieldOffset,start: recordPosOffsetFromStartOfPage[record_index].start + VariableLengthFieldOffset})
}
}
for (let varIndex=0; varIndex < listOfOffsets.length;varIndex++) {
if (varIndex == (listOfOffsets.length - 1) ) {
let varColData = listOfOffsets[ varIndex ]
varColData.length = 2
varColData.end = varColData.start + 2
} else {
let varColData = listOfOffsets[ varIndex ]
let nextColData = listOfOffsets[ varIndex + 1 ]
varColData.length = nextColData.relative_offset - varColData.relative_offset
varColData.end = nextColData.start - 1
}
}
for (let i=0;i<20;i++){
tempoffset = lastOffset - 2
lastOffset = tempoffset
let Eod = getVar({length: 2,name: "Eod",type: "number"})
}
for (let varIndex=0; varIndex < listOfOffsets.length;varIndex++){
tempoffset = listOfOffsets[varIndex].start
let varName = notNullVarList[varIndex]
if (varName) {
varName = varName
}
if (listOfOffsets[varIndex].length == 2) {
let VariableLengthFieldOffset = getVar({length: listOfOffsets[varIndex].length ,
name: "VariableLengthFieldOffset",type:"number"})
tableRecord.data[varName] = VariableLengthFieldOffset
} else {
//
let fieldDefnIndex = notNullVarListFieldOffset[varIndex]
let fieldDefn = wholeDb.table_pages[pageNum].col_defns[fieldDefnIndex]
//if wholeDb.table_pages[pageNum].col_defns[varIndex].UnicodeFlag
if (fieldDefn.UnicodeFlag == "1") {
let VariableLengthFieldOffset = getVar({length: listOfOffsets[varIndex].length ,
name: "VariableLengthFieldOffset"})
let lelll1 = getVar({length: 1,name: "len", type: "number"})
let lelll2 = getVar({length: 1,name: "len", type: "number"})
tableRecord.data[varName] = ("" + VariableLengthFieldOffset).substring(2)
} else {
let VariableLengthFieldOffset = getVar({length: listOfOffsets[varIndex].length ,
name: "VariableLengthFieldOffset"})
tableRecord.data[varName] = toUTF8Array(VariableLengthFieldOffset)
}
}
}
tableRecord.meta = null
}
}
}
wholeDb.table_pages[pageNum].data = {}
wholeDb.table_pages[pageNum].data = tableData
}
function genData(usePage)
{
let rtv2 = []
let colNames = Object.keys(wholeDb.table_pages[usePage].columnNames)
rtv2.push(colNames)
for (let row=0; row < wholeDb.table_pages[usePage].data.length; row++) {
let rowData = wholeDb.table_pages[usePage].data[row].data
let outputCols = []
//console.log(JSON.stringify(row,null,2))
for (let OutIn=0;OutIn<colNames.length;OutIn++) {
let colName = colNames[OutIn]
//console.log(JSON.stringify(colName,null,2))
//console.log(JSON.stringify(rowData,null,2))
let rowDataItem = rowData[colName]
if (rowData) {
rowDataItem = JSON.stringify(rowDataItem,null,2)
try {
rowDataItem = rowDataItem.replace(/[\x00-\x1F\x7F-\x9F]/g, "");
} catch(err ){
rowDataItem = "."//err
}
outputCols.push(rowDataItem)
} else {
outputCols.push("null")
}