From fdc25967c29e90043ee9e833a4a8f898306f5d5e Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 26 Aug 2012 15:26:17 +0000 Subject: [PATCH] merged string function cleanup from RC_0_16 --- CMakeLists.txt | 1 + Jamfile | 1 + include/libtorrent/Makefile.am | 1 + include/libtorrent/config.hpp | 18 ---- include/libtorrent/error_code.hpp | 4 +- include/libtorrent/escape_string.hpp | 11 --- include/libtorrent/policy.hpp | 3 +- include/libtorrent/string_util.hpp | 58 ++++++++++++ src/Makefile.am | 1 + src/escape_string.cpp | 81 ---------------- src/file_storage.cpp | 3 +- src/identify_client.cpp | 2 +- src/string_util.cpp | 133 +++++++++++++++++++++++++++ src/torrent.cpp | 3 +- src/udp_socket.cpp | 4 +- 15 files changed, 206 insertions(+), 118 deletions(-) create mode 100644 include/libtorrent/string_util.hpp create mode 100644 src/string_util.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 110bfc60a..a391232f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ set(sources file_storage lazy_bdecode escape_string + string_util file gzip http_connection diff --git a/Jamfile b/Jamfile index 295eb85c5..209f81997 100755 --- a/Jamfile +++ b/Jamfile @@ -479,6 +479,7 @@ SOURCES = file_storage lazy_bdecode escape_string + string_util file gzip http_connection diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index fea92be1c..2916acf77 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -99,6 +99,7 @@ nobase_include_HEADERS = \ stat.hpp \ storage.hpp \ storage_defs.hpp \ + string_util.hpp \ struct_debug.hpp \ thread.hpp \ time.hpp \ diff --git a/include/libtorrent/config.hpp b/include/libtorrent/config.hpp index 410220c10..9c95e2a71 100644 --- a/include/libtorrent/config.hpp +++ b/include/libtorrent/config.hpp @@ -144,9 +144,6 @@ POSSIBILITY OF SUCH DAMAGE. #pragma warning(disable:4251) // '_vsnprintf': This function or variable may be unsafe #pragma warning(disable:4996) -// 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup -#pragma warning(disable: 4996) -#define strdup _strdup #define TORRENT_DEPRECATED_PREFIX __declspec(deprecated) @@ -445,10 +442,6 @@ inline int snprintf(char* buf, int len, char const* fmt, ...) #define TORRENT_USE_I2P 1 #endif -#ifndef TORRENT_HAS_STRDUP -#define TORRENT_HAS_STRDUP 1 -#endif - #if !defined TORRENT_IOV_MAX #ifdef IOV_MAX #define TORRENT_IOV_MAX IOV_MAX @@ -502,17 +495,6 @@ inline int snprintf(char* buf, int len, char const* fmt, ...) #endif -#if !TORRENT_HAS_STRDUP -inline char* strdup(char const* str) -{ - if (str == 0) return 0; - char* tmp = (char*)malloc(strlen(str) + 1); - if (tmp == 0) return 0; - strcpy(tmp, str); - return tmp; -} -#endif - // for non-exception builds #ifdef BOOST_NO_EXCEPTIONS #define TORRENT_TRY if (true) diff --git a/include/libtorrent/error_code.hpp b/include/libtorrent/error_code.hpp index c890128b6..f4b676bb9 100644 --- a/include/libtorrent/error_code.hpp +++ b/include/libtorrent/error_code.hpp @@ -47,7 +47,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #endif -#include // strdup +#include "libtorrent/string_util.hpp" // for allocate_string_copy #include // free namespace libtorrent @@ -383,7 +383,7 @@ namespace libtorrent if (!m_msg) { std::string msg = m_error.message(); - m_msg = strdup(msg.c_str()); + m_msg = allocate_string_copy(msg.c_str()); } return m_msg; diff --git a/include/libtorrent/escape_string.hpp b/include/libtorrent/escape_string.hpp index 4d2863884..20e7f07b9 100644 --- a/include/libtorrent/escape_string.hpp +++ b/include/libtorrent/escape_string.hpp @@ -43,17 +43,6 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { TORRENT_EXTRA_EXPORT boost::array::digits10> to_string(size_type n); - TORRENT_EXTRA_EXPORT bool is_alpha(char c); - TORRENT_EXPORT bool is_digit(char c); - TORRENT_EXTRA_EXPORT bool is_print(char c); - TORRENT_EXTRA_EXPORT bool is_space(char c); - TORRENT_EXTRA_EXPORT char to_lower(char c); - - TORRENT_EXTRA_EXPORT int split_string(char const** tags, int buf_size, char* in); - TORRENT_EXTRA_EXPORT bool string_begins_no_case(char const* s1, char const* s2); - TORRENT_EXTRA_EXPORT bool string_equal_no_case(char const* s1, char const* s2); - - TORRENT_EXTRA_EXPORT void url_random(char* begin, char* end); TORRENT_EXTRA_EXPORT std::string unescape_string(std::string const& s, error_code& ec); // replaces all disallowed URL characters by their %-encoding diff --git a/include/libtorrent/policy.hpp b/include/libtorrent/policy.hpp index f2243f65a..18c386e1d 100644 --- a/include/libtorrent/policy.hpp +++ b/include/libtorrent/policy.hpp @@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include "libtorrent/string_util.hpp" // for allocate_string_copy #include "libtorrent/peer.hpp" #include "libtorrent/piece_picker.hpp" @@ -487,7 +488,7 @@ namespace libtorrent #if TORRENT_USE_I2P inline policy::i2p_peer::i2p_peer(char const* dest, bool connectable, int src) - : peer(0, connectable, src), destination(strdup(dest)) + : peer(0, connectable, src), destination(allocate_string_copy(dest)) { #if TORRENT_USE_IPV6 is_v6_addr = false; diff --git a/include/libtorrent/string_util.hpp b/include/libtorrent/string_util.hpp new file mode 100644 index 000000000..79feefb5d --- /dev/null +++ b/include/libtorrent/string_util.hpp @@ -0,0 +1,58 @@ +/* + +Copyright (c) 2012, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef TORRENT_STRING_UTIL_HPP_INCLUDED +#define TORRENT_STRING_UTIL_HPP_INCLUDED + +namespace libtorrent +{ + TORRENT_EXTRA_EXPORT bool is_alpha(char c); + TORRENT_EXPORT bool is_digit(char c); + TORRENT_EXTRA_EXPORT bool is_print(char c); + TORRENT_EXTRA_EXPORT bool is_space(char c); + TORRENT_EXTRA_EXPORT char to_lower(char c); + + TORRENT_EXTRA_EXPORT int split_string(char const** tags, int buf_size, char* in); + TORRENT_EXTRA_EXPORT bool string_begins_no_case(char const* s1, char const* s2); + TORRENT_EXTRA_EXPORT bool string_equal_no_case(char const* s1, char const* s2); + + TORRENT_EXTRA_EXPORT void url_random(char* begin, char* end); + + // strdup is not part of the C standard. Some systems + // don't have it and it won't be available when building + // in strict ansi mode + char* allocate_string_copy(char const* str); + +} + +#endif + diff --git a/src/Makefile.am b/src/Makefile.am index a2b3fbf72..f68ba2634 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -84,6 +84,7 @@ libtorrent_rasterbar_la_SOURCES = \ socks5_stream.cpp \ stat.cpp \ storage.cpp \ + string_util.cpp \ thread.cpp \ torrent.cpp \ torrent_handle.cpp \ diff --git a/src/escape_string.cpp b/src/escape_string.cpp index 4ad7ece3e..c46320e73 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -84,87 +84,6 @@ namespace libtorrent return ret; } - bool is_alpha(char c) - { - return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); - } - - bool is_digit(char c) - { - return c >= '0' && c <= '9'; - } - - bool is_print(char c) - { - return c >= 32 && c < 127; - } - - bool is_space(char c) - { - const static char* ws = " \t\n\r\f\v"; - return std::strchr(ws, c) != 0; - } - - // generate a url-safe random string - void url_random(char* begin, char* end) - { - // http-accepted characters: - // excluding ', since some buggy trackers don't support that - static char const printable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz-_.!~*()"; - - // the random number - while (begin != end) - *begin++ = printable[random() % (sizeof(printable)-1)]; - } - - char to_lower(char c) - { - return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; - } - - int split_string(char const** tags, int buf_size, char* in) - { - int ret = 0; - char* i = in; - for (;*i; ++i) - { - if (!is_print(*i) || is_space(*i)) - { - *i = 0; - if (ret == buf_size) return ret; - continue; - } - if (i == in || i[-1] == 0) - { - tags[ret++] = i; - } - } - return ret; - } - - bool string_begins_no_case(char const* s1, char const* s2) - { - while (*s1 != 0) - { - if (to_lower(*s1) != to_lower(*s2)) return false; - ++s1; - ++s2; - } - return true; - } - - bool string_equal_no_case(char const* s1, char const* s2) - { - while (to_lower(*s1) == to_lower(*s2)) - { - if (*s1 == 0) return true; - ++s1; - ++s2; - } - return false; - } - std::string unescape_string(std::string const& s, error_code& ec) { std::string ret; diff --git a/src/file_storage.cpp b/src/file_storage.cpp index b8b0ad857..d492047b6 100644 --- a/src/file_storage.cpp +++ b/src/file_storage.cpp @@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include "libtorrent/file_storage.hpp" +#include "libtorrent/string_util.hpp" // for allocate_string_copy #include "libtorrent/file.hpp" #include "libtorrent/utf8.hpp" #include @@ -147,7 +148,7 @@ namespace libtorrent } else { - name = borrow_chars ? n : strdup(n); + name = borrow_chars ? n : allocate_string_copy(n); } name_len = borrow_chars; } diff --git a/src/identify_client.cpp b/src/identify_client.cpp index 59fc09976..93f1ef09c 100644 --- a/src/identify_client.cpp +++ b/src/identify_client.cpp @@ -48,7 +48,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/identify_client.hpp" #include "libtorrent/fingerprint.hpp" -#include "libtorrent/escape_string.hpp" +#include "libtorrent/string_util.hpp" namespace { diff --git a/src/string_util.cpp b/src/string_util.cpp new file mode 100644 index 000000000..69a91240b --- /dev/null +++ b/src/string_util.cpp @@ -0,0 +1,133 @@ +/* + +Copyright (c) 2012, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#include "libtorrent/config.hpp" +#include "libtorrent/string_util.hpp" + +#include // for malloc/free +#include // for strcpy/strlen + +namespace libtorrent +{ + + bool is_alpha(char c) + { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); + } + + bool is_digit(char c) + { + return c >= '0' && c <= '9'; + } + + bool is_print(char c) + { + return c >= 32 && c < 127; + } + + bool is_space(char c) + { + const static char* ws = " \t\n\r\f\v"; + return strchr(ws, c) != 0; + } + + char to_lower(char c) + { + return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; + } + + int split_string(char const** tags, int buf_size, char* in) + { + int ret = 0; + char* i = in; + for (;*i; ++i) + { + if (!is_print(*i) || is_space(*i)) + { + *i = 0; + if (ret == buf_size) return ret; + continue; + } + if (i == in || i[-1] == 0) + { + tags[ret++] = i; + } + } + return ret; + } + + bool string_begins_no_case(char const* s1, char const* s2) + { + while (*s1 != 0) + { + if (to_lower(*s1) != to_lower(*s2)) return false; + ++s1; + ++s2; + } + return true; + } + + bool string_equal_no_case(char const* s1, char const* s2) + { + while (to_lower(*s1) == to_lower(*s2)) + { + if (*s1 == 0) return true; + ++s1; + ++s2; + } + return false; + } + + // generate a url-safe random string + void url_random(char* begin, char* end) + { + // http-accepted characters: + // excluding ', since some buggy trackers don't support that + static char const printable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz-_.!~*()"; + + // the random number + while (begin != end) + *begin++ = printable[random() % (sizeof(printable)-1)]; + } + + char* allocate_string_copy(char const* str) + { + if (str == 0) return 0; + char* tmp = (char*)malloc(strlen(str) + 1); + if (tmp == 0) return 0; + strcpy(tmp, str); + return tmp; + } + +} + diff --git a/src/torrent.cpp b/src/torrent.cpp index cc65ac8df..5769b9495 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -81,6 +81,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/http_connection.hpp" #include "libtorrent/gzip.hpp" // for inflate_gzip #include "libtorrent/random.hpp" +#include "libtorrent/string_util.hpp" // for allocate_string_copy #ifdef TORRENT_USE_OPENSSL #include "libtorrent/ssl_stream.hpp" @@ -2129,7 +2130,7 @@ namespace libtorrent INVARIANT_CHECK; m_net_interfaces.clear(); - char* str = strdup(net_interfaces.c_str()); + char* str = allocate_string_copy(net_interfaces.c_str()); char* ptr = str; while (ptr) diff --git a/src/udp_socket.cpp b/src/udp_socket.cpp index 1c440e1a5..14b94b66f 100644 --- a/src/udp_socket.cpp +++ b/src/udp_socket.cpp @@ -34,9 +34,9 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket.hpp" #include "libtorrent/udp_socket.hpp" #include "libtorrent/connection_queue.hpp" -#include "libtorrent/escape_string.hpp" #include "libtorrent/socket_io.hpp" #include "libtorrent/error.hpp" +#include "libtorrent/string_util.hpp" // for allocate_string_copy #include #include #include @@ -144,7 +144,7 @@ void udp_socket::send_hostname(char const* hostname, int port m_queue.push_back(queued_packet()); queued_packet& qp = m_queue.back(); qp.ep.port(port); - qp.hostname = strdup(hostname); + qp.hostname = allocate_string_copy(hostname); qp.buf.insert(qp.buf.begin(), p, p + len); qp.flags = 0; }