[C]urldecode()的实现

2015-12-18 写技术

int urldecode(char *str)
{
        int i, j;
        int len;
        if(!str) return 0;

        if(!strchr(str, '%')) return 0;

        len = strlen(str);
        for(i=0; i<len; i++){
                if(str[i] == '%'){
                        if(i+2 >= len){
                                return 1;
                        }
                        if(str[i+1] == '2' && str[i+2] == '5'){
                                str[i] = '%';
                                len -= 2;
                                for(j=i+1; j<len; j++){
                                        str[j] = str[j+2];
                                }
                                str[j] = '\0';
                        }else if(str[i+1] == '3' && (str[i+2] == 'A' || str[i+2] == 'a')){
                                str[i] = ':';
                                len -= 2;
                                for(j=i+1; j<len; j++){
                                        str[j] = str[j+2];
                                }
                                str[j] = '\0';
                        }else if(str[i+1] == '4' && str[i+2] == '0'){
                                str[i] = ':';
                                len -= 2;
                                for(j=i+1; j<len; j++){
                                        str[j] = str[j+2];
                                }
                                str[j] = '\0';
                        }else{
                                return 1;
                        }
                }
        }
        return 0;
}

标签: C

发表评论:

Powered by anycle 湘ICP备15001973号-1