Revision | 9d3da6b9bd308bc7a9c55d38848b5cfddf900eb0 (tree) |
---|---|
Zeit | 2022-04-10 03:35:49 |
Autor | dyknon <dyknon@user...> |
Commiter | dyknon |
example perl
@@ -0,0 +1,44 @@ | ||
1 | +#!/usr/bin/perl | |
2 | + | |
3 | +use warnings; | |
4 | +use strict; | |
5 | +use feature qw(say); | |
6 | +use List::Util qw(reduce); | |
7 | + | |
8 | +if(@ARGV != 2){ | |
9 | + say "Usage: $0 ../ipv4.bin 192.0.2.0"; | |
10 | + exit 1; | |
11 | +} | |
12 | + | |
13 | +my $size = 2 + 4 + 4; | |
14 | +my $file = $ARGV[0]; | |
15 | +my $addr = reduce{$a << 8 | $b}split(/\./, $ARGV[1]); | |
16 | + | |
17 | +open(my $fh, "<:raw", $file); | |
18 | +my $fsize = (stat $fh)[7]; | |
19 | +die if(!$fsize || $fsize % $size); | |
20 | +my $from = 0; | |
21 | +my $to = $fsize / $size - 1; | |
22 | +my $buf; | |
23 | +my $cnt = 0; | |
24 | + | |
25 | +# Binary search | |
26 | +while($from <= $to){ | |
27 | + my $center = int(($from + $to) / 2); | |
28 | + $fh->seek($center * $size, 0); | |
29 | + $fh->read($buf, $size); | |
30 | + $cnt++; | |
31 | + my ($cc, $start, $end) = unpack("A2L>2", $buf); | |
32 | + if($start <= $addr && $addr <= $end){ | |
33 | + say $cc, " (read $cnt times)"; | |
34 | + exit; | |
35 | + } | |
36 | + if($start < $addr){ | |
37 | + $from = $center + 1; | |
38 | + }else{ | |
39 | + $to = $center - 1; | |
40 | + } | |
41 | +} | |
42 | +say "No Match (read $cnt times)"; | |
43 | + | |
44 | +$fh->close; |