Commit 71dd7a96 authored by Sergey Lyubka's avatar Sergey Lyubka

mg_url_decode() moved to string.c

parent 886dcb3f
...@@ -259,31 +259,6 @@ int mg_write(struct mg_connection *conn, const void *buf, int len) { ...@@ -259,31 +259,6 @@ int mg_write(struct mg_connection *conn, const void *buf, int len) {
(int64_t) len); (int64_t) len);
} }
int mg_url_decode(const char *src, int src_len, char *dst,
int dst_len, int is_form_url_encoded) {
int i, j, a, b;
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
if (src[i] == '%' && i < src_len - 2 &&
isxdigit(* (const unsigned char *) (src + i + 1)) &&
isxdigit(* (const unsigned char *) (src + i + 2))) {
a = tolower(* (const unsigned char *) (src + i + 1));
b = tolower(* (const unsigned char *) (src + i + 2));
dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b));
i += 2;
} else if (is_form_url_encoded && src[i] == '+') {
dst[j] = ' ';
} else {
dst[j] = src[i];
}
}
dst[j] = '\0'; // Null-terminate the destination
return i >= src_len ? j : -1;
}
int mg_get_var(const char *data, size_t data_len, const char *name, int mg_get_var(const char *data, size_t data_len, const char *name,
char *dst, size_t dst_len) { char *dst, size_t dst_len) {
const char *p, *e, *s; const char *p, *e, *s;
......
...@@ -286,6 +286,31 @@ void mg_url_encode(const char *src, char *dst, size_t dst_len) { ...@@ -286,6 +286,31 @@ void mg_url_encode(const char *src, char *dst, size_t dst_len) {
*dst = '\0'; *dst = '\0';
} }
int mg_url_decode(const char *src, int src_len, char *dst,
int dst_len, int is_form_url_encoded) {
int i, j, a, b;
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
if (src[i] == '%' && i < src_len - 2 &&
isxdigit(* (const unsigned char *) (src + i + 1)) &&
isxdigit(* (const unsigned char *) (src + i + 2))) {
a = tolower(* (const unsigned char *) (src + i + 1));
b = tolower(* (const unsigned char *) (src + i + 2));
dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b));
i += 2;
} else if (is_form_url_encoded && src[i] == '+') {
dst[j] = ' ';
} else {
dst[j] = src[i];
}
}
dst[j] = '\0'; // Null-terminate the destination
return i >= src_len ? j : -1;
}
// Check whether full request is buffered. Return: // Check whether full request is buffered. Return:
// -1 if request is malformed // -1 if request is malformed
// 0 if request is not yet fully buffered // 0 if request is not yet fully buffered
......
...@@ -766,6 +766,31 @@ void mg_url_encode(const char *src, char *dst, size_t dst_len) { ...@@ -766,6 +766,31 @@ void mg_url_encode(const char *src, char *dst, size_t dst_len) {
*dst = '\0'; *dst = '\0';
} }
int mg_url_decode(const char *src, int src_len, char *dst,
int dst_len, int is_form_url_encoded) {
int i, j, a, b;
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
if (src[i] == '%' && i < src_len - 2 &&
isxdigit(* (const unsigned char *) (src + i + 1)) &&
isxdigit(* (const unsigned char *) (src + i + 2))) {
a = tolower(* (const unsigned char *) (src + i + 1));
b = tolower(* (const unsigned char *) (src + i + 2));
dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b));
i += 2;
} else if (is_form_url_encoded && src[i] == '+') {
dst[j] = ' ';
} else {
dst[j] = src[i];
}
}
dst[j] = '\0'; // Null-terminate the destination
return i >= src_len ? j : -1;
}
// Check whether full request is buffered. Return: // Check whether full request is buffered. Return:
// -1 if request is malformed // -1 if request is malformed
// 0 if request is not yet fully buffered // 0 if request is not yet fully buffered
...@@ -2828,31 +2853,6 @@ int mg_write(struct mg_connection *conn, const void *buf, int len) { ...@@ -2828,31 +2853,6 @@ int mg_write(struct mg_connection *conn, const void *buf, int len) {
(int64_t) len); (int64_t) len);
} }
int mg_url_decode(const char *src, int src_len, char *dst,
int dst_len, int is_form_url_encoded) {
int i, j, a, b;
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) {
if (src[i] == '%' && i < src_len - 2 &&
isxdigit(* (const unsigned char *) (src + i + 1)) &&
isxdigit(* (const unsigned char *) (src + i + 2))) {
a = tolower(* (const unsigned char *) (src + i + 1));
b = tolower(* (const unsigned char *) (src + i + 2));
dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b));
i += 2;
} else if (is_form_url_encoded && src[i] == '+') {
dst[j] = ' ';
} else {
dst[j] = src[i];
}
}
dst[j] = '\0'; // Null-terminate the destination
return i >= src_len ? j : -1;
}
int mg_get_var(const char *data, size_t data_len, const char *name, int mg_get_var(const char *data, size_t data_len, const char *name,
char *dst, size_t dst_len) { char *dst, size_t dst_len) {
const char *p, *e, *s; const char *p, *e, *s;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment