diff --git a/Makefile b/Makefile index 1467ca9c..cf14f5c4 100644 --- a/Makefile +++ b/Makefile @@ -163,6 +163,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_timer.c \ src/httplib_tls_dtor.c \ src/httplib_uninitialize_ssl.c \ + src/httplib_url_decode.c \ src/httplib_url_encode.c \ src/httplib_version.c \ src/httplib_websocket_client_thread.c \ diff --git a/src/httplib_url_decode.c b/src/httplib_url_decode.c new file mode 100644 index 00000000..98879354 --- /dev/null +++ b/src/httplib_url_decode.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2016 Lammert Bies + * Copyright (c) 2013-2016 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + + +#include "libhttp-private.h" + + + +int mg_url_decode( const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded ) { + + int i; + int j; + int a; + int b; +#define HEXTOI(x) (isdigit(x) ? (x - '0') : (x - 'W')) + + for (i = j = 0; (i < src_len) && (j < (dst_len - 1)); i++, j++) { + if (i < src_len - 2 && src[i] == '%' + && 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; + +} /* mg_url_decode */ diff --git a/src/libhttp.c b/src/libhttp.c index 25ff5869..193b65c0 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -3165,10 +3165,11 @@ int XX_httplib_vprintf( struct mg_connection *conn, const char *fmt, va_list ap if (buf != mem && buf != NULL) XX_httplib_free(buf); return len; + } /* XX_httplib_vprintf */ -int mg_printf(struct mg_connection *conn, const char *fmt, ...) { +int mg_printf( struct mg_connection *conn, const char *fmt, ... ) { va_list ap; int result; @@ -3178,31 +3179,5 @@ int mg_printf(struct mg_connection *conn, const char *fmt, ...) { va_end(ap); return result; -} - -int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded) { - - int i; - int j; - int a; - int b; -#define HEXTOI(x) (isdigit(x) ? (x - '0') : (x - 'W')) - - for (i = j = 0; (i < src_len) && (j < (dst_len - 1)); i++, j++) { - if (i < src_len - 2 && src[i] == '%' - && 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; -} +} /* mg_printf */