Skip to content

Instantly share code, notes, and snippets.

@VictorZhang2014
Created November 20, 2016 11:33
Show Gist options
  • Save VictorZhang2014/36a095b45c721103b5872b66bc8a91dc to your computer and use it in GitHub Desktop.
Save VictorZhang2014/36a095b45c721103b5872b66bc8a91dc to your computer and use it in GitHub Desktop.
C URLEncode/Decode
/**
* @brief URLEncode 对字符串URL编码
*
* @param str 原字符串
* @param strSize 原字符串长度(不包括最后的\0)
* @param result 结果缓冲区的地址
* @param resultSize 结果缓冲区的大小(包括最后的\0)
*
* @return: >0:resultstring 里实际有效的长度
* 0: 解码失败.
*/
int URLEncode(const char* str, const int strSize, char* result, const int resultSize);
/**
* @brief URLEncode 对字符串URL编码
*
* @param str 原字符串
* @param strSize 原字符串长度(不包括最后的\0)
* @param result 结果缓冲区的地址
* @param resultSize 结果缓冲区的大小(包括最后的\0)
*
* @return: >0:resultstring 里实际有效的长度
* 0: 解码失败.
*/
int URLEncode2(const char* str, const int strSize, char* result, const int resultSize);
char *urlencode3(char *str);
int URLEncode(const char* str, const int strSize, char* result, const int resultSize)
{
int i;
int j = 0;//for result index
char ch;
if ((str==NULL) || (result==NULL) || (strSize<=0) || (resultSize<=0)) {
return 0;
}
for ( i=0; (i<strSize)&&(j<resultSize); ++i) {
ch = str[i];
if (((ch>='A') && (ch<'Z')) ||
((ch>='a') && (ch<'z')) ||
((ch>='0') && (ch<'9'))) {
result[j++] = ch;
} else if (ch == ' ') {
result[j++] = '+';
} else if (ch == '.' || ch == '-' || ch == '_' || ch == '*') {
result[j++] = ch;
} else {
if (j+3 < resultSize) {
sprintf(result+j, "%%%02X", (unsigned char)ch);
j += 3;
} else {
return 0;
}
}
}
result[j] = '\0';
return j;
}
/**
* @brief URLEncode 对字符串URL编码
*
* @param str 原字符串
* @param strSize 原字符串长度(不包括最后的\0)
* @param result 结果缓冲区的地址
* @param resultSize 结果缓冲区的大小(包括最后的\0)
*
* @return: >0:resultstring 里实际有效的长度
* 0: 解码失败.
*/
int URLEncode2(const char* str, const int strSize, char* result, const int resultSize)
{
int i;
int j = 0;//for result index
char ch;
if ((str==NULL) || (result==NULL) || (strSize<=0) || (resultSize<=0)) {
return 0;
}
for ( i=0; (i<strSize)&&(j<resultSize); ++i) {
ch = str[i];
if (((ch>='A') && (ch<'Z')) ||
((ch>='a') && (ch<'z')) ||
((ch>='0') && (ch<'9'))) {
result[j++] = ch;
} else if (ch == ' ') {
result[j++] = '+';
} else if (ch == '.' || ch == '-' || ch == '_' || ch == '*') {
result[j++] = ch;
} else {
if (j+3 < resultSize) {
sprintf(result+j, "%%%02X", (unsigned char)ch);
j += 3;
} else {
return 0;
}
}
}
result[j] = '\0';
return j;
}
/* Converts a hex character to its integer value */
char from_hex(char ch) {
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}
/* Converts an integer value to its hex character*/
char to_hex(char code) {
static char hex[] = "0123456789abcdef";
return hex[code & 15];
}
/* Returns a url-encoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
char *urlencode3(char *str) {
char *pstr = str, *buf = (char *)malloc(strlen(str) * 3 + 1), *pbuf = buf;
while (*pstr) {
if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~')
*pbuf++ = *pstr;
else if (*pstr == ' ')
*pbuf++ = '+';
else
*pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
pstr++;
}
*pbuf = '\0';
return buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment