linux url解码工具,Linux C语言实现urlencode和urldecode
本文主要记录一下urlencode和urldecode的C语言实现,作为一个简易工具使用。
1. urlencode编码的基本规则
URL编码做了如下操作:
字符"a"-"z","A"-"Z","0"-"9",".","-","*",和"_" 都不被编码,维持原值;
空格" "被转换为加号"+"。
其他每个字节都被表示成"%XY"格式的由3个字符组成的字符串,编码为UTF-8(特别需要注意: 这里是大写形式的hexchar)。
2. urlencode编码
#include
#include
#include
#include
#include
#include
#include
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
{
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
printf("%c",result[i]);
}
if(ret < 0)
{
printf("encode data failure\n");
}
close(fd);
return ret;
}
3. urldecode解码
#include
#include
#include
#include
#include
#include
#include
static unsigned char hexchars[] = "0123456789ABCDEF";
/**
* @brief URLDecode : decode the urlencoded str to base64 encoded string
*
* @param str: the urlencoded 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 decoded result length
* <0 encode failure
*
* Note:
* 1) to ensure the result buffer has enough space to contain the decoded string, we'd better
* to set resultsz to strsz
*
*/
int URLDecode(const char *str, const int strsz, char *result, const int resultsz, const char **last_pos)
{
int i,j;
char ch;
char a;
*last_pos = str;
if(strsz < 0 || resultsz < 0)
return -1;
for(i = 0,j = 0;i
{
ch = *(str + i);
if(ch == '+')
{
result[j] = ' ';
i += 1;
}
else if(ch == '%')
{
if(i+3 <= strsz)
{
ch = *(str + i + 1);
if(ch >= 'A' && ch <= 'F')
{
a = (ch - 'A')+10;
}
else if(ch >= '0' && ch <= '9')
{
a = ch - '0';
}
else if(ch >= 'a' && ch <= 'f')
{
a = (ch - 'a') + 10;
}
else{
return -2;
}
a <<= 4;
ch = *(str + i + 2);
if(ch >= 'A' && ch <= 'F')
{
a |= (ch - 'A') + 10;
}
else if(ch >= '0' && ch <= '9')
{
a |= (ch - '0');
}
else if(ch >= 'a' && ch <= 'f')
{
a |= (ch - 'a') + 10;
}
else{
return -2;
}
result[j] = a;
i += 3;
}
else
break;
}
else if((ch >= 'A' && ch <= 'Z') ||
(ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9') ||
ch == '.' || ch == '-' || ch == '*' || ch == '_'){
result[j] = ch;
i+=1;
}
else{
return -2;
}
}
*last_pos = str + i;
return j;
}
// return < 0: represent failure
int main(int argc,char *argv[])
{
int fd = -1;
char buf[4096],result[4096];
char *start_pos;
const char * last_pos;
int ret,sz;
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;
}
start_pos = buf;
last_pos = NULL;
while((ret = read(fd,start_pos,buf + 4096 - start_pos)) >= 0)
{
if(ret == 0)
{
if(start_pos == buf)
break;
else
{
ret = -3;
break;
}
}
sz = URLDecode(buf,start_pos - buf + ret,result,4096,&last_pos);
if(sz < 0)
{
ret = -4;
break;
}
if(last_pos != start_pos + ret)
{
memcpy(buf,last_pos,start_pos + ret - last_pos);
start_pos = buf + (start_pos + ret - last_pos);
}
else{
start_pos = buf;
}
for(i = 0;i
printf("%c",result[i]);
}
if(ret < 0)
{
printf("decode data failure\n");
}
close(fd);
return ret;
}
4. 说明
值得指出的是,实际上在对URL进行urlencode的时候(例如: http://127.0.0.1:8000/file/测试/只用于测试.txt?username=小明),不能简单的调用上面的URLEncode()函数,否则可能把:、/这样的字符也进行编码,而实际上
一个对如http://这样的部分是不需要进行修正的。
5. Go语言中的一个urlencode
package main
import (
"fmt"
"net/url"
)
func main(){
urlStr := "http://127.0.0.1:17480/userDownload/F6678E6FD4054150BA37521FBA8A67A6/tsp_test_file/批量上传走joss文件 -003-KZyxg.docx?certification=v1645f22bf4084cc7cf38092cd1b52ef6e3e"
urlObj, err := url.Parse(urlStr)
if err != nil{
fmt.Printf("part url failure: %s\n", err.Error())
return
}
fmt.Println(urlObj.String())
}
编译运行:
http://127.0.0.1:17480/userDownload/F6678E6FD4054150BA37521FBA8A67A6/tsp_test_file/%E6%89%B9%E9%87%8F%E4%B8%8A%E4%BC%A0%E8%B5%B0joss%E6%96%87%E4%BB%B6%20-003-KZyxg.docx?certification=v1645f22bf4084cc7cf38092cd1b52ef6e3e
[参看]:
linux url解码工具,Linux C语言实现urlencode和urldecode相关推荐
- Linux漏洞建议工具Linux Exploit Suggester
Linux漏洞建议工具Linux Exploit Suggester 在Linux系统渗透测试中,通常使用Nessus.OpenVAS对目标主机进行扫描,获取目标主机可能存在的漏洞.如果无法进行漏洞 ...
- linux中top工具,Linux命令工具 top详解
Linux命令工具 top详解 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.top是一个动态显示过程,即可以通过用户按键来不 ...
- linux的locate工具,linux文本查找工具之locate、find
linux文本查找工具之locate.find 一.文件查找分为两类: 1.非实时查找:locate 2.实时查找:find 二.非实时查找:locate 非实时查找:查找速度快.非精准查找.模糊查找 ...
- linux url解码,js对url进行编码和解码(三种方式区别)
*** 只有 0-9[a-Z] $ - _ . + ! * ' ( ) , 以及某些保留字,才能不经过编码直接用于 URL. ***例如:搜索的中文关键字,复制网址之后再粘贴就会发现该URL已经被转码 ...
- linux x下载工具,Linux下强大的Axel下载工具
Linux下经常用的下载软件有wget,wget是单线程下载,断点不能续传,Axel工具是linux下的http/ftp中强大下载工具,支持多线程下载和断点续下. 这里介绍Alex的安装.和简单使用. ...
- linux收发十六进制工具,linux下的十六进制编辑器---wxHexEdit
....其实wxHexEdit是一个跨平台的十六进制编辑器,支持windows,linux,mac. 之所以标题用linux...是因为windows下多数都用winhex,UE之类的编辑器,而lin ...
- Linux命令辅助工具,linux命令快查小助手
linux命令快查小助手是一款使用易语言编写的简单Linux系统快捷键查询工具,支持中文模糊查询,可右键复制命令,查看命令详情,对于使用linux系统的朋友来说非常实用!有需要的小伙伴欢迎来西西下载体 ...
- linux 3d开发工具,Linux 3D 编程介绍 (转)
最近用SDL,glut,g++做了一下Linux下的3D编程,有一点心得,想与大家分享一下. 我没有自己从零开始,先D了quake2和quake3的一些代码,关于MD2,MD3和bsp的,然后用D的l ...
- linux网页设计工具,linux ubuntu 网页设计 网页制作软件工具
0. Bluefish 是一个基于Gtk的HTML的编辑器,它支持语法加亮,支 持HTML.CSS.JAVASCRIPT.Java server pages (JSP).Python.Perl.SSI ...
最新文章
- eclipse在网页进入时显示重定向过多_使用eclipse快速开发jsp以及编码问题、jsp页面元素、request对象学习的粗略记录...
- Xcode(7.0以上版本)真机调试
- 【学习笔记】Python - PyQt
- 【转】SAP的标准委外采购中都有哪些坑
- HDU - 3126 Nova(最大流+二分+简单几何)
- 【Ubuntu-Tensorflow】InvalidArgumentError GPU不能使用的问题
- [渝粤教育] 平顶山学院 传播理论与技巧 参考 资料
- Spring Boot之自定义属性
- php堆栈溢出,php - 警告:php_eval() - 堆栈内存溢出
- 网络安全系列之二十五 配置SSH
- 【JQuery】数据
- NLP学习02--卷积神经网络CNN
- OpenCV学习——图像二值化处理及二维傅里叶变换
- ros launch中的节点工作空间路径
- StatsD,collected,fluentd和其他守护程序
- [转]浅析360的危害 我为什么推荐卸载360
- 色彩心理学:为什么快餐店不适合等人?
- Python列表、元组、字典 集合简单基础
- 读一本好书,享一段时光
- 机器人运动控制算法专栏介绍
热门文章
- 随心所欲的醇香,Barsetto百胜图TripressoES意式便携咖啡机测评
- 干货 | 工程师必收藏的电路图符号大全,人手一份!
- 如何求cosx的二分之三次方的定积分
- 2022圣诞代码合集(圣诞树+圣诞老人)
- 计算机作业批改反思,作业批改有效性的反思
- 学生拍照上传作业老师在线批改html源码,作业1 作业提交与批改系统 HTML界面1308190102郑玉梅...
- 基于Python的ZED2教程 3.深度感知
- 服务治理在猫眼的演进之路-Service Mesh
- linux 6不能自动挂载u盘,Linux CentOS 6.7 挂载U盘
- 火狐浏览器图片不能显示的问题