lib/App/FuguSeed/Codewords.pm

filter: Statementstmt 100% 83 / 83 Branchbran 83.3% 5 / 6 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 19 / 19 Podpod 100% 1 / 1 totaltotal 95.6% 109 / 114 CC 8 SCAR 6.2
CC
8
Cov
97%
_multiply
3.7
_check
3.0
_generator
3.0
CRAP 1.9
« blib/lib/App/FuguSeed/ListFile.pm ↑ Summary lib/App/FuguSeed/Mnemonic.pm »
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2026 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 package App::FuguSeed::Codewords;
19 2 our $VERSION = '0.2.0';
20  
21 2 ▶use v5.34;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶no feature qw(indirect multidimensional bareword_filehandles);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # App::FuguSeed::Codewords - the 44 codewords of a Standard SeedQR.
27 #
28 # The module turns the 48 digits of QR-MNEMONIC-3 into the 34 data
29 # codewords and the 10 error correction codewords of QR-CODEWORDS.
30 # The code is QR version 2, error correction level L, numeric mode,
31 # in one block.
32 #
33 # Every method is a class method, and every method is pure: it takes
34 # values and it returns values. The module touches no stream, and it
35 # loads no module.
36 #
37 # Each constant of the QR standard names the table that it comes from
38 # (QR-PROGRAM-7). The standard is ISO/IEC 18004.
39  
40 # MODE:
41 # The mode indicator of numeric mode (Table 2).
42 2 ▶use constant MODE => '0001';
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 # COUNT_BITS:
45 # The bit width of the character count in numeric mode, for
46 # version 1 to version 9 (Table 3).
47 2 ▶use constant COUNT_BITS => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 # GROUP_DIGITS and GROUP_BITS:
50 # Numeric mode takes the digits in groups of 3, and it writes
51 # each group as 10 bits (8.4.2).
52 2 ▶use constant GROUP_DIGITS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use constant GROUP_BITS => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 # TERMINATOR:
56 # The terminator after the data bits (8.4.8).
57 2 ▶use constant TERMINATOR => '0000';
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 # CODEWORD_BITS:
60 # The bit width of one codeword (8.4.8).
61 2 ▶use constant CODEWORD_BITS => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 # DATA_CODEWORDS and CHECK_CODEWORDS:
64 # The codeword counts of version 2 at error correction level L,
65 # in one block (Table 9).
66 2 ▶use constant DATA_CODEWORDS => 34;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use constant CHECK_CODEWORDS => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 # PAD:
70 # The two pad codewords, in turn, after the terminator (8.4.9).
71 2 ▶use constant PAD => [ 0xEC, 0x11 ];
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 # FIELD:
74 # The prime modulus of the field GF(256) of the error
75 # correction codewords (Annex A).
76 2 ▶use constant FIELD => 0x11D;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 # The power table and the logarithm table of the field. The table
79 # entry $EXP[$i] is 2 to the power $i over GF(256), and $LOG is its
80 # inverse (Annex A).
81 2 my ( @EXP, @LOG );
82 {
83 2 my $value = 1;
84 2 for my $power ( 0 .. 254 ) {
85 510 $EXP[$power] = $value;
86 510 $LOG[$value] = $power;
87 510 $value <<= 1;
88 510 ▶ $value ^= FIELD if $value & 0x100;
Branch: 1 branch
BranchTrueFalseTotal
if $value & 256 256 254 510
89 }
90 }
91  
92 # $class->encode($digits):
93 # The 44 codewords of the digit string $digits: the 34 data
94 # codewords, then the 10 error correction codewords.
95 5 ▶sub encode ( $class, $digits )
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
96 {
97 5 my @data = $class->_data($digits);
98  
99 5 return @data, $class->_check(@data);
100 }
101  
102 # $class->_data($digits):
103 # The 34 data codewords of QR-CODEWORDS-2. The bit stream holds
104 # the mode indicator, the character count, the digit groups,
105 # the terminator, and the zero bits to a codeword boundary. The
106 # two pad codewords fill the rest in turn.
107 5 ▶sub _data ( $, $digits )
Subroutine _data: called CC=2 SCAR=6.9
108 {
109 5 my $bits = MODE . sprintf( '%0' . COUNT_BITS . 'b', length $digits );
110 $bits .= join q{},
111 5 map { sprintf '%0' . GROUP_BITS . 'b', $_ }
112 unpack( '(A' . GROUP_DIGITS . ')*', $digits );
113 5 $bits .= TERMINATOR;
114 5 $bits .= '0' x ( ( CODEWORD_BITS - length($bits) % CODEWORD_BITS )
115 % CODEWORD_BITS );
116  
117 5 my @data = map { oct( '0b' . $_ ) }
118 unpack( '(A' . CODEWORD_BITS . ')*', $bits );
119  
120 5 my @pad = @{ +PAD };
121 5 my $next = 0;
122 5 ▶ while ( @data < DATA_CODEWORDS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data < 34) 55 5 60
123 55 push @data, $pad[ $next++ % @pad ];
124 }
125  
126 5 return @data;
127 }
128  
129 # $class->_check(@data):
130 # The 10 error correction codewords of @data: the remainder of
131 # the data polynomial over the generator polynomial, over
132 # GF(256) (QR-CODEWORDS-3).
133 5 ▶sub _check ( $, @data )
Subroutine _check: called CC=3 SCAR=11.0
134 {
135 5 my @generator = _generator(CHECK_CODEWORDS);
136 5 my @remainder = (0) x CHECK_CODEWORDS;
137  
138 5 for my $codeword (@data) {
139 170 my $factor = $codeword ^ shift @remainder;
140 170 push @remainder, 0;
141 170 for my $term ( 0 .. $#remainder ) {
142 1700 $remainder[$term] ^=
143 _multiply( $generator[ $term + 1 ], $factor );
144 }
145 }
146  
147 5 return @remainder;
148 }
149  
150 # _generator($degree):
151 # The generator polynomial of degree $degree, the coefficient
152 # of the highest power first. It is the product of the terms
153 # (x - 2**$i) over GF(256), for $i from 0 to $degree - 1
154 # (Annex A).
155 5 ▶sub _generator ($degree)
Subroutine _generator: called CC=3 SCAR=11.0
156 {
157 5 my @polynomial = (1);
158  
159 5 for my $power ( 0 .. $degree - 1 ) {
160 50 my @product = ( @polynomial, 0 );
161 50 for my $term ( 0 .. $#polynomial ) {
162 275 $product[ $term + 1 ] ^=
163 _multiply( $polynomial[$term], $EXP[$power] );
164 }
165 50 @polynomial = @product;
166 }
167  
168 5 return @polynomial;
169 }
170  
171 # _multiply($left, $right):
172 # The product of $left and $right over GF(256) (Annex A).
173 1975 ▶sub _multiply ( $left, $right )
Subroutine _multiply: called CC=3 SCAR=13.1
174 {
175 1975 ▶ return 0 if $left == 0 || $right == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $left == 0 or $right == 0 0 1975 1975
Condition33%1/3
$left == 0 or $right == 0
l!l&&r!l&&!r
001975
MC/DC0%0/2
$left == 0 or $right == 0
$left == 0 $right == 0
Truth table33%1/3
A or B
A= $left == 0B= $right == 0
ABresult
1X1
011
000
176  
177 1975 return $EXP[ ( $LOG[$left] + $LOG[$right] ) % 255 ];
178 }
179  
180 2 1;
« blib/lib/App/FuguSeed/ListFile.pm ↑ Summary lib/App/FuguSeed/Mnemonic.pm »