include/qr/qr_img.h

filter: Statementstmt 88.8% 64 / 72 Branchbran 64.1% 59 / 92 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.0% 123 / 164 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/QR/Code/Install/Files.pm ↑ Summary Code.xs »
1 #ifndef QR_IMG_H
2 #define QR_IMG_H
3  
4 #include <stddef.h>
5  
6 /* qr_img.h - what a raster logo needs from its bytes, and nothing more.
7 *
8 * A PNG or JPEG logo is never decoded: the SVG output embeds the bytes
9 * as a data: URI and the renderer does the decoding. The only facts the
10 * box geometry needs are the format (for the MIME type) and the pixel
11 * dimensions (for the aspect ratio), and both formats state them in
12 * their headers.
13 *
14 * The format comes from magic bytes, never from a file extension. A
15 * file named .png that is actually a JPEG is a thing that happens, and
16 * an <image> whose MIME type does not match its bytes renders as
17 * nothing in some viewers.
18 */
19  
20 #define QR_IMG_UNKNOWN 0
21 #define QR_IMG_PNG 1
22 #define QR_IMG_JPEG 2
23 #define QR_IMG_SVG 3
24  
25 /* Sniff the format. SVG is anything that opens with '<' once an
26 * optional UTF-8 BOM and leading whitespace are skipped - the XML
27 * declaration, a comment, or the root element itself all qualify. */
28 20 static int qr_img_sniff(const unsigned char *p, size_t n)
29 {
30 static const unsigned char png_sig[8] =
31 { 0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A };
32 20 size_t i = 0;
33  
34 20 ▶ if (n >= 8 && memcmp(p, png_sig, 8) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (n >= 8 && memcmp(p, png_sig, 8) == 0) 20 0 20
if (n >= 8 && memcmp(p, png_sig, 8) == 0) 6 14 20
35 6 return QR_IMG_PNG;
36  
37 14 ▶ if (n >= 3 && p[0] == 0xFF && p[1] == 0xD8 && p[2] == 0xFF)
Branch: 4 branches
BranchTrueFalseTotal
if (n >= 3 && p[0] == 0xFF && p[1] == 0xD8 && p[2] == 0xFF) 14 0 14
if (n >= 3 && p[0] == 0xFF && p[1] == 0xD8 && p[2] == 0xFF) 7 7 14
if (n >= 3 && p[0] == 0xFF && p[1] == 0xD8 && p[2] == 0xFF) 7 0 7
if (n >= 3 && p[0] == 0xFF && p[1] == 0xD8 && p[2] == 0xFF) 7 0 7
38 7 return QR_IMG_JPEG;
39  
40 7 ▶ if (n >= 3 && p[0] == 0xEF && p[1] == 0xBB && p[2] == 0xBF)
Branch: 4 branches
BranchTrueFalseTotal
if (n >= 3 && p[0] == 0xEF && p[1] == 0xBB && p[2] == 0xBF) 7 0 7
if (n >= 3 && p[0] == 0xEF && p[1] == 0xBB && p[2] == 0xBF) 1 6 7
if (n >= 3 && p[0] == 0xEF && p[1] == 0xBB && p[2] == 0xBF) 1 0 1
if (n >= 3 && p[0] == 0xEF && p[1] == 0xBB && p[2] == 0xBF) 1 0 1
41 1 i = 3;
42 9 ▶ while (i < n && (p[i] == ' ' || p[i] == '\t' ||
Branch: 3 branches
BranchTrueFalseTotal
while (i < n && (p[i] == ' ' || p[i] == '\t' || 9 0 9
while (i < n && (p[i] == ' ' || p[i] == '\t' || 2 7 9
while (i < n && (p[i] == ' ' || p[i] == '\t' || 0 7 7
43 7 ▶ p[i] == '\r' || p[i] == '\n'))
Branch: 2 branches
BranchTrueFalseTotal
p[i] == '\r' || p[i] == '\n')) 0 7 7
p[i] == '\r' || p[i] == '\n')) 0 7 7
44 2 i++;
45 7 ▶ if (i < n && p[i] == '<')
Branch: 2 branches
BranchTrueFalseTotal
if (i < n && p[i] == '<') 7 0 7
if (i < n && p[i] == '<') 4 3 7
46 4 return QR_IMG_SVG;
47  
48 3 return QR_IMG_UNKNOWN;
49 }
50  
51 /* Pixel dimensions from the header. Returns 0 and fills *w, *h, or -1
52 * when the bytes are truncated or structurally wrong.
53 *
54 * PNG: the IHDR chunk is required to come first, so width and height
55 * sit at fixed offsets - big-endian 32-bit at 16 and 20, after the
56 * 8-byte signature, the 4-byte chunk length, and "IHDR". No chunk walk.
57 *
58 * JPEG: segments are a 0xFF marker byte, a marker, and for most
59 * markers a two-byte big-endian length that includes itself. Walk them
60 * until a start-of-frame marker - 0xC0 to 0xCF excluding 0xC4 (DHT),
61 * 0xC8 (JPG extension), 0xCC (DAC) - which covers baseline SOF0 and
62 * progressive SOF2 alike. Height then width sit at offsets 3 and 5 of
63 * the segment body, after the length and the precision byte. EXIF
64 * orientation is deliberately ignored; the POD owns that caveat. */
65 18 static int qr_img_dims(const unsigned char *p, size_t n, int fmt,
66 unsigned long *w, unsigned long *h)
67 {
68 18 ▶ if (fmt == QR_IMG_PNG) {
Branch: 1 branch
BranchTrueFalseTotal
if (fmt == QR_IMG_PNG) { 8 10 18
69 8 ▶ if (n < 24)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 24) 2 6 8
70 2 return -1;
71 6 ▶ if (memcmp(p + 12, "IHDR", 4) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(p + 12, "IHDR", 4) != 0) 0 6 6
72 0 return -1;
73 6 *w = ((unsigned long)p[16] << 24) | ((unsigned long)p[17] << 16) |
74 6 ((unsigned long)p[18] << 8) | (unsigned long)p[19];
75 6 *h = ((unsigned long)p[20] << 24) | ((unsigned long)p[21] << 16) |
76 6 ((unsigned long)p[22] << 8) | (unsigned long)p[23];
77 6 ▶ return (*w == 0 || *h == 0) ? -1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (*w == 0 || *h == 0) ? -1 : 0; 6 0 6
return (*w == 0 || *h == 0) ? -1 : 0; 0 6 6
78 }
79  
80 10 ▶ if (fmt == QR_IMG_JPEG) {
Branch: 1 branch
BranchTrueFalseTotal
if (fmt == QR_IMG_JPEG) { 10 0 10
81 10 size_t i = 2;
82  
83 12 ▶ while (i + 3 < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i + 3 < n) { 11 1 12
84 unsigned int marker, len;
85  
86 11 ▶ if (p[i] != 0xFF)
Branch: 1 branch
BranchTrueFalseTotal
if (p[i] != 0xFF) 0 11 11
87 0 return -1;
88 22 ▶ while (i < n && p[i] == 0xFF) /* fill bytes are legal */
Branch: 2 branches
BranchTrueFalseTotal
while (i < n && p[i] == 0xFF) /* fill bytes are legal */ 22 0 22
while (i < n && p[i] == 0xFF) /* fill bytes are legal */ 11 11 22
89 11 i++;
90 11 ▶ if (i >= n)
Branch: 1 branch
BranchTrueFalseTotal
if (i >= n) 0 11 11
91 0 return -1;
92 11 marker = p[i++];
93  
94 /* standalone markers carry no length */
95 11 ▶ if (marker == 0x01 || (marker >= 0xD0 && marker <= 0xD9))
Branch: 3 branches
BranchTrueFalseTotal
if (marker == 0x01 || (marker >= 0xD0 && marker <= 0xD9)) 11 0 11
if (marker == 0x01 || (marker >= 0xD0 && marker <= 0xD9)) 2 9 11
if (marker == 0x01 || (marker >= 0xD0 && marker <= 0xD9)) 0 2 2
96 0 continue;
97 11 ▶ if (i + 1 >= n)
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 >= n) 0 11 11
98 0 return -1;
99 11 len = ((unsigned int)p[i] << 8) | p[i + 1];
100 11 ▶ if (len < 2 || i + len > n)
Branch: 2 branches
BranchTrueFalseTotal
if (len < 2 || i + len > n) 11 0 11
if (len < 2 || i + len > n) 0 11 11
101 0 return -1;
102  
103 11 ▶ if (marker >= 0xC0 && marker <= 0xCF &&
Branch: 3 branches
BranchTrueFalseTotal
if (marker >= 0xC0 && marker <= 0xCF && 11 0 11
if (marker >= 0xC0 && marker <= 0xCF && 9 2 11
if (marker >= 0xC0 && marker <= 0xCF && 9 0 9
104 9 ▶ marker != 0xC4 && marker != 0xC8 && marker != 0xCC) {
Branch: 2 branches
BranchTrueFalseTotal
marker != 0xC4 && marker != 0xC8 && marker != 0xCC) { 9 0 9
marker != 0xC4 && marker != 0xC8 && marker != 0xCC) { 9 0 9
105 9 ▶ if (len < 7)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 7) 0 9 9
106 0 return -1;
107 9 *h = ((unsigned long)p[i + 3] << 8) | p[i + 4];
108 9 *w = ((unsigned long)p[i + 5] << 8) | p[i + 6];
109 9 ▶ return (*w == 0 || *h == 0) ? -1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
return (*w == 0 || *h == 0) ? -1 : 0; 9 0 9
return (*w == 0 || *h == 0) ? -1 : 0; 0 9 9
110 }
111  
112 2 i += len;
113 }
114 1 return -1;
115 }
116  
117 0 return -1;
118 }
119  
120 /* --- base64, for the data: URI ------------------------------------------- */
121  
122 5 static size_t qr_b64_len(size_t n)
123 {
124 5 return ((n + 2) / 3) * 4;
125 }
126  
127 5 static void qr_b64_encode(const unsigned char *src, size_t n, char *dst)
128 {
129 static const char tab[] =
130 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
131 size_t i;
132  
133 46 ▶ for (i = 0; i + 2 < n; i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 2 < n; i += 3) { 41 5 46
134 41 unsigned long v = ((unsigned long)src[i] << 16) |
135 41 ((unsigned long)src[i + 1] << 8) | src[i + 2];
136 41 *dst++ = tab[(v >> 18) & 63];
137 41 *dst++ = tab[(v >> 12) & 63];
138 41 *dst++ = tab[(v >> 6) & 63];
139 41 *dst++ = tab[v & 63];
140 }
141 5 ▶ if (i < n) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < n) { 3 2 5
142 3 unsigned long v = (unsigned long)src[i] << 16;
143 3 ▶ if (i + 1 < n)
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 < n) 3 0 3
144 3 v |= (unsigned long)src[i + 1] << 8;
145 3 *dst++ = tab[(v >> 18) & 63];
146 3 *dst++ = tab[(v >> 12) & 63];
147 3 ▶ *dst++ = (i + 1 < n) ? tab[(v >> 6) & 63] : '=';
Branch: 1 branch
BranchTrueFalseTotal
*dst++ = (i + 1 < n) ? tab[(v >> 6) & 63] : '='; 3 0 3
148 3 *dst++ = '=';
149 }
150 5 *dst = '\0';
151 5 }
152  
153 #endif /* QR_IMG_H */
« blib/arch/QR/Code/Install/Files.pm ↑ Summary Code.xs »