| 1 |
root |
1.1 |
package Net::Knuddels; |
| 2 |
|
|
|
| 3 |
root |
1.3 |
use Net::Knuddels::Dictionary; |
| 4 |
|
|
|
| 5 |
root |
1.2 |
use strict; |
| 6 |
|
|
use utf8; |
| 7 |
|
|
|
| 8 |
root |
1.4 |
use Carp; |
| 9 |
|
|
use Math::BigInt; |
| 10 |
|
|
|
| 11 |
|
|
sub hash_pw($$) { |
| 12 |
|
|
my ($challenge, $pw) = @_; |
| 13 |
|
|
|
| 14 |
|
|
my $l1 = length $pw; |
| 15 |
|
|
my $l2 = length $challenge; |
| 16 |
|
|
|
| 17 |
|
|
my $k = chr ($l1 ^ ($l2 << 4)); |
| 18 |
|
|
|
| 19 |
|
|
my $l = $l1 < $l2 ? $l2 : $l1; |
| 20 |
|
|
|
| 21 |
|
|
my $xor = substr +($pw x 100) ^ ($challenge x 100) ^ ($k x 100), 0, $l; |
| 22 |
|
|
|
| 23 |
|
|
my ($i, $j); |
| 24 |
|
|
|
| 25 |
|
|
--$l; |
| 26 |
|
|
|
| 27 |
|
|
if ($l <= 17) { |
| 28 |
|
|
for (0 .. $l) { |
| 29 |
|
|
$i = $i * 3 + ord substr $xor, $l - $_; |
| 30 |
|
|
$j = $j * 5 + ord substr $xor, $_; |
| 31 |
|
|
|
| 32 |
|
|
$i = unpack "l", pack "L", (new Math::BigInt $i) & 0xffffffff; |
| 33 |
|
|
$j = unpack "l", pack "L", (new Math::BigInt $j) & 0xffffffff; |
| 34 |
|
|
} |
| 35 |
|
|
} else { |
| 36 |
|
|
for ($_ = $l; $_ >= 0; $_ -= int $_/19) { |
| 37 |
|
|
$i = $i * 5 + ord substr $xor, $_; |
| 38 |
|
|
$j = $j * 3 + ord substr $xor, $l - $_; |
| 39 |
|
|
|
| 40 |
|
|
$i = unpack "l", pack "L", (new Math::BigInt $i) & 0xffffffff; |
| 41 |
|
|
$j = unpack "l", pack "L", (new Math::BigInt $j) & 0xffffffff; |
| 42 |
|
|
} |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
|
|
$i ^= $j; |
| 46 |
|
|
|
| 47 |
|
|
($i & 0xffffff) ^ ($i >> 24); |
| 48 |
|
|
} |
| 49 |
|
|
|
| 50 |
root |
1.2 |
package Net::Knuddels::Receiver; |
| 51 |
|
|
|
| 52 |
|
|
sub new { |
| 53 |
|
|
my $class = shift; |
| 54 |
|
|
|
| 55 |
root |
1.4 |
my %data; |
| 56 |
|
|
|
| 57 |
|
|
my $self = bless { |
| 58 |
|
|
@_ |
| 59 |
|
|
}, $class; |
| 60 |
|
|
|
| 61 |
|
|
$self->register ("(" => sub { |
| 62 |
|
|
$self->{challenge} = $_[1]; |
| 63 |
|
|
$self->{room} = $_[2]; |
| 64 |
|
|
$self->feed_event ("connected"); |
| 65 |
|
|
}); |
| 66 |
|
|
|
| 67 |
|
|
$self; |
| 68 |
root |
1.2 |
} |
| 69 |
|
|
|
| 70 |
|
|
sub feed_data($$) { |
| 71 |
|
|
my ($self, $data) = @_; |
| 72 |
|
|
|
| 73 |
|
|
# split data stream into packets |
| 74 |
|
|
|
| 75 |
|
|
$data = "$self->{rbuf}$data"; |
| 76 |
|
|
|
| 77 |
|
|
while () { |
| 78 |
|
|
1 <= length $data or last; |
| 79 |
|
|
my $len = ord substr $data, 0, 1; |
| 80 |
|
|
|
| 81 |
|
|
my $skip; |
| 82 |
|
|
if ($len & 0x80) { |
| 83 |
|
|
my $tail = (($len >> 5) & 3) - 1; |
| 84 |
|
|
$len = ($len & 0x1f) + 1; |
| 85 |
|
|
|
| 86 |
|
|
$tail < length $data or last; |
| 87 |
|
|
$len += (ord substr $data, $_ + 1, 1) << ($_ * 8 + 5) |
| 88 |
|
|
for 0 .. $tail; |
| 89 |
|
|
|
| 90 |
|
|
$skip = 2 + $tail; |
| 91 |
|
|
} else { |
| 92 |
|
|
$skip = 1; |
| 93 |
|
|
$len++; |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
|
$len + $skip <= length $data or last; |
| 97 |
|
|
substr $data, 0, $skip, ""; |
| 98 |
|
|
my $msg = substr $data, 0, $len, ""; |
| 99 |
|
|
|
| 100 |
|
|
$self->feed_msg ($msg); |
| 101 |
|
|
} |
| 102 |
|
|
|
| 103 |
|
|
$self->{rbuf} = $data; |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
root |
1.3 |
my $RE_dec = join "|", keys %$Net::Knuddels::Dictionary; |
| 107 |
root |
1.1 |
|
| 108 |
root |
1.2 |
sub feed_msg($$) { |
| 109 |
|
|
my ($self, $msg) = @_; |
| 110 |
root |
1.1 |
|
| 111 |
|
|
my $bin = unpack "b*", $msg; |
| 112 |
|
|
my $res = ""; |
| 113 |
|
|
|
| 114 |
root |
1.2 |
while ($bin =~ /\G($RE_dec)/cmog) { |
| 115 |
root |
1.3 |
my $frag = $Net::Knuddels::Dictionary->{$1}; |
| 116 |
root |
1.1 |
$frag = pack "b*", substr $bin, 0, 16, "" if $frag eq "\\\\\\"; |
| 117 |
|
|
$res .= $frag; |
| 118 |
|
|
} |
| 119 |
root |
1.2 |
$bin =~ /\G(.*[^0].*)$/ and die "Net::Knuddels::Receiver: undecodable message tail '$1'"; |
| 120 |
|
|
|
| 121 |
|
|
$self->feed_event (split /\0/, $res); |
| 122 |
root |
1.1 |
} |
| 123 |
|
|
|
| 124 |
root |
1.2 |
sub feed_event($@) { |
| 125 |
|
|
my ($self, $type, @arg) = @_; |
| 126 |
root |
1.1 |
|
| 127 |
root |
1.2 |
for ($type, "ALL") { |
| 128 |
|
|
my $ev = $self->{cb}{$_}; |
| 129 |
|
|
$_->($type, @arg) for values %$ev; |
| 130 |
root |
1.1 |
} |
| 131 |
root |
1.2 |
} |
| 132 |
root |
1.1 |
|
| 133 |
root |
1.2 |
sub register { |
| 134 |
|
|
my ($self, $type, $cb) = @_; |
| 135 |
root |
1.1 |
|
| 136 |
root |
1.2 |
$self->{cb}{$type}{$cb} = $cb; |
| 137 |
root |
1.1 |
} |
| 138 |
|
|
|
| 139 |
root |
1.2 |
1; |
| 140 |
|
|
|