Code.xs

filter: Statementstmt 86.9% 406 / 467 Branchbran 71.1% 242 / 340 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.3% 648 / 807 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/qr/qr_img.h ↑ Summary include/qr/qr_svg.h »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include <errno.h>
7  
8 #include "qr/qr_encode.h"
9 #include "qr/qr_img.h"
10 #include "qr/qr_svg.h"
11 #include "qr_abi.h"
12  
13 /* G_LIST is the 5.36 name for G_ARRAY */
14 #ifndef G_LIST
15 #define G_LIST G_ARRAY
16 #endif
17  
18 /* The whole battery is C: option parsing and validation, the encoder,
19 * the serialisers, the ABI. The .pm is a version number and the POD.
20 *
21 * Every croak message here is pinned by a test; the messages are the
22 * interface as much as the arguments are. */
23  
24 static const char qr_ecc_letter[4] = { 'L', 'M', 'Q', 'H' };
25  
26 /* ---- fetch helpers ------------------------------------------------------- */
27  
28 1568 static SV *qc_fetch(pTHX_ HV *hv, const char *key)
29 {
30 1568 SV **svp = hv_fetch(hv, key, (I32)strlen(key), 0);
31 1568 ▶ if (!svp || !SvOK(*svp))
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || !SvOK(*svp)) 628 940 1568
if (!svp || !SvOK(*svp)) 0 628 628
32 940 return NULL;
33 628 return *svp;
34 }
35  
36 43 static double qc_fetch_nv(pTHX_ HV *hv, const char *key, double dflt)
37 {
38 43 SV *sv = qc_fetch(aTHX_ hv, key);
39 43 ▶ return sv ? SvNV(sv) : dflt;
Branch: 1 branch
BranchTrueFalseTotal
return sv ? SvNV(sv) : dflt; 1 42 43
40 }
41  
42 /* ---- scalar option validation -------------------------------------------- */
43  
44 360 static int qc_ecc_of(pTHX_ SV *sv)
45 {
46 STRLEN n;
47 const char *p;
48  
49 360 ▶ if (!sv)
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) 93 267 360
50 93 return QR_ECC_M;
51 267 p = SvPV_const(sv, n);
52 267 ▶ if (n == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 1) 267 0 267
53 267 switch (*p) {
54 64 case 'L': case 'l': return QR_ECC_L;
55 68 case 'M': case 'm': return QR_ECC_M;
56 64 case 'Q': case 'q': return QR_ECC_Q;
57 69 case 'H': case 'h': return QR_ECC_H;
58 }
59 2 croak("ecc must be L, M, Q or H, not '%.*s'", (int)n, p);
60 return -1;
61 }
62  
63 163 static int qc_all_digits(const char *p, STRLEN n)
64 {
65 STRLEN i;
66 163 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 0 163 163
67 0 return 0;
68 385 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 223 162 385
69 223 ▶ if (p[i] < '0' || p[i] > '9')
Branch: 2 branches
BranchTrueFalseTotal
if (p[i] < '0' || p[i] > '9') 223 0 223
if (p[i] < '0' || p[i] > '9') 1 222 223
70 1 return 0;
71 162 return 1;
72 }
73  
74 369 static int qc_version_of(pTHX_ SV *sv)
75 {
76 STRLEN n;
77 const char *p;
78 IV v;
79  
80 369 ▶ if (!sv)
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) 212 157 369
81 212 return 0;
82 /* the digit check gates the numification: SvIV on 'seven' would
83 * warn before the croak names it */
84 157 p = SvPV_const(sv, n);
85 157 ▶ if (!qc_all_digits(p, n))
Branch: 1 branch
BranchTrueFalseTotal
if (!qc_all_digits(p, n)) 1 156 157
86 1 croak("version must be 1 to 15, not '%.*s'", (int)n, p);
87 156 v = SvIV(sv);
88 156 ▶ if (v < 1 || v > 15)
Branch: 2 branches
BranchTrueFalseTotal
if (v < 1 || v > 15) 155 1 156
if (v < 1 || v > 15) 2 153 155
89 3 croak("version must be 1 to 15, not '%.*s'", (int)n, p);
90 153 return (int)v;
91 }
92  
93 86 static int qc_quiet_of(pTHX_ SV *sv)
94 {
95 STRLEN n;
96 const char *p;
97 IV q;
98  
99 86 ▶ if (!sv)
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) 80 6 86
100 80 return 4;
101 6 p = SvPV_const(sv, n);
102 6 ▶ if (!qc_all_digits(p, n))
Branch: 1 branch
BranchTrueFalseTotal
if (!qc_all_digits(p, n)) 0 6 6
103 0 croak("quiet must be 0 to 16, not '%.*s'", (int)n, p);
104 6 q = SvIV(sv);
105 6 ▶ if (q > 16)
Branch: 1 branch
BranchTrueFalseTotal
if (q > 16) 0 6 6
106 0 croak("quiet must be 0 to 16, not '%.*s'", (int)n, p);
107 6 return (int)q;
108 }
109  
110 /* Unknown-key detection over a hash, in sorted order so the croak is
111 * deterministic when several keys are wrong at once. */
112 489 static void qc_check_keys(pTHX_ const char *what, HV *hv,
113 const char *const *ok, int nok)
114 {
115 489 AV *keys = newAV();
116 HE *he;
117 SSize_t i, n;
118  
119 489 sv_2mortal((SV *)keys);
120 489 hv_iterinit(hv);
121 1126 ▶ while ((he = hv_iternext(hv)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) 637 489 1126
122 637 ▶ av_push(keys, newSVpvn(HePV(he, PL_na), HeKLEN(he)));
Branch: 1 branch
BranchTrueFalseTotal
av_push(keys, newSVpvn(HePV(he, PL_na), HeKLEN(he))); 0 637 637
123 489 n = av_len(keys) + 1;
124 489 ▶ if (n > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) { 160 329 489
125 /* insertion sort: option hashes are tiny */
126 323 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 163 160 323
127 163 SSize_t j = i;
128 233 ▶ while (j > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (j > 0) { 165 68 233
129 165 SV **a = av_fetch(keys, j - 1, 0);
130 165 SV **b = av_fetch(keys, j, 0);
131 495 ▶ if (strcmp(SvPV_nolen(*a), SvPV_nolen(*b)) <= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(SvPV_nolen(*a), SvPV_nolen(*b)) <= 0) 165 0 165
if (strcmp(SvPV_nolen(*a), SvPV_nolen(*b)) <= 0) 95 70 165
132 95 break;
133 {
134 70 SV *tmp = *a;
135 70 AvARRAY(keys)[j - 1] = *b;
136 70 AvARRAY(keys)[j] = tmp;
137 }
138 70 j--;
139 }
140 }
141 }
142 1118 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 636 482 1118
143 636 SV **k = av_fetch(keys, i, 0);
144 636 const char *kp = SvPV_nolen(*k);
145 636 int j, found = 0;
146 1430 ▶ for (j = 0; j < nok; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nok; j++) 1423 7 1430
147 1423 ▶ if (strEQ(kp, ok[j])) { found = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(kp, ok[j])) { found = 1; break; } 629 794 1423
148 636 ▶ if (!found)
Branch: 1 branch
BranchTrueFalseTotal
if (!found) 7 629 636
149 7 croak("unknown %s option '%s'", what, kp);
150 }
151 482 }
152  
153 /* Build an option HV from (key => value) stack pairs. */
154 398 static HV *qc_pairs(pTHX_ const char *what, SV **base, I32 from, I32 items)
155 {
156 398 HV *hv = newHV();
157 I32 i;
158  
159 398 sv_2mortal((SV *)hv);
160 398 ▶ if ((items - from) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if ((items - from) % 2) 0 398 398
161 0 croak("odd number of options given to %s", what);
162 926 ▶ for (i = from; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = from; i < items; i += 2) { 528 398 926
163 STRLEN kl;
164 528 const char *k = SvPV_const(base[i], kl);
165 528 (void)hv_store(hv, k, (I32)kl, SvREFCNT_inc(base[i + 1]), 0);
166 }
167 398 return hv;
168 }
169  
170 /* ---- image sniffing ------------------------------------------------------ */
171  
172 /* Sniff logo bytes and, for rasters, prove the dimensions parse. The
173 * messages are shared between the public _sniff and the logo path, so
174 * a defect reports identically wherever the bytes came from. */
175 20 static int qc_sniff(pTHX_ const unsigned char *p, STRLEN len,
176 unsigned long *w, unsigned long *h)
177 {
178 20 int fmt = qr_img_sniff(p, len);
179  
180 20 *w = *h = 0;
181 20 ▶ if (fmt == QR_IMG_UNKNOWN) {
Branch: 1 branch
BranchTrueFalseTotal
if (fmt == QR_IMG_UNKNOWN) { 3 17 20
182 3 ▶ if (len >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 4) 3 0 3
183 3 croak("logo bytes are neither PNG, JPEG nor SVG "
184 "(starts %02x %02x %02x %02x)",
185 p[0], p[1], p[2], p[3]);
186 0 croak("logo bytes are neither PNG, JPEG nor SVG "
187 "(%d bytes long)", (int)len);
188 }
189 17 ▶ if (fmt != QR_IMG_SVG && qr_img_dims(p, len, fmt, w, h) < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (fmt != QR_IMG_SVG && qr_img_dims(p, len, fmt, w, h) < 0) 13 4 17
if (fmt != QR_IMG_SVG && qr_img_dims(p, len, fmt, w, h) < 0) 3 10 13
190 3 ▶ croak("%s bytes are truncated or malformed; no pixel "
Branch: 1 branch
BranchTrueFalseTotal
croak("%s bytes are truncated or malformed; no pixel " 2 1 3
191 "dimensions found",
192 fmt == QR_IMG_PNG ? "PNG" : "JPEG");
193 14 return fmt;
194 }
195  
196 /* ---- the logo option ----------------------------------------------------- */
197  
198 3 static SV *qc_slurp(pTHX_ const char *path)
199 {
200 3 PerlIO *f = PerlIO_open(path, "rb");
201 SV *out;
202 char buf[65536];
203 SSize_t n;
204  
205 3 ▶ if (!f)
Branch: 1 branch
BranchTrueFalseTotal
if (!f) 2 1 3
206 2 croak("logo file '%s': %s", path, Strerror(errno));
207 1 out = sv_2mortal(newSVpvs(""));
208 2 ▶ while ((n = PerlIO_read(f, buf, sizeof buf)) > 0)
Branch: 1 branch
BranchTrueFalseTotal
while ((n = PerlIO_read(f, buf, sizeof buf)) > 0) 1 1 2
209 1 sv_catpvn(out, buf, (STRLEN)n);
210 1 PerlIO_close(f);
211 1 return out;
212 }
213  
214 /* Fills *lg. Any SV whose bytes the logo borrows is mortal and lives
215 * to the end of the XSUB, which outlives the render. */
216 35 static void qc_logo_of(pTHX_ SV *sv, qr_logo *lg)
217 {
218 static const char *const ok[] =
219 { "text", "svg", "image", "file", "scale", "em" };
220 HV *h;
221 SV *text, *markup, *image, *file, *bytes;
222 int kinds;
223  
224 35 qr_logo_init(lg);
225  
226 35 ▶ if (!SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) { 13 22 35
227 STRLEN n;
228 13 const char *p = SvPV_const(sv, n);
229 13 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 1 12 13
230 1 croak("logo text is empty");
231 12 lg->kind = QR_LOGO_TEXT;
232 12 lg->text = p;
233 12 lg->text_len = n;
234 12 return;
235 }
236 22 ▶ if (SvTYPE(SvRV(sv)) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(sv)) != SVt_PVHV) 0 22 22
237 0 croak("logo must be a string or a hashref");
238 22 h = (HV *)SvRV(sv);
239 22 qc_check_keys(aTHX_ "logo", h, ok, 6);
240  
241 20 text = qc_fetch(aTHX_ h, "text");
242 20 markup = qc_fetch(aTHX_ h, "svg");
243 20 image = qc_fetch(aTHX_ h, "image");
244 20 file = qc_fetch(aTHX_ h, "file");
245 20 kinds = !!text + !!markup + !!image + !!file;
246 20 ▶ if (kinds != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (kinds != 1) 4 16 20
247 4 croak("logo needs exactly one of text, svg, image or file");
248  
249 16 lg->scale = qc_fetch_nv(aTHX_ h, "scale", 0.0);
250 16 lg->em = qc_fetch_nv(aTHX_ h, "em", 0.0);
251  
252 16 ▶ if (text) {
Branch: 1 branch
BranchTrueFalseTotal
if (text) { 1 15 16
253 1 lg->text = SvPV_const(text, lg->text_len);
254 1 ▶ if (!lg->text_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!lg->text_len) 1 0 1
255 1 croak("logo text is empty");
256 0 lg->kind = QR_LOGO_TEXT;
257 0 return;
258 }
259 15 ▶ if (markup) {
Branch: 1 branch
BranchTrueFalseTotal
if (markup) { 3 12 15
260 3 lg->markup = SvPV_const(markup, lg->markup_len);
261 3 lg->kind = QR_LOGO_SVG;
262 3 return;
263 }
264  
265 /* image bytes, or a file holding any of the three formats; the
266 * format always comes from the bytes, never from the name */
267 15 ▶ bytes = image ? image : qc_slurp(aTHX_ SvPV_nolen_const(file));
Branch: 1 branch
BranchTrueFalseTotal
bytes = image ? image : qc_slurp(aTHX_ SvPV_nolen_const(file)); 3 9 12
268 {
269 const unsigned char *p;
270 STRLEN n;
271 unsigned long w, hgt;
272 int fmt;
273  
274 10 p = (const unsigned char *)SvPVbyte(bytes, n);
275 10 fmt = qc_sniff(aTHX_ p, n, &w, &hgt);
276 7 ▶ if (fmt == QR_IMG_SVG) {
Branch: 1 branch
BranchTrueFalseTotal
if (fmt == QR_IMG_SVG) { 2 5 7
277 2 lg->kind = QR_LOGO_SVG;
278 2 lg->markup = (const char *)p;
279 2 lg->markup_len = n;
280 } else {
281 5 lg->kind = QR_LOGO_IMAGE;
282 5 lg->img = p;
283 5 lg->img_len = n;
284 5 lg->img_fmt = fmt;
285 }
286 }
287 }
288  
289 /* ---- the style option ---------------------------------------------------- */
290  
291 46 static void qc_color_copy(pTHX_ SV *sv, char *dst, size_t dstlen)
292 {
293 STRLEN n;
294 46 const char *p = SvPV_const(sv, n);
295  
296 46 ▶ if (n >= dstlen)
Branch: 1 branch
BranchTrueFalseTotal
if (n >= dstlen) 0 46 46
297 0 n = dstlen - 1;
298 46 memcpy(dst, p, n);
299 46 dst[n] = '\0';
300 46 }
301  
302 57 static void qc_style_of(pTHX_ SV *sv, qr_style *st)
303 {
304 static const char *const ok[] =
305 { "shape", "radius", "finder", "dark", "light", "finder_dark",
306 "gradient" };
307 HV *h;
308 SV *v;
309  
310 57 ▶ if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) 57 0 57
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) 0 57 57
311 0 croak("style must be a hashref");
312 57 h = (HV *)SvRV(sv);
313 57 qc_check_keys(aTHX_ "style", h, ok, 7);
314  
315 55 ▶ if ((v = qc_fetch(aTHX_ h, "shape"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = qc_fetch(aTHX_ h, "shape"))) { 16 39 55
316 16 const char *p = SvPV_nolen_const(v);
317 16 ▶ if (strEQ(p, "square")) st->shape = QR_SHAPE_SQUARE;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(p, "square")) st->shape = QR_SHAPE_SQUARE; 1 15 16
318 15 ▶ else if (strEQ(p, "rounded")) st->shape = QR_SHAPE_ROUNDED;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(p, "rounded")) st->shape = QR_SHAPE_ROUNDED; 3 12 15
319 12 ▶ else if (strEQ(p, "dot")) st->shape = QR_SHAPE_DOT;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(p, "dot")) st->shape = QR_SHAPE_DOT; 10 2 12
320 2 else croak("style shape must be square, rounded or dot, "
321 "not '%s'", p);
322 }
323 53 ▶ if ((v = qc_fetch(aTHX_ h, "finder"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = qc_fetch(aTHX_ h, "finder"))) { 4 49 53
324 4 const char *p = SvPV_nolen_const(v);
325 4 ▶ if (strEQ(p, "square")) st->finder = QR_FINDER_SQUARE;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(p, "square")) st->finder = QR_FINDER_SQUARE; 0 4 4
326 4 ▶ else if (strEQ(p, "rounded")) st->finder = QR_FINDER_ROUNDED;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(p, "rounded")) st->finder = QR_FINDER_ROUNDED; 1 3 4
327 3 ▶ else if (strEQ(p, "circle")) st->finder = QR_FINDER_CIRCLE;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(p, "circle")) st->finder = QR_FINDER_CIRCLE; 2 1 3
328 1 else croak("style finder must be square, rounded or circle, "
329 "not '%s'", p);
330 }
331 52 ▶ if ((v = qc_fetch(aTHX_ h, "radius")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = qc_fetch(aTHX_ h, "radius"))) 7 45 52
332 7 st->radius = SvNV(v);
333 52 ▶ if ((v = qc_fetch(aTHX_ h, "dark")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = qc_fetch(aTHX_ h, "dark"))) 17 35 52
334 17 qc_color_copy(aTHX_ v, st->dark, sizeof st->dark);
335 52 ▶ if ((v = qc_fetch(aTHX_ h, "light")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = qc_fetch(aTHX_ h, "light"))) 10 42 52
336 10 qc_color_copy(aTHX_ v, st->light, sizeof st->light);
337 52 ▶ if ((v = qc_fetch(aTHX_ h, "finder_dark")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = qc_fetch(aTHX_ h, "finder_dark"))) 2 50 52
338 2 qc_color_copy(aTHX_ v, st->finder_dark, sizeof st->finder_dark);
339  
340 52 ▶ if ((v = qc_fetch(aTHX_ h, "gradient"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = qc_fetch(aTHX_ h, "gradient"))) { 12 40 52
341 static const char *const gok[] = { "type", "angle", "stops" };
342 HV *g;
343 SV *t, *stops;
344  
345 12 ▶ if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV) 12 0 12
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV) 0 12 12
346 0 croak("style gradient must be a hashref");
347 12 g = (HV *)SvRV(v);
348 12 qc_check_keys(aTHX_ "gradient", g, gok, 3);
349  
350 12 st->grad_radial = 0;
351 12 ▶ if ((t = qc_fetch(aTHX_ g, "type"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((t = qc_fetch(aTHX_ g, "type"))) { 3 9 12
352 3 const char *p = SvPV_nolen_const(t);
353 3 ▶ if (strEQ(p, "radial")) st->grad_radial = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(p, "radial")) st->grad_radial = 1; 2 1 3
354 1 ▶ else if (!strEQ(p, "linear"))
Branch: 1 branch
BranchTrueFalseTotal
else if (!strEQ(p, "linear")) 1 0 1
355 1 croak("gradient type must be linear or radial, "
356 "not '%s'", p);
357 }
358 11 st->grad_angle = qc_fetch_nv(aTHX_ g, "angle", 0.0);
359  
360 11 stops = qc_fetch(aTHX_ g, "stops");
361 11 ▶ if (!stops || !SvROK(stops) ||
Branch: 2 branches
BranchTrueFalseTotal
if (!stops || !SvROK(stops) || 11 0 11
if (!stops || !SvROK(stops) || 11 0 11
362 11 ▶ SvTYPE(SvRV(stops)) != SVt_PVAV ||
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(SvRV(stops)) != SVt_PVAV || 11 0 11
363 11 ▶ av_len((AV *)SvRV(stops)) + 1 < 2)
Branch: 1 branch
BranchTrueFalseTotal
av_len((AV *)SvRV(stops)) + 1 < 2) 2 9 11
364 2 croak("gradient needs an arrayref of at least two stops");
365 {
366 9 AV *av = (AV *)SvRV(stops);
367 9 SSize_t n = av_len(av) + 1, i;
368  
369 9 ▶ if (n > QR_MAX_STOPS)
Branch: 1 branch
BranchTrueFalseTotal
if (n > QR_MAX_STOPS) 1 8 9
370 1 croak("gradient takes at most %d stops, got %d",
371 QR_MAX_STOPS, (int)n);
372 25 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 17 8 25
373 17 SV **e = av_fetch(av, i, 0);
374 17 ▶ if (!e || !SvOK(*e))
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvOK(*e)) 17 0 17
if (!e || !SvOK(*e)) 0 17 17
375 0 croak("gradient stop %d is undef", (int)i + 1);
376 17 qc_color_copy(aTHX_ *e, st->stops[i],
377 sizeof st->stops[i]);
378 }
379 8 st->nstops = (int)n;
380 }
381 }
382 48 }
383  
384 /* ---- shared build steps -------------------------------------------------- */
385  
386 364 static void qc_encode_or_croak(pTHX_ qr_matrix *q,
387 SV *data, int ecc, int version)
388 {
389 STRLEN len;
390 364 const unsigned char *p = (const unsigned char *)SvPVbyte(data, len);
391 364 int rc = qr_encode(q, p, (int)len, ecc, version);
392  
393 364 ▶ if (rc == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc == 0) 357 7 364
394 357 return;
395 7 ▶ if (rc == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc == -1) { 7 0 7
396 7 ▶ int v = version ? version : QR_MAX_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
int v = version ? version : QR_MAX_VERSION; 2 5 7
397 7 croak("payload of %d bytes exceeds the %d byte capacity of "
398 "version %d at ECC %c",
399 (int)len, qr_capacity(v, ecc), v, qr_ecc_letter[ecc]);
400 }
401 0 croak("qr_encode rejected its arguments (ecc %d, version %d)",
402 ecc, version);
403 }
404  
405 /* Parse the svg/analyse option set, encode, render. The returned
406 * string is malloc'd; the caller frees. */
407 113 static char *qc_render(pTHX_ const char *what, SV *data,
408 SV **base, I32 from, I32 items,
409 qr_svg_info *info)
410 {
411 static const char *const ok[] =
412 { "ecc", "version", "quiet", "logo", "style" };
413 113 HV *opt = qc_pairs(aTHX_ what, base, from, items);
414 qr_matrix q;
415 qr_style st;
416 qr_logo lg;
417 char err[256];
418 SV *osv;
419 int ecc, version, quiet;
420 char *out;
421  
422 113 qc_check_keys(aTHX_ what, opt, ok, 5);
423  
424 111 qr_style_init(&st);
425 111 qr_logo_init(&lg);
426  
427 111 ▶ if ((osv = qc_fetch(aTHX_ opt, "logo"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((osv = qc_fetch(aTHX_ opt, "logo"))) { 38 73 111
428 /* a logo is erasures, and erasures need budget from a standing
429 * start: Q or H only, and H unless the caller says otherwise */
430 38 SV *esv = qc_fetch(aTHX_ opt, "ecc");
431 38 ▶ ecc = esv ? qc_ecc_of(aTHX_ esv) : QR_ECC_H;
Branch: 1 branch
BranchTrueFalseTotal
ecc = esv ? qc_ecc_of(aTHX_ esv) : QR_ECC_H; 4 34 38
432 38 ▶ if (ecc < QR_ECC_Q)
Branch: 1 branch
BranchTrueFalseTotal
if (ecc < QR_ECC_Q) 3 35 38
433 3 croak("a centre logo needs ECC level Q or H, not %c",
434 qr_ecc_letter[ecc]);
435 35 qc_logo_of(aTHX_ osv, &lg);
436 } else {
437 73 ecc = qc_ecc_of(aTHX_ qc_fetch(aTHX_ opt, "ecc"));
438 }
439  
440 94 ▶ if ((osv = qc_fetch(aTHX_ opt, "style")))
Branch: 1 branch
BranchTrueFalseTotal
if ((osv = qc_fetch(aTHX_ opt, "style"))) 57 37 94
441 57 qc_style_of(aTHX_ osv, &st);
442  
443 85 version = qc_version_of(aTHX_ qc_fetch(aTHX_ opt, "version"));
444 82 quiet = qc_quiet_of(aTHX_ qc_fetch(aTHX_ opt, "quiet"));
445  
446 82 qc_encode_or_croak(aTHX_ &q, data, ecc, version);
447  
448 77 out = qr_svg_render(&q, quiet, &st, &lg, info, err, sizeof err);
449 77 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 23 54 77
450 23 ▶ if (err[0])
Branch: 1 branch
BranchTrueFalseTotal
if (err[0]) 23 0 23
451 23 croak("%s", err);
452 0 croak("out of memory rendering SVG");
453 }
454 54 return out;
455 }
456  
457 18 static HV *qc_info_hv(pTHX_ const qr_svg_info *info, int has_logo)
458 {
459 18 HV *ih = newHV();
460  
461 18 (void)hv_stores(ih, "version", newSViv(info->version));
462 18 (void)hv_stores(ih, "ecc", newSVpvn(&qr_ecc_letter[info->ecc], 1));
463 18 (void)hv_stores(ih, "mask", newSViv(info->mask));
464 18 (void)hv_stores(ih, "size", newSViv(info->size));
465 18 ▶ if (has_logo) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_logo) { 15 3 18
466 15 HV *lh = newHV();
467 15 (void)hv_stores(lh, "x", newSVnv(info->bx));
468 15 (void)hv_stores(lh, "y", newSVnv(info->by));
469 15 (void)hv_stores(lh, "width", newSVnv(info->bw));
470 15 (void)hv_stores(lh, "height", newSVnv(info->bh));
471 15 (void)hv_stores(lh, "covered", newSViv(info->covered));
472 15 (void)hv_stores(lh, "function_hits", newSViv(info->fn_hit));
473 15 (void)hv_stores(lh, "hits", newSVpv(info->hits, 0));
474 15 (void)hv_stores(ih, "logo", newRV_noinc((SV *)lh));
475 }
476 18 return ih;
477 }
478  
479 /* ---- the ABI ------------------------------------------------------------- */
480  
481 2 static int qc_abi_matrix(const unsigned char *data, int len, int ecc,
482 int want_version,
483 unsigned char *mod, unsigned char *fixed,
484 int *size, int *version_out, int *mask)
485 {
486 qr_matrix q;
487 2 int rc = qr_encode(&q, data, len, ecc, want_version);
488  
489 2 ▶ if (rc != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc != 0) 0 2 2
490 0 return rc;
491 2 memcpy(mod, q.mod, (size_t)(q.size * q.size));
492 2 memcpy(fixed, q.fixed, (size_t)(q.size * q.size));
493 2 ▶ if (size) *size = q.size;
Branch: 1 branch
BranchTrueFalseTotal
if (size) *size = q.size; 2 0 2
494 2 ▶ if (version_out) *version_out = q.version;
Branch: 1 branch
BranchTrueFalseTotal
if (version_out) *version_out = q.version; 2 0 2
495 2 ▶ if (mask) *mask = q.mask;
Branch: 1 branch
BranchTrueFalseTotal
if (mask) *mask = q.mask; 2 0 2
496 2 return 0;
497 }
498  
499 2 static char *qc_abi_svg(const unsigned char *data, int len, int ecc,
500 int want_version, int quiet)
501 {
502 qr_matrix q;
503 qr_style st;
504 qr_logo lg;
505 char err[256];
506  
507 2 ▶ if (qr_encode(&q, data, len, ecc, want_version) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_encode(&q, data, len, ecc, want_version) != 0) 0 2 2
508 0 return NULL;
509 2 qr_style_init(&st);
510 2 qr_logo_init(&lg);
511 2 return qr_svg_render(&q, quiet, &st, &lg, NULL, err, sizeof err);
512 }
513  
514 6 static int qc_abi_capacity(int ecc, int version)
515 {
516 6 ▶ if (ecc < 0 || ecc > 3 ||
Branch: 3 branches
BranchTrueFalseTotal
if (ecc < 0 || ecc > 3 || 6 0 6
if (ecc < 0 || ecc > 3 || 6 0 6
if (ecc < 0 || ecc > 3 || 6 0 6
517 6 ▶ version < QR_MIN_VERSION || version > QR_MAX_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
version < QR_MIN_VERSION || version > QR_MAX_VERSION) 2 4 6
518 2 return -1;
519 4 return qr_capacity(version, ecc);
520 }
521  
522 /* Read only what the caller's declared struct size covers: a consumer
523 * compiled against an older, shorter layout hands over a shorter
524 * struct, and the fields beyond it stay at their defaults. */
525 #define QC_HAS(sp, field) \
526 ((sp)->size >= (size_t)((char *)(&(sp)->field + 1) - (char *)(sp)))
527  
528 2 static char *qc_abi_svg_styled(const unsigned char *data, int len,
529 int ecc, int want_version, int quiet,
530 const qr_abi_style_t *style,
531 const qr_abi_logo_t *logo,
532 qr_abi_info_t *info,
533 char *err, size_t errlen)
534 {
535 qr_matrix q;
536 qr_style st;
537 qr_logo lg;
538 qr_svg_info si;
539 char lerr[256];
540 char *out;
541  
542 2 ▶ if (!err || !errlen) {
Branch: 2 branches
BranchTrueFalseTotal
if (!err || !errlen) { 2 0 2
if (!err || !errlen) { 0 2 2
543 0 err = lerr;
544 0 errlen = sizeof lerr;
545 }
546 2 err[0] = '\0';
547  
548 2 qr_style_init(&st);
549 2 qr_logo_init(&lg);
550  
551 2 ▶ if (style) {
Branch: 1 branch
BranchTrueFalseTotal
if (style) { 2 0 2
552 2 ▶ if (QC_HAS(style, shape)) st.shape = style->shape;
Branch: 1 branch
BranchTrueFalseTotal
if (QC_HAS(style, shape)) st.shape = style->shape; 2 0 2
553 2 ▶ if (QC_HAS(style, finder)) st.finder = style->finder;
Branch: 1 branch
BranchTrueFalseTotal
if (QC_HAS(style, finder)) st.finder = style->finder; 2 0 2
554 2 ▶ if (QC_HAS(style, radius)) st.radius = style->radius;
Branch: 1 branch
BranchTrueFalseTotal
if (QC_HAS(style, radius)) st.radius = style->radius; 2 0 2
555 2 ▶ if (QC_HAS(style, dark) && style->dark[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (QC_HAS(style, dark) && style->dark[0]) { 2 0 2
if (QC_HAS(style, dark) && style->dark[0]) { 2 0 2
556 2 memcpy(st.dark, style->dark, sizeof st.dark - 1);
557 2 st.dark[sizeof st.dark - 1] = '\0';
558 }
559 2 ▶ if (QC_HAS(style, light) && style->light[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (QC_HAS(style, light) && style->light[0]) { 2 0 2
if (QC_HAS(style, light) && style->light[0]) { 0 2 2
560 0 memcpy(st.light, style->light, sizeof st.light - 1);
561 0 st.light[sizeof st.light - 1] = '\0';
562 }
563 2 ▶ if (QC_HAS(style, finder_dark) && style->finder_dark[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (QC_HAS(style, finder_dark) && style->finder_dark[0]) { 2 0 2
if (QC_HAS(style, finder_dark) && style->finder_dark[0]) { 0 2 2
564 0 memcpy(st.finder_dark, style->finder_dark,
565 sizeof st.finder_dark - 1);
566 0 st.finder_dark[sizeof st.finder_dark - 1] = '\0';
567 }
568 2 ▶ if (QC_HAS(style, stops) && style->grad_type != QR_ABI_GRAD_NONE) {
Branch: 2 branches
BranchTrueFalseTotal
if (QC_HAS(style, stops) && style->grad_type != QR_ABI_GRAD_NONE) { 2 0 2
if (QC_HAS(style, stops) && style->grad_type != QR_ABI_GRAD_NONE) { 0 2 2
569 0 int i, n = style->nstops;
570 0 ▶ if (n < 2 || n > QR_MAX_STOPS) {
Branch: 2 branches
BranchTrueFalseTotal
if (n < 2 || n > QR_MAX_STOPS) { 0 0 0
if (n < 2 || n > QR_MAX_STOPS) { 0 0 0
571 0 snprintf(err, errlen, "gradient needs 2 to %d stops, "
572 "got %d", QR_MAX_STOPS, n);
573 0 return NULL;
574 }
575 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
576 0 memcpy(st.stops[i], style->stops[i],
577 sizeof st.stops[i] - 1);
578 0 st.stops[i][sizeof st.stops[i] - 1] = '\0';
579 }
580 0 st.nstops = n;
581 0 st.grad_radial = style->grad_type == QR_ABI_GRAD_RADIAL;
582 0 st.grad_angle = style->grad_angle;
583 }
584 }
585  
586 2 ▶ if (logo && QC_HAS(logo, kind) && logo->kind) {
Branch: 3 branches
BranchTrueFalseTotal
if (logo && QC_HAS(logo, kind) && logo->kind) { 2 0 2
if (logo && QC_HAS(logo, kind) && logo->kind) { 2 0 2
if (logo && QC_HAS(logo, kind) && logo->kind) { 2 0 2
587 2 ▶ if (ecc < QR_ECC_Q) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecc < QR_ECC_Q) { 0 2 2
588 0 snprintf(err, errlen,
589 "a centre logo needs ECC level Q or H");
590 0 return NULL;
591 }
592 2 switch (logo->kind) {
593 2 case QR_ABI_LOGO_TEXT:
594 2 lg.kind = QR_LOGO_TEXT;
595 2 lg.text = logo->text;
596 2 lg.text_len = logo->text_len;
597 2 break;
598 0 case QR_ABI_LOGO_SVG:
599 0 lg.kind = QR_LOGO_SVG;
600 0 lg.markup = logo->markup;
601 0 lg.markup_len = logo->markup_len;
602 0 break;
603 0 case QR_ABI_LOGO_IMAGE:
604 0 lg.kind = QR_LOGO_IMAGE;
605 0 lg.img = logo->img;
606 0 lg.img_len = logo->img_len;
607 0 lg.img_fmt = logo->img_fmt;
608 0 break;
609 0 default:
610 0 snprintf(err, errlen, "unknown logo kind %d", logo->kind);
611 0 return NULL;
612 }
613 2 ▶ if (QC_HAS(logo, scale)) lg.scale = logo->scale;
Branch: 1 branch
BranchTrueFalseTotal
if (QC_HAS(logo, scale)) lg.scale = logo->scale; 2 0 2
614 2 ▶ if (QC_HAS(logo, em)) lg.em = logo->em;
Branch: 1 branch
BranchTrueFalseTotal
if (QC_HAS(logo, em)) lg.em = logo->em; 2 0 2
615 }
616  
617 2 ▶ if (qr_encode(&q, data, len, ecc, want_version) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (qr_encode(&q, data, len, ecc, want_version) != 0) { 0 2 2
618 0 snprintf(err, errlen, "payload does not fit (ecc %d, "
619 "version %d)", ecc, want_version);
620 0 return NULL;
621 }
622  
623 2 out = qr_svg_render(&q, quiet, &st, &lg, &si, err, errlen);
624 2 ▶ if (out && info && info->size >= sizeof(qr_abi_info_t)) {
Branch: 3 branches
BranchTrueFalseTotal
if (out && info && info->size >= sizeof(qr_abi_info_t)) { 2 0 2
if (out && info && info->size >= sizeof(qr_abi_info_t)) { 2 0 2
if (out && info && info->size >= sizeof(qr_abi_info_t)) { 2 0 2
625 2 info->version = si.version;
626 2 info->ecc = si.ecc;
627 2 info->mask = si.mask;
628 2 info->symbol_size = si.size;
629 2 info->logo_x = si.bx;
630 2 info->logo_y = si.by;
631 2 info->logo_w = si.bw;
632 2 info->logo_h = si.bh;
633 2 info->logo_covered = si.covered;
634 2 info->logo_function_hits = si.fn_hit;
635 }
636 2 return out;
637 }
638  
639 static const qr_abi_t qc_abi = {
640 QR_ABI_VERSION,
641 qc_abi_matrix,
642 qc_abi_svg,
643 free,
644 qc_abi_capacity,
645 qc_abi_svg_styled
646 };
647  
648 MODULE = QR::Code PACKAGE = QR::Code
649  
650 PROTOTYPES: DISABLE
651  
652 void
653 matrix(class, data, ...)
654 SV *class
655 SV *data
656 PPCODE:
657 {
658 static const char *const ok[] = { "ecc", "version" };
659 281 HV *opt = qc_pairs(aTHX_ "matrix", &ST(0), 2, items);
660 qr_matrix q;
661 AV *rows, *frows;
662 int r, c;
663  
664 PERL_UNUSED_VAR(class);
665 281 qc_check_keys(aTHX_ "matrix", opt, ok, 2);
666 280 qc_encode_or_croak(aTHX_ &q, data,
667 qc_ecc_of(aTHX_ qc_fetch(aTHX_ opt, "ecc")),
668 qc_version_of(aTHX_ qc_fetch(aTHX_ opt, "version")));
669  
670 276 rows = newAV();
671 276 ▶ frows = (GIMME_V == G_LIST) ? newAV() : NULL;
Branch: 1 branch
BranchTrueFalseTotal
frows = (GIMME_V == G_LIST) ? newAV() : NULL; 143 133 276
672 13216 ▶ for (r = 0; r < q.size; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < q.size; r++) { 12940 276 13216
673 12940 AV *row = newAV();
674 12940 av_extend(row, q.size - 1);
675 707280 ▶ for (c = 0; c < q.size; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < q.size; c++) 694340 12940 707280
676 694340 av_push(row, newSViv(q.mod[r * q.size + c]));
677 12940 av_push(rows, newRV_noinc((SV *)row));
678 12940 ▶ if (frows) {
Branch: 1 branch
BranchTrueFalseTotal
if (frows) { 6803 6137 12940
679 6803 AV *frow = newAV();
680 6803 av_extend(frow, q.size - 1);
681 371546 ▶ for (c = 0; c < q.size; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < q.size; c++) 364743 6803 371546
682 364743 av_push(frow, newSViv(q.fixed[r * q.size + c]));
683 6803 av_push(frows, newRV_noinc((SV *)frow));
684 }
685 }
686  
687 276 ▶ if (frows) {
Branch: 1 branch
BranchTrueFalseTotal
if (frows) { 143 133 276
688 143 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 143 143
689 143 mPUSHs(newRV_noinc((SV *)rows));
690 143 mPUSHs(newRV_noinc((SV *)frows));
691 143 mPUSHi(q.version);
692 143 mPUSHi(q.mask);
693 143 mPUSHi(q.size);
694 } else {
695 133 ▶ mXPUSHs(newRV_noinc((SV *)rows));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)rows)); 0 133 133
696 }
697 }
698  
699 void
700 svg(class, data, ...)
701 SV *class
702 SV *data
703 PPCODE:
704 {
705 qr_svg_info info;
706 113 int want_info = GIMME_V == G_LIST;
707 char *out;
708 int has_logo;
709  
710 PERL_UNUSED_VAR(class);
711 113 out = qc_render(aTHX_ "svg", data, &ST(0), 2, items, &info);
712 54 has_logo = info.bw > 0;
713  
714 54 ▶ mXPUSHs(newSVpvn(out, strlen(out)));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newSVpvn(out, strlen(out))); 0 54 54
715 54 free(out);
716 54 ▶ if (want_info)
Branch: 1 branch
BranchTrueFalseTotal
if (want_info) 18 36 54
717 18 ▶ mXPUSHs(newRV_noinc((SV *)qc_info_hv(aTHX_ &info, has_logo)));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)qc_info_hv(aTHX_ &info, has_logo))); 0 18 18
718 }
719  
720 void
721 analyse(class, data, ...)
722 SV *class
723 SV *data
724 ALIAS:
725 analyze = 1
726 PPCODE:
727 {
728 qr_svg_info info;
729 char *out;
730 HV *ih;
731  
732 PERL_UNUSED_VAR(class);
733 PERL_UNUSED_VAR(ix);
734 0 out = qc_render(aTHX_ "analyse", data, &ST(0), 2, items, &info);
735 0 free(out);
736  
737 0 ih = qc_info_hv(aTHX_ &info, info.bw > 0);
738 0 (void)hv_stores(ih, "capacity",
739 newSViv(qr_capacity(info.version, info.ecc)));
740 0 ▶ if (info.bw > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (info.bw > 0) { 0 0 0
741 0 (void)hv_stores(ih, "logo_covered", newSViv(info.covered));
742 0 (void)hv_stores(ih, "logo_function_hit",
743 newSViv(info.fn_hit));
744 0 (void)hv_stores(ih, "logo_box_w", newSVnv(info.bw));
745 0 (void)hv_stores(ih, "logo_box_h", newSVnv(info.bh));
746 }
747 0 ▶ mXPUSHs(newRV_noinc((SV *)ih));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)ih)); 0 0 0
748 }
749  
750 SV *
751 pbm(class, data, ...)
752 SV *class
753 SV *data
754 CODE:
755 {
756 static const char *const ok[] = { "ecc", "version", "quiet" };
757 4 HV *opt = qc_pairs(aTHX_ "pbm", &ST(0), 2, items);
758 qr_matrix q;
759 int quiet, span, r, c;
760  
761 PERL_UNUSED_VAR(class);
762 4 qc_check_keys(aTHX_ "pbm", opt, ok, 3);
763 4 quiet = qc_quiet_of(aTHX_ qc_fetch(aTHX_ opt, "quiet"));
764 4 qc_encode_or_croak(aTHX_ &q, data,
765 qc_ecc_of(aTHX_ qc_fetch(aTHX_ opt, "ecc")),
766 qc_version_of(aTHX_ qc_fetch(aTHX_ opt, "version")));
767  
768 4 span = q.size + 2 * quiet;
769 4 RETVAL = newSVpvf("P1\n%d %d\n", span, span);
770 100 ▶ for (r = 0; r < span; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < span; r++) { 96 4 100
771 96 int rr = r - quiet;
772 2444 ▶ for (c = 0; c < span; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < span; c++) { 2348 96 2444
773 2348 int cc = c - quiet;
774 2182 ▶ int dark = rr >= 0 && cc >= 0 &&
Branch: 1 branch
BranchTrueFalseTotal
int dark = rr >= 0 && cc >= 0 && 2036 146 2182
775 6294 ▶ rr < q.size && cc < q.size &&
Branch: 3 branches
BranchTrueFalseTotal
rr < q.size && cc < q.size && 2182 166 2348
rr < q.size && cc < q.size && 1890 146 2036
rr < q.size && cc < q.size && 1764 126 1890
776 1764 ▶ q.mod[rr * q.size + cc];
Branch: 1 branch
BranchTrueFalseTotal
q.mod[rr * q.size + cc]; 870 894 1764
777 2348 ▶ sv_catpvn(RETVAL, dark ? "1" : "0", 1);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(RETVAL, dark ? "1" : "0", 1); 870 1478 2348
778 2348 ▶ sv_catpvn(RETVAL, c + 1 < span ? " " : "\n", 1);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvn(RETVAL, c + 1 < span ? " " : "\n", 1); 2252 96 2348
779 }
780 }
781 }
782 OUTPUT:
783 RETVAL
784  
785 int
786 _capacity(ecc, version)
787 int ecc
788 int version
789 CODE:
790 156 ▶ if (ecc < 0 || ecc > 3 ||
Branch: 3 branches
BranchTrueFalseTotal
if (ecc < 0 || ecc > 3 || 156 0 156
if (ecc < 0 || ecc > 3 || 156 0 156
if (ecc < 0 || ecc > 3 || 156 0 156
791 156 ▶ version < QR_MIN_VERSION || version > QR_MAX_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
version < QR_MIN_VERSION || version > QR_MAX_VERSION) 0 156 156
792 0 croak("_capacity: ecc %d version %d out of range", ecc, version);
793 156 RETVAL = qr_capacity(version, ecc);
794 OUTPUT:
795 RETVAL
796  
797 void
798 _sniff(bytes)
799 SV *bytes
800 PPCODE:
801 {
802 const unsigned char *p;
803 STRLEN len;
804 unsigned long w, h;
805 int fmt;
806 const char *name;
807  
808 10 p = (const unsigned char *)SvPVbyte(bytes, len);
809 10 fmt = qc_sniff(aTHX_ p, len, &w, &h);
810 7 name = fmt == QR_IMG_PNG ? "png"
811 7 ▶ : fmt == QR_IMG_JPEG ? "jpeg" : "svg";
Branch: 2 branches
BranchTrueFalseTotal
: fmt == QR_IMG_JPEG ? "jpeg" : "svg"; 5 2 7
: fmt == QR_IMG_JPEG ? "jpeg" : "svg"; 3 2 5
812 7 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 7 7
813 7 mPUSHp(name, strlen(name));
814 7 mPUSHu((UV)w);
815 7 mPUSHu((UV)h);
816 }
817  
818 # Address of the qr_abi table (qr_abi.h). A consumer XS module fetches this
819 # once at boot, INT2PTRs it, and checks ->version before using it. Not part
820 # of the public Perl API.
821 #
822 # Unsigned, not IV: where the loader maps this object decides the sign bit.
823 # A 32-bit perl above 0x7fffffff, or illumos putting shared objects up at
824 # 0xfffffd7f..., would hand back a negative number from PTR2IV.
825 UV
826 _abi_ptr()
827 CODE:
828 3 ▶ RETVAL = PTR2UV(&qc_abi);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(&qc_abi); 0 3 3
829 OUTPUT:
830 RETVAL
831  
832 void
833 _abi_selftest()
834 PPCODE:
835 {
836 2 HV *hv = newHV();
837 unsigned char mod[QR_ABI_MAX_SIZE * QR_ABI_MAX_SIZE];
838 unsigned char fixed[QR_ABI_MAX_SIZE * QR_ABI_MAX_SIZE];
839 2 int size = 0, ver = 0, mask = 0;
840 static const unsigned char payload[] = "qr-abi-selftest";
841 2 int plen = (int)sizeof(payload) - 1;
842 2 int matrix_ok = 0, svg_ok = 0, capacity_ok = 0, styled_ok = 0;
843 qr_matrix q;
844 char *s;
845  
846 4 if (qc_abi.matrix &&
847 2 qc_abi.matrix(payload, plen, QR_ABI_ECC_M, 0,
848 2 ▶ mod, fixed, &size, &ver, &mask) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
mod, fixed, &size, &ver, &mask) == 0 && 2 0 2
849 2 qr_encode(&q, payload, plen, QR_ECC_M, 0) == 0 &&
850 2 ▶ size == q.size && ver == q.version && mask == q.mask &&
Branch: 3 branches
BranchTrueFalseTotal
size == q.size && ver == q.version && mask == q.mask && 2 0 2
size == q.size && ver == q.version && mask == q.mask && 2 0 2
size == q.size && ver == q.version && mask == q.mask && 2 0 2
851 2 ▶ memcmp(mod, q.mod, (size_t)(size * size)) == 0 &&
Branch: 1 branch
BranchTrueFalseTotal
memcmp(mod, q.mod, (size_t)(size * size)) == 0 && 2 0 2
852 2 ▶ memcmp(fixed, q.fixed, (size_t)(size * size)) == 0)
Branch: 1 branch
BranchTrueFalseTotal
memcmp(fixed, q.fixed, (size_t)(size * size)) == 0) 2 0 2
853 2 matrix_ok = 1;
854  
855 2 s = qc_abi.svg ? qc_abi.svg(payload, plen, QR_ABI_ECC_M, 0, 4)
856 2 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 2 0 2
857 2 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 2 0 2
858 2 svg_ok = strncmp(s, "<svg ", 5) == 0;
859 2 qc_abi.free_fn(s);
860 }
861  
862 2 ▶ capacity_ok = qc_abi.capacity &&
Branch: 1 branch
BranchTrueFalseTotal
capacity_ok = qc_abi.capacity && 2 0 2
863 4 ▶ qc_abi.capacity(QR_ABI_ECC_M, 1) == qr_capacity(1, QR_ECC_M) &&
Branch: 1 branch
BranchTrueFalseTotal
qc_abi.capacity(QR_ABI_ECC_M, 1) == qr_capacity(1, QR_ECC_M) && 2 0 2
864 6 ▶ qc_abi.capacity(QR_ABI_ECC_H, 15) == qr_capacity(15, QR_ECC_H) &&
Branch: 2 branches
BranchTrueFalseTotal
qc_abi.capacity(QR_ABI_ECC_H, 15) == qr_capacity(15, QR_ECC_H) && 2 0 2
qc_abi.capacity(QR_ABI_ECC_H, 15) == qr_capacity(15, QR_ECC_H) && 2 0 2
865 2 qc_abi.capacity(0, 99) == -1;
866  
867 2 ▶ if (qc_abi.svg_styled) {
Branch: 1 branch
BranchTrueFalseTotal
if (qc_abi.svg_styled) { 2 0 2
868 qr_abi_style_t st;
869 qr_abi_logo_t lg;
870 qr_abi_info_t inf;
871 char err[128];
872  
873 2 memset(&st, 0, sizeof st);
874 2 st.size = sizeof st;
875 2 st.shape = QR_ABI_SHAPE_ROUNDED;
876 2 strcpy(st.dark, "#102a43");
877  
878 2 memset(&lg, 0, sizeof lg);
879 2 lg.size = sizeof lg;
880 2 lg.kind = QR_ABI_LOGO_TEXT;
881 2 lg.text = "ok";
882 2 lg.text_len = 2;
883  
884 2 memset(&inf, 0, sizeof inf);
885 2 inf.size = sizeof inf;
886  
887 2 s = qc_abi.svg_styled(payload, plen, QR_ABI_ECC_H, 0, 4,
888 &st, &lg, &inf, err, sizeof err);
889 2 ▶ if (s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s) { 2 0 2
890 6 styled_ok = strncmp(s, "<svg ", 5) == 0 &&
891 2 ▶ strstr(s, "#102a43") != NULL &&
Branch: 1 branch
BranchTrueFalseTotal
strstr(s, "#102a43") != NULL && 2 0 2
892 2 ▶ strstr(s, ">ok</text>") != NULL &&
Branch: 1 branch
BranchTrueFalseTotal
strstr(s, ">ok</text>") != NULL && 2 0 2
893 6 ▶ inf.symbol_size == 17 + 4 * inf.version &&
Branch: 2 branches
BranchTrueFalseTotal
inf.symbol_size == 17 + 4 * inf.version && 2 0 2
inf.symbol_size == 17 + 4 * inf.version && 2 0 2
894 2 ▶ inf.logo_covered > 0;
Branch: 1 branch
BranchTrueFalseTotal
inf.logo_covered > 0; 2 0 2
895 2 qc_abi.free_fn(s);
896 }
897 }
898  
899 2 (void)hv_stores(hv, "version", newSVuv(qc_abi.version));
900 2 (void)hv_stores(hv, "matrix_ok", newSViv(matrix_ok));
901 2 (void)hv_stores(hv, "svg_ok", newSViv(svg_ok));
902 2 (void)hv_stores(hv, "free_ok", newSViv(qc_abi.free_fn != NULL));
903 2 (void)hv_stores(hv, "capacity_ok", newSViv(capacity_ok));
904 2 (void)hv_stores(hv, "styled_ok", newSViv(styled_ok));
905 2 ▶ mXPUSHs(newRV_noinc((SV *)hv));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)hv)); 0 2 2
906 }
« include/qr/qr_img.h ↑ Summary include/qr/qr_svg.h »