'programming'에 해당되는 글 387건

  1. 2017.11.22 URL ENCODE

URL ENCODE

programming/C_C++ 2017. 11. 22. 18:25
반응형

 

https://www.joinc.co.kr/w/Site/Code/C/urlencode

 

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

 

------

#define IS_ALNUM(ch) \
            (( ch >= 'a' && ch <= 'z' ) || ( ch >= 'A' && ch <= 'Z' ) || ( ch >= '0' && ch <= '9' ) || ( ch >= '-' && ch <= '.' ))


int url_encode(char *str, char *encstr, int len)
{
    char buf[2+1];
    int i, j;
    unsigned char c;

    if(str == NULL) {
        return 0;
    }

    for(i = j = 0; i<len; i++) {

        c = (unsigned char)str[i];

        if (c == ' ') encstr[j++] = '+';
        else if ((c >= '0') && (c <= '9')) encstr[j++] = c;
        else if ((c >= 'A') && (c <= 'Z')) encstr[j++] = c;
        else if ((c >= 'a') && (c <= 'z')) encstr[j++] = c;
        else if ((c == '@') || (c == '.')) encstr[j++] = c;
        else {
            sprintf(buf, "%02X", c);
            encstr[j++] = '%';
            encstr[j++] = buf[0];
            encstr[j++] = buf[1];
        }

    }

    encstr[j] = '\0';

    return (strlen(encstr));
}


int url_decode(char *str, char *decstr, int len)
{
    int i, j = 0;
    char hex[3];

    len = strlen( str );
    hex[2] = 0;

    for( i = 0 ; i < len ; i++, j++ ) {

        if( str[i] != '%' ) {

            decstr[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];

                decstr[j] = strtol( hex, NULL, 16 );

                i += 2;

            }
            else {

                decstr[j] = '%';

            }

        }

    }

    decstr[j] = '\0';

    return (strlen(decstr));

}

 

 

 

 

 

반응형
Posted by 공간사랑
,