Petitcom/QRCode.pm

filter: Statementstmt 92.1% 153 / 166 Branchbran 65.3% 17 / 26 Conditioncond 73.3% 11 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 94.7% 36 / 38 Podpod 0.0% 0 / 7 totaltotal 84.6% 221 / 261 CC 25 SCAR 13.4
CC
25
Cov
87%
term
20.0
_max_qr_data_size
8.2
init
4.1
CRAP 3.8
« Petitcom.pm ↑ Summary Petitcom/Resource/GRP.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 {
5 package Data::Petitcom::QRCode;
6  
7 3 ▶ use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶ use bytes();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶ use base qw{ Exporter };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 our @EXPORT_OK = qw{ plot_qrcode };
12  
13 3 ▶ use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶ use Compress::Zlib ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶ use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶ use POSIX qw{ ceil floor };
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶ use GD::Barcode::QRcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶ use constant PTC_OFFSET_FILENAME => 0x0C;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use constant PTC_OFFSET_DATA => 0x24;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use constant PTC_OFFSET_RESOURCENAME => 0x2C;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶ use constant PTC_QR_SIGNATURE => 'PT';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶ use constant DEFAULT_PTC_QR_VERSION_IMAGE => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶ use constant DEFAULT_PTC_QR_VERSION_TERM => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶ use constant DEFAULT_PTC_QR_IMAGE_MODULESIZE => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 3 ▶ use constant QR_ECC => +{ L => 0, M => 1, Q => 2, H => 3 };
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶ use constant QR_VERSION => [
Subroutine BEGIN: called CC=1 SCAR=0.0
30 [ 0, 0, 0, 0 ],
31 [ 17, 14, 11, 7 ],
32 [ 32, 26, 20, 14 ],
33 [ 53, 42, 32, 24 ],
34 [ 78, 62, 46, 34 ],
35 [ 106, 84, 60, 44 ],
36 [ 134, 106, 74, 58 ],
37 [ 154, 122, 86, 64 ],
38 [ 192, 152, 108, 84 ],
39 [ 230, 180, 130, 98 ],
40 [ 271, 213, 151, 119 ],
41 [ 321, 251, 177, 137 ],
42 [ 367, 287, 203, 155 ],
43 [ 425, 331, 241, 177 ],
44 [ 458, 362, 258, 194 ],
45 [ 520, 412, 292, 220 ],
46 [ 586, 450, 322, 250 ],
47 [ 644, 504, 364, 280 ],
48 [ 718, 560, 394, 310 ],
49 [ 792, 624, 442, 338 ],
50 [ 858, 666, 482, 382 ],
51 [ 929, 711, 509, 403 ],
52 [ 1003, 779, 565, 439 ],
53 [ 1091, 857, 611, 461 ],
54 [ 1171, 911, 661, 511 ],
55 [ 1273, 997, 715, 535 ],
56 [ 1367, 1059, 751, 593 ],
57 [ 1465, 1125, 805, 625 ],
58 [ 1528, 1190, 868, 658 ],
59 [ 1628, 1264, 908, 698 ],
60 [ 1732, 1370, 982, 742 ],
61 [ 1840, 1452, 1030, 790 ],
62 [ 1952, 1538, 1112, 842 ],
63 [ 2068, 1628, 1168, 898 ],
64 [ 2188, 1722, 1228, 958 ],
65 [ 2303, 1809, 1283, 983 ],
66 [ 2431, 1911, 1351, 1051 ],
67 [ 2563, 1989, 1423, 1093 ],
68 [ 2699, 2099, 1499, 1139 ],
69 [ 2809, 2213, 1579, 1219 ],
70 [ 2953, 2331, 1663, 1273 ],
71 3 ];
72  
73 3 my %defaults = (
74 type => 'text',
75 ecc => 'M',
76 version => DEFAULT_PTC_QR_VERSION_IMAGE
77 );
78 10 ▶ eval "sub $_ { \@_ > 1 ? \$_[0]->{$_} = \$_[1] : \$_[0]->{$_} }" for keys %defaults;
Subroutine ecc: called CC=2 SCAR=6.9
Subroutine type: called CC=2 SCAR=6.9
Subroutine version: called CC=2 SCAR=6.9
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
@_ > 1 ? : 5 5 10
@_ > 1 ? : 5 5 10
@_ > 1 ? : 5 5 10
79  
80 5 ▶ sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod undocumented
81 5 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 5 5
82 5 my $self = bless {@_}, $class;
83 5 ▶ $self->init() if ( $self->can('init') );
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("init") 5 0 5
84 5 return $self;
85 }
86  
87 5 ▶ sub init {
Subroutine init: called CC=4 SCAR=14.2
Pod undocumented
88 5 my $self = shift;
89 5 for ( keys %defaults ) {
90 15 ▶ my $value = $self->{$_} || $defaults{$_};
Condition66%2/3
$self->{$_} || $defaults{$_}
l!l&&r!l&&!r
1140
MC/DC0%0/2
$self->{$_} || $defaults{$_}
$self->{$_} $defaults{$_}
Truth table66%2/3
A || B
A= $self->{$_}B= $defaults{$_}
ABresult
1X1
011
000
91 15 ▶ ( $self->can($_) ) ? $self->$_($value) : ( $self->{$_} = $value );
Branch: 1 branch
BranchTrueFalseTotal
$self->can($_) ? : 15 0 15
92 }
93 5 return $self;
94 }
95  
96 0 ▶ sub plot_qrcode {
Subroutine plot_qrcode: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my ($ptc, %opts) = @_;
98 0 my $qrcode = __PACKAGE__->new(%opts);
99 0 return $qrcode->plot($ptc);
100 }
101  
102 4 ▶ sub plot {
Subroutine plot: called CC=1 SCAR=0.0
Pod undocumented
103 4 my $self = shift;
104 4 my $raw_ptc = shift;
105 4 return $self->_generate_qrcode($raw_ptc);
106 }
107  
108 4 ▶ sub _generate_qrcode {
Subroutine _generate_qrcode: called CC=3 SCAR=11.0
109 4 my $self = shift;
110 4 my $raw_ptc = shift;
111  
112 4 my $plot_type = $self->type;
113 4 my $ecc = $self->ecc;
114 4 my $version = $self->version;
115  
116 4 my $qr_bin = _create_qr_bin($raw_ptc);
117 4 my $max_qr_data_size = _max_qr_data_size( $version, QR_ECC->{$ecc} );
118 4 my $number_of_qr = _number_of_qr( bytes::length($qr_bin), $max_qr_data_size );
119 4 ▶ my $qr_opts = {
Branch: 1 branch
BranchTrueFalseTotal
$plot_type eq 'image' ? : 1 3 4
120 Ecc => $ecc,
121 Version => $version,
122 ModuleSize => ( $plot_type eq 'image' )
123 ? DEFAULT_PTC_QR_IMAGE_MODULESIZE
124 : 1
125 };
126  
127 4 my @qrcode = ();
128 4 for my $count_qr ( 1 .. $number_of_qr ) {
129 4 my $a_qr_data = bytes::substr( $qr_bin, ( $count_qr - 1 ) * $max_qr_data_size, $max_qr_data_size );
130  
131 4 my $a_qr_bin = PTC_QR_SIGNATURE;
132 4 $a_qr_bin .= pack 'C', $count_qr;
133 4 $a_qr_bin .= pack 'C', $number_of_qr;
134 4 $a_qr_bin .= Digest::MD5::md5($a_qr_data);
135 4 $a_qr_bin .= Digest::MD5::md5($qr_bin);
136 4 $a_qr_bin .= $a_qr_data;
137  
138 4 my $qrcode = undef;
139 4 given ($plot_type) {
140 4 when ('term') {
141 0 $qrcode = GD::Barcode::QRcode::Text->new( $a_qr_bin, $qr_opts )->term;
142 }
143 4 when ('image') {
144 1 my $gd = GD::Barcode::QRcode->new( $a_qr_bin, $qr_opts )->plot;
145 0 $gd->string(
146 GD::Font->Large,
147 5, 2, # 0, 0 => left-top
148 "$count_qr / $number_of_qr",
149 $gd->colorAllocate( 0, 0, 0 ), # black
150 );
151 0 $qrcode = $gd->png;
152 }
153 3 default {
154 3 $qrcode = GD::Barcode::QRcode::Text->new( $a_qr_bin, $qr_opts )->barcode;
155 }
156 }
157 3 push @qrcode, $qrcode;
158 }
159  
160 3 return \@qrcode;
161 }
162  
163 4 ▶ sub _deflate_data {
Subroutine _deflate_data: called CC=2 SCAR=7.0
164 4 my $code = shift;
165 4 ▶ my $deflater = Compress::Zlib::deflateInit()
Branch: 1 branch
BranchTrueFalseTotal
my $deflater = &Compress::Zlib::deflateInit() or Carp::croak("deflateInit() failed: $!") 4 0 4
166 or Carp::croak "deflateInit() failed: $!";
167  
168 4 my $zdata = $deflater->deflate($code);
169 4 $zdata .= $deflater->flush();
170  
171 4 return $zdata;
172 }
173  
174 4 ▶ sub _create_qr_bin {
Subroutine _create_qr_bin: called CC=2 SCAR=6.9
175 4 ▶ my $raw_ptc = shift || return;
Condition50%1/2
shift() || (return)
l!l
40
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
176  
177 4 my $filename = unpack 'Z*', bytes::substr( $raw_ptc, PTC_OFFSET_FILENAME, 8 );
178 4 my $resource = bytes::substr( $raw_ptc, PTC_OFFSET_RESOURCENAME, 4 );
179 4 my $data = bytes::substr( $raw_ptc, PTC_OFFSET_DATA );
180 4 my $zdata = _deflate_data($data);
181  
182 4 my $qr_bin = bytes::substr( $filename . "\x00" x 8, 0, 8 );
183 4 $qr_bin .= $resource;
184 4 $qr_bin .= pack 'I', bytes::length($zdata);
185 4 $qr_bin .= pack 'I', ( bytes::length($raw_ptc) - PTC_OFFSET_DATA );
186 4 $qr_bin .= $zdata;
187  
188 4 return $qr_bin;
189 }
190  
191 16 ▶ sub _max_qr_data_size {
Subroutine _max_qr_data_size: called CC=8 SCAR=21.0
192 16 ▶ my $version = shift || DEFAULT_PTC_QR_VERSION_TERM;
Condition100%2/2
shift() || 4
l!l
151
MC/DC100%1/1
shift() || 4
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
193 16 ▶ my $ecc = shift // 1;
Condition100%2/2
shift() // 1
l!l
97
MC/DC100%1/1
shift() // 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
194  
195 16 ▶ Carp::croak "version between 1 and 24"
Branch: 1 branch
BranchTrueFalseTotal
if $version < 1 or $version > 24 2 14 16
Condition100%3/3
$version < 1 or $version > 24
l!l&&r!l&&!r
1114
MC/DC100%2/2
$version < 1 or $version > 24
$version < 1 $version > 24
Truth table100%3/3
A or B
A= $version < 1B= $version > 24
ABresult
1X1
011
000
196 if ( $version < 1 || $version > 24 );
197 14 ▶ Carp::croak "ecc between 0 and 3"
Branch: 1 branch
BranchTrueFalseTotal
if $ecc < 0 or $ecc > 3 0 14 14
Condition33%1/3
$ecc < 0 or $ecc > 3
l!l&&r!l&&!r
0014
MC/DC0%0/2
$ecc < 0 or $ecc > 3
$ecc < 0 $ecc > 3
Truth table33%1/3
A or B
A= $ecc < 0B= $ecc > 3
ABresult
1X1
011
000
198 if ( $ecc < 0 || $ecc > 3 );
199 14 ▶ Carp::croak "invalid combination of version x ecc: $version x $ecc"
Branch: 1 branch
BranchTrueFalseTotal
if [[0,0,0,0],[17,14,11,7],[32,26,20,14],[53,42,32,24],[78,62,46,34],[106,84,60,44],[134,106,74,58],[154,122,86,64],[192,152,108,84],[230,180,130,98],[271,213,151,119],[321,251,177,137],[367,287,203,155],[425,331,241,177],[458,362,258,194],[520,412,292,220],[586,450,322,250],[644,504,364,280],[718,560,394,310],[792,624,442,338],[858,666,482,382],[929,711,509,403],[1003,779,565,439],[1091,857,611,461],[1171,911,661,511],[1273,997,715,535],[1367,1059,751,593],[1465,1125,805,625],[1528,1190,868,658],[1628,1264,908,698],[1732,1370,982,742],[1840,1452,1030,790],[1952,1538,1112,842],[2068,1628,1168,898],[2188,1722,1228,958],[2303,1809,1283,983],[2431,1911,1351,1051],[2563,1989,1423,1093],[2699,2099,1499,1139],[2809,2213,1579,1219],[2953,2331,1663,1273]]->[$version][$ecc] <= 36 1 13 14
200 if (QR_VERSION->[$version]->[$ecc] <= PTC_OFFSET_DATA);
201  
202 13 return QR_VERSION->[$version]->[$ecc] - PTC_OFFSET_DATA;
203 }
204  
205 4 ▶ sub _number_of_qr {
Subroutine _number_of_qr: called CC=1 SCAR=0.0
206 4 my ( $total_size, $part_size ) = @_;
207 4 return floor( $total_size / $part_size ) + 1;
208 }
209 }
210  
211 {
212 3 package GD::Barcode::QRcode::Text;
213  
214 3 ▶ use parent qw{ GD::Barcode::QRcode };
Subroutine BEGIN: called CC=1 SCAR=0.0
215 3 ▶ use bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
216 3 ▶ use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218 3 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
219 3 my $class = shift;
220 3 my $self = $class->SUPER::new(@_);
221 3 bless $self, $class;
222 3 return $self;
223 }
224  
225 3 ▶ sub barcode {
Subroutine barcode: called CC=1 SCAR=0.0
226 3 my $self = shift;
227 3 return _trim_margin( $self->SUPER::barcode() );
228 }
229  
230 0 ▶ sub term {
Subroutine term: not called CC=4 SCAR=30.0
231 0 my $self = shift;
232 0 my $qr_text = $self->barcode;
233 0 my $term_text = '';
234  
235 0 for my $i ( 0 .. ( bytes::length($qr_text) - 1 ) ) {
236 0 my $module = bytes::substr $qr_text, $i, 1;
237 0 ▶ $term_text .=
Branch: 2 branches
BranchTrueFalseTotal
$module =~ /^[01]$/ ? : 0 0 0
$module ? : 0 0 0
238 ( $module =~ /^[01]$/ )
239 ? colored( ' ', ($module) ? 'on_black' : 'on_white' ) # 2 spaces width
240 : $module;
241 }
242  
243 0 return $term_text;
244 }
245  
246 3 ▶ sub _trim_margin {
Subroutine _trim_margin: called CC=3 SCAR=11.0
247 3 my $qr_text = shift;
248 3 my $qr_aryref = [ map { [ split //, $_ ] } split /\n/, $qr_text ];
249 3 my $ret_text = '';
250  
251 3 for my $i ( 3 .. ( ( @$qr_aryref - 1 ) - 3 ) ) {
252 297 my $line = $qr_aryref->[$i];
253 297 for my $j ( 3 .. ( ( @$line - 1 ) - 3 ) ) {
254 29403 $ret_text .= $line->[$j];
255 }
256 297 $ret_text .= "\n";
257 }
258  
259 3 return $ret_text;
260 }
261 }
262  
263 3 1;
« Petitcom.pm ↑ Summary Petitcom/Resource/GRP.pm »