~hexmode/ubuntu/lucid/php5/php5.fix-php-bug-33210

« back to all changes in this revision

Viewing changes to ext/standard/url_scanner.c

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-11-06 01:44:25 UTC
  • mfrom: (1.1.15 upstream) (0.3.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091106014425-5dsqjhbg303h3kfn
Tags: 5.2.11.dfsg.1-1ubuntu1
* Merge from debian testing, remaining changes:
  - debian/control, debian/rules: Disable a few build dependencies and
    accompanying, binary packages which we do not want to support in main:
    + firebird2-dev/php5-interbase (we have a seperate php-interbase source)
    + libc-client/php5-imap (we have a seperate php-imap source)
    + libmcrypt-dev/php5-mcrypt (seperate php-mycrpt source)
    + readline support again, now that the libedit issue is fixed.
  - debian/control: Add build dependency: libdedit-dev (>= 2.9.cvs.20050518-1)
    CLI readline support.
  - debian/rules:
    + Correctly mangle PHP5_* macros for lpia
  - debian/control:
    + Use libdb-4.6-dev.
    + Rename Vcs-Browser & Vcs-Git to XS-Original-Vcs-Browser & XS-Original-Vcs-Git (LP: #323731).
  - Dropped patches:
    - debian/patches/119-sybase-alias.patch: Use Debian's patch.
    - debian/patches/fix-autoconf-ftbfs.patch: Use Debian's patch
    - debian/patches/fix-zlib-decompression: Already in upstream
  - Use the default pear/install-pear-nozlib.phar in php 5.2.11
  - debian/control: Move php5-suhoshin to Suggests.
  - debian/rules: Fix broken symlink for pear.
  - Dropped dependency on autoconf2.13.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
   +----------------------------------------------------------------------+
3
 
   | PHP Version 5                                                        |
4
 
   +----------------------------------------------------------------------+
5
 
   | Copyright (c) 1997-2009 The PHP Group                                |
6
 
   +----------------------------------------------------------------------+
7
 
   | This source file is subject to version 3.01 of the PHP license,      |
8
 
   | that is bundled with this package in the file LICENSE, and is        |
9
 
   | available through the world-wide-web at the following url:           |
10
 
   | http://www.php.net/license/3_01.txt                                  |
11
 
   | If you did not receive a copy of the PHP license and are unable to   |
12
 
   | obtain it through the world-wide-web, please send a note to          |
13
 
   | license@php.net so we can mail you a copy immediately.               |
14
 
   +----------------------------------------------------------------------+
15
 
   | Author: Hartmut Holzgraefe <hholzgra@php.net>                        |
16
 
   +----------------------------------------------------------------------+
17
 
 */
18
 
/* $Id: url_scanner.c,v 1.44.2.1.2.5 2008/12/31 11:17:46 sebastian Exp $ */
19
 
 
20
 
#include "php.h"
21
 
 
22
 
#include "php_globals.h"
23
 
 
24
 
#include <sys/types.h>
25
 
#include <stdio.h>
26
 
#include <stdlib.h>
27
 
#include <string.h>
28
 
#include "basic_functions.h"
29
 
#include "url_scanner.h"
30
 
 
31
 
#ifndef BUFSIZE
32
 
#define BUFSIZE 256
33
 
#endif
34
 
 
35
 
int php_url_scanner_activate(TSRMLS_D)
36
 
{
37
 
        url_adapt(NULL,0,NULL,NULL);
38
 
        return SUCCESS;
39
 
}
40
 
 
41
 
 
42
 
int php_url_scanner_deactivate(TSRMLS_D)
43
 
{
44
 
        url_adapt(NULL,0,NULL,NULL);
45
 
        return SUCCESS;
46
 
}
47
 
 
48
 
/* {{{ url_attr_addon
49
 
 */
50
 
static char *url_attr_addon(const char *tag,const char *attr,const char *val,const char *buf)
51
 
{
52
 
        int flag = 0;
53
 
 
54
 
        if (!strcasecmp(tag,"a") && !strcasecmp(attr,"href")) {
55
 
                flag = 1;
56
 
        } else if (!strcasecmp(tag,"area" ) && !strcasecmp(attr,"href"   )) {
57
 
                flag = 1;
58
 
        } else if (!strcasecmp(tag,"form" ) && !strcasecmp(attr,"action" )) {
59
 
                flag = 1;
60
 
        } else if (!strcasecmp(tag,"frame") && !strcasecmp(attr,"source" )) {
61
 
                flag = 1;
62
 
        } else if (!strcasecmp(tag,"img"  ) && !strcasecmp(attr,"action" )) {
63
 
                flag = 1;
64
 
        }
65
 
        if(flag && !strstr(val,buf) && !strchr(val,':')) {
66
 
                char *result;
67
 
                TSRMLS_FETCH();
68
 
 
69
 
                spprintf(&result, 0, "%s%s", (strchr(val,'?') ? PG(arg_separator).output : "?"), buf);
70
 
                return result;
71
 
        } 
72
 
        return NULL;
73
 
}
74
 
/* }}} */
75
 
 
76
 
#define US BG(url_adapt_state)
77
 
 
78
 
/* {{{ url_adapt_ext
79
 
 */
80
 
char *url_adapt_ext(const char *src, uint srclen, const char *name, const char *val, size_t *newlen)
81
 
{
82
 
        char buf[1024];
83
 
 
84
 
        snprintf(buf, sizeof(buf)-1, "%s=%s", name, val);
85
 
 
86
 
        return url_adapt(src, srclen, buf, newlen);
87
 
}
88
 
/* }}} */
89
 
 
90
 
/* {{{ url_adapt
91
 
 */
92
 
char *url_adapt(const char *src, size_t srclen, const char *data, size_t *newlen)
93
 
{
94
 
        char *out,*outp;
95
 
        int maxl,n;
96
 
        TSRMLS_FETCH();
97
 
 
98
 
        if(src==NULL) {
99
 
                US.state=STATE_NORMAL;
100
 
                if(US.tag)  { efree(US.tag);  US.tag =NULL; }
101
 
                if(US.attr) { efree(US.attr); US.attr=NULL; }
102
 
                if(US.val)  { efree(US.val);  US.val =NULL; }
103
 
                return NULL;
104
 
        }
105
 
 
106
 
        if(srclen==0) 
107
 
                srclen=strlen(src);
108
 
 
109
 
        out=malloc(srclen+1);
110
 
        maxl=srclen;
111
 
        n=srclen;
112
 
 
113
 
        *newlen=0;
114
 
        outp=out;
115
 
 
116
 
        while(n--) {
117
 
                switch(US.state) {
118
 
                case STATE_NORMAL:
119
 
                        if(*src=='<') 
120
 
                                US.state=STATE_TAG_START;
121
 
                        break;
122
 
 
123
 
                case STATE_TAG_START:
124
 
                        if(! isalnum(*src))
125
 
                                US.state=STATE_NORMAL;
126
 
                        US.state=STATE_TAG;
127
 
                        US.ml=BUFSIZE;
128
 
                        US.p=US.tag=erealloc(US.tag,US.ml);
129
 
                        *(US.p)++=*src;
130
 
                        US.l=1;
131
 
                        break;
132
 
 
133
 
                case STATE_TAG:
134
 
                        if(isalnum(*src)) {
135
 
                                *(US.p)++ = *src;
136
 
                                US.l++; 
137
 
                                if(US.l==US.ml) {
138
 
                                        US.ml+=BUFSIZE;
139
 
                                        US.tag=erealloc(US.tag,US.ml);
140
 
                                        US.p = US.tag+US.l;
141
 
                                }
142
 
                        } else if (isspace(*src)) {
143
 
                                US.state = STATE_IN_TAG;
144
 
                                *US.p='\0';
145
 
                                US.tag=erealloc(US.tag,US.l);
146
 
                        } else {
147
 
                                US.state = STATE_NORMAL;
148
 
                                efree(US.tag);
149
 
                                US.tag=NULL;
150
 
                        }
151
 
                        break;
152
 
 
153
 
                case STATE_IN_TAG:
154
 
                        if(isalnum(*src)) {
155
 
                                US.state=STATE_TAG_ATTR;
156
 
                                US.ml=BUFSIZE;
157
 
                                US.p=US.attr=erealloc(US.attr,US.ml);
158
 
                                *(US.p)++=*src;
159
 
                                US.l=1;
160
 
                        } else if (! isspace(*src)) {
161
 
                                US.state = STATE_NORMAL;
162
 
                                efree(US.tag);
163
 
                                US.tag=NULL;
164
 
                        }
165
 
                        break;
166
 
 
167
 
                case STATE_TAG_ATTR:
168
 
                        if(isalnum(*src)) {
169
 
                                *US.p++=*src;
170
 
                                ++US.l;
171
 
                                if(US.l==US.ml) {
172
 
                                        US.ml+=BUFSIZE;
173
 
                                        US.attr=erealloc(US.attr,US.ml);
174
 
                                        US.p = US.attr+US.l;
175
 
                                }
176
 
                                if(US.l==US.ml) {
177
 
                                        US.ml+=BUFSIZE;
178
 
                                        US.attr=erealloc(US.attr,US.ml);
179
 
                                        US.p = US.attr+US.l;
180
 
                                }
181
 
                        } else if(isspace(*src)||(*src=='=')){
182
 
                                US.state=STATE_TAG_IS;
183
 
                                *US.p=0;
184
 
                                US.attr=erealloc(US.attr,US.l);
185
 
                        } else if(*src=='>') {
186
 
                                US.state=STATE_NORMAL;
187
 
                        } else {
188
 
                                efree(US.attr);
189
 
                                US.attr=NULL;
190
 
                                US.state=STATE_IN_TAG;
191
 
                        }
192
 
                        break;
193
 
                        
194
 
                case STATE_TAG_IS:
195
 
                case STATE_TAG_IS2:
196
 
                        if(*src=='>'){
197
 
                                        US.state=STATE_NORMAL;
198
 
                                        if(! (US.attr_done)) {
199
 
                                                char *p;
200
 
                                                p=url_attr_addon(US.tag,US.attr,"",data);
201
 
                                                if(p) {
202
 
                                                        int l= strlen(p);
203
 
                                                        maxl+=l;
204
 
                                                        out=realloc(out,maxl);
205
 
                                                        outp=out+*newlen;
206
 
                                                        strlcpy(outp,p,maxl);
207
 
                                                        outp+=l;
208
 
                                                        *newlen+=l;
209
 
                                                        efree(p);
210
 
                                                }
211
 
                                        }
212
 
                        } else if(*src=='#') {
213
 
                                if(! (US.attr_done)) {
214
 
                                        char *p;
215
 
                                        US.attr_done=1;
216
 
                                        p=url_attr_addon(US.tag,US.attr,"#",data);
217
 
                                        if(p) {
218
 
                                                int l= strlen(p);
219
 
                                                maxl+=l;
220
 
                                                out=realloc(out,maxl);
221
 
                                                outp=out+*newlen;
222
 
                                                strlcpy(outp, p, maxl);
223
 
                                                outp+=l;
224
 
                                                *newlen+=l;
225
 
                                                efree(p);
226
 
                                        }
227
 
                                }
228
 
                        } else if(!isspace(*src)&&(*src!='=')) {
229
 
                                US.ml=BUFSIZE;
230
 
                                US.p=US.val=erealloc(US.val,US.ml);
231
 
                                US.l=0;
232
 
                                US.attr_done=0;
233
 
                                if((*src=='"')||(*src=='\'')) {
234
 
                                        US.state=STATE_TAG_QVAL2;
235
 
                                        US.delim=*src;
236
 
                                } else {
237
 
                                        US.state=STATE_TAG_VAL;
238
 
                                        *US.p++=*src;
239
 
                                        US.l++;
240
 
                                }
241
 
                        }
242
 
                        break;
243
 
 
244
 
 
245
 
                case STATE_TAG_QVAL2:
246
 
                        if(*src=='#') {
247
 
                                if(! (US.attr_done)) {
248
 
                                        char *p;
249
 
                                        US.attr_done=1;
250
 
                                        *US.p='\0';
251
 
                                        p=url_attr_addon(US.tag,US.attr,US.val,data);
252
 
                                        if(p) {
253
 
                                                int l= strlen(p);
254
 
                                                maxl+=l;
255
 
                                                out=realloc(out,maxl);
256
 
                                                outp=out+*newlen;
257
 
                                                strlcpy(outp,p,maxl);
258
 
                                                outp+=l;
259
 
                                                *newlen+=l;
260
 
                                                efree(p);
261
 
                                        }
262
 
                                }
263
 
                        } else if(*src==US.delim) {
264
 
                                US.state=STATE_IN_TAG;
265
 
                                *US.p='\0';
266
 
                                if(! (US.attr_done)) {
267
 
                                        char *p;
268
 
                                        p=url_attr_addon(US.tag,US.attr,US.val,data);
269
 
                                        if(p) {
270
 
                                                int l= strlen(p);
271
 
                                                maxl+=l;
272
 
                                                out=realloc(out,maxl);
273
 
                                                outp=out+*newlen;
274
 
                                                strlcpy(outp,p,maxl);
275
 
                                                outp+=l;
276
 
                                                *newlen+=l;
277
 
                                                efree(p);
278
 
                                        }
279
 
                                }
280
 
                                break;
281
 
                        } else if(*src=='\\') {
282
 
                                US.state=STATE_TAG_QVAL2b;
283
 
                        } else if (*src=='>') {
284
 
                                US.state=STATE_NORMAL;
285
 
                        }
286
 
                        
287
 
                        *US.p++=*src;
288
 
                        ++US.l; 
289
 
                        if(US.l==US.ml) {
290
 
                                US.ml+=BUFSIZE;
291
 
                                US.val=erealloc(US.val,US.ml);
292
 
                                US.p = US.val+US.l;
293
 
                        }
294
 
                        
295
 
                        break;
296
 
                        
297
 
                case STATE_TAG_QVAL2b:
298
 
                        US.state=STATE_TAG_QVAL2;
299
 
                        *US.p++=*src;
300
 
                        ++US.l; 
301
 
                        if(US.l==US.ml) {
302
 
                                US.ml+=BUFSIZE;
303
 
                                US.val=erealloc(US.val,US.ml);
304
 
                                US.p = US.val+US.l;
305
 
                        }
306
 
                        break;
307
 
 
308
 
                case STATE_TAG_VAL:
309
 
                case STATE_TAG_VAL2:
310
 
                        if(*src=='#') {
311
 
                                if(! (US.attr_done)) {
312
 
                                        char *p;
313
 
                                        US.attr_done=1;
314
 
                                        *US.p='\0';
315
 
                                        p=url_attr_addon(US.tag,US.attr,US.val,data);
316
 
                                        if(p) {
317
 
                                                int l= strlen(p);
318
 
                                                maxl+=l;
319
 
                                                out=realloc(out,maxl);
320
 
                                                outp=out+*newlen;
321
 
                                                strlcpy(outp,p,maxl);
322
 
                                                outp+=l;
323
 
                                                *newlen+=l;
324
 
                                                efree(p);
325
 
                                        }
326
 
                                        }
327
 
                        } else if(isspace(*src)||(*src=='>')) {
328
 
                                US.state=(*src=='>')?STATE_NORMAL:STATE_IN_TAG;
329
 
                                *US.p='\0';
330
 
                                if(! (US.attr_done)) {
331
 
                                        char *p;
332
 
                                        p=url_attr_addon(US.tag,US.attr,US.val,data);
333
 
                                        if(p) {
334
 
                                                int l= strlen(p);
335
 
                                                maxl+=l;
336
 
                                                out=realloc(out,maxl);
337
 
                                                outp=out+*newlen;
338
 
                                                strlcpy(outp,p,maxl);
339
 
                                                outp+=l;
340
 
                                                *newlen+=l;
341
 
                                                efree(p);
342
 
                                        }
343
 
                                }
344
 
                        } else {
345
 
                                *US.p++=*src;
346
 
                                US.l++; 
347
 
                                if(US.l==US.ml) {
348
 
                                        US.ml+=BUFSIZE;
349
 
                                        US.val=erealloc(US.val,US.ml);
350
 
                                        US.p = US.val+US.l;
351
 
                                }
352
 
                        }
353
 
                        break;
354
 
                default:
355
 
                        break;
356
 
                }
357
 
 
358
 
                *outp++=*src++;
359
 
                *newlen+=1;
360
 
        }
361
 
        *outp='\0';
362
 
        return out;
363
 
}
364
 
/* }}} */
365
 
 
366
 
/*
367
 
 * Local variables:
368
 
 * tab-width: 4
369
 
 * c-basic-offset: 4
370
 
 * End:
371
 
 * vim600: sw=4 ts=4 fdm=marker
372
 
 * vim<600: sw=4 ts=4
373
 
 */