URL加解密 URLEncode

url

Linux C语言实现urlencode和urldecode

urlencode编码的基本规则

  • 字符"a"-“z”,“A”-“Z”,“0”-“9”,“.”,“-”,“*”,和"_" 都不被编码,维持原值;
  • 空格" “被转换为加号”+"。
  • 其他每个字节都被表示成"%XY"格式的由3个字符组成的字符串,编码为UTF-8(特别需要注意: 这里是大写形式的hexchar)。

urlencode编码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

static unsigned char hexchars[] = "0123456789ABCDEF";

/**
 * @brief URLEncode : encode the base64 string "str"
 *
 * @param str:  the base64 encoded string
 * @param strsz:  the str length (exclude the last \0)
 * @param result:  the result buffer
 * @param resultsz: the result buffer size(exclude the last \0)
 *
 * @return: >=0 represent the encoded result length
 *              <0 encode failure
 *
 * Note:
 * 1) to ensure the result buffer has enough space to contain the encoded string, we'd better
 *     to set resultsz to 3*strsz
 *
 * 2) we don't check whether str has really been base64 encoded
 */
int URLEncode(const char *str, const int strsz, char *result, const int resultsz)
{
    int i, j;
    char ch;

    if(strsz < 0 || resultsz < 0)
        return -1;

    for(i = 0, j = 0; i < strsz && j < resultsz; i++)
    {
        ch = *(str + i);
        if((ch >= 'A' && ch <= 'Z') ||
                (ch >= 'a' && ch <= 'z') ||
                (ch >= '0' && ch <= '9') ||
                ch == '.' || ch == '-' || ch == '*' || ch == '_')
            result[j++] = ch;
        else if(ch == ' ')
            result[j++] = '+';
        else
        {
            if(j + 3 <= resultsz)
            {
                result[j++] = '%';
                result[j++] = hexchars[(unsigned char)ch >> 4];
                result[j++] = hexchars[(unsigned char)ch & 0xF];
            }
            else
            {
                return -2;
            }
        }
    }

    if(i == 0)
        return 0;
    else if(i == strsz)
        return j;
    return -2;
}
// return < 0: represent failure

int main(int argc, char *argv[])

{
    int fd = -1;
    char buf[1024], result[1024 * 3];
    int ret;
    int i = 0;

    if(argc != 2)
    {
        printf("please input the encoding filename\n");
        return -1;
    }

    if((fd = open(argv[1], O_RDONLY)) == -1)
    {
        printf("open file %s failure\n", argv[1]);
        return -2;
    }

    while((ret = read(fd, buf, 1024)) >= 0)
    {
        if(ret == 0)
            break;

        ret = URLEncode(buf, ret, result, 1024 * 3);
        if(ret < 0)
            break;

        for(i = 0; i < ret; i++)
            printf("%c", result[i]);

    }

    if(ret < 0)
    {
        printf("encode data failure\n");
    }

    close(fd);
    return ret;
}

urldecode解码

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赤龙绕月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值