| 1 | package POE::Component::Client::opentick::Util; | ||||||||||||||
| 2 | # | ||||||||||||||
| 3 | # opentick.com POE client | ||||||||||||||
| 4 | # | ||||||||||||||
| 5 | # Low-level utility routines | ||||||||||||||
| 6 | # | ||||||||||||||
| 7 | # infi/2008 | ||||||||||||||
| 8 | # | ||||||||||||||
| 9 | # $Id: Util.pm 56 2009-01-08 16:51:14Z infidel $ | ||||||||||||||
| 10 | # | ||||||||||||||
| 11 | # Full POD documentation after __END__ | ||||||||||||||
| 12 | # | ||||||||||||||
| 13 | |||||||||||||||
| 14 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 6 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 6 | ▶ | use Carp qw( carp ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 6 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | 6 | ▶ | use POE::Component::Client::opentick::Error; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | |||||||||||||||
| 21 | 6 | ▶ | use vars qw( $VERSION $TRUE $FALSE ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | 6 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 6 | require Exporter; | |||||||||||||
| 25 | 6 | our @ISA = qw( Exporter ); | |||||||||||||
| 26 | 6 | our @EXPORT = qw( pack_binary unpack_binary count_fields check_fields | |||||||||||||
| 27 | dump_hex pack_macaddr is_error pack_bytes | ||||||||||||||
| 28 | asc2longlong ); | ||||||||||||||
| 29 | 6 | ($VERSION) = q$Revision: 56 $ =~ /(\d+)/; | |||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | ### | ||||||||||||||
| 33 | ### Variables | ||||||||||||||
| 34 | ### | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 6 | *TRUE = \1; | |||||||||||||
| 37 | 6 | *FALSE = \0; | |||||||||||||
| 38 | |||||||||||||||
| 39 | # NOTE: This is not complete, nor is it intended to be. | ||||||||||||||
| 40 | # Probably not platform-safe, either. | ||||||||||||||
| 41 | 6 | my $pack_bytes = { | |||||||||||||
| 42 | x => 1, a => 1, A => 1, c => 1, | ||||||||||||||
| 43 | C => 1, s => 2, S => 2, i => 4, | ||||||||||||||
| 44 | I => 4, l => 4, L => 4, n => 2, | ||||||||||||||
| 45 | N => 4, v => 2, V => 4, f => 4, | ||||||||||||||
| 46 | d => 8, p => 4, P => 4, | ||||||||||||||
| 47 | }; | ||||||||||||||
| 48 | |||||||||||||||
| 49 | ### | ||||||||||||||
| 50 | ### Functions | ||||||||||||||
| 51 | ### | ||||||||||||||
| 52 | |||||||||||||||
| 53 | sub pack_binary | ||||||||||||||
| 54 | 13 | ▶ | { | ||||||||||||
|
Subroutine
pack_binary: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 55 | 13 | my( $pack_tmpl, @fields ) = @_; | |||||||||||||
| 56 | |||||||||||||||
| 57 | 13 | my $field_count = count_fields( $pack_tmpl ); | |||||||||||||
| 58 | 13 | ▶ | throw( "Require $field_count fields for template '$pack_tmpl', but " . | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | "received " . scalar( @fields ) ) | ||||||||||||||
| 60 | unless( check_fields( $field_count, @fields ) ); | ||||||||||||||
| 61 | 13 | my $string = pack( $pack_tmpl, @fields ); | |||||||||||||
| 62 | |||||||||||||||
| 63 | 13 | return( $string ); | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | sub unpack_binary | ||||||||||||||
| 67 | 10 | ▶ | { | ||||||||||||
|
Subroutine
unpack_binary: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 68 | 10 | my( $pack_tmpl, $string ) = @_; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 10 | my $field_count = count_fields( $pack_tmpl ); | |||||||||||||
| 71 | 10 | my( @fields ) = unpack( $pack_tmpl, $string ); | |||||||||||||
| 72 | |||||||||||||||
| 73 | # NOTE: Some incoming packets are of variable length, so this is bogus. | ||||||||||||||
| 74 | # throw( "Should expand $field_count fields from template '$pack_tmpl', " . | ||||||||||||||
| 75 | # "but got " . scalar( @fields ) ) | ||||||||||||||
| 76 | # unless( check_fields( $field_count, @fields ) ); | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 10 | ▶ | return( wantarray ? @fields : $fields[0] ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | # NOTE: Not complete for all pack templates. | ||||||||||||||
| 82 | sub count_fields | ||||||||||||||
| 83 | 26 | ▶ | { | ||||||||||||
|
Subroutine
count_fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 84 | 26 | my( $pack_tmpl ) = @_; | |||||||||||||
| 85 | |||||||||||||||
| 86 | 26 | my $count = grep { ! /^x/ } split( /\s+/, $pack_tmpl ); | |||||||||||||
| 87 | |||||||||||||||
| 88 | 26 | return( $count ); | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | sub check_fields | ||||||||||||||
| 92 | 19 | ▶ | { | ||||||||||||
|
Subroutine
check_fields: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 93 | 19 | my( $count, @fields ) = @_; | |||||||||||||
| 94 | |||||||||||||||
| 95 | 19 | my $field_count = grep { defined } @fields; | |||||||||||||
| 96 | |||||||||||||||
| 97 | 19 | ▶ | return( $count == $field_count ? $TRUE : $FALSE ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | sub dump_hex | ||||||||||||||
| 101 | 1 | ▶ | { | ||||||||||||
|
Subroutine
dump_hex: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 102 | 1 | my( $data ) = @_; | |||||||||||||
| 103 | |||||||||||||||
| 104 | # Stolen from perlpacktut.pod, because I'm lazy. | ||||||||||||||
| 105 | 1 | my $i; | |||||||||||||
| 106 | 1 | ▶ | my $hex = join( '', map( ++$i % 16 ? "$_ " : "$_\n", | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 107 | unpack( 'H2' x length( $data ), $data ) ), | ||||||||||||||
| 108 | length( $data ) % 16 ? "\n" : '' ); | ||||||||||||||
| 109 | |||||||||||||||
| 110 | 1 | return( $hex ); | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | |||||||||||||||
| 113 | sub pack_macaddr | ||||||||||||||
| 114 | 2 | ▶ | { | ||||||||||||
|
Subroutine
pack_macaddr: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 115 | 2 | my ( $macaddr ) = @_; | |||||||||||||
| 116 | |||||||||||||||
| 117 | # FIXME: ensure validity | ||||||||||||||
| 118 | 2 | ▶ | check_fields( 6, $macaddr =~ m/([0-9a-f]{2})[:-]?/ig ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | or throw( "Invalid MAC address: $macaddr; expected in " . | ||||||||||||||
| 120 | "xx:xx:xx:xx:xx:xx format." ); | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 2 | $macaddr =~ s/[:-]//g; | |||||||||||||
| 123 | |||||||||||||||
| 124 | 2 | return( pack( 'H*', $macaddr ) ); | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | sub is_error | ||||||||||||||
| 128 | 6 | ▶ | { | ||||||||||||
|
Subroutine
is_error: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 129 | 6 | my( $object ) = @_; | |||||||||||||
| 130 | |||||||||||||||
| 131 | 6 | ▶ | return( ref( $object ) eq 'POE::Component::Client::opentick::Error' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | ? $TRUE | ||||||||||||||
| 133 | : $FALSE ); | ||||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | # NOTE: This is not complete, nor is it intended to be. | ||||||||||||||
| 137 | sub pack_bytes | ||||||||||||||
| 138 | 1 | ▶ | { | ||||||||||||
|
Subroutine
pack_bytes: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 139 | 1 | my( $template, $input ) = @_; | |||||||||||||
| 140 | 1 | ▶ | return 0 unless( $template ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | |||||||||||||||
| 142 | 1 | my @tokens = $template =~ m#([A-Za-z]\d*)+\s*#g; | |||||||||||||
| 143 | |||||||||||||||
| 144 | 1 | my $count = 0; | |||||||||||||
| 145 | 1 | for( @tokens ) | |||||||||||||
| 146 | { | ||||||||||||||
| 147 | 12 | my( $digit, $repeat ) = m#(.)(\d*)#; | |||||||||||||
| 148 | 12 | ▶ | $repeat ||= 1; | ||||||||||||
|
Condition100%2/2
$repeat ||= 1
MC/DC100%1/1
$repeat ||= 1
$repeat
Truth table100%2/2
A
A= $repeat
| |||||||||||||||
| 149 | 12 | $count += $pack_bytes->{$digit} * $repeat; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | 1 | return( $count ); | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | |||||||||||||||
| 155 | sub asc2longlong | ||||||||||||||
| 156 | 1 | ▶ | { | ||||||||||||
|
Subroutine
asc2longlong: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 157 | 1 | my( $string ) = @_; | |||||||||||||
| 158 | |||||||||||||||
| 159 | 1 | my( $i1, $i2 ) = unpack( 'VV', pack( 'a8', $string ) ); | |||||||||||||
| 160 | 1 | my $ll = ( $i2 * ( 2**32 )) + $i1; | |||||||||||||
| 161 | |||||||||||||||
| 162 | 1 | return( $ll ); | |||||||||||||
| 163 | } | ||||||||||||||
| 164 | |||||||||||||||
| 165 | 6 | 1; | |||||||||||||
| 166 | |||||||||||||||
| 167 | __END__ | ||||||||||||||