Created
July 3, 2011 03:39
-
-
Save gugod/1061933 to your computer and use it in GitHub Desktop.
subroutines for decoding SMS user_data in hlr.sqlite3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub dbd_decode_binary { | |
use bytes; | |
my $in = shift; | |
my @in = map { ord } split("", $in); | |
my $c; | |
my $e = shift(@in); | |
my $out = ""; | |
while($c = shift(@in)) { | |
if ($c == 1) { | |
$c = (shift(@in)||0) - 1; | |
} | |
$out .= chr($c + $e); | |
} | |
return $out; | |
} | |
my @gsm_7bit_alphabet = ( | |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0xff, 0xff, 0x0d, 0xff, | |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
0xff, 0xff, 0x20, 0x21, 0x22, 0x23, 0x02, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, | |
0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, | |
0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, | |
0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, | |
0x5a, 0x3c, 0x2f, 0x3e, 0x14, 0x11, 0xff, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, | |
0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, | |
0x78, 0x79, 0x7a, 0x28, 0x40, 0x29, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
0xff, 0xff, 0x0c, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff, | |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x40, 0xff, 0x01, 0xff, | |
0x03, 0xff, 0x7b, 0x7d, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0xff, 0xff, 0xff, 0xff, | |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5b, 0x7e, 0x5d, 0xff, 0x7c, 0xff, 0xff, 0xff, | |
0xff, 0x5b, 0x0e, 0x1c, 0x09, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5d, | |
0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0x0b, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff, 0x1e, 0x7f, | |
0xff, 0xff, 0xff, 0x7b, 0x0f, 0x1d, 0xff, 0x04, 0x05, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff, | |
0xff, 0x7d, 0x08, 0xff, 0xff, 0xff, 0x7c, 0xff, 0x0c, 0x06, 0xff, 0xff, 0x7e, 0xff, 0xff | |
); | |
sub gsm_septet_lookup { | |
my $x = shift; | |
my $i = 0; | |
for my $i (0..$#gsm_7bit_alphabet) { | |
return $i if ($gsm_7bit_alphabet[$i] == $x); | |
} | |
return -1; | |
} | |
sub gsm_7bit_decode { | |
use bytes; | |
my $data = shift; | |
my $length = length($data); | |
my $septet_l = int( ($length * 8) / 7); | |
my $rtext = ""; | |
my @data = map { ord($_) } split "", $data; | |
my @rtext = (); | |
for (my $i = 0; $i < $septet_l; $i++ ) { | |
$rtext[$i] = ( | |
( ($data[($i * 7 + 7) >> 3]||0) << (7 - (($i * 7 + 7) & 7))) | | |
( ($data[($i * 7) >> 3]||0) >> (($i * 7) & 7)) | |
) & 0x7f; | |
} | |
# $text is the return value. | |
my $text = ""; | |
for (my $i = 0; $i < $septet_l; $i++ ) { | |
if ($rtext[$i] == 0x1b && $i + 1 < $length) { | |
my $tmp = $rtext[$i + 1]; | |
$text .= chr $gsm_7bit_alphabet[0x7f + $tmp]; | |
$i++; | |
next; | |
} | |
$text .= chr gsm_septet_lookup( $rtext[$i] ); | |
} | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment