util.c

filter: Statementstmt 98.9% 183 / 185 Branchbran 92.3% 144 / 156 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 95.8% 327 / 341 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 30680 sv_lower(pTHX_ SV* sv)
15 {
16 STRLEN len;
17 30680 char *s = SvPV_force(sv, len);
18 742365 ▶ for (; len--; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (; len--; s++) 711685 30680 742365
19 711685 ▶ *s = toLOWER(*s);
Branch: 1 branch
BranchTrueFalseTotal
*s = toLOWER(*s); 73490 638195 711685
20 30680 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 35 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 35 STRLEN t_offset = *t - SvPVX(sv);
50 35 STRLEN s_offset = *s - SvPVX(sv);
51 35 STRLEN e_offset = *e - SvPVX(sv);
52  
53 35 ▶ SvGROW(sv, e_offset + grow + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(sv, e_offset + grow + 1); 35 0 35
SvGROW(sv, e_offset + grow + 1); 6 29 35
54  
55 35 *t = SvPVX(sv) + t_offset;
56 35 *s = SvPVX(sv) + s_offset;
57 35 *e = SvPVX(sv) + e_offset;
58  
59 35 Move(*s, *s+grow, *e - *s, char);
60 35 *s += grow;
61 35 *e += grow;
62 35 }
63  
64 static void
65 408 save_original(pTHX_ SV* sv, STRLEN len, char** orig)
66 {
67 408 ▶ if (*orig)
Branch: 1 branch
BranchTrueFalseTotal
if (*orig) 323 85 408
68 323 return;
69 85 ▶ Newx(*orig, len ? len : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(*orig, len ? len : 1, char); 85 0 85
70 85 SAVEFREEPV(*orig);
71 85 Copy(SvPVX(sv), *orig, len, char);
72 }
73  
74 EXTERN SV*
75 3570 decode_entities(pTHX_ SV* sv, HV* entity2char, bool expand_prefix)
76 {
77 STRLEN len;
78 3570 char *s = SvPV_force(sv, len);
79 3570 char *t = s;
80 3570 char *end = s + len;
81 char *ent_start;
82  
83 char *repl;
84 STRLEN repl_len;
85 3570 char *orig = 0;
86 3570 int orig_utf8 = SvUTF8(sv);
87 char buf[UTF8_MAXLEN];
88 int repl_utf8;
89 3570 int high_surrogate = 0;
90  
91 #if defined(__GNUC__)
92 /* gcc -Wall reports this variable as possibly used uninitialized */
93 3570 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 3570 ENTER;
99 3570 ▶ SvREFCNT_inc_simple_void(sv);
Branch: 1 branch
BranchTrueFalseTotal
SvREFCNT_inc_simple_void(sv); 3570 0 3570
100 3570 SAVEFREESV(sv);
101  
102 63221 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 59654 3567 63221
103 assert(t <= s);
104  
105 59654 ▶ if ((*t++ = *s++) != '&')
Branch: 1 branch
BranchTrueFalseTotal
if ((*t++ = *s++) != '&') 59034 620 59654
106 59034 continue;
107  
108 620 ent_start = s;
109 620 repl = 0;
110  
111 730 ▶ if (s < end && *s == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (s < end && *s == '#') { 616 4 620
if (s < end && *s == '#') { 110 506 616
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 510 char *ent_name = s;
189 2103 ▶ while (s < end && isALNUM(*s))
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isALNUM(*s)) 2078 25 2103
while (s < end && isALNUM(*s)) 1593 485 2078
190 1593 s++;
191 510 ▶ if (ent_name != s && entity2char) {
Branch: 2 branches
BranchTrueFalseTotal
if (ent_name != s && entity2char) { 382 128 510
if (ent_name != s && entity2char) { 381 1 382
192 SV** svp;
193 381 char *pvx = SvPVX(sv);
194 381 STRLEN cur = SvCUR(sv);
195 381 STRLEN alloc = SvLEN(sv);
196 381 U32 utf8 = SvUTF8(sv);
197 381 ▶ 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)) || 88 293 381
198 88 ▶ (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))) 79 9 88
(s < end && *s == ';' && (svp = hv_fetch(entity2char, ent_name, s - ent_name + 1, 0))) 19 60 79
(s < end && *s == ';' && (svp = hv_fetch(entity2char, ent_name, s - ent_name + 1, 0))) 14 5 19
199 )
200 {
201 307 ▶ if ((SV*)*svp == sv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((SV*)*svp == sv) { 5 302 307
202 5 save_original(aTHX_ sv, len, &orig);
203 5 repl = orig;
204 5 repl_len = len;
205 5 repl_utf8 = orig_utf8;
206 } else {
207 302 repl = SvPV(*svp, repl_len);
208 302 repl_utf8 = SvUTF8(*svp);
209 }
210 }
211 74 ▶ else if (expand_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
else if (expand_prefix) { 5 69 74
212 5 char *ss = s - 1;
213 19 ▶ while (ss > ent_name) {
Branch: 1 branch
BranchTrueFalseTotal
while (ss > ent_name) { 17 2 19
214 17 svp = hv_fetch(entity2char, ent_name, ss - ent_name, 0);
215 17 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 3 14 17
216 3 ▶ if ((SV*)*svp == sv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((SV*)*svp == sv) { 1 2 3
217 1 save_original(aTHX_ sv, len, &orig);
218 1 repl = orig;
219 1 repl_len = len;
220 1 repl_utf8 = orig_utf8;
221 } else {
222 2 repl = SvPV(*svp, repl_len);
223 2 repl_utf8 = SvUTF8(*svp);
224 }
225 3 s = ss;
226 3 break;
227 }
228 14 ss--;
229 }
230 }
231 /* The lookups may have run Perl code, and we still hold
232 * pointers into the buffer it could have changed. */
233 381 ▶ if (SvPVX(sv) != pvx || SvCUR(sv) != cur ||
Branch: 2 branches
BranchTrueFalseTotal
if (SvPVX(sv) != pvx || SvCUR(sv) != cur || 378 3 381
if (SvPVX(sv) != pvx || SvCUR(sv) != cur || 378 0 378
234 378 ▶ SvLEN(sv) != alloc || SvUTF8(sv) != utf8)
Branch: 2 branches
BranchTrueFalseTotal
SvLEN(sv) != alloc || SvUTF8(sv) != utf8) 378 0 378
SvLEN(sv) != alloc || SvUTF8(sv) != utf8) 0 378 378
235 3 croak("String being decoded was modified"
236 " while fetching an entity value");
237 }
238 507 high_surrogate = 0;
239 }
240  
241 617 ▶ if (repl) {
Branch: 1 branch
BranchTrueFalseTotal
if (repl) { 402 215 617
242 402 char *repl_allocated = 0;
243 402 save_original(aTHX_ sv, len, &orig);
244 402 ▶ if (s < end && *s == ';')
Branch: 2 branches
BranchTrueFalseTotal
if (s < end && *s == ';') 377 25 402
if (s < end && *s == ';') 256 121 377
245 256 s++;
246 402 t--; /* '&' already copied, undo it */
247  
248 402 ▶ if (s >= end || *s != '&') {
Branch: 2 branches
BranchTrueFalseTotal
if (s >= end || *s != '&') { 356 46 402
if (s >= end || *s != '&') { 64 292 356
249 110 high_surrogate = 0;
250 }
251  
252 402 ▶ if (!SvUTF8(sv) && repl_utf8) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvUTF8(sv) && repl_utf8) { 140 262 402
if (!SvUTF8(sv) && repl_utf8) { 19 121 140
253 /* need to upgrade sv before we continue */
254 19 STRLEN before_gap_len = t - SvPVX(sv);
255 19 char *before_gap = (char*)bytes_to_utf8((U8*)SvPVX(sv), &before_gap_len);
256 19 STRLEN after_gap_len = end - s;
257 19 char *after_gap = (char*)bytes_to_utf8((U8*)s, &after_gap_len);
258  
259 19 sv_setpvn(sv, before_gap, before_gap_len);
260 19 sv_catpvn(sv, after_gap, after_gap_len);
261 19 SvUTF8_on(sv);
262  
263 19 Safefree(before_gap);
264 19 Safefree(after_gap);
265  
266 19 s = t = SvPVX(sv) + before_gap_len;
267 19 end = SvPVX(sv) + before_gap_len + after_gap_len;
268 }
269 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
270 213 repl = (char*)bytes_to_utf8((U8*)repl, &repl_len);
271 213 repl_allocated = repl;
272 }
273  
274 402 ▶ if (t + repl_len > s) {
Branch: 1 branch
BranchTrueFalseTotal
if (t + repl_len > s) { 35 367 402
275 /* need to grow the string */
276 35 grow_gap(aTHX_ sv, repl_len - (s - t), &t, &s, &end);
277 }
278  
279 /* copy replacement string into string */
280 9487 ▶ while (repl_len--)
Branch: 1 branch
BranchTrueFalseTotal
while (repl_len--) 9085 402 9487
281 9085 *t++ = *repl++;
282  
283 402 ▶ if (repl_allocated)
Branch: 1 branch
BranchTrueFalseTotal
if (repl_allocated) 213 189 402
284 213 Safefree(repl_allocated);
285 }
286 else {
287 410 ▶ while (ent_start < s)
Branch: 1 branch
BranchTrueFalseTotal
while (ent_start < s) 195 215 410
288 195 *t++ = *ent_start++;
289 }
290 }
291  
292 3567 *t = '\0';
293 3567 SvCUR_set(sv, t - SvPVX(sv));
294  
295 3567 LEAVE;
296  
297 3567 return sv;
298 }
299  
300 static bool
301 13 has_hibit(char *s, char *e)
302 {
303 29 ▶ while (s < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < e) { 25 4 29
304 25 U8 ch = *s++;
305 25 ▶ if (!UTF8_IS_INVARIANT(ch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!UTF8_IS_INVARIANT(ch)) { 9 16 25
306 9 return 1;
307 }
308 }
309 4 return 0;
310 }
311  
312  
313 EXTERN bool
314 13 probably_utf8_chunk(pTHX_ char *s, STRLEN len)
315 {
316 13 char *e = s + len;
317 STRLEN clen;
318  
319 /* ignore partial utf8 char at end of buffer */
320 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
321 5 e--;
322 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
323 6 e--;
324 13 clen = len - (e - s);
325 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
326 /* all promised continuation bytes are present */
327 2 e = s + len;
328 }
329  
330 13 ▶ if (!has_hibit(s, e))
Branch: 1 branch
BranchTrueFalseTotal
if (!has_hibit(s, e)) 4 9 13
331 4 return 0;
332  
333 9 return is_utf8_string((U8*)s, e - s);
334 }
« tokenpos.h ↑ Summary blib/lib/HTML/Filter.pm »