src/pdfmake_markup_profile.c

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 188 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/pdfmake_image_scale.c ↑ Summary src/pdfmake_render.c »
1 /*
2 * pdfmake_markup_profile.c - the rules a template runs under.
3 *
4 * Two things a template must not be able to do, and both are decided here:
5 * reach code, and turn a data value into markup. The Perl side holds no part
6 * of either; see PDF::Make::Markup::Profile for why each rule exists.
7 */
8  
9 #include "pdfmake_markup_profile.h"
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <ctype.h>
14  
15 /*
16 * {% raw value %} prints a value with no escaping, which is exactly the
17 * injection the profile exists to prevent, and Template::Stencil offers no
18 * way to disable it. A template that uses it is refused before it compiles.
19 *
20 * The scan is over the source rather than the compiled program because that
21 * is the only place the construct is still visible.
22 */
23 0 int pdfmake_profile_check_source(const char *src, size_t len,
24 uint32_t *err_line,
25 char *err, size_t errlen)
26 {
27 0 size_t i = 0;
28 0 uint32_t line = 1;
29  
30 0 ▶ if (!src) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!src) return 1; 0 0 0
31  
32 0 ▶ for (i = 0; i + 1 < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < len; i++) { 0 0 0
33 0 ▶ if (src[i] == '\n') { line++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (src[i] == '\n') { line++; continue; } 0 0 0
34 0 ▶ if (src[i] != '{' || src[i + 1] != '%') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (src[i] != '{' || src[i + 1] != '%') continue; 0 0 0
if (src[i] != '{' || src[i + 1] != '%') continue; 0 0 0
35  
36 {
37 0 size_t j = i + 2;
38 0 ▶ while (j < len && (src[j] == ' ' || src[j] == '\t')) j++;
Branch: 3 branches
BranchTrueFalseTotal
while (j < len && (src[j] == ' ' || src[j] == '\t')) j++; 0 0 0
while (j < len && (src[j] == ' ' || src[j] == '\t')) j++; 0 0 0
while (j < len && (src[j] == ' ' || src[j] == '\t')) j++; 0 0 0
39 0 ▶ if (j + 3 > len) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (j + 3 > len) continue; 0 0 0
40 0 ▶ if (memcmp(src + j, "raw", 3) != 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(src + j, "raw", 3) != 0) continue; 0 0 0
41 /* "raw" must be the whole word: {% rawtotal %} is a variable. */
42 0 ▶ if (j + 3 < len && (isalnum((unsigned char)src[j + 3]) ||
Branch: 2 branches
BranchTrueFalseTotal
if (j + 3 < len && (isalnum((unsigned char)src[j + 3]) || 0 0 0
if (j + 3 < len && (isalnum((unsigned char)src[j + 3]) || 0 0 0
43 0 ▶ src[j + 3] == '_' || src[j + 3] == '.'))
Branch: 2 branches
BranchTrueFalseTotal
src[j + 3] == '_' || src[j + 3] == '.')) 0 0 0
src[j + 3] == '_' || src[j + 3] == '.')) 0 0 0
44 0 continue;
45  
46 0 ▶ if (err_line) *err_line = line;
Branch: 1 branch
BranchTrueFalseTotal
if (err_line) *err_line = line; 0 0 0
47 0 ▶ if (err && errlen)
Branch: 2 branches
BranchTrueFalseTotal
if (err && errlen) 0 0 0
if (err && errlen) 0 0 0
48 0 snprintf(err, errlen,
49 "{%% raw %%} is not available. It prints a value without "
50 "escaping, so a name or address containing < or & would "
51 "become document structure. Values are escaped; markup "
52 "belongs in the template.");
53 0 return 0;
54 }
55 }
56 0 return 1;
57 }
58  
59 /*
60 * Formatting a template can use without being able to compute. Both of these
61 * are pure functions of their input: a filter that could do anything else
62 * would be a way back to code.
63 */
64  
65 0 static int numeric(const char *v, size_t len, double *out) {
66 char buf[64];
67 0 size_t n = 0, i;
68 0 int digits = 0, dot = 0;
69  
70 0 ▶ while (len && isspace((unsigned char)*v)) { v++; len--; }
Branch: 2 branches
BranchTrueFalseTotal
while (len && isspace((unsigned char)*v)) { v++; len--; } 0 0 0
while (len && isspace((unsigned char)*v)) { v++; len--; } 0 0 0
71 0 ▶ while (len && isspace((unsigned char)v[len - 1])) len--;
Branch: 2 branches
BranchTrueFalseTotal
while (len && isspace((unsigned char)v[len - 1])) len--; 0 0 0
while (len && isspace((unsigned char)v[len - 1])) len--; 0 0 0
72 0 ▶ if (!len || len >= sizeof(buf)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!len || len >= sizeof(buf)) return 0; 0 0 0
if (!len || len >= sizeof(buf)) return 0; 0 0 0
73  
74 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
75 0 char c = v[i];
76 0 ▶ if (i == 0 && (c == '-' || c == '+')) { buf[n++] = c; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (i == 0 && (c == '-' || c == '+')) { buf[n++] = c; continue; } 0 0 0
if (i == 0 && (c == '-' || c == '+')) { buf[n++] = c; continue; } 0 0 0
if (i == 0 && (c == '-' || c == '+')) { buf[n++] = c; continue; } 0 0 0
77 0 ▶ if (c == '.') { if (dot) return 0; dot = 1; buf[n++] = c; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '.') { if (dot) return 0; dot = 1; buf[n++] = c; continue; } 0 0 0
if (c == '.') { if (dot) return 0; dot = 1; buf[n++] = c; continue; } 0 0 0
78 0 ▶ if (c < '0' || c > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c < '0' || c > '9') return 0; 0 0 0
if (c < '0' || c > '9') return 0; 0 0 0
79 0 buf[n++] = c;
80 0 digits++;
81 }
82 0 ▶ if (!digits) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!digits) return 0; 0 0 0
83 0 buf[n] = '\0';
84 0 *out = strtod(buf, NULL);
85 0 return 1;
86 }
87  
88 /* Group the integer part in threes, in place. */
89 0 static size_t group(char *s, size_t len) {
90 0 size_t start = (s[0] == '-') ? 1 : 0;
91 0 size_t digits = len - start;
92 0 ▶ size_t commas = digits ? (digits - 1) / 3 : 0;
Branch: 1 branch
BranchTrueFalseTotal
size_t commas = digits ? (digits - 1) / 3 : 0; 0 0 0
93 size_t i, from, to;
94  
95 0 ▶ if (!commas) return len;
Branch: 1 branch
BranchTrueFalseTotal
if (!commas) return len; 0 0 0
96  
97 0 from = len;
98 0 to = len + commas;
99 0 s[to] = '\0';
100 0 ▶ for (i = 0; from > start; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; from > start; ) { 0 0 0
101 0 s[--to] = s[--from];
102 0 ▶ if (++i % 3 == 0 && from > start) s[--to] = ',';
Branch: 2 branches
BranchTrueFalseTotal
if (++i % 3 == 0 && from > start) s[--to] = ','; 0 0 0
if (++i % 3 == 0 && from > start) s[--to] = ','; 0 0 0
103 }
104 0 return len + commas;
105 }
106  
107 0 size_t pdfmake_profile_money(const char *v, size_t len, char *out, size_t outlen)
108 {
109 double d;
110 int n;
111 char *dot;
112 char frac[8];
113 size_t ilen, flen;
114  
115 0 ▶ if (!numeric(v, len, &d)) { if (outlen) out[0] = '\0'; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (!numeric(v, len, &d)) { if (outlen) out[0] = '\0'; return 0; } 0 0 0
if (!numeric(v, len, &d)) { if (outlen) out[0] = '\0'; return 0; } 0 0 0
116 0 n = snprintf(out, outlen, "%.2f", d);
117 0 ▶ if (n < 0 || (size_t)n >= outlen) { if (outlen) out[0] = '\0'; return 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (n < 0 || (size_t)n >= outlen) { if (outlen) out[0] = '\0'; return 0; } 0 0 0
if (n < 0 || (size_t)n >= outlen) { if (outlen) out[0] = '\0'; return 0; } 0 0 0
if (n < 0 || (size_t)n >= outlen) { if (outlen) out[0] = '\0'; return 0; } 0 0 0
118  
119 /* Group the integer part only. Grouping the whole string counts from the
120 * end and puts a separator in the pence: 1240.50 became 1,240,.50. */
121 0 dot = strchr(out, '.');
122 0 ▶ if (!dot) return group(out, (size_t)n);
Branch: 1 branch
BranchTrueFalseTotal
if (!dot) return group(out, (size_t)n); 0 0 0
123  
124 0 flen = strlen(dot);
125 0 ▶ if (flen >= sizeof(frac)) return (size_t)n;
Branch: 1 branch
BranchTrueFalseTotal
if (flen >= sizeof(frac)) return (size_t)n; 0 0 0
126 0 memcpy(frac, dot, flen + 1);
127 0 *dot = '\0';
128  
129 0 ilen = group(out, strlen(out));
130 0 ▶ if (ilen + flen + 1 >= outlen) { out[0] = '\0'; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (ilen + flen + 1 >= outlen) { out[0] = '\0'; return 0; } 0 0 0
131 0 memcpy(out + ilen, frac, flen + 1);
132 0 return ilen + flen;
133 }
134  
135 0 size_t pdfmake_profile_number(const char *v, size_t len, char *out, size_t outlen)
136 {
137 double d;
138 int n;
139 size_t ilen;
140 char frac[32];
141  
142 0 ▶ if (!numeric(v, len, &d)) { if (outlen) out[0] = '\0'; return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (!numeric(v, len, &d)) { if (outlen) out[0] = '\0'; return 0; } 0 0 0
if (!numeric(v, len, &d)) { if (outlen) out[0] = '\0'; return 0; } 0 0 0
143  
144 0 n = snprintf(out, outlen, "%.10g", d);
145 0 ▶ if (n < 0 || (size_t)n >= outlen) { if (outlen) out[0] = '\0'; return 0; }
Branch: 3 branches
BranchTrueFalseTotal
if (n < 0 || (size_t)n >= outlen) { if (outlen) out[0] = '\0'; return 0; } 0 0 0
if (n < 0 || (size_t)n >= outlen) { if (outlen) out[0] = '\0'; return 0; } 0 0 0
if (n < 0 || (size_t)n >= outlen) { if (outlen) out[0] = '\0'; return 0; } 0 0 0
146  
147 { /* split off the fraction, group the integer part, put it back */
148 0 char *dot = strchr(out, '.');
149 0 frac[0] = '\0';
150 0 ▶ if (dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (dot) { 0 0 0
151 0 size_t fl = strlen(dot);
152 0 ▶ if (fl >= sizeof(frac)) { out[0] = '\0'; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (fl >= sizeof(frac)) { out[0] = '\0'; return 0; } 0 0 0
153 0 memcpy(frac, dot, fl + 1);
154 0 *dot = '\0';
155 }
156 0 ilen = group(out, strlen(out));
157 0 ▶ if (frac[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (frac[0]) { 0 0 0
158 0 size_t fl = strlen(frac);
159 0 ▶ if (ilen + fl + 1 >= outlen) { out[0] = '\0'; return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (ilen + fl + 1 >= outlen) { out[0] = '\0'; return 0; } 0 0 0
160 0 memcpy(out + ilen, frac, fl + 1);
161 0 ilen += fl;
162 }
163 }
164 0 return ilen;
165 }
« src/pdfmake_image_scale.c ↑ Summary src/pdfmake_render.c »