2012-08-26 17:26:17 +02:00
|
|
|
/*
|
|
|
|
|
2014-02-23 20:12:25 +01:00
|
|
|
Copyright (c) 2012-2014, Arvid Norberg
|
2012-08-26 17:26:17 +02:00
|
|
|
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"
|
2012-08-27 07:39:34 +02:00
|
|
|
#include "libtorrent/random.hpp"
|
2012-08-26 17:26:17 +02:00
|
|
|
|
2015-03-15 05:25:54 +01:00
|
|
|
#include <cstdlib> // for malloc
|
|
|
|
#include <cstring> // for memmov/strcpy/strlen
|
2012-08-26 17:26:17 +02:00
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2015-03-15 00:10:20 +01:00
|
|
|
// lexical_cast's result depends on the locale. We need
|
|
|
|
// a well defined result
|
|
|
|
boost::array<char, 4 + std::numeric_limits<boost::int64_t>::digits10>
|
|
|
|
to_string(boost::int64_t n)
|
|
|
|
{
|
|
|
|
boost::array<char, 4 + std::numeric_limits<boost::int64_t>::digits10> ret;
|
|
|
|
char *p = &ret.back();
|
|
|
|
*p = '\0';
|
|
|
|
boost::uint64_t un = n;
|
|
|
|
if (n < 0) un = -un; // TODO: warning C4146: unary minus operator applied to unsigned type, result still unsigned
|
|
|
|
do {
|
|
|
|
*--p = '0' + un % 10;
|
|
|
|
un /= 10;
|
|
|
|
} while (un);
|
|
|
|
if (n < 0) *--p = '-';
|
|
|
|
std::memmove(&ret[0], p, &ret.back() - p + 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-08-26 17:26:17 +02:00
|
|
|
bool is_alpha(char c)
|
|
|
|
{
|
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_print(char c)
|
|
|
|
{
|
|
|
|
return c >= 32 && c < 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_space(char c)
|
|
|
|
{
|
2015-03-14 01:42:27 +01:00
|
|
|
static const char* ws = " \t\n\r\f\v";
|
2012-08-26 17:26:17 +02:00
|
|
|
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;
|
2015-03-15 05:25:54 +01:00
|
|
|
char* tmp = (char*)std::malloc(std::strlen(str) + 1);
|
2012-08-26 17:26:17 +02:00
|
|
|
if (tmp == 0) return 0;
|
2015-03-15 05:25:54 +01:00
|
|
|
std::strcpy(tmp, str);
|
2012-08-26 17:26:17 +02:00
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2013-01-28 04:47:36 +01:00
|
|
|
// 8-byte align pointer
|
|
|
|
void* align_pointer(void* p)
|
|
|
|
{
|
|
|
|
int offset = uintptr_t(p) & 0x7;
|
|
|
|
// if we're already aligned, don't do anything
|
|
|
|
if (offset == 0) return p;
|
|
|
|
|
|
|
|
// offset is how far passed the last aligned address
|
|
|
|
// we are. We need to go forward to the next aligned
|
|
|
|
// one. Since aligned addresses are 8 bytes apart, add
|
|
|
|
// 8 - offset.
|
|
|
|
return static_cast<char*>(p) + (8 - offset);
|
|
|
|
}
|
2014-05-05 00:40:30 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// this parses the string that's used as the liste_interfaces setting.
|
|
|
|
// it is a comma-separated list of IP or device names with ports. For
|
|
|
|
// example: "eth0:6881,eth1:6881" or "127.0.0.1:6881"
|
|
|
|
void parse_comma_separated_string_port(std::string const& in
|
|
|
|
, std::vector<std::pair<std::string, int> >& out)
|
|
|
|
{
|
|
|
|
out.clear();
|
|
|
|
|
|
|
|
std::string::size_type start = 0;
|
|
|
|
std::string::size_type end = 0;
|
|
|
|
|
|
|
|
while (start < in.size())
|
|
|
|
{
|
|
|
|
// skip leading spaces
|
|
|
|
while (start < in.size()
|
|
|
|
&& is_space(in[start]))
|
|
|
|
++start;
|
|
|
|
|
|
|
|
end = in.find_first_of(',', start);
|
|
|
|
if (end == std::string::npos) end = in.size();
|
|
|
|
|
|
|
|
std::string::size_type colon = in.find_last_of(':', end);
|
|
|
|
|
|
|
|
if (colon != std::string::npos && colon > start)
|
|
|
|
{
|
|
|
|
int port = atoi(in.substr(colon + 1, end - colon - 1).c_str());
|
|
|
|
|
|
|
|
// skip trailing spaces
|
|
|
|
std::string::size_type soft_end = colon;
|
|
|
|
while (soft_end > start
|
|
|
|
&& is_space(in[soft_end-1]))
|
|
|
|
--soft_end;
|
|
|
|
|
|
|
|
// in case this is an IPv6 address, strip off the square brackets
|
|
|
|
// to make it more easily parseable into an ip::address
|
|
|
|
if (in[start] == '[') ++start;
|
|
|
|
if (soft_end > start && in[soft_end-1] == ']') --soft_end;
|
|
|
|
|
|
|
|
out.push_back(std::make_pair(in.substr(start, soft_end - start), port));
|
|
|
|
}
|
|
|
|
|
|
|
|
start = end + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_comma_separated_string(std::string const& in, std::vector<std::string>& out)
|
|
|
|
{
|
|
|
|
out.clear();
|
|
|
|
|
|
|
|
std::string::size_type start = 0;
|
|
|
|
std::string::size_type end = 0;
|
|
|
|
|
|
|
|
while (start < in.size())
|
|
|
|
{
|
|
|
|
// skip leading spaces
|
|
|
|
while (start < in.size()
|
|
|
|
&& is_space(in[start]))
|
|
|
|
++start;
|
|
|
|
|
|
|
|
end = in.find_first_of(',', start);
|
|
|
|
if (end == std::string::npos) end = in.size();
|
|
|
|
|
|
|
|
// skip trailing spaces
|
|
|
|
std::string::size_type soft_end = end;
|
|
|
|
while (soft_end > start
|
|
|
|
&& is_space(in[soft_end-1]))
|
|
|
|
--soft_end;
|
|
|
|
|
|
|
|
out.push_back(in.substr(start, soft_end - start));
|
|
|
|
start = end + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 00:40:30 +02:00
|
|
|
char* string_tokenize(char* last, char sep, char** next)
|
|
|
|
{
|
|
|
|
if (last == 0) return 0;
|
|
|
|
if (last[0] == '"')
|
|
|
|
{
|
|
|
|
*next = strchr(last + 1, '"');
|
|
|
|
// consume the actual separator as well.
|
|
|
|
if (*next != NULL)
|
|
|
|
*next = strchr(*next, sep);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*next = strchr(last, sep);
|
|
|
|
}
|
|
|
|
if (*next == 0) return last;
|
|
|
|
**next = 0;
|
|
|
|
++(*next);
|
|
|
|
while (**next == sep && **next) ++(*next);
|
|
|
|
return last;
|
|
|
|
}
|
|
|
|
|
2012-08-26 17:26:17 +02:00
|
|
|
}
|
|
|
|
|