Browse code

un_escape function introduced

Jiri Kuthan authored on 18/01/2003 10:50:43
Showing 1 changed files
... ...
@@ -25,6 +25,11 @@
25 25
  * You should have received a copy of the GNU General Public License 
26 26
  * along with this program; if not, write to the Free Software 
27 27
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
+ *
29
+ * History
30
+ * ------
31
+ * 2003-01-18 un_escape function introduced for convenience of code needing
32
+ *            the complex&slow feature of unescaping
28 33
  */
29 34
 
30 35
 
... ...
@@ -38,6 +43,7 @@
38 38
 
39 39
 
40 40
 #include "dprint.h"
41
+#include "str.h"
41 42
 
42 43
 
43 44
 struct sip_msg;
... ...
@@ -225,4 +231,81 @@ inline static void sleep_us( unsigned int nusecs )
225 225
 	select(0, NULL, NULL, NULL, &tval );
226 226
 }
227 227
 
228
+inline static int hex2int(char hex_digit)
229
+{
230
+	if (hex_digit>='0' && hex_digit<='9')
231
+		return hex_digit-'0';
232
+	if (hex_digit>='a' && hex_digit<='f')
233
+		return hex_digit-'a'+10;
234
+	if (hex_digit>='A' && hex_digit<='F')
235
+		return hex_digit-'A'+10;
236
+	/* no valid hex digit ... */
237
+	LOG(L_ERR, "ERROR: hex2int: '%c' is no hex char\n", hex_digit );
238
+	return -1;
239
+}
240
+
241
+/* Un-escape URI user  -- it takes a pointer to original user
242
+   str, as well as the new, unescaped one, which MUST have
243
+   an allocated buffer linked to the 'str' structure ;
244
+   (the buffer can be allocated with the same length as
245
+   the original string -- the output string is always
246
+   shorter (if escaped characters occur) or same-long
247
+   as the original one).
248
+
249
+   only printeable characters are permitted
250
+
251
+	<0 is returned on an uneascaping error, length of the
252
+	unescaped string otherwise
253
+*/
254
+inline static int un_escape(str *user, str *new_user ) 
255
+{
256
+ 	int i, j, value;
257
+	int hi, lo;
258
+
259
+	new_user->len = 0;
260
+	j = 0;
261
+
262
+	for (i = 0; i < user->len; i++) {
263
+		if (user->s[i] == '%') {
264
+			if (i + 2 >= user->len) {
265
+				LOG(L_ERR, "ERROR: un_escape: escape sequence too short in"
266
+					" '%.*s' @ %d\n",
267
+					user->len, user->s, i );
268
+				goto error;
269
+			}
270
+			hi=hex2int(user->s[i + 1]);
271
+			if (hi<0) {
272
+				LOG(L_ERR, "ERROR: un_escape: non-hex high digit in an escape sequence in"
273
+					" '%.*s' @ %d\n",
274
+					user->len, user->s, i+1 );
275
+				goto error;
276
+			}
277
+			lo=hex2int(user->s[i + 2]);
278
+			if (lo<0) {
279
+				LOG(L_ERR, "ERROR: non-hex low digit in an escape sequence in "
280
+					"'%.*s' @ %d\n",
281
+					user->len, user->s, i+2 );
282
+				goto error;
283
+			}
284
+			value=(hi<<4)+lo;
285
+			if (value < 32 || value > 126) {
286
+				LOG(L_ERR, "ERROR: non-ASCII escaped character in '%.*s' @ %d\n",
287
+					user->len, user->s, i );
288
+				goto error;
289
+			}
290
+			new_user->s[j] = value;
291
+			i+=2; /* consume the two hex digits, for cycle will move to the next char */
292
+		} else {
293
+			new_user->s[j] = user->s[i];
294
+		}
295
+        j++; /* good -- we translated another character */
296
+	}
297
+	new_user->len = j;
298
+	return j;
299
+
300
+error:
301
+	new_user->len = j;
302
+	return -1;
303
+} 
304
+
228 305
 #endif