include/qr/qr_bits.h

filter: Statementstmt 93.1% 27 / 29 Branchbran 88.8% 16 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.4% 43 / 47 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/qr/qr_svg.h ↑ Summary include/qr/qr_encode.h »
1 #ifndef QR_BITS_H
2 #define QR_BITS_H
3  
4 #include <string.h>
5  
6 /* qr_bits.h - the bit writer that assembles a QR data stream.
7 *
8 * The stream is, in order: a 4-bit mode indicator, a character count
9 * whose width depends on the version, the payload, a terminator of up to
10 * four zero bits, zero padding to the next byte boundary, and then the
11 * alternating pad bytes 0xEC 0x11 until the data capacity is full.
12 *
13 * Bits are written most significant first, which is the only ordering
14 * the format allows and the one thing here that is easy to get backwards.
15 */
16  
17 typedef struct {
18 unsigned char *buf; /* caller-owned, at least cap bytes */
19 int cap; /* capacity in BYTES */
20 int len; /* length written so far, in BITS */
21 } qr_bits;
22  
23 365 static void qr_bits_init(qr_bits *b, unsigned char *buf, int cap)
24 {
25 365 b->buf = buf;
26 365 b->cap = cap;
27 365 b->len = 0;
28 365 memset(buf, 0, (size_t)cap);
29 365 }
30  
31 /* Append the low `n` bits of `val`, most significant first. Silently
32 * refuses to run past the buffer: capacity is chosen by the version
33 * selector before anything is written, so an overrun here would be a
34 * bug in the caller rather than bad input, and a truncated symbol is
35 * easier to diagnose than a smashed stack. */
36 45814 static void qr_bits_put(qr_bits *b, unsigned int val, int n)
37 {
38 int i;
39  
40 401478 ▶ for (i = n - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = n - 1; i >= 0; i--) { 355664 45814 401478
41 355664 int bit = (int)((val >> i) & 1u);
42 355664 int pos = b->len;
43  
44 355664 ▶ if (pos >= b->cap * 8)
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= b->cap * 8) 0 355664 355664
45 0 return;
46  
47 355664 ▶ if (bit)
Branch: 1 branch
BranchTrueFalseTotal
if (bit) 162960 192704 355664
48 162960 b->buf[pos >> 3] |= (unsigned char)(0x80 >> (pos & 7));
49  
50 355664 b->len++;
51 }
52 }
53  
54 /* The character count indicator width for byte mode. Eight bits through
55 * version 9, sixteen from version 10 - a boundary that sits inside the
56 * supported range, so a wrong branch here is invisible for short inputs
57 * and only shows up near capacity. */
58 2020 static int qr_bits_count_len(int version)
59 {
60 2020 ▶ return version < 10 ? 8 : 16;
Branch: 1 branch
BranchTrueFalseTotal
return version < 10 ? 8 : 16; 1606 414 2020
61 }
62  
63 /* Close the stream: terminator, byte alignment, then pad bytes to fill
64 * `data_cw` codewords. */
65 365 static void qr_bits_finish(qr_bits *b, int data_cw)
66 {
67 365 int limit = data_cw * 8;
68 365 int remain = limit - b->len;
69 int i;
70  
71 /* terminator: up to four zero bits, fewer if the stream is nearly
72 * full, none if it is exactly full */
73 365 ▶ if (remain > 4)
Branch: 1 branch
BranchTrueFalseTotal
if (remain > 4) 205 160 365
74 205 remain = 4;
75 1825 ▶ for (i = 0; i < remain; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < remain; i++) 1460 365 1825
76 1460 qr_bits_put(b, 0, 1);
77  
78 /* pad to a byte boundary */
79 365 ▶ while (b->len % 8 != 0)
Branch: 1 branch
BranchTrueFalseTotal
while (b->len % 8 != 0) 0 365 365
80 0 qr_bits_put(b, 0, 1);
81  
82 /* alternating pad codewords until the data capacity is used */
83 10141 ▶ for (i = 0; b->len < limit; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; b->len < limit; i++) 9776 365 10141
84 9776 ▶ qr_bits_put(b, (i % 2) ? 0x11u : 0xECu, 8);
Branch: 1 branch
BranchTrueFalseTotal
qr_bits_put(b, (i % 2) ? 0x11u : 0xECu, 8); 4811 4965 9776
85 365 }
86  
87 #endif /* QR_BITS_H */
« include/qr/qr_svg.h ↑ Summary include/qr/qr_encode.h »