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