-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_header.js
92 lines (73 loc) · 2.51 KB
/
db_header.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
// Martin Pravda
// header.js
// This file contains an implementation for header parser.
// It parses out all necessary information stored in the header.
// Header allocates first 100 bytes
// A complete documentation regarding the sqlite file format
// can be found here - https://www.sqlite.org/fileformat.html
/*jslint browser, node */
import utils from "./utils.js";
const encodings = [
"utf-8",
"utf-16le",
"utf-16be"
];
function parse(view) {
// the sqlite magic header is first 16 bytes
const header_string = utils.decode_text(
new Uint8Array(view.buffer).slice(0, 16)
);
// size 2 bytes
const page_size = view.getUint16(16);
// size 1 bytes
const write_version = view.getUint8(18);
const read_version = view.getUint8(19);
const reserved_space = view.getUint8(20);
const maximum_payload_fraction = view.getUint8(21);
const minimum_payload_fraction = view.getUint8(22);
const leaf_payload_fraction = view.getUint8(23);
// size 4 bytes
const file_change_counter = view.getUint32(24);
const db_pages_count = view.getUint32(28);
const free_list_trunk_page_nr = view.getUint32(32);
const free_list_pages_count = view.getUint32(36);
const schema_cookie = view.getUint32(40);
const schema_format_nr = view.getUint32(44);
const default_page_cache_size = view.getUint32(48);
const largest_root_b_tree = view.getUint32(52);
const db_text_encoding = view.getUint32(56);
const user_version = view.getUint32(60);
const incremental_vacuum_mode = view.getUint32(64);
const application_id = view.getUint32(68);
// there's 20 bytes reserved for expansion
// so we skip 71 - 91 bytes
const version_valid_for = view.getUint32(92);
const sqlite_version = view.getUint32(96);
return Object.freeze({
application_id,
db_pages_count,
db_text_encoding,
default_page_cache_size,
file_change_counter,
free_list_pages_count,
free_list_trunk_page_nr,
header_string,
incremental_vacuum_mode,
largest_root_b_tree,
leaf_payload_fraction,
maximum_payload_fraction,
minimum_payload_fraction,
page_size,
read_version,
reserved_space,
schema_cookie,
schema_format_nr,
sqlite_version,
user_version,
version_valid_for,
write_version
});
}
//demo import header_test from "./mocks/db_header.js"
//demo parse(header_test.create_view());
export default Object.freeze({encodings, parse});