util.c

filter: Statementstmt 98.9% 197 / 199 Branchbran 89.8% 169 / 188 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.5% 366 / 387 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« tokenpos.h ↑ Summary blib/lib/HTML/Filter.pm »
1 /*
2 * Copyright 1999-2009, Gisle Aas.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the same terms as Perl itself.
6 */
7  
8 #ifndef EXTERN
9 #define EXTERN extern
10 #endif
11  
12  
13 EXTERN SV*
14 31268 sv_lower(pTHX_ SV* sv)
15 {
16 STRLEN len;
17 31268 char *s = SvPV_force(sv, len);
18 760073 ▶ for (; len--; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (; len--; s++) 728805 31268 760073
19 728805 ▶ *s = toLOWER(*s);
Branch: 1 branch
BranchTrueFalseTotal
*s = toLOWER(*s); 74311 654494 728805
20 31268 return sv;
21 }
22  
23 EXTERN int
24 270 strnEQx(const char* s1, const char* s2, STRLEN n, int ignore_case)
25 {
26 2149 ▶ while (n--) {
Branch: 1 branch
BranchTrueFalseTotal
while (n--) { 1880 269 2149
27 1880 ▶ if (ignore_case) {
Branch: 1 branch
BranchTrueFalseTotal
if (ignore_case) { 1874 6 1880
28 1874 ▶ if (toLOWER(*s1) != toLOWER(*s2))
Branch: 3 branches
BranchTrueFalseTotal
if (toLOWER(*s1) != toLOWER(*s2)) 1868 6 1874
if (toLOWER(*s1) != toLOWER(*s2)) 1874 0 1874
if (toLOWER(*s1) != toLOWER(*s2)) 1 1873 1874
29 1 return 0;
30 }
31 else {
32 6 ▶ if (*s1 != *s2)
Branch: 1 branch
BranchTrueFalseTotal
if (*s1 != *s2) 0 6 6
33 0 return 0;
34 }
35 1879 s1++;
36 1879 s2++;
37 }
38 269 return 1;
39 }
40  
41 static void
42 37 grow_gap(pTHX_ SV* sv, STRLEN grow, char** t, char** s, char** e)
43 {
44 /*
45 SvPVX ---> AAAAAA...BBBBBB
46 ^ ^ ^
47 t s e
48 */
49 37 STRLEN t_offset = *t - SvPVX(sv);
50 37 STRLEN s_offset = *s - SvPVX(sv);
51 37 STRLEN e_offset = *e - SvPVX(sv);
52  
53 37 ▶ SvGROW(sv, e_offset + grow + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(sv, e_offset + grow + 1); 37 0 37
SvGROW(sv, e_offset + grow + 1); 6 31 37
54  
55 37 *t = SvPVX(sv) + t_offset;
56 37 *s = SvPVX(sv) + s_offset;
57 37 *e = SvPVX(sv) + e_offset;
58  
59 37 Move(*s, *s+grow, *e - *s, char);
60 37 *s += grow;
61 37 *e += grow;
62 37 }
63  
64 static void
65 410 save_original(pTHX_ SV* sv, STRLEN len, char** orig)
66 {
67 410 ▶ if (*orig)
Branch: 1 branch
BranchTrueFalseTotal
if (*orig) 323 87 410
68 323 return;
69 87 ▶ Newx(*orig, len ? len : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(*orig, len ? len : 1, char); 87 0 87
70 87 SAVEFREEPV(*orig);
71 87 Copy(SvPVX(sv), *orig, len, char);
72 }
73  
74 EXTERN SV*
75 3713 decode_entities(pTHX_ SV* sv, HV* entity2char, bool expand_prefix)
76 {
77 STRLEN len;
78 3713 char *s = SvPV_force(sv, len);
79 3713 char *t = s;
80 3713 char *end = s + len;
81 char *ent_start;
82  
83 char *repl;
84 STRLEN repl_len;
85 3713 char *orig = 0;
86 3713 int orig_utf8 = SvUTF8(sv);
87 char buf[UTF8_MAXLEN];
88 int repl_utf8;
89 3713 int high_surrogate = 0;
90  
91 #if defined(__GNUC__)
92 /* gcc -Wall reports this variable as possibly used uninitialized */
93 3713 repl_utf8 = 0;
94 #endif
95  
96 /* Looking up an entity value can run Perl code, which must not be
97 * able to free sv while we hold pointers into it. */
98 3713 ENTER;
99 3713 ▶ SvREFCNT_inc_simple_void(sv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(sv); 3713 0 3713
100 3713 SAVEFREESV(sv);
101  
102 68288 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 64580 3708 68288
103 assert(t <= s);
104  
105 64580 ▶ if ((*t++ = *s++) != '&')
Branch: 1 branch
BranchTrueFalseTotal
if ((*t++ = *s++) != '&') 63895 685 64580
106 63895 continue;
107  
108 685 ent_start = s;
109 685 repl = 0;
110  
111 795 ▶ if (s < end && *s == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (s < end && *s == '#') { 677 8 685
if (s < end && *s == '#') { 110 567 677
112 110 UV num = 0;
113 110 int ok = 0;
114 110 s++;
115 110 ▶ if (s < end && (*s == 'x' || *s == 'X')) {
Branch: 3 branches
BranchTrueFalseTotal
if (s < end && (*s == 'x' || *s == 'X')) { 110 0 110
if (s < end && (*s == 'x' || *s == 'X')) { 72 38 110
if (s < end && (*s == 'x' || *s == 'X')) { 3 69 72
116 41 s++;
117 174 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 162 12 174
118 162 char *tmp = strchr(PL_hexdigit, *s);
119 162 ▶ if (!tmp)
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) 25 137 162
120 25 break;
121 137 num = num << 4 | ((tmp - PL_hexdigit) & 15);
122 137 ▶ if (num > 0x10FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (num > 0x10FFFF) { 4 133 137
123 /* overflow */
124 4 ok = 0;
125 4 break;
126 }
127 133 s++;
128 133 ok = 1;
129 }
130 }
131 else {
132 281 ▶ while (s < end && isDIGIT(*s)) {
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isDIGIT(*s)) { 277 4 281
while (s < end && isDIGIT(*s)) { 214 63 277
133 214 num = num * 10 + (*s - '0');
134 214 ▶ if (num > 0x10FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (num > 0x10FFFF) { 2 212 214
135 /* overflow */
136 2 ok = 0;
137 2 break;
138 }
139 212 s++;
140 212 ok = 1;
141 }
142 }
143 110 ▶ if (num && ok) {
Branch: 2 branches
BranchTrueFalseTotal
if (num && ok) { 102 8 110
if (num && ok) { 96 6 102
144 96 ▶ if (!SvUTF8(sv) && num <= 255) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvUTF8(sv) && num <= 255) { 56 40 96
if (!SvUTF8(sv) && num <= 255) { 41 15 56
145 41 buf[0] = (char) num;
146 41 repl = buf;
147 41 repl_len = 1;
148 41 repl_utf8 = 0;
149 }
150 55 ▶ else if (num == 0xFFFE || num == 0xFFFF) {
Branch: 2 branches
BranchTrueFalseTotal
else if (num == 0xFFFE || num == 0xFFFF) { 55 0 55
else if (num == 0xFFFE || num == 0xFFFF) { 54 1 55
151 /* illegal */
152 }
153 else {
154 char *tmp;
155 54 ▶ if ((num & 0xFFFFFC00) == 0xDC00) { /* low-surrogate */
Branch: 1 branch
BranchTrueFalseTotal
if ((num & 0xFFFFFC00) == 0xDC00) { /* low-surrogate */ 1 53 54
156 1 ▶ if (high_surrogate != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (high_surrogate != 0) { 1 0 1
157 1 t -= 3; /* Back up past 0xFFFD */
158 1 num = ((high_surrogate - 0xD800) << 10) +
159 (num - 0xDC00) + 0x10000;
160 1 high_surrogate = 0;
161 } else {
162 0 num = 0xFFFD;
163 }
164 }
165 53 ▶ else if ((num & 0xFFFFFC00) == 0xD800) { /* high-surrogate */
Branch: 1 branch
BranchTrueFalseTotal
else if ((num & 0xFFFFFC00) == 0xD800) { /* high-surrogate */ 2 51 53
166 2 high_surrogate = num;
167 2 num = 0xFFFD;
168 }
169 else {
170 51 high_surrogate = 0;
171 /* otherwise invalid? */
172 51 ▶ if ((num >= 0xFDD0 && num <= 0xFDEF) ||
Branch: 2 branches
BranchTrueFalseTotal
if ((num >= 0xFDD0 && num <= 0xFDEF) || 8 43 51
if ((num >= 0xFDD0 && num <= 0xFDEF) || 4 4 8
173 47 ▶ ((num & 0xFFFE) == 0xFFFE) ||
Branch: 2 branches
BranchTrueFalseTotal
((num & 0xFFFE) == 0xFFFE) || 46 1 47
((num & 0xFFFE) == 0xFFFE) || 0 46 46
174 num > 0x10FFFF)
175 {
176 5 num = 0xFFFD;
177 }
178 }
179  
180 54 tmp = (char*)uvchr_to_utf8((U8*)buf, num);
181 54 repl = buf;
182 54 repl_len = tmp - buf;
183 54 repl_utf8 = 1;
184 }
185 }
186 }
187 else {
188 575 char *ent_name = s;
189 2178 ▶ while (s < end && isALNUM(*s))
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isALNUM(*s)) 2150 28 2178
while (s < end && isALNUM(*s)) 1603 547 2150
190 1603 s++;
191 575 ▶ if (ent_name != s && entity2char) {
Branch: 2 branches
BranchTrueFalseTotal
if (ent_name != s && entity2char) { 386 189 575
if (ent_name != s && entity2char) { 385 1 386
192 SV** svp;
193 385 char *pvx = SvPVX(sv);
194 385 STRLEN cur = SvCUR(sv);
195 385 STRLEN alloc = SvLEN(sv);
196 385 U32 utf8 = SvUTF8(sv);
197 /* SvPV can call magic or overload. A callback may change entity2char
198 * and invalidate svp, so retain the value until its text is copied.
199 * A plain undefined value is not a valid replacement. */
200 385 ▶ if ( (svp = hv_fetch(entity2char, ent_name, s - ent_name, 0)) ||
Branch: 1 branch
BranchTrueFalseTotal
if ( (svp = hv_fetch(entity2char, ent_name, s - ent_name, 0)) || 90 295 385
201 90 ▶ (s < end && *s == ';' && (svp = hv_fetch(entity2char, ent_name, s - ent_name + 1, 0)))
Branch: 3 branches
BranchTrueFalseTotal
(s < end && *s == ';' && (svp = hv_fetch(entity2char, ent_name, s - ent_name + 1, 0))) 82 8 90
(s < end && *s == ';' && (svp = hv_fetch(entity2char, ent_name, s - ent_name + 1, 0))) 19 63 82
(s < end && *s == ';' && (svp = hv_fetch(entity2char, ent_name, s - ent_name + 1, 0))) 14 5 19
202 )
203 {
204 309 ▶ if ((SV*)*svp == sv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((SV*)*svp == sv) { 5 304 309
205 5 save_original(aTHX_ sv, len, &orig);
206 5 repl = orig;
207 5 repl_len = len;
208 5 repl_utf8 = orig_utf8;
209 } else {
210 304 SV *repl_sv = *svp;
211 /* Check magic first: it may make the scalar defined or undef. */
212 304 ▶ bool may_call_perl = SvGAMAGIC(repl_sv);
Branch: 4 branches
BranchTrueFalseTotal
bool may_call_perl = SvGAMAGIC(repl_sv); 302 2 304
bool may_call_perl = SvGAMAGIC(repl_sv); 3 299 302
bool may_call_perl = SvGAMAGIC(repl_sv); 3 0 3
bool may_call_perl = SvGAMAGIC(repl_sv); 3 0 3
213 304 ▶ if (!may_call_perl && !SvOK(repl_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!may_call_perl && !SvOK(repl_sv)) 299 5 304
if (!may_call_perl && !SvOK(repl_sv)) 1 298 299
214 1 croak("Entity value is undefined");
215 303 ▶ if (may_call_perl) {
Branch: 1 branch
BranchTrueFalseTotal
if (may_call_perl) { 5 298 303
216 5 ▶ SvREFCNT_inc_simple_void(repl_sv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(repl_sv); 5 0 5
217 5 SAVEFREESV(repl_sv);
218 }
219 303 repl = SvPV(repl_sv, repl_len);
220 303 repl_utf8 = SvUTF8(repl_sv);
221 }
222 }
223 76 ▶ else if (expand_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
else if (expand_prefix) { 7 69 76
224 7 char *ss = s - 1;
225 25 ▶ while (ss > ent_name) {
Branch: 1 branch
BranchTrueFalseTotal
while (ss > ent_name) { 23 2 25
226 23 svp = hv_fetch(entity2char, ent_name, ss - ent_name, 0);
227 23 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 5 18 23
228 5 ▶ if ((SV*)*svp == sv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((SV*)*svp == sv) { 1 4 5
229 1 save_original(aTHX_ sv, len, &orig);
230 1 repl = orig;
231 1 repl_len = len;
232 1 repl_utf8 = orig_utf8;
233 } else {
234 4 SV *repl_sv = *svp;
235 /* Check magic first: it may make the scalar defined or undef. */
236 4 ▶ bool may_call_perl = SvGAMAGIC(repl_sv);
Branch: 4 branches
BranchTrueFalseTotal
bool may_call_perl = SvGAMAGIC(repl_sv); 4 0 4
bool may_call_perl = SvGAMAGIC(repl_sv); 1 3 4
bool may_call_perl = SvGAMAGIC(repl_sv); 1 0 1
bool may_call_perl = SvGAMAGIC(repl_sv); 1 0 1
237 4 ▶ if (!may_call_perl && !SvOK(repl_sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!may_call_perl && !SvOK(repl_sv)) 3 1 4
if (!may_call_perl && !SvOK(repl_sv)) 1 2 3
238 1 croak("Entity value is undefined");
239 3 ▶ if (may_call_perl) {
Branch: 1 branch
BranchTrueFalseTotal
if (may_call_perl) { 1 2 3
240 1 ▶ SvREFCNT_inc_simple_void(repl_sv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(repl_sv); 1 0 1
241 1 SAVEFREESV(repl_sv);
242 }
243 3 repl = SvPV(repl_sv, repl_len);
244 3 repl_utf8 = SvUTF8(repl_sv);
245 }
246 4 s = ss;
247 4 break;
248 }
249 18 ss--;
250 }
251 }
252 /* The lookups may have run Perl code, and we still hold
253 * pointers into the buffer it could have changed. */
254 383 ▶ if (SvPVX(sv) != pvx || SvCUR(sv) != cur ||
Branch: 2 branches
BranchTrueFalseTotal
if (SvPVX(sv) != pvx || SvCUR(sv) != cur || 380 3 383
if (SvPVX(sv) != pvx || SvCUR(sv) != cur || 380 0 380
255 380 ▶ SvLEN(sv) != alloc || SvUTF8(sv) != utf8)
Branch: 2 branches
BranchTrueFalseTotal
SvLEN(sv) != alloc || SvUTF8(sv) != utf8) 380 0 380
SvLEN(sv) != alloc || SvUTF8(sv) != utf8) 0 380 380
256 3 croak("String being decoded was modified"
257 " while fetching an entity value");
258 }
259 570 high_surrogate = 0;
260 }
261  
262 680 ▶ if (repl) {
Branch: 1 branch
BranchTrueFalseTotal
if (repl) { 404 276 680
263 404 char *repl_allocated = 0;
264 404 save_original(aTHX_ sv, len, &orig);
265 404 ▶ if (s < end && *s == ';')
Branch: 2 branches
BranchTrueFalseTotal
if (s < end && *s == ';') 379 25 404
if (s < end && *s == ';') 257 122 379
266 257 s++;
267 404 t--; /* '&' already copied, undo it */
268  
269 404 ▶ if (s >= end || *s != '&') {
Branch: 2 branches
BranchTrueFalseTotal
if (s >= end || *s != '&') { 357 47 404
if (s >= end || *s != '&') { 65 292 357
270 112 high_surrogate = 0;
271 }
272  
273 404 ▶ if (!SvUTF8(sv) && repl_utf8) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvUTF8(sv) && repl_utf8) { 142 262 404
if (!SvUTF8(sv) && repl_utf8) { 21 121 142
274 /* need to upgrade sv before we continue */
275 21 STRLEN before_gap_len = t - SvPVX(sv);
276 21 char *before_gap = (char*)bytes_to_utf8((U8*)SvPVX(sv), &before_gap_len);
277 21 STRLEN after_gap_len = end - s;
278 21 char *after_gap = (char*)bytes_to_utf8((U8*)s, &after_gap_len);
279  
280 21 sv_setpvn(sv, before_gap, before_gap_len);
281 21 sv_catpvn(sv, after_gap, after_gap_len);
282 21 SvUTF8_on(sv);
283  
284 21 Safefree(before_gap);
285 21 Safefree(after_gap);
286  
287 21 s = t = SvPVX(sv) + before_gap_len;
288 21 end = SvPVX(sv) + before_gap_len + after_gap_len;
289 }
290 383 ▶ else if (SvUTF8(sv) && !repl_utf8) {
Branch: 2 branches
BranchTrueFalseTotal
else if (SvUTF8(sv) && !repl_utf8) { 262 121 383
else if (SvUTF8(sv) && !repl_utf8) { 213 49 262
291 213 repl = (char*)bytes_to_utf8((U8*)repl, &repl_len);
292 213 repl_allocated = repl;
293 }
294  
295 404 ▶ if (t + repl_len > s) {
Branch: 1 branch
BranchTrueFalseTotal
if (t + repl_len > s) { 37 367 404
296 /* need to grow the string */
297 37 grow_gap(aTHX_ sv, repl_len - (s - t), &t, &s, &end);
298 }
299  
300 /* copy replacement string into string */
301 9493 ▶ while (repl_len--)
Branch: 1 branch
BranchTrueFalseTotal
while (repl_len--) 9089 404 9493
302 9089 *t++ = *repl++;
303  
304 404 ▶ if (repl_allocated)
Branch: 1 branch
BranchTrueFalseTotal
if (repl_allocated) 213 191 404
305 213 Safefree(repl_allocated);
306 }
307 else {
308 463 ▶ while (ent_start < s)
Branch: 1 branch
BranchTrueFalseTotal
while (ent_start < s) 187 276 463
309 187 *t++ = *ent_start++;
310 }
311 }
312  
313 3708 *t = '\0';
314 3708 SvCUR_set(sv, t - SvPVX(sv));
315  
316 3708 LEAVE;
317  
318 3708 return sv;
319 }
320  
321 static bool
322 13 has_hibit(char *s, char *e)
323 {
324 29 ▶ while (s < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < e) { 25 4 29
325 25 U8 ch = *s++;
326 25 ▶ if (!UTF8_IS_INVARIANT(ch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!UTF8_IS_INVARIANT(ch)) { 9 16 25
327 9 return 1;
328 }
329 }
330 4 return 0;
331 }
332  
333  
334 EXTERN bool
335 13 probably_utf8_chunk(pTHX_ char *s, STRLEN len)
336 {
337 13 char *e = s + len;
338 STRLEN clen;
339  
340 /* ignore partial utf8 char at end of buffer */
341 18 ▶ while (s < e && UTF8_IS_CONTINUATION((U8)*(e - 1)))
Branch: 2 branches
BranchTrueFalseTotal
while (s < e && UTF8_IS_CONTINUATION((U8)*(e - 1))) 17 1 18
while (s < e && UTF8_IS_CONTINUATION((U8)*(e - 1))) 5 12 17
342 5 e--;
343 13 ▶ if (s < e && UTF8_IS_START((U8)*(e - 1)))
Branch: 2 branches
BranchTrueFalseTotal
if (s < e && UTF8_IS_START((U8)*(e - 1))) 12 1 13
if (s < e && UTF8_IS_START((U8)*(e - 1))) 6 6 12
344 6 e--;
345 13 clen = len - (e - s);
346 13 ▶ if (clen && UTF8SKIP(e) == clen) {
Branch: 2 branches
BranchTrueFalseTotal
if (clen && UTF8SKIP(e) == clen) { 6 7 13
if (clen && UTF8SKIP(e) == clen) { 2 4 6
347 /* all promised continuation bytes are present */
348 2 e = s + len;
349 }
350  
351 13 ▶ if (!has_hibit(s, e))
Branch: 1 branch
BranchTrueFalseTotal
if (!has_hibit(s, e)) 4 9 13
352 4 return 0;
353  
354 9 return is_utf8_string((U8*)s, e - s);
355 }
« tokenpos.h ↑ Summary blib/lib/HTML/Filter.pm »