[go: up one dir, main page]

File: url_encode.c

package info (click to toggle)
cctools 9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,624 kB
  • sloc: ansic: 192,539; python: 20,827; cpp: 20,199; sh: 11,719; perl: 4,106; xml: 3,688; makefile: 1,224
file content (50 lines) | stat: -rw-r--r-- 890 bytes parent folder | download | duplicates (3)
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
/*
Copyright (C) 2008- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/

#include "url_encode.h"

#include <stdio.h>
#include <string.h>

void url_encode(const char *s, char *t, int length)
{
	if(s) {
		while(*s && length > 1) {
			if(*s <= 32 || *s == '%' || *s == '\\' || *s == '<' || *s == '>' || *s == '\'' || *s == '\"' || *s > 122) {
				if(length > 3) {
					snprintf(t, length, "%%%2X", *s);
					t += 3;
					length -= 3;
					s++;
				} else {
					break;
				}
			} else {
				*t++ = *s++;
				length--;
			}
		}
	}
	*t = 0;
}

void url_decode(const char *s, char *t, int length)
{
	while(*s && length > 1) {
		if(*s == '%') {
			unsigned int x;
			sscanf(s + 1, "%2x", &x);
			*t++ = x;
			s += 3;
		} else {
			*t++ = *s++;
		}
		length--;
	}
	*t = 0;
}

/* vim: set noexpandtab tabstop=4: */