strip_html.c

filter: Statementstmt 80.3% 123 / 153 Branchbran 66.2% 98 / 148 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.4% 221 / 301 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/HTML/Strip.pm ↑ Summary Strip.xs »
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4  
5 #include "strip_html.h"
6  
7 #ifdef _MSC_VER
8 #define strcasecmp(a,b) stricmp(a,b)
9 #endif
10  
11 void
12 41 _strip_html( Stripper * stripper, char * raw, char * output, int is_utf8_p ) {
13 41 char * p_raw = raw;
14 41 char * raw_end = raw + strlen(raw);
15 41 char * p_output = output;
16 int width;
17  
18 41 ▶ if( stripper->o_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->o_debug ) { 0 41 41
19 0 printf( "[DEBUG] input string: %s\n", p_raw );
20 }
21  
22 3288 ▶ while( p_raw < raw_end ) {
Branch: 1 branch
BranchTrueFalseTotal
while( p_raw < raw_end ) { 3247 41 3288
23 3247 ▶ width = is_utf8_p ? utf8_char_width(p_raw) : 1;
Branch: 1 branch
BranchTrueFalseTotal
width = is_utf8_p ? utf8_char_width(p_raw) : 1; 1873 1374 3247
24  
25 3247 ▶ if( stripper->o_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->o_debug ) { 0 3247 3247
26 0 printf( "[DEBUG] char:%C w%i state:%c%c%c tag:%5s last:%c%c%c%c in:%c%c%c quote:%c ",
27 0 *p_raw,
28 width,
29 0 ▶ (stripper->f_closing ? 'C' : ' '),
Branch: 1 branch
BranchTrueFalseTotal
(stripper->f_closing ? 'C' : ' '), 0 0 0
30 0 ▶ (stripper->f_in_tag ? 'T' : ' '),
Branch: 1 branch
BranchTrueFalseTotal
(stripper->f_in_tag ? 'T' : ' '), 0 0 0
31 0 (stripper->f_full_tagname ? 'F' : ' '),
32 0 ▶ stripper->tagname,
Branch: 1 branch
BranchTrueFalseTotal
stripper->tagname, 0 0 0
33 0 ▶ (stripper->f_just_seen_tag ? 'T' : ' '),
Branch: 1 branch
BranchTrueFalseTotal
(stripper->f_just_seen_tag ? 'T' : ' '), 0 0 0
34 0 ▶ (stripper->f_outputted_space ? 'S' : ' '),
Branch: 1 branch
BranchTrueFalseTotal
(stripper->f_outputted_space ? 'S' : ' '), 0 0 0
35 0 ▶ (stripper->f_lastchar_slash ? '/' : ' '),
Branch: 1 branch
BranchTrueFalseTotal
(stripper->f_lastchar_slash ? '/' : ' '), 0 0 0
36 0 ▶ (stripper->f_lastchar_minus ? '-' : ' '),
Branch: 1 branch
BranchTrueFalseTotal
(stripper->f_lastchar_minus ? '-' : ' '), 0 0 0
37 0 ▶ (stripper->f_in_decl ? 'D' : ' '),
Branch: 1 branch
BranchTrueFalseTotal
(stripper->f_in_decl ? 'D' : ' '), 0 0 0
38 0 ▶ (stripper->f_in_comment ? 'C' : ' '),
Branch: 1 branch
BranchTrueFalseTotal
(stripper->f_in_comment ? 'C' : ' '), 0 0 0
39 0 ▶ (stripper->f_in_striptag ? 'X' : ' '),
Branch: 1 branch
BranchTrueFalseTotal
(stripper->f_in_striptag ? 'X' : ' '), 0 0 0
40 0 ▶ (stripper->f_in_quote ? stripper->quote : ' ')
Branch: 1 branch
BranchTrueFalseTotal
(stripper->f_in_quote ? stripper->quote : ' ') 0 0 0
41 );
42 }
43  
44 // either a single char or a set of unicode code points;
45 3247 ▶ if( stripper->f_in_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->f_in_tag ) { 723 2524 3247
46 /* inside a tag */
47 /* check if we know either the tagname, or that we're in a declaration */
48 723 ▶ if( !stripper->f_full_tagname && !stripper->f_in_decl ) {
Branch: 2 branches
BranchTrueFalseTotal
if( !stripper->f_full_tagname && !stripper->f_in_decl ) { 622 101 723
if( !stripper->f_full_tagname && !stripper->f_in_decl ) { 429 193 622
49 /* if this is the first character, check if it's a '!'; if so, we're in a declaration */
50 429 ▶ if( stripper->p_tagname == stripper->tagname && *p_raw == '!' ) {
Branch: 2 branches
BranchTrueFalseTotal
if( stripper->p_tagname == stripper->tagname && *p_raw == '!' ) { 162 267 429
if( stripper->p_tagname == stripper->tagname && *p_raw == '!' ) { 6 156 162
51 6 stripper->f_in_decl = 1;
52 }
53 /* then check if the first character is a '/', in which case, this is a closing tag */
54 423 ▶ else if( stripper->p_tagname == stripper->tagname && *p_raw == '/' ) {
Branch: 2 branches
BranchTrueFalseTotal
else if( stripper->p_tagname == stripper->tagname && *p_raw == '/' ) { 156 267 423
else if( stripper->p_tagname == stripper->tagname && *p_raw == '/' ) { 47 109 156
55 47 stripper->f_closing = 1;
56 }
57 /* if the first character wasn't a '/', and we're in a stripped block,
58 * assume this is a mathematical operator and reset */
59 376 ▶ else if( !stripper->f_closing && stripper->f_in_striptag && stripper->p_tagname == stripper->tagname && *p_raw != '/' ) {
Branch: 4 branches
BranchTrueFalseTotal
else if( !stripper->f_closing && stripper->f_in_striptag && stripper->p_tagname == stripper->tagname && *p_raw != '/' ) { 197 179 376
else if( !stripper->f_closing && stripper->f_in_striptag && stripper->p_tagname == stripper->tagname && *p_raw != '/' ) { 3 194 197
else if( !stripper->f_closing && stripper->f_in_striptag && stripper->p_tagname == stripper->tagname && *p_raw != '/' ) { 3 0 3
else if( !stripper->f_closing && stripper->f_in_striptag && stripper->p_tagname == stripper->tagname && *p_raw != '/' ) { 3 0 3
60 3 stripper->f_in_tag = 0;
61 3 stripper->f_closing = 0;
62 /* we only care about closing tags within a stripped tags block (e.g. scripts) */
63 373 ▶ } else if( !stripper->f_in_striptag || stripper->f_closing ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if( !stripper->f_in_striptag || stripper->f_closing ) { 48 325 373
} else if( !stripper->f_in_striptag || stripper->f_closing ) { 48 0 48
64 /* if we don't have the full tag name yet, add p_raw character unless it's whitespace, a '/', or a '>';
65 otherwise null pad the string and set the full tagname flag, and check the tagname against stripped ones.
66 also sanity check we haven't reached the array bounds, and truncate the tagname here if we have */
67 373 ▶ if( (!isspace( *p_raw ) && *p_raw != '/' && *p_raw != '>') &&
Branch: 3 branches
BranchTrueFalseTotal
if( (!isspace( *p_raw ) && *p_raw != '/' && *p_raw != '>') && 369 4 373
if( (!isspace( *p_raw ) && *p_raw != '/' && *p_raw != '>') && 369 0 369
if( (!isspace( *p_raw ) && *p_raw != '/' && *p_raw != '>') && 267 102 369
68 267 ▶ !( (stripper->p_tagname - stripper->tagname) == MAX_TAGNAMELENGTH ) ) {
Branch: 1 branch
BranchTrueFalseTotal
!( (stripper->p_tagname - stripper->tagname) == MAX_TAGNAMELENGTH ) ) { 267 0 267
69 267 *stripper->p_tagname++ = *p_raw;
70 } else {
71 106 *stripper->p_tagname = 0;
72 106 stripper->f_full_tagname = 1;
73 /* if we're in a stripped tag block, and this is a closing tag, check to see if it ends the stripped block */
74 106 ▶ if( stripper->f_in_striptag && stripper->f_closing ) {
Branch: 2 branches
BranchTrueFalseTotal
if( stripper->f_in_striptag && stripper->f_closing ) { 8 98 106
if( stripper->f_in_striptag && stripper->f_closing ) { 8 0 8
75 15 ▶ if( strcasecmp( stripper->tagname, stripper->striptag ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( strcasecmp( stripper->tagname, stripper->striptag ) == 0 ) { 7 1 8
76 7 stripper->f_in_striptag = 0;
77 }
78 /* if we're outside a stripped tag block, check tagname against stripped tag list */
79 98 ▶ } else if( !stripper->f_in_striptag && !stripper->f_closing ) {
Branch: 2 branches
BranchTrueFalseTotal
} else if( !stripper->f_in_striptag && !stripper->f_closing ) { 98 0 98
} else if( !stripper->f_in_striptag && !stripper->f_closing ) { 59 39 98
80 int i;
81 279 ▶ for( i = 0; i < stripper->numstriptags; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( i = 0; i < stripper->numstriptags; i++ ) { 220 59 279
82 220 ▶ if( strcasecmp( stripper->tagname, stripper->o_striptags[i] ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( strcasecmp( stripper->tagname, stripper->o_striptags[i] ) == 0 ) { 8 212 220
83 8 stripper->f_in_striptag = 1;
84 8 strcpy( stripper->striptag, stripper->tagname );
85 }
86 }
87 }
88 106 check_end( stripper, *p_raw );
89 }
90 }
91 } else {
92 294 ▶ if( stripper->f_in_quote ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->f_in_quote ) { 113 181 294
93 /* inside a quote */
94 /* end of quote if p_raw character matches the opening quote character */
95 113 ▶ if( *p_raw == stripper->quote ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *p_raw == stripper->quote ) { 5 108 113
96 5 stripper->quote = 0;
97 5 stripper->f_in_quote = 0;
98 }
99 } else {
100 /* not in a quote */
101 /* check for quote characters, but not in a comment */
102 181 ▶ if( !stripper->f_in_comment &&
Branch: 1 branch
BranchTrueFalseTotal
if( !stripper->f_in_comment && 73 108 181
103 73 ▶ ( *p_raw == '\'' || *p_raw == '\"' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
( *p_raw == '\'' || *p_raw == '\"' ) ) { 73 0 73
( *p_raw == '\'' || *p_raw == '\"' ) ) { 5 68 73
104 5 stripper->f_in_quote = 1;
105 5 stripper->quote = *p_raw;
106 /* reset lastchar_* flags in case we have something perverse like '-"' or '/"' */
107 5 stripper->f_lastchar_minus = 0;
108 5 stripper->f_lastchar_slash = 0;
109 } else {
110 176 ▶ if( stripper->f_in_decl ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->f_in_decl ) { 146 30 176
111 /* inside a declaration */
112 146 ▶ if( stripper->f_lastchar_minus ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->f_lastchar_minus ) { 10 136 146
113 /* last character was a minus, so if p_raw one is, then we're either entering or leaving a comment */
114 10 ▶ if( *p_raw == '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *p_raw == '-' ) { 10 0 10
115 10 stripper->f_in_comment = !stripper->f_in_comment;
116 }
117 10 stripper->f_lastchar_minus = 0;
118 } else {
119 /* if p_raw character is a minus, we might be starting a comment marker */
120 136 ▶ if( *p_raw == '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *p_raw == '-' ) { 10 126 136
121 10 stripper->f_lastchar_minus = 1;
122 }
123 }
124 146 ▶ if( !stripper->f_in_comment ) {
Branch: 1 branch
BranchTrueFalseTotal
if( !stripper->f_in_comment ) { 38 108 146
125 38 check_end( stripper, *p_raw );
126 }
127 } else {
128 30 check_end( stripper, *p_raw );
129 }
130 } /* quote character check */
131 } /* in quote check */
132 } /* full tagname check */
133 }
134 else {
135 /* not in a tag */
136 /* check for tag opening, and reset parameters if one has */
137 2524 ▶ if( *p_raw == '<' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( *p_raw == '<' ) { 115 2409 2524
138 115 stripper->f_in_tag = 1;
139 115 stripper->tagname[0] = 0;
140 115 stripper->p_tagname = stripper->tagname;
141 115 stripper->f_full_tagname = 0;
142 115 stripper->f_closing = 0;
143 115 stripper->f_just_seen_tag = 1;
144 }
145 else {
146 /* copy to stripped provided we're not in a stripped block */
147 2409 ▶ if( !stripper->f_in_striptag ) {
Branch: 1 branch
BranchTrueFalseTotal
if( !stripper->f_in_striptag ) { 2066 343 2409
148 /* only emit spaces if we're configured to do so (on by default) */
149 2066 ▶ if( stripper->o_emit_spaces ){
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->o_emit_spaces ){ 2066 0 2066
150 /* output a space in place of tags we have previously parsed,
151 and set a flag so we only do this once for every group of tags.
152 done here to prevent unnecessary trailing spaces */
153 2066 ▶ if( !isspace(*p_raw) &&
Branch: 1 branch
BranchTrueFalseTotal
if( !isspace(*p_raw) && 1779 287 2066
154 /* don't output a space if this character is one anyway */
155 1779 ▶ !stripper->f_outputted_space &&
Branch: 1 branch
BranchTrueFalseTotal
!stripper->f_outputted_space && 1492 287 1779
156 1492 ▶ stripper->f_just_seen_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
stripper->f_just_seen_tag ) { 20 1472 1492
157 20 ▶ if( stripper->o_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->o_debug ) { 0 20 20
158 0 printf("SPACE ");
159 }
160 20 *p_output++ = ' ';
161 20 stripper->f_outputted_space = 1;
162 }
163 }
164 2066 strncpy(p_output, p_raw, width);
165 2066 ▶ if( stripper->o_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->o_debug ) { 0 2066 2066
166 0 printf("CHAR %c", *p_raw);
167 }
168 2066 p_output += width;
169  
170 /* reset 'just seen tag' flag */
171 2066 stripper->f_just_seen_tag = 0;
172 /* reset 'outputted space' flag if character is not one */
173 2066 ▶ if (!isspace(*p_raw)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!isspace(*p_raw)) { 1779 287 2066
174 1779 stripper->f_outputted_space = 0;
175 } else {
176 287 stripper->f_outputted_space = 1;
177 }
178 }
179 }
180 } /* in tag check */
181 3247 p_raw += width;
182 3247 ▶ if( stripper->o_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->o_debug ) { 0 3247 3247
183 0 printf("\n");
184 }
185 } /* while loop */
186  
187 41 *p_output = 0;
188  
189 41 ▶ if (stripper->o_auto_reset) {
Branch: 1 branch
BranchTrueFalseTotal
if (stripper->o_auto_reset) { 6 35 41
190 6 _reset( stripper );
191 }
192 41 }
193  
194 int
195 1873 utf8_char_width(unsigned char * string) {
196 1873 ▶ if (~*string & 128) { // 0xxxxxxx
Branch: 1 branch
BranchTrueFalseTotal
if (~*string & 128) { // 0xxxxxxx 568 1305 1873
197 568 return 1;
198 1305 ▶ } else if ((*string & 192) == 128) { // 10xxxxxx
Branch: 1 branch
BranchTrueFalseTotal
} else if ((*string & 192) == 128) { // 10xxxxxx 0 1305 1305
199 /* latter bytes of a multibyte utf8 char
200 XXX this should never happen in practice XXX
201 but we account for it anyway */
202 0 int width = 1;
203 0 char * p = string;
204 0 ▶ while ((*p++ & 192) == 128) {
Branch: 1 branch
BranchTrueFalseTotal
while ((*p++ & 192) == 128) { 0 0 0
205 0 width++;
206 }
207 0 return width;
208 1305 ▶ } else if ((*string & 224) == 192) { // 110xxxxx
Branch: 1 branch
BranchTrueFalseTotal
} else if ((*string & 224) == 192) { // 110xxxxx 1302 3 1305
209 1302 return 2;
210 3 ▶ } else if ((*string & 240) == 224) { // 1110xxxx
Branch: 1 branch
BranchTrueFalseTotal
} else if ((*string & 240) == 224) { // 1110xxxx 3 0 3
211 3 return 3;
212 0 ▶ } else if ((*string & 248) == 240) { // 11110xxx
Branch: 1 branch
BranchTrueFalseTotal
} else if ((*string & 248) == 240) { // 11110xxx 0 0 0
213 0 return 4;
214 /* part of original utf8 spec, but not used
215 } else if ((*string & 252) == 248) { // 111110xx
216 return 5;
217 } else if ((*string & 254) == 252) { // 1111110x
218 return 6;
219 */
220 } else {
221 0 printf( "[WARN] invalid utf8 char ord=%i\n", *string );
222 0 return 1;
223 }
224 }
225  
226 void
227 48 _reset( Stripper * stripper ) {
228 48 stripper->f_in_tag = 0;
229 48 stripper->f_closing = 0;
230 48 stripper->f_lastchar_slash = 0;
231 48 stripper->f_full_tagname = 0;
232 /* hack to stop a space being output on strings starting with a tag */
233 48 stripper->f_outputted_space = 1;
234 48 stripper->f_just_seen_tag = 0;
235  
236 48 stripper->f_in_quote = 0;
237  
238 48 stripper->f_in_decl = 0;
239 48 stripper->f_in_comment = 0;
240 48 stripper->f_lastchar_minus = 0;
241  
242 48 stripper->f_in_striptag = 0;
243  
244 48 memset(stripper->tagname, 0, sizeof(stripper->tagname));
245 48 }
246  
247 void
248 20 clear_striptags( Stripper * stripper ) {
249 20 strcpy(stripper->o_striptags[0], "");
250 20 stripper->numstriptags = 0;
251 20 }
252  
253 void
254 75 add_striptag( Stripper * stripper, char * striptag ) {
255 75 ▶ if( stripper->numstriptags < MAX_STRIPTAGS-1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->numstriptags < MAX_STRIPTAGS-1 ) { 75 0 75
256 75 strcpy(stripper->o_striptags[stripper->numstriptags++], striptag);
257 } else {
258 0 fprintf( stderr, "Cannot have more than %i strip tags", MAX_STRIPTAGS );
259 }
260 75 }
261  
262 void
263 174 check_end( Stripper * stripper, char end ) {
264 /* if p_raw character is a slash, may be a closed tag */
265 174 ▶ if( end == '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if( end == '/' ) { 1 173 174
266 1 stripper->f_lastchar_slash = 1;
267 } else {
268 /* if the p_raw character is a '>', then the tag has ended */
269 /* slight hack to deal with mathematical characters in script tags:
270 * if we're in a stripped block, and this is a closing tag, spaces
271 * will also end the tag, since we only want it for comparison with
272 * the opening one */
273 173 ▶ if( (end == '>') ||
Branch: 1 branch
BranchTrueFalseTotal
if( (end == '>') || 61 112 173
274 61 ▶ (stripper->f_in_striptag && stripper->f_closing && isspace(end)) ) {
Branch: 3 branches
BranchTrueFalseTotal
(stripper->f_in_striptag && stripper->f_closing && isspace(end)) ) { 6 55 61
(stripper->f_in_striptag && stripper->f_closing && isspace(end)) ) { 0 6 6
(stripper->f_in_striptag && stripper->f_closing && isspace(end)) ) { 0 0 0
275 112 stripper->f_in_quote = 0;
276 112 stripper->f_in_comment = 0;
277 112 stripper->f_in_decl = 0;
278 112 stripper->f_in_tag = 0;
279 112 stripper->f_closing = 0;
280 /* Do not start a stripped tag block if the tag is a closed one, e.g. '<script src="foo" />' */
281 112 ▶ if( stripper->f_lastchar_slash &&
Branch: 1 branch
BranchTrueFalseTotal
if( stripper->f_lastchar_slash && 0 112 112
282 0 ▶ (strcasecmp( stripper->striptag, stripper->tagname ) == 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
(strcasecmp( stripper->striptag, stripper->tagname ) == 0) ) { 0 0 0
283 0 stripper->f_in_striptag = 0;
284 }
285 }
286 173 stripper->f_lastchar_slash = 0;
287 }
288 174 }
« blib/lib/HTML/Strip.pm ↑ Summary Strip.xs »