summaryrefslogtreecommitdiff
path: root/ext/standard/url_scanner.re
blob: 2939582b68e584f7814680dcee72bdf075105741 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997, 1998, 1999 The PHP Group                         |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.0 of the PHP license,       |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_0.txt.                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: Sascha Schumann <sascha@schumann.cx>                         |
   +----------------------------------------------------------------------+
 */
/* $Id$ */

#include "php.h"
#include "snprintf.h"

#ifdef TRANS_SID

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#undef MIN
#define MIN(a,b) (a)<(b)?(a):(b)

#define YYCTYPE char
#define YYCURSOR state->crs
#define YYLIMIT state->end
#define YYMARKER state->ptr
#define YYFILL(n)

typedef enum {
	INITIAL,
	REF
} state;

typedef struct {
	state state;
	const char *crs;
	const char *end;
	const char *ptr;
	const char *start;
	char *target;
	size_t targetsize;
	const char *data;
} lexdata;

#define FINISH { catchup(state); goto finish; }

#define BEGIN(x) 						\
		switch(state->state) { 			\
			case INITIAL: 				\
				catchup(state); 		\
				break; 					\
			case REF: 					\
				screw_url(state); 		\
				break; 					\
		} 								\
		state->state = x; 				\
		state->start = state->crs; 		\
		goto nextiter

#define ATTACH(s, n) 										\
{ 															\
	size_t _newlen = state->targetsize + n; 				\
	state->target = realloc(state->target, _newlen + 1); 	\
	memcpy(state->target + state->targetsize, s, n); 		\
	state->targetsize = _newlen; 							\
	state->target[_newlen] = '\0'; 							\
}
	
#define URLLEN 512
	
static void screw_url(lexdata *state)
{
	int len;
	char buf[URLLEN];
	char url[URLLEN];
	const char *p, *q;
	char c;

	/* search outer limits for URI */
	for(p = state->start; p < state->crs && (c = *p); p++)
		if(c != '"' && c != ' ') break;

	/*  
	 *  we look at q-1, because q points to the character behind the last
	 *  character we are going to copy and the decision is based on that last
	 *  character 
	 */

	for(q = state->crs; q > state->start && (c = *(q-1)); q--)
		if(c != '"' && c != ' ') break;

	/* attach beginning */
	
	ATTACH(state->start, p-state->start);
	
	/* copy old URI */
	len = MIN(q - p, sizeof(buf) - 1);
	memcpy(url, p, len);
	url[len] = '\0';
	
	/* construct new URI */
	len = snprintf(buf, sizeof(buf), "%s%c%s", url,
			memchr(state->start, '?', len) ? '&' : '?',
			state->data);

	/* attach new URI */
	ATTACH(buf, len);
	
	/* attach rest */
	ATTACH(q, state->crs - q);
}

static void catchup(lexdata *state) 
{
	ATTACH(state->start, (state->crs - state->start));
}

/*!re2c
all = [\001-\377];
eof = [\000];
ws = [ \t\v\f];
A = [aA];
C = [cC];
E = [eE];
F = [fF];
H = [hH];
I = [iI];
M = [mM];
N = [nN];
O = [oO];
R = [rR];
S = [sS];
T = [tT];
*/

static void url_scanner(lexdata *state)
{
	while(state->crs < state->end) {
	
	switch(state->state) {
		case INITIAL: 
/*!re2c
  	"<" F R A M E ws+ S R C ws* "=" ws*		{ BEGIN(REF); }
  	"<" A ws+ H R E F ws* "="	ws*			{ BEGIN(REF); }
	"<" F O R M ws+ A C T I O N ws* "=" ws*	{ BEGIN(REF); }
	"<" A R E A ws+ H R E F ws* "=" ws*		{ BEGIN(REF); }
	(all\[<])+							{ BEGIN(INITIAL); }
	eof									{ FINISH; }
*/
			break;
		case REF: 
/*!re2c
    ["]? ws* (all\[> \t\v\f":#])* ws* ["]? { BEGIN(INITIAL); }
    ["]? ws* (all\[> \t\v\f":#])* /[#]  { BEGIN(INITIAL); }
    ["]? ws* (all\[> \t\v\f"#])* ws* ["]? { 
			/* don't modify absolute links */
			state->state = INITIAL; BEGIN(INITIAL); 
	}
*/
  			break;
	}
nextiter:
	;
	}
finish:
	;
}

char *url_adapt(const char *src, size_t srclen, const char *data, size_t *newlen)
{
	lexdata state;

	state.state = INITIAL;
	state.start = state.crs = src;
	state.end = src + srclen;
	state.ptr = NULL;
	state.target = NULL;
	state.targetsize = 0;
	state.data = data;

	url_scanner(&state);

	if(newlen) *newlen = state.targetsize;

	return state.target;
}

#endif