-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathzone-build.js
executable file
·148 lines (109 loc) · 3 KB
/
zone-build.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
#!/usr/bin/env node
'use strict';
/* eslint quotes: off */
const assert = require('assert');
const Path = require('path');
const fs = require('bfile');
const bio = require('bufio');
const {Resource} = require('hsd/lib/dns/resource');
const util = require('./util');
const ZONE_JSON = Path.resolve(__dirname, 'build', 'root.json');
const TLD_H = Path.resolve(__dirname, 'build', 'tld.h');
const TLD_JSON = Path.resolve(__dirname, 'build', 'tld.json');
const TLD_DB = Path.resolve(__dirname, 'build', 'tld.db');
function prepend(data) {
assert(data.length <= 512);
const out = Buffer.allocUnsafe(2 + data.length);
out.writeUInt16LE(data.length, 0);
data.copy(out, 2);
return out;
}
function toHex(data) {
const hex = prepend(data).toString('hex');
const chunks = [];
for (let i = 0; i < hex.length; i += 26)
chunks.push(` "${hex.slice(i, i + 26)}"`);
const str = chunks.join('\n');
const cstr = str.replace(/([a-f0-9]{2})/g, '\\x$1');
return `${cstr},`;
}
const json = fs.readFileSync(ZONE_JSON, 'utf8');
const root = JSON.parse(json);
const keys = Object.keys(root).sort(util.compare);
const items = [];
for (const key of keys) {
const res = Resource.fromJSON(root[key]);
const blob = res.encode();
assert(blob.length <= 512);
items.push([key.slice(0, -1), blob]);
}
{
const code = [
'#ifndef _HSK_TLD_H',
'#define _HSK_TLD_H',
'',
'/* Autogenerated, do not edit. */',
'',
`#define HSK_TLD_SIZE ${keys.length}`,
'',
'static const char *HSK_TLD_NAMES[] = {'
];
for (const [name] of items)
code.push(` "${name}",`);
code.push(' NULL');
code.push('};');
code.push('');
code.push('static const char *HSK_TLD_DATA[] = {');
for (const [, blob] of items)
code.push(toHex(blob));
code.push(' NULL');
code.push('};');
code.push('');
code.push('#endif');
code.push('');
fs.writeFileSync(TLD_H, code.join('\n'));
}
{
const json = [
'{'
];
for (const [name, blob] of items)
json.push(` "${name}": "${blob.toString('base64')}",`);
json[json.length - 1] = json[json.length - 1].slice(0, -1);
json.push('}');
json.push('');
fs.writeFileSync(TLD_JSON, json.join('\n'));
}
{
const bw = bio.write(10 << 20);
const {data} = bw;
let nameSize = 0;
for (const [name] of items) {
if (name.length > nameSize)
nameSize = name.length;
}
if (nameSize > 32)
throw new Error('Upgrade serialization!');
assert(nameSize <= 63);
bw.writeU32(items.length);
bw.writeU8(nameSize);
const offsets = [];
for (const [name] of items) {
bw.writeU8(name.length);
bw.writeString(name, 'ascii');
bw.fill(0x00, nameSize - name.length);
offsets.push(bw.offset);
bw.writeU32(0);
}
for (let i = 0; i < items.length; i++) {
const [, blob] = items[i];
const {offset} = bw;
const pos = offsets[i];
bio.writeU32(data, offset, pos);
assert(blob.length <= 512);
bw.writeU16(blob.length);
bw.writeBytes(blob);
}
const raw = bw.slice();
fs.writeFileSync(TLD_DB, raw);
}