util.c

filter: Statementstmt 95.5% 150 / 157 Branchbran 81.5% 75 / 92 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.3% 225 / 249 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« XSHeaders.xs ↑ Summary
1 #define PERL_NO_GET_CONTEXT /* we want efficiency */
2 #include "EXTERN.h"
3 #include "perl.h"
4  
5 #include "glog.h"
6 #include "gmem.h"
7 #include "header.h"
8 #include "util.h"
9  
10 /* Append string str at pos in buf; stops at max to prevent overrun. */
11 static int string_append(char* buf, int pos, int max, const char* str);
12  
13 /* Cleanup string str (as used in as_string), leaving cleaned up result in */
14 /* buf, with maximum length len; use newl as new line terminator. */
15 static int string_cleanup(const char* str, char* buf, int len, const char* newl);
16  
17 119 void set_value(pTHX_ HList* h, const char* ckey, SV* pval) {
18 SV *deref;
19 AV *array;
20  
21 119 ▶ if ( ! SvOK(pval) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ! SvOK(pval) ) { 1 118 119
22 GLOG(("=X= deleting [%s]", ckey));
23 1 hlist_del( h, ckey );
24 1 return;
25 }
26  
27 118 ▶ if ( ! SvROK(pval) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ! SvROK(pval) ) { 109 9 118
28 109 set_scalar(aTHX_ h, ckey, pval);
29 109 return;
30 }
31  
32 9 deref = SvRV(pval);
33 9 ▶ if (SvTYPE(deref) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(deref) != SVt_PVAV) { 3 6 9
34 3 set_scalar(aTHX_ h, ckey, pval);
35 3 return;
36 }
37  
38 6 array = (AV*) deref;
39 6 set_array(aTHX_ h, ckey, array);
40 }
41  
42 112 void set_scalar(pTHX_ HList* h, const char* ckey, SV* pval) {
43 112 hlist_add(h, ckey, newSVsv(pval));
44 GLOG(("=X= set scalar [%s] => [%s]", ckey, SvPV_nolen(pval)));
45 112 }
46  
47 6 void set_array(pTHX_ HList* h, const char* ckey, AV* pval) {
48 6 int count = av_len(pval) + 1;
49 int j;
50 19 ▶ for (j = 0; j < count; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; ++j) { 13 6 19
51 13 SV** svp = av_fetch(pval, j, 0);
52 GLOG(("=X= set array %2d [%s]", j, ckey));
53 13 set_value(aTHX_ h, ckey, *svp);
54 }
55 6 }
56  
57 9 void return_hlist(pTHX_ HList* list, const char* func, int want) {
58 9 dSP;
59 int count;
60  
61 9 ▶ if (want == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (want == G_VOID) { 0 9 9
62 GLOG(("=X= %s: no return expected, nothing will be returned", func));
63 0 return;
64 }
65  
66 9 count = hlist_size(list);
67  
68 9 ▶ if (want == G_SCALAR) {
Branch: 1 branch
BranchTrueFalseTotal
if (want == G_SCALAR) { 3 6 9
69 GLOG(("=X= %s: returning number of elements", func));
70 3 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 3 3
71 3 PUSHs(sv_2mortal(newSViv(count)));
72 3 PUTBACK;
73 }
74  
75 9 ▶ if (count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count <= 0) { 2 7 9
76 GLOG(("=X= %s: hlist is empty, returning nothing", func));
77 2 return;
78 }
79  
80 7 ▶ if (want == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (want == G_ARRAY) { 5 2 7
81 5 int num = 0;
82 int j;
83  
84 GLOG(("=X= %s: returning as %d elements", func, count));
85 5 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 5 0 5
EXTEND(SP, count); 0 5 5
86  
87 18 ▶ for (j = 0; j < list->ulen; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < list->ulen; ++j) { 13 5 18
88 13 HNode* node = &list->data[j];
89 13 const char* s = node->header->name;
90 13 ++num;
91  
92 GLOG(("=X= %s: returning %2d - str [%s]", func, num, s));
93 13 PUSHs(sv_2mortal(newSVpv(s, 0)));
94 }
95 5 PUTBACK;
96 }
97 }
98  
99 89 void return_plist(pTHX_ PList* list, const char* func, int want) {
100 int count;
101 89 dSP;
102  
103 89 ▶ if (want == G_VOID) {
Branch: 1 branch
BranchTrueFalseTotal
if (want == G_VOID) { 6 83 89
104 GLOG(("=X= %s: no return expected, nothing will be returned", func));
105 6 return;
106 }
107  
108 83 count = plist_size(list);
109  
110 83 ▶ if (count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count <= 0) { 0 83 83
111 0 ▶ if (want == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (want == G_ARRAY) { 0 0 0
112 GLOG(("=X= %s: plist is empty, wantarray => 0", func));
113 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
114 0 PUSHs(sv_2mortal(newSViv(0)));
115 0 PUTBACK;
116 } else {
117 GLOG(("=X= %s: plist is empty, returning nothing", func));
118 }
119 0 return;
120 }
121  
122 GLOG(("=X= %s: returning %d values", func, count));
123  
124 83 ▶ if (want == G_SCALAR) {
Branch: 1 branch
BranchTrueFalseTotal
if (want == G_SCALAR) { 38 45 83
125 GLOG(("=X= %s: returning as single string", func));
126 38 ▶ EXTEND( SP, 1 );
Branch: 1 branch
BranchTrueFalseTotal
EXTEND( SP, 1 ); 0 38 38
127  
128 38 ▶ if ( count == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( count == 1 ) { 34 4 38
129 /*
130 * handle returning one value, useful when storing an object
131 */
132 34 PNode* node = &list->data[0];
133 34 PUSHs( (SV*)node->ptr );
134  
135 } else {
136  
137 /*
138 * concatenate values, useful for full header strings
139 */
140  
141 4 int size = 16;
142 int j;
143 15 ▶ for (j = 0; j < list->ulen; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < list->ulen; ++j) { 11 4 15
144 11 PNode* node = &list->data[j];
145 STRLEN len;
146 11 SvPV( (SV*)node->ptr, len ); /* We just need the length */
147 11 size += len + 2;
148 }
149  
150 {
151 char* rstr;
152 4 int rpos = 0;
153 4 int num = 0;
154 int j;
155 4 GMEM_NEW(rstr, char*, size);
156 15 ▶ for (j = 0; j < list->ulen; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < list->ulen; ++j) { 11 4 15
157 11 PNode* node = &list->data[j];
158 11 ++num;
159  
160 STRLEN len;
161 11 char* str = SvPV( (SV*)node->ptr, len );
162 GLOG(("=X= %s: returning %2d - str [%s]", func, num, str));
163 11 ▶ if (rpos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rpos > 0) { 7 4 11
164 7 rstr[rpos++] = ',';
165 7 rstr[rpos++] = ' ';
166 }
167  
168 11 memcpy(rstr + rpos, str, len);
169 11 rpos += len;
170 }
171  
172 4 rstr[rpos] = '\0';
173 4 PUSHs(sv_2mortal(newSVpv(rstr, rpos)));
174 4 GMEM_DEL(rstr, char*, size);
175 }
176 }
177  
178 38 PUTBACK;
179 }
180  
181 83 ▶ if (want == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (want == G_ARRAY) { 45 38 83
182 45 int num = 0;
183 int j;
184 GLOG(("=X= %s: returning as %d elements", func, count));
185 45 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 45 0 45
EXTEND(SP, count); 0 45 45
186 103 ▶ for (j = 0; j < list->ulen; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < list->ulen; ++j) { 58 45 103
187 58 PNode* node = &list->data[j];
188 58 ++num;
189  
190 58 PUSHs( (SV*)node->ptr );
191 }
192  
193 45 PUTBACK;
194 }
195 }
196  
197 32 char* format_all(pTHX_ HList* h, int sort, const char* endl, int* size) {
198 32 int le = strlen(endl);
199 int j;
200 32 *size = 64;
201  
202 32 ▶ if (sort) {
Branch: 1 branch
BranchTrueFalseTotal
if (sort) { 30 2 32
203 30 hlist_sort(h);
204 }
205  
206 154 ▶ for (j = 0; j < h->ulen; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < h->ulen; ++j) { 122 32 154
207 122 HNode* hn = &h->data[j];
208 122 const char* header = hn->header->name;
209 122 int lh = strlen(header);
210 122 PList* pl = hn->values;
211 int k;
212 253 ▶ for (k = 0; k < pl->ulen; ++k) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < pl->ulen; ++k) { 131 122 253
213 131 PNode* pn = &pl->data[k];
214 131 const char* value = SvPV_nolen( (SV*) pn->ptr );
215 131 int lv = strlen(value);
216 /* string_cleanup always emits a trailing newl ("le" bytes) for
217 * values that don't already end in "\n"; the original formula
218 * did not account for that, producing a heap-buffer-overflow
219 * when many empty (lv == 0) values were serialised. The +le
220 * term below closes that gap. */
221 131 *size += lh + 2 + lv + lv * le + le;
222 }
223 }
224  
225 {
226 char* rstr;
227 32 int rpos = 0;
228 32 GMEM_NEW(rstr, char*, *size);
229 154 ▶ for (j = 0; j < h->ulen; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < h->ulen; ++j) { 122 32 154
230 122 HNode* hn = &h->data[j];
231 122 const char* header = hn->header->name;
232 122 int lh = strlen(header);
233 122 PList* pl = hn->values;
234 int k;
235 PNode *pn;
236 const char *value;
237 253 ▶ for (k = 0; k < pl->ulen; ++k) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < pl->ulen; ++k) { 131 122 253
238 131 memcpy(rstr + rpos, header, lh);
239 131 rpos += lh;
240 131 rstr[rpos++] = ':';
241 131 rstr[rpos++] = ' ';
242  
243 131 pn = &pl->data[k];
244 131 value = SvPV_nolen( (SV*) pn->ptr );
245 131 rpos += string_cleanup(value, rstr + rpos, *size - rpos, endl);
246 }
247 }
248  
249 32 rstr[rpos] = '\0';
250 GLOG(("=X= format_all (%d/%d) [%s]", rpos, *size, rstr));
251 32 return rstr;
252 }
253 }
254  
255 144 static int string_append(char* buf, int pos, int max, const char* str) {
256 int k;
257 316 ▶ for (k = 0; str[k] != '\0' && pos < max; ++k) {
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; str[k] != '\0' && pos < max; ++k) { 172 144 316
for (k = 0; str[k] != '\0' && pos < max; ++k) { 172 0 172
258 172 buf[pos++] = str[k];
259 }
260 144 return pos;
261 }
262  
263 131 static int string_cleanup(const char* str, char* buf, int len, const char* newl) {
264 131 int pos = 0;
265 131 int last_nonblank = -1;
266 131 int saw_newline = 0;
267 int j;
268 923 ▶ for (j = 0; str[j] != '\0'; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; str[j] != '\0'; ++j) { 792 131 923
269 792 char c = str[j];
270 792 int emit_cr = 0;
271 792 ▶ if (c == '\r' && str[j + 1] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\r' && str[j + 1] == '\n') { 10 782 792
if (c == '\r' && str[j + 1] == '\n') { 10 0 10
272 10 ▶ if (strchr(newl, '\r') == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (strchr(newl, '\r') == NULL) { 8 2 10
273 8 emit_cr = 1;
274 }
275 10 ++j;
276 10 c = '\n';
277 }
278 792 ▶ if (pos >= len) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= len) { 0 792 792
279 0 break;
280 }
281 792 ▶ if (isspace((unsigned char)c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isspace((unsigned char)c)) { 44 748 792
282 44 ▶ if (saw_newline) {
Branch: 1 branch
BranchTrueFalseTotal
if (saw_newline) { 29 15 44
283 /* ignore */
284 } else {
285 29 ▶ if (c == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\n') { 15 14 29
286 15 ▶ if (emit_cr && pos < len) {
Branch: 2 branches
BranchTrueFalseTotal
if (emit_cr && pos < len) { 4 11 15
if (emit_cr && pos < len) { 4 0 4
287 4 buf[pos++] = '\r';
288 }
289 15 pos = string_append(buf, pos, len, newl);
290 15 saw_newline = 1;
291 15 last_nonblank = pos-1;
292 } else {
293 14 buf[pos++] = c;
294 }
295 }
296 } else {
297 748 ▶ if (saw_newline) {
Branch: 1 branch
BranchTrueFalseTotal
if (saw_newline) { 13 735 748
298 13 buf[pos++] = ' ';
299 }
300 748 buf[pos++] = c;
301 748 last_nonblank = pos-1;
302 748 saw_newline = 0;
303 }
304 }
305  
306 131 ▶ if (! saw_newline) {
Branch: 1 branch
BranchTrueFalseTotal
if (! saw_newline) { 129 2 131
307 129 pos = string_append(buf, last_nonblank+1, len, newl);
308 129 last_nonblank = pos-1;
309 }
310 131 ▶ if (last_nonblank + 1 < len) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_nonblank + 1 < len) { 131 0 131
311 131 buf[++last_nonblank] = '\0';
312 }
313 131 return last_nonblank;
314  
315 /*
316 * This is the original code in Perl, for reference.
317  
318 sub _process_newline {
319 local $_ = shift;
320 my $endl = shift;
321 # must handle header values with embedded newlines with care
322 s/\s+$//; # trailing newlines and space must go
323 s/\n(\x0d?\n)+/\n/g; # no empty lines
324 s/\n([^\040\t])/\n $1/g; # intial space for continuation
325 s/\n/$endl/g; # substitute with requested line ending
326 $_;
327  
328 */
329 }
« XSHeaders.xs ↑ Summary