blib/lib/GSM/Nbit.pm

filter: Statementstmt 82.8% 77 / 93 Branchbran 65.7% 25 / 38 Conditioncond 78.5% 11 / 14 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 83.3% 10 / 12 Podpod 100% 9 / 9 totaltotal 77.0% 134 / 174 CC 26 SCAR 17.7
CC
26
Cov
78%
decode_8bit_wlen
12.0
decode_7bit
9.1
encode_7bit
6.0
CRAP 5.9
↑ Summary
1 package GSM::Nbit;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 GSM::Nbit - GSM 7bit and 8bit data encoder and decoder.
10  
11 =head1 VERSION
12  
13 Version 0.08
14  
15 =cut
16  
17 3 our $VERSION = '0.08';
18  
19 =head1 SYNOPSIS
20  
21 GSM::Nbit
22  
23 Throughout GSM world "special" encodings called 7bit and 8bit are used.
24 Encoding in 8bit is just plain HEX value and is provided here for completeness
25 and ease of use, 7bit packs 8bit data into 7bit HEX value by limiting it to the
26 lower 127 characters - and hence gaining 1 extra char every 8 characters.
27  
28 That's how you get 160 characters limit on plain text (ASCII + few Greek chars)
29 messages with only 140 bytes for data.
30  
31 Since many modules need such encodings in them, those functions are refactored
32 here. It's released as separate module and not part of some other distribution
33 exactly for that reason.
34  
35 =head1 Code Sample
36  
37 use Encode qw/encode decode/;
38 use GSM::Nbit;
39  
40 my $gsm = GSM::Nbit->new();
41 my $txt = "some text";
42
43 # We need to encode it first - for details see:
44 # http://www.dreamfabric.com/sms/default_alphabet.html
45 my $txt0338 = encode("gsm0338", $txt);
46 my $txt_7bit = $gsm->encode_7bit_wlen($txt);
47
48 # ... we submit it to the GSM network
49 # ... latter we receive something from GSM network
50
51 my $txt_gsm = $gsm->decode_7bit_wlen($txt_7bit);
52
53 # we need to decode it back to computer/Perl representation
54 my $txt_orig = decode("gsm0338", $txt_gsm);
55  
56 =head1 METHODS
57  
58 =head2 new
59  
60 This is the constructor. Accepts no params.
61  
62 =cut
63  
64 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
65 1 my $class = shift;
66 1 my $self = bless {}, $class;
67 1 return $self;
68 }
69  
70 =head2 encode_7bit
71  
72 This function encodes the string as 7bit packed data in HEX representation.
73 Please note that you probably need to convert the text into gsm0338 format first -
74 we don't automatically do that.
75  
76 For details see L<http://www.dreamfabric.com/sms/default_alphabet.html>.
77  
78 You can use Perl's Encode module for that - see:
79 L<http://search.cpan.org/perldoc?Encode::GSM0338>
80  
81 =cut
82  
83 6 ▶sub encode_7bit {
Subroutine encode_7bit: called CC=6 SCAR=18.0
Pod documented
84 6 my($self, $data) = @_;
85 6 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 6 6
86
87 6 my($bit_string, $message) = ('','');
88 6 my($octet, $rest);
89  
90 6 for(split(//,$data)) {
91 50 $bit_string.=unpack('b7',$_);
92 }
93  
94 6 ▶ while(defined($bit_string) && (length($bit_string))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bit_string and length $bit_string) 46 6 52
Condition66%2/3
defined $bit_string and length $bit_string
!ll&&!rl&&r
0646
MC/DC50%1/2
defined $bit_string and length $bit_string
defined $bit_string length $bit_string
Truth table66%2/3
A and B
A= defined $bit_stringB= length $bit_string
ABresult
0X0
100
111
95 46 $rest = $octet = substr($bit_string,0,8);
96 46 $message .= unpack("H2",pack("b8",substr($octet.'0'x7,0,8)));
97 46 ▶ $bit_string = (length($bit_string) > 8) ? substr($bit_string,8) : '';
Branch: 1 branch
BranchTrueFalseTotal
length $bit_string > 8 ? : 40 6 46
98 }
99
100 6 return uc($message);
101 }
102  
103 =head2 encode_7bit_wlen
104  
105 Beside encoding the string as 7bit packed data in HEX representation, this
106 method also adds the length in front of the encoded string, it's needed in some
107 GSM protocols as a kind of checksum and to help with certain edge cases.
108  
109 =cut
110  
111 3 ▶sub encode_7bit_wlen {
Subroutine encode_7bit_wlen: called CC=2 SCAR=7.0
Pod documented
112 3 my($self, $data) = @_;
113 3 ▶ return '00' unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 3 3
114
115 3 my $text_7bit = $self->encode_7bit($data);
116
117 3 return sprintf("%02X", length($data)).($text_7bit);
118 }
119  
120 =head2 decode_7bit
121  
122 This function decodes the 7bit data in HEX representation back to a "readable"
123 string. Second optional parameter is length - it's used in edge cases when
124 we can't be sure if the last seven 0's in bit representation are meant to
125 be @ sign, or it's a filler and there to just fit the 7bit representation into
126 8bit data computers (and cellphones) use.
127  
128 Edge cases happen when length of original text is 7, 15, 23, 31 ... (+8) chars.
129  
130 =cut
131  
132 4 ▶sub decode_7bit {
Subroutine decode_7bit: called CC=9 SCAR=22.1
Pod documented
133 4 my $self = shift();
134 4 my $data = shift();
135
136 4 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 1 3 4
137
138 3 ▶ my $length = shift || undef;
Condition100%2/2
shift() || undef
l!l
21
MC/DC100%1/1
shift() || undef
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
139 3 my $message = "";
140 3 my $len = length($data);
141
142 3 my $bytes;
143 3 my $i = 0;
144
145 3 my $repeat = int(length($data)) / 2;
146
147 3 ▶ for($i=0; $i < $repeat; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $repeat) 25 3 28
148 25 my $hex = substr($data, $i * 2, 2);
149 25 my $hex_b = unpack('b8',pack('H2', $hex));
150 25 $bytes .= $hex_b;
151 }
152
153 3 ▶ $repeat = $length || int(length($bytes) / 7);
Condition66%2/3
$length || int length($bytes) / 7
l!l&&r!l&&!r
210
MC/DC0%0/2
$length || int length($bytes) / 7
$length int length($bytes) / 7
Truth table66%2/3
A || B
A= $lengthB= int length($bytes) / 7
ABresult
1X1
011
000
154 3 my $last_loop = int(length($bytes) / 7) - 1;
155 3 ▶ for($i = 0; $i < $repeat; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $repeat) 28 3 31
156 28 my $letter = substr($bytes, ($i * 7), 7);
157 28 ▶ if(($i == $last_loop) && ($letter eq '0000000') && (not defined $length)){
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $last_loop and $letter eq "0000000" and not defined $length) 0 28 28
Condition83%5/6
$i == $last_loop and $letter eq "0000000" and not defined $length
!ll&&!rl&&r
2710
$i == $last_loop and $letter eq "0000000"
!ll&&!rl&&r
2521
MC/DC0%0/3
$i == $last_loop and $letter eq "0000000" and not defined $length
$i == $last_loop $letter eq "0000000" not defined $length
Truth table75%3/4
A and B and not(C)
A= $i == $last_loopB= $letter eq "0000000"C= not defined $length
ABCresult
0XX0
10X0
1100
1111
158 0 cluck "Possible edge case, can't be sure if last character is " .
159 'really @ or just a filler.'
160 }
161
162 28 $message .= pack('b7', $letter);
163 }
164
165 3 return $message;
166 }
167  
168 =head2 decode_7bit_wlen
169  
170 This function decodes back to a "readable" text string the 7bit data in HEX
171 representation that includes the length as the first value.
172  
173 =cut
174  
175 6 ▶sub decode_7bit_wlen {
Subroutine decode_7bit_wlen: called CC=5 SCAR=16.3
Pod documented
176 6 my($self, $data) = @_;
177
178 6 ▶ unless($data){
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 6 6
179 0 return;
180 }
181  
182 6 ▶ if(length($data) < 2){
Branch: 1 branch
BranchTrueFalseTotal
if (length $data < 2) 1 5 6
183 1 cluck "Invalid data - must be at least 2 characters of HEX representation";
184 1 return;
185 }
186
187 5 my $len = hex(substr($data,0,2));
188
189 5 my $real_length = (length($data) / 2 * 8 / 7 ) - 2;
190 5 ▶ if( $len <= $real_length){
Branch: 1 branch
BranchTrueFalseTotal
if ($len <= $real_length) 2 3 5
191 2 cluck "Something is wrong with the data you want me to decode, length " .
192 "indicates $len chars, but there aren't that much chars.";
193 2 return;
194 }
195 # but then it shouldn't be too long for provided length either
196 3 ▶ if($len + 1 <= $real_length){
Branch: 1 branch
BranchTrueFalseTotal
if ($len + 1 <= $real_length) 0 3 3
197 0 cluck "Provided length is much shorter than the actual length, ".
198 "something is probably wrong - but I'll continue...";
199 }
200 3 $data = substr($data, 2, length($data) - 2);
201
202 3 my $message = $self->decode_7bit($data, $len);
203
204 3 return $message;
205 }
206  
207 =head2 encode_8bit
208  
209 This function encodes the string as 8bit HEX representation of the string.
210  
211 =cut
212  
213 1 ▶sub encode_8bit {
Subroutine encode_8bit: called CC=3 SCAR=11.0
Pod documented
214 1 my ($self, $data) = @_;
215 1 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 1 1
216
217 1 my $message = "";
218  
219 1 ▶ while (length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 10 1 11
220 10 $message .= sprintf("%.2X", ord(substr($data,0,1)));
221 10 $data = substr($data,1);
222 }
223
224 1 return $message;
225 }
226  
227 =head2 encode_8bit_wlen
228  
229 This function encodes the string as 8bit HEX representation of the string
230 and also adds the length in front of the encoded string since it's needed in some
231 GSM protocols as a kind of checksum.
232  
233 =cut
234  
235 0 ▶sub encode_8bit_wlen {
Subroutine encode_8bit_wlen: not called CC=2 SCAR=17.9
Pod documented
236 0 my($self, $data) = @_;
237 0 ▶ return '00' unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
238
239 0 my $text_8bit = $self->encode_8bit($data);
240
241 0 return sprintf("%02X", length($data)).($text_8bit);
242 }
243  
244 =head2 decode_8bit
245  
246 This function decodes back to a "readable" text string the 8bit HEX representation
247 with length at the start of the string.
248  
249 =cut
250  
251 1 ▶sub decode_8bit {
Subroutine decode_8bit: called CC=3 SCAR=11.0
Pod documented
252 1 my ($self, $data) = @_;
253 1 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 1 1
254
255 1 my $message;
256  
257 1 ▶ while ( length($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 10 1 11
258 10 $message .= pack('H2',substr($data,0,2));
259 10 $data = substr($data,2);
260 }
261
262 1 return $message;
263 }
264  
265 =head2 decode_8bit_wlen
266  
267 This function decodes the 8bit HEX representation of the string back to
268 readable text string.
269  
270 =cut
271  
272 0 ▶sub decode_8bit_wlen {
Subroutine decode_8bit_wlen: not called CC=3 SCAR=24.8
Pod documented
273 0 my ($self, $data) = @_;
274 0 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
275  
276 0 my $len = hex(substr($data,0,2));
277 0 $data = substr($data, 2, length($data) - 2);
278
279 0 my $message = "";
280  
281 0 ▶ while ( length($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 0 0 0
282 0 $message .= pack('H2',substr($data,0,2));
283 0 $data = substr($data,2);
284 }
285
286 0 return $message;
287 }
288  
289 =head1 INCOMPATIBILITIES
290  
291 Note that you might need to update your Encode.pm module beforehand for tests
292 to pass (and to be able to use this in a meaningful way) since older version
293 had a bug for gsm0338 encode/decode of @ char.
294  
295 =head1 AUTHOR
296  
297 Aleksandar Petrovic, C<< <techcode at cpan.org> >>
298  
299 =head1 BUGS
300  
301 Please report any bugs or feature requests to C<bug-gsm-nbit at rt.cpan.org>,
302 or through the web interface at
303 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=GSM-Nbit>.
304 I will be notified, and then you'll automatically be notified of progress on
305 your bug as I make changes.
306  
307  
308 =head1 SUPPORT
309  
310 You can find documentation for this module with the perldoc command.
311  
312 perldoc GSM::Nbit
313  
314  
315 You can also look for information at:
316  
317 =over 4
318  
319 =item * RT: CPAN's request tracker
320  
321 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=GSM-Nbit>
322  
323 =item * AnnoCPAN: Annotated CPAN documentation
324  
325 L<http://annocpan.org/dist/GSM-Nbit>
326  
327 =item * CPAN Ratings
328  
329 L<http://cpanratings.perl.org/d/GSM-Nbit>
330  
331 =item * Search CPAN
332  
333 L<http://search.cpan.org/dist/GSM-Nbit/>
334  
335 =back
336  
337  
338 =head1 ACKNOWLEDGEMENTS
339  
340  
341 =head1 LICENSE AND COPYRIGHT
342  
343 Copyright 2010 Aleksandar Petrovic.
344  
345 This program is free software; you can redistribute it and/or modify it
346 under the terms of either: the GNU General Public License as published
347 by the Free Software Foundation; or the Artistic License.
348  
349 See http://dev.perl.org/licenses/ for more information.
350  
351  
352 =cut
353  
354 3 1; # End of GSM::Nbit
↑ Summary