-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuildpub.php
More file actions
executable file
·249 lines (197 loc) · 4.84 KB
/
buildpub.php
File metadata and controls
executable file
·249 lines (197 loc) · 4.84 KB
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
#!/usr/bin/env php
<?php
require dirname(__FILE__) . '/pubcommon.php';
class PubWriter
{
private $format = array();
private $fh;
private $filename;
public function ENumber($value, $n)
{
if ($value < 0)
throw new Exception("Negative number passed to ENumber");
if ($value > pow(253, $n))
throw new Exception("Value too large for data type");
$b = array_fill(0, $n, chr(254));
for ($i = 0; $i < $n; ++$i)
{
$b[$i] = chr($value % 253 + 1);
$value = floor($value / 253);
if ($value == 0)
break;
}
return implode('', $b);
}
public function __construct($format, $filename)
{
$this->format = $format;
$this->fh = fopen($filename, 'wb');
$this->filename = $filename;
if (!$this->fh)
throw new Exception("Failed to create pub file: " . $filename);
}
public function writeHeader($header)
{
fwrite($this->fh, $header['type'], 3);
fwrite($this->fh, "\x0\x0\x0\x0");
fwrite($this->fh, self::ENumber($header['num'], 2));
fwrite($this->fh, self::ENumber(0, 1));
}
public function writeDummyEntry()
{
$this->writeEntry(array());
}
public function writeEOFEntry()
{
$this->writeEntry(array('name' => 'eof'));
}
public function writeEntry($entry)
{
foreach ($this->format as $fmt)
{
if ($fmt[PUB_KEY_TYPE] == PUB_STRING)
{
if (isset($entry[$fmt[PUB_KEY_NAME]]))
$length = strlen($entry[$fmt[PUB_KEY_NAME]]);
else
$length = 0;
fwrite($this->fh, self::ENumber($length, 1));
}
}
foreach ($this->format as $fmt)
{
switch ($fmt[PUB_KEY_TYPE])
{
case PUB_STRING:
if (isset($entry[$fmt[PUB_KEY_NAME]]))
fwrite($this->fh, $entry[$fmt[PUB_KEY_NAME]]);
break;
default:
// What a coincidence, INT1..INT4 are mapped to 1..4!
if (isset($entry[$fmt[PUB_KEY_NAME]]))
$value = $entry[$fmt[PUB_KEY_NAME]];
else
$value = 0;
if (isset($fmt[PUB_KEY_ENUM]))
{
$lower_value = strtolower($value);
foreach ($fmt[PUB_KEY_ENUM] as $k => $v)
{
if ($lower_value == strtolower($v))
{
$value = $k;
break;
}
}
}
fwrite($this->fh, self::ENumber($value, $fmt[PUB_KEY_TYPE]));
}
}
}
public function finish()
{
fflush($this->fh);
$rid = crc32(file_get_contents($this->filename)) | 0x01010101;
fseek($this->fh, 3, SEEK_SET);
fwrite($this->fh, pack('n', ($rid >> 16) & 0xFFFF) . pack('n', $rid & 0xFFFF), 4);
return $rid;
}
}
function generate_pub($filetype, $filename, $formatfile, $prefix)
{
$entries = array();
$format = pub_format_parse_file($formatfile);
$pub = new PubWriter($format, $filename);
$entries = array();
$max_id = 0;
foreach (glob($prefix . '*.json') as $jsonfile)
{
$entry = json_clean_decode(file_get_contents($jsonfile), true);
if (!$entry)
throw new Exception("Bad JSON file: $jsonfile");
$entries[$entry['id']] = $entry;
$max_id = max($max_id, $entry['id']);
}
$pub->writeHeader(array(
'type' => $filetype,
'num' => $max_id + 1
));
for ($i = 1; $i <= $max_id; ++$i)
{
// testing
/*
if ($filetype == 'ENF')
{
foreach (array('unknown1', 'unknown4') as $k)
{
if ($entries[$i][$k] != 0)
{
echo "$i (" . $entries[$i]['name'] . ") has $k = ", $entries[$i][$k], "\n";
}
}
}
*/
if (isset($entries[$i]))
{
$pub->writeEntry($entries[$i]);
}
else
{
echo "$filetype #$i unused.\n";
$pub->writeDummyEntry();
}
}
$pub->writeEOFEntry();
$rid = sprintf('%x', $pub->finish());
$kbsize = number_format(filesize($filename) / 1024, 1);
echo "Wrote $max_id+1 entries to $filename ($kbsize kB). RID=$rid\n";
if ($kbsize > 62.5)
{
echo "WARNING! $filename may be too large.\n";
}
}
function usage()
{
echo "usage: buildpub.php [EIF|ENF|ESF|ECF] pubfile indir\n\n";
echo "Builds a pub file from a directory full of JSON files.\n";
exit(1);
}
$argp = 1;
if ($argc < 3)
usage();
$filetype = 'Could not by detected from file extension.';
if ($argc > 3)
{
$filetype = $argv[$argp++];
}
$filename = $argv[$argp++];
$indir = $argv[$argp++];
if (substr($filename, -4) == '.eif') $filetype = 'EIF';
else if (substr($filename, -4) == '.enf') $filetype = 'ENF';
else if (substr($filename, -4) == '.esf') $filetype = 'ESF';
else if (substr($filename, -4) == '.ecf') $filetype = 'ECF';
if (substr($indir, -1) != '/')
$indir .= '/';
$formatfile = strtolower($filetype) . '_format.txt';
if (!in_array($filetype, array('EIF', 'ENF', 'ESF', 'ECF')))
{
echo "Unknown file type: ", $filetype, "\n";
echo "Valid file types are: EIF, ENF, ESF, ECF\n";
exit(1);
}
if (!is_file($formatfile))
{
$formatfile2 = dirname(__FILE__) . '/' . $formatfile;
if (!is_file($formatfile2))
{
echo "Can not find $formatfile.\n";
exit(1);
}
$formatfile = $formatfile2;
}
if (!is_dir($indir))
{
echo "Input directory does not exist.";
exit(1);
}
generate_pub($filetype, $filename, $formatfile, $indir);