-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep4-savebin.php
45 lines (33 loc) · 975 Bytes
/
step4-savebin.php
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
<?php
declare(strict_types = 1);
require_once('boot.php');
require_once('console.php');
require_once('lib/tree.php');
define('IP2C_DB_IDENT', 'IP2C');
define('IP2C_DB_VERS_HI', 2);
define('IP2C_DB_VERS_LO', 1);
$db = new CountryRangeDB("ALL");
$db->loadFile("combined.db");
$ranges = $db->getRanges();
$rec_count = count($ranges);
$root = build_tree($ranges, 0, $rec_count - 1);
$ip_count = $root->max - $root->min + 2; // 2=including range ends
$f = fopen('ip2c2-'.date('Ymd').'.db', 'wb');
$format = "Z".(strlen(IP2C_DB_IDENT) + 1)."ccVV"; # unsigned long (always 32 bit, little endian byte order)
fwrite($f, pack($format,
IP2C_DB_IDENT,
IP2C_DB_VERS_HI,
IP2C_DB_VERS_LO,
$rec_count,
$ip_count
));
foreach($ranges as $r)
fwrite($f, pack("VVa4", $r->start, $r->end, $r->iso)); # a4 to align C struct
fclose($f);
printf("Database version: %s %d.%d, %d records, %d IPs covered",
IP2C_DB_IDENT,
IP2C_DB_VERS_HI,
IP2C_DB_VERS_LO,
$rec_count,
$ip_count
);