GET혹은 POST방식으로 전달되는 URL 문자열을 인코딩하거나 디코딩 한다.
클라이언트에서 위의 방법을 이용해서 데이터를 전달할 경우에, 이외의 모든 단어는 %HEX방식으로 인코딩되어서 전달된다. 서버측에서는 이를 다시 디코딩해야 한다. CGI(12) 프로그래밍을 한다면 반드시 인코딩/디코딩과정을 거쳐야 한다.
인코딩 방식에 대한 내용은 CGI 프로그래밍을 참고하기 바란다.
사용방법
int urlencode(unsigned char *str, unsigned char *dest);
int urldecode(unsigned char *str, unsigned char *dest);
str을 인코딩하거나 디코딩해서 dest에 복사한다. 리턴값은 인코딩/디코딩 결과 데이터의 길이다.
사용예
int main()
{
int i, n;
unsigned char dest[256] = {0x00,};
unsigned char test[256] = {0x00,};
unsigned char *str = "hello world 안녕하세요\n";
unsigned char *str2 = "hello\%20world\%20\%be\%c8";
printf("Original : %d %s", strlen(str), str2);
n = urlencode(str, dest);
printf("decode : %d %s\n", n, dest);
n = urldecode(str2, test);
printf("encode : %d %s\n", n, test);
}
설명
사용방법
int main() { int i, n; unsigned char dest[256] = {0x00,}; unsigned char test[256] = {0x00,}; unsigned char *str = "hello world 안녕하세요\n"; unsigned char *str2 = "hello\%20world\%20\%be\%c8"; printf("Original : %d %s", strlen(str), str2); n = urlencode(str, dest); printf("decode : %d %s\n", n, dest); n = urldecode(str2, test); printf("encode : %d %s\n", n, test); }코드
#include <stdio.h> #include <string.h> #include <unistd.h> /* * urldecode * 입력된 source를 urldecode해서 dest에 복사한다. * */ int urldecode(unsigned char *source, unsigned char *dest) { int num=0, i, hexv, index=0; int retval=0; while(*source) { if (*source == '%') { num = 0; retval = 0; for (i = 0; i < 2; i++) { *source++; if (*(source) < ':') { num = *(source) - 48; } else if (*(source) > '@' && *(source) < '[') { num = (*(source) - 'A')+10; } else { num = (*(source) - 'a')+10; } if ((16*(1-i))) num = (num*16); retval += num; } dest[index] = retval; index++; } else { dest[index] = *source; index++; } *source++; } return index; } int urlencode(unsigned char *source, unsigned char *dest) { unsigned char hex[4]; unsigned char *sbuf; int size = 0; sbuf = dest; while(*source) { if ((*source > 47 && *source < 57) || (*source > 64 && *source < 92) || (*source > 96 && *source < 123) || *source == '-' || *source == '.' || *source == '_') { *sbuf = *source; } else { sprintf(hex, "%%%02X", *source); strncat(sbuf, hex,3); *sbuf++; *sbuf++; size += 2; } *source++; *sbuf++; size++; } return size; }다른 코드
#include <string.h> #include <stdlib.h> #include <stdio.h> #define IS_ALNUM(ch) \ ( ch >= 'a' && ch <= 'z' ) || \ ( ch >= 'A' && ch <= 'Z' ) || \ ( ch >= '0' && ch <= '9' ) || \ ( ch >= '-' && ch <= '.' ) char* url_decode( const char* str ){ int i, j = 0, len; char* tmp; char hex[3]; len = strlen( str ); hex[2] = 0; tmp = (char*)malloc( sizeof(char) * (len+1) ); for( i = 0 ; i < len ; i++, j++ ){ if( str[i] != '%' ) tmp[j] = str[i]; else{ if( IS_ALNUM(str[i+1]) && IS_ALNUM(str[i+2]) && i < (len-2) ){ hex[0] = str[i+1]; hex[1] = str[i+2]; tmp[j] = strtol( hex, NULL, 16 ); i += 2; } else tmp[j] = '%'; } } tmp[j] = 0; return tmp; } char* url_encode( const char* str ){ int i, j = 0, len; char* tmp; len = strlen( str ); tmp = (char*) malloc( (sizeof(char) * 3 * len) +1 ); for( i = 0 ; i < len ; i++ ){ if( IS_ALNUM( str[i] ) ) tmp[j] = str[i]; else{ snprintf( &tmp[j], 4, "%%%02X\n", (unsigned char)str[i] ); j += 2; } j++; } tmp[j] = 0; return tmp; }2004/05/12
Recent Posts
Archive Posts
Tags