Subject: | Incorrect decoding of text7 |
Hello Cosimo,
I've found a bug in Device::Gsm::Pdu ( $Id: Pdu.pm 246 2009-08-18
19:16:55Z cosimo_2 $ ) This is a part of Device::Gsm v1.54
The example below prints 'Not match!' The bug is in decode_text7
procedure. It does not take in account the length value of encoded text
and this leads to extra "\0" in the end of decoded text in some cases.
#!/usr/bin/perl -l --
use strict;
use warnings;
use Device::Gsm::Pdu;
my $text = '1234567';
if ( $text eq
Device::Gsm::Pdu::decode_text7(Device::Gsm::Pdu::encode_text7( $text ))) {
print 'Match';
}
else {
print 'Not match!';
};
__END__
The patch is
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
*** Pdu.pm 2010-06-27 18:42:33.268194936 +0300
--- Pdu.pm.fix 2010-06-27 18:43:11.028184539 +0300
***************
*** 93,99 ****
$bits = substr $bits, 7;
}
! $decoded;
}
# decode 8-bit encoded text
--- 93,99 ----
$bits = substr $bits, 7;
}
! substr $decoded, 0, $len;
}
# decode 8-bit encoded text
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
As for me, I d like to see more perlish code for the module e.g.
sub encode_text7 {
uc join '',
map { unpack 'H2' }
chr length $_[0],
map { pack 'b8', $_ }
unpack '(A8)*',
join '',
unpack '(b7)*', $_[0];
}
sub decode_text7 {
substr join('', map { pack 'b7', $_}
unpack '(A7)*',
join '',
map { unpack '(b8)*', pack 'H2', $_ }
unpack '(A2)*',
substr $_[0], 2),
0,
hex substr $_[0], 0, 2;
}
--
Sincerely yours,
Alexander Onokhov