-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.go
71 lines (67 loc) · 1.67 KB
/
header.go
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
package bakelite
import (
"bytes"
"encoding/binary"
)
var (
headerMagic = "SQLite format 3\x00"
)
func header(pageCount int) []byte {
ps := PageSize
if ps == 1<<16 {
ps = 1
}
// the file header, as described in "1.2. The Database Header"
h := struct {
Magic [16]byte
PageSize uint16
WriteVersion uint8
ReadVersion uint8
ReservedSpace uint8
MaxFraction uint8
MinFraction uint8
LeafFraction uint8
ChangeCounter uint32
PageCount uint32
FirstFreelist uint32
FreelistCount uint32
SchemaCookie uint32
SchemaFormat uint32
PageCacheSize uint32
_ uint32
TextEncoding uint32
_ uint32
_ uint32
_ uint32
ReservedForExpansion [20]byte
VersionValidFor uint32
SQLiteVersion uint32
}{
Magic: asByte(headerMagic),
PageSize: uint16(ps),
WriteVersion: 1, // "journal". "2" is WAL, but sqlittle doesn't read those
ReadVersion: 1, // "journal"
ReservedSpace: 0,
MaxFraction: 64,
MinFraction: 32,
LeafFraction: 32,
ChangeCounter: 42,
PageCount: uint32(pageCount),
FirstFreelist: 0,
FreelistCount: 0,
SchemaCookie: 1, // we don't change the schema
SchemaFormat: 4,
PageCacheSize: 0,
TextEncoding: 1, // "UTF-8"
VersionValidFor: 42, // must match ChangeCounter
SQLiteVersion: 0, // ??
}
b := &bytes.Buffer{}
binary.Write(b, binary.BigEndian, h)
return b.Bytes()
}
func asByte(s string) [16]byte {
var r [16]byte
copy(r[:], s)
return r
}