util.c

filter: Statementstmt 97.0% 162 / 167 Branchbran 92.2% 131 / 142 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.8% 293 / 309 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 28034 sv_lower(pTHX_ SV* sv)
15 {
16 STRLEN len;
17 28034 char *s = SvPV_force(sv, len);
18 655899 ▶ for (; len--; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (; len--; s++) 627865 28034 655899
19 627865 ▶ *s = toLOWER(*s);
Branch: 1 branch
BranchTrueFalseTotal
*s = toLOWER(*s); 63867 563998 627865
20 28034 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 27 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 27 STRLEN t_offset = *t - SvPVX(sv);
50 27 STRLEN s_offset = *s - SvPVX(sv);
51 27 STRLEN e_offset = *e - SvPVX(sv);
52  
53 27 ▶ SvGROW(sv, e_offset + grow + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(sv, e_offset + grow + 1); 27 0 27
SvGROW(sv, e_offset + grow + 1); 4 23 27
54  
55 27 *t = SvPVX(sv) + t_offset;
56 27 *s = SvPVX(sv) + s_offset;
57 27 *e = SvPVX(sv) + e_offset;
58  
59 27 Move(*s, *s+grow, *e - *s, char);
60 27 *s += grow;
61 27 *e += grow;
62 27 }
63  
64 EXTERN SV*
65 3406 decode_entities(pTHX_ SV* sv, HV* entity2char, bool expand_prefix)
66 {
67 STRLEN len;
68 3406 char *s = SvPV_force(sv, len);
69 3406 char *t = s;
70 3406 char *end = s + len;
71 char *ent_start;
72  
73 char *repl;
74 STRLEN repl_len;
75 3406 char *repl_allocated = 0;
76 char buf[UTF8_MAXLEN];
77 int repl_utf8;
78 3406 int high_surrogate = 0;
79  
80 #if defined(__GNUC__)
81 /* gcc -Wall reports this variable as possibly used uninitialized */
82 3406 repl_utf8 = 0;
83 #endif
84  
85 59014 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 55608 3406 59014
86 assert(t <= s);
87  
88 55608 ▶ if ((*t++ = *s++) != '&')
Branch: 1 branch
BranchTrueFalseTotal
if ((*t++ = *s++) != '&') 54996 612 55608
89 54996 continue;
90  
91 612 ent_start = s;
92 612 repl = 0;
93 612 repl_allocated = 0;
94  
95 722 ▶ if (s < end && *s == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (s < end && *s == '#') { 604 8 612
if (s < end && *s == '#') { 110 494 604
96 110 UV num = 0;
97 110 int ok = 0;
98 110 s++;
99 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
100 41 s++;
101 174 ▶ while (s < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < end) { 162 12 174
102 162 char *tmp = strchr(PL_hexdigit, *s);
103 162 ▶ if (!tmp)
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) 25 137 162
104 25 break;
105 137 num = num << 4 | ((tmp - PL_hexdigit) & 15);
106 137 ▶ if (num > 0x10FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (num > 0x10FFFF) { 4 133 137
107 /* overflow */
108 4 ok = 0;
109 4 break;
110 }
111 133 s++;
112 133 ok = 1;
113 }
114 }
115 else {
116 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
117 214 num = num * 10 + (*s - '0');
118 214 ▶ if (num > 0x10FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (num > 0x10FFFF) { 2 212 214
119 /* overflow */
120 2 ok = 0;
121 2 break;
122 }
123 212 s++;
124 212 ok = 1;
125 }
126 }
127 110 ▶ if (num && ok) {
Branch: 2 branches
BranchTrueFalseTotal
if (num && ok) { 102 8 110
if (num && ok) { 96 6 102
128 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
129 41 buf[0] = (char) num;
130 41 repl = buf;
131 41 repl_len = 1;
132 41 repl_utf8 = 0;
133 }
134 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
135 /* illegal */
136 }
137 else {
138 char *tmp;
139 54 ▶ if ((num & 0xFFFFFC00) == 0xDC00) { /* low-surrogate */
Branch: 1 branch
BranchTrueFalseTotal
if ((num & 0xFFFFFC00) == 0xDC00) { /* low-surrogate */ 1 53 54
140 1 ▶ if (high_surrogate != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (high_surrogate != 0) { 1 0 1
141 1 t -= 3; /* Back up past 0xFFFD */
142 1 num = ((high_surrogate - 0xD800) << 10) +
143 (num - 0xDC00) + 0x10000;
144 1 high_surrogate = 0;
145 } else {
146 0 num = 0xFFFD;
147 }
148 }
149 53 ▶ else if ((num & 0xFFFFFC00) == 0xD800) { /* high-surrogate */
Branch: 1 branch
BranchTrueFalseTotal
else if ((num & 0xFFFFFC00) == 0xD800) { /* high-surrogate */ 2 51 53
150 2 high_surrogate = num;
151 2 num = 0xFFFD;
152 }
153 else {
154 51 high_surrogate = 0;
155 /* otherwise invalid? */
156 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
157 47 ▶ ((num & 0xFFFE) == 0xFFFE) ||
Branch: 2 branches
BranchTrueFalseTotal
((num & 0xFFFE) == 0xFFFE) || 46 1 47
((num & 0xFFFE) == 0xFFFE) || 0 46 46
158 num > 0x10FFFF)
159 {
160 5 num = 0xFFFD;
161 }
162 }
163  
164 54 tmp = (char*)uvchr_to_utf8((U8*)buf, num);
165 54 repl = buf;
166 54 repl_len = tmp - buf;
167 54 repl_utf8 = 1;
168 }
169 }
170 }
171 else {
172 502 char *ent_name = s;
173 2042 ▶ while (s < end && isALNUM(*s))
Branch: 2 branches
BranchTrueFalseTotal
while (s < end && isALNUM(*s)) 2015 27 2042
while (s < end && isALNUM(*s)) 1540 475 2015
174 1540 s++;
175 502 ▶ if (ent_name != s && entity2char) {
Branch: 2 branches
BranchTrueFalseTotal
if (ent_name != s && entity2char) { 362 140 502
if (ent_name != s && entity2char) { 361 1 362
176 SV** svp;
177 361 ▶ 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)) || 80 281 361
178 80 ▶ (*s == ';' && (svp = hv_fetch(entity2char, ent_name, s - ent_name + 1, 0)))
Branch: 2 branches
BranchTrueFalseTotal
(*s == ';' && (svp = hv_fetch(entity2char, ent_name, s - ent_name + 1, 0))) 16 64 80
(*s == ';' && (svp = hv_fetch(entity2char, ent_name, s - ent_name + 1, 0))) 13 3 16
179 )
180 294 {
181 294 char *src = SvPV(*svp, repl_len);
182 294 repl_utf8 = SvUTF8(*svp);
183 294 ▶ if ((SV*)*svp == sv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((SV*)*svp == sv) { 1 293 294
184 /* Self-aliased: hash entry SV == input SV.
185 * grow_gap() may realloc sv's PV later; copy
186 * the entity value into an owned buffer first.
187 * Freed by the repl_allocated cleanup below. */
188 1 ▶ Newx(repl_allocated, repl_len ? repl_len : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(repl_allocated, repl_len ? repl_len : 1, char); 1 0 1
189 1 Copy(src, repl_allocated, repl_len, char);
190 1 repl = repl_allocated;
191 } else {
192 293 repl = src;
193 }
194 }
195 67 ▶ else if (expand_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
else if (expand_prefix) { 4 63 67
196 4 char *ss = s - 1;
197 16 ▶ while (ss > ent_name) {
Branch: 1 branch
BranchTrueFalseTotal
while (ss > ent_name) { 14 2 16
198 14 svp = hv_fetch(entity2char, ent_name, ss - ent_name, 0);
199 14 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 2 12 14
200 2 char *src = SvPV(*svp, repl_len);
201 2 repl_utf8 = SvUTF8(*svp);
202 2 ▶ if ((SV*)*svp == sv) {
Branch: 1 branch
BranchTrueFalseTotal
if ((SV*)*svp == sv) { 0 2 2
203 0 ▶ Newx(repl_allocated, repl_len ? repl_len : 1, char);
Branch: 1 branch
BranchTrueFalseTotal
Newx(repl_allocated, repl_len ? repl_len : 1, char); 0 0 0
204 0 Copy(src, repl_allocated, repl_len, char);
205 0 repl = repl_allocated;
206 } else {
207 2 repl = src;
208 }
209 2 s = ss;
210 2 break;
211 }
212 12 ss--;
213 }
214 }
215 }
216 502 high_surrogate = 0;
217 }
218  
219 612 ▶ if (repl) {
Branch: 1 branch
BranchTrueFalseTotal
if (repl) { 391 221 612
220 /* repl_allocated is now function-scoped; set by the
221 * named-entity self-alias path above or by the UTF8 mismatch
222 * branch below. Same cleanup in either case. */
223 391 ▶ if (s < end && *s == ';')
Branch: 2 branches
BranchTrueFalseTotal
if (s < end && *s == ';') 366 25 391
if (s < end && *s == ';') 246 120 366
224 246 s++;
225 391 t--; /* '&' already copied, undo it */
226  
227 391 ▶ if (*s != '&') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s != '&') { 102 289 391
228 102 high_surrogate = 0;
229 }
230  
231 391 ▶ if (!SvUTF8(sv) && repl_utf8) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvUTF8(sv) && repl_utf8) { 130 261 391
if (!SvUTF8(sv) && repl_utf8) { 18 112 130
232 /* need to upgrade sv before we continue */
233 18 STRLEN before_gap_len = t - SvPVX(sv);
234 18 char *before_gap = (char*)bytes_to_utf8((U8*)SvPVX(sv), &before_gap_len);
235 18 STRLEN after_gap_len = end - s;
236 18 char *after_gap = (char*)bytes_to_utf8((U8*)s, &after_gap_len);
237  
238 18 sv_setpvn(sv, before_gap, before_gap_len);
239 18 sv_catpvn(sv, after_gap, after_gap_len);
240 18 SvUTF8_on(sv);
241  
242 18 Safefree(before_gap);
243 18 Safefree(after_gap);
244  
245 18 s = t = SvPVX(sv) + before_gap_len;
246 18 end = SvPVX(sv) + before_gap_len + after_gap_len;
247 }
248 373 ▶ else if (SvUTF8(sv) && !repl_utf8) {
Branch: 2 branches
BranchTrueFalseTotal
else if (SvUTF8(sv) && !repl_utf8) { 261 112 373
else if (SvUTF8(sv) && !repl_utf8) { 212 49 261
249 212 repl = (char*)bytes_to_utf8((U8*)repl, &repl_len);
250 212 repl_allocated = repl;
251 }
252  
253 391 ▶ if (t + repl_len > s) {
Branch: 1 branch
BranchTrueFalseTotal
if (t + repl_len > s) { 27 364 391
254 /* need to grow the string */
255 27 grow_gap(aTHX_ sv, repl_len - (s - t), &t, &s, &end);
256 }
257  
258 /* copy replacement string into string */
259 9371 ▶ while (repl_len--)
Branch: 1 branch
BranchTrueFalseTotal
while (repl_len--) 8980 391 9371
260 8980 *t++ = *repl++;
261  
262 391 ▶ if (repl_allocated)
Branch: 1 branch
BranchTrueFalseTotal
if (repl_allocated) 213 178 391
263 213 Safefree(repl_allocated);
264 }
265 else {
266 408 ▶ while (ent_start < s)
Branch: 1 branch
BranchTrueFalseTotal
while (ent_start < s) 187 221 408
267 187 *t++ = *ent_start++;
268 }
269 }
270  
271 3406 *t = '\0';
272 3406 SvCUR_set(sv, t - SvPVX(sv));
273  
274 3406 return sv;
275 }
276  
277 static bool
278 13 has_hibit(char *s, char *e)
279 {
280 29 ▶ while (s < e) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < e) { 25 4 29
281 25 U8 ch = *s++;
282 25 ▶ if (!UTF8_IS_INVARIANT(ch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!UTF8_IS_INVARIANT(ch)) { 9 16 25
283 9 return 1;
284 }
285 }
286 4 return 0;
287 }
288  
289  
290 EXTERN bool
291 13 probably_utf8_chunk(pTHX_ char *s, STRLEN len)
292 {
293 13 char *e = s + len;
294 STRLEN clen;
295  
296 /* ignore partial utf8 char at end of buffer */
297 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
298 5 e--;
299 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
300 6 e--;
301 13 clen = len - (e - s);
302 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
303 /* all promised continuation bytes are present */
304 2 e = s + len;
305 }
306  
307 13 ▶ if (!has_hibit(s, e))
Branch: 1 branch
BranchTrueFalseTotal
if (!has_hibit(s, e)) 4 9 13
308 4 return 0;
309  
310 9 return is_utf8_string((U8*)s, e - s);
311 }
« tokenpos.h ↑ Summary blib/lib/HTML/Filter.pm »