'programming/C_C++'에 해당되는 글 279건

  1. 2013.04.24 urldecode urlencode

반응형

 


출처 : http://hi.blog.qrobo.com/?p=71

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>

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;
   
}

int main(void)
{
 
  int n;
  unsigned char test[256] = {0×00,};
  unsigned char* str =”%B3%C3%C0%E5%B0%ED”;
 
  n = urldecode(str, test);
  printf(“encode   : %d %s\n”, n, test);
 
  return 0;

}


 

반응형
Posted by 공간사랑
,