mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-02 08:55:53 +00:00
Split string utilities from utils.h
This commit is contained in:
264
modules/ripple_basics/memory/ripple_StringUtilities.cpp
Normal file
264
modules/ripple_basics/memory/ripple_StringUtilities.cpp
Normal file
@@ -0,0 +1,264 @@
|
||||
|
||||
char charHex(int iDigit)
|
||||
{
|
||||
return iDigit < 10 ? '0' + iDigit : 'A' - 10 + iDigit;
|
||||
}
|
||||
|
||||
int charUnHex(char cDigit)
|
||||
{
|
||||
return cDigit >= '0' && cDigit <= '9'
|
||||
? cDigit - '0'
|
||||
: cDigit >= 'A' && cDigit <= 'F'
|
||||
? cDigit - 'A' + 10
|
||||
: cDigit >= 'a' && cDigit <= 'f'
|
||||
? cDigit - 'a' + 10
|
||||
: -1;
|
||||
}
|
||||
|
||||
int strUnHex(std::string& strDst, const std::string& strSrc)
|
||||
{
|
||||
int iBytes = (strSrc.size()+1)/2;
|
||||
|
||||
strDst.resize(iBytes);
|
||||
|
||||
const char* pSrc = &strSrc[0];
|
||||
char* pDst = &strDst[0];
|
||||
|
||||
if (strSrc.size() & 1)
|
||||
{
|
||||
int c = charUnHex(*pSrc++);
|
||||
|
||||
if (c < 0)
|
||||
{
|
||||
iBytes = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
*pDst++ = c;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i=0; iBytes >= 0 && i != iBytes; i++)
|
||||
{
|
||||
int cHigh = charUnHex(*pSrc++);
|
||||
int cLow = charUnHex(*pSrc++);
|
||||
|
||||
if (cHigh < 0 || cLow < 0)
|
||||
{
|
||||
iBytes = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
strDst[i] = (cHigh << 4) | cLow;
|
||||
}
|
||||
}
|
||||
|
||||
if (iBytes < 0)
|
||||
strDst.clear();
|
||||
|
||||
return iBytes;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> strUnHex(const std::string& strSrc)
|
||||
{
|
||||
std::string strTmp;
|
||||
|
||||
strUnHex(strTmp, strSrc);
|
||||
|
||||
return strCopy(strTmp);
|
||||
}
|
||||
|
||||
uint64_t uintFromHex(const std::string& strSrc)
|
||||
{
|
||||
uint64_t uValue = 0;
|
||||
|
||||
BOOST_FOREACH(char c, strSrc)
|
||||
uValue = (uValue << 4) | charUnHex(c);
|
||||
|
||||
return uValue;
|
||||
}
|
||||
|
||||
//
|
||||
// Misc string
|
||||
//
|
||||
|
||||
std::vector<unsigned char> strCopy(const std::string& strSrc)
|
||||
{
|
||||
std::vector<unsigned char> vucDst;
|
||||
|
||||
vucDst.resize(strSrc.size());
|
||||
|
||||
std::copy(strSrc.begin(), strSrc.end(), vucDst.begin());
|
||||
|
||||
return vucDst;
|
||||
}
|
||||
|
||||
std::string strCopy(const std::vector<unsigned char>& vucSrc)
|
||||
{
|
||||
std::string strDst;
|
||||
|
||||
strDst.resize(vucSrc.size());
|
||||
|
||||
std::copy(vucSrc.begin(), vucSrc.end(), strDst.begin());
|
||||
|
||||
return strDst;
|
||||
|
||||
}
|
||||
|
||||
extern std::string urlEncode(const std::string& strSrc)
|
||||
{
|
||||
std::string strDst;
|
||||
int iOutput = 0;
|
||||
int iSize = strSrc.length();
|
||||
|
||||
strDst.resize(iSize*3);
|
||||
|
||||
for (int iInput = 0; iInput < iSize; iInput++) {
|
||||
unsigned char c = strSrc[iInput];
|
||||
|
||||
if (c == ' ')
|
||||
{
|
||||
strDst[iOutput++] = '+';
|
||||
}
|
||||
else if (isalnum(c))
|
||||
{
|
||||
strDst[iOutput++] = c;
|
||||
}
|
||||
else
|
||||
{
|
||||
strDst[iOutput++] = '%';
|
||||
strDst[iOutput++] = charHex(c >> 4);
|
||||
strDst[iOutput++] = charHex(c & 15);
|
||||
}
|
||||
}
|
||||
|
||||
strDst.resize(iOutput);
|
||||
|
||||
return strDst;
|
||||
}
|
||||
|
||||
//
|
||||
// IP Port parsing
|
||||
//
|
||||
// <-- iPort: "" = -1
|
||||
bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort)
|
||||
{
|
||||
boost::smatch smMatch;
|
||||
bool bValid = false;
|
||||
|
||||
static boost::regex reEndpoint("\\`\\s*(\\S+)(?:\\s+(\\d+))?\\s*\\'");
|
||||
|
||||
if (boost::regex_match(strSource, smMatch, reEndpoint))
|
||||
{
|
||||
boost::system::error_code err;
|
||||
std::string strIPRaw = smMatch[1];
|
||||
std::string strPortRaw = smMatch[2];
|
||||
|
||||
boost::asio::ip::address addrIP = boost::asio::ip::address::from_string(strIPRaw, err);
|
||||
|
||||
bValid = !err;
|
||||
if (bValid)
|
||||
{
|
||||
strIP = addrIP.to_string();
|
||||
iPort = strPortRaw.empty() ? -1 : boost::lexical_cast<int>(strPortRaw);
|
||||
}
|
||||
}
|
||||
|
||||
return bValid;
|
||||
}
|
||||
|
||||
bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, int& iPort, std::string& strPath)
|
||||
{
|
||||
// scheme://username:password@hostname:port/rest
|
||||
static boost::regex reUrl("(?i)\\`\\s*([[:alpha:]][-+.[:alpha:][:digit:]]*)://([^:/]+)(?::(\\d+))?(/.*)?\\s*?\\'");
|
||||
boost::smatch smMatch;
|
||||
|
||||
bool bMatch = boost::regex_match(strUrl, smMatch, reUrl); // Match status code.
|
||||
|
||||
if (bMatch)
|
||||
{
|
||||
std::string strPort;
|
||||
|
||||
strScheme = smMatch[1];
|
||||
strDomain = smMatch[2];
|
||||
strPort = smMatch[3];
|
||||
strPath = smMatch[4];
|
||||
|
||||
boost::algorithm::to_lower(strScheme);
|
||||
|
||||
iPort = strPort.empty() ? -1 : lexical_cast_s<int>(strPort);
|
||||
// std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPort << "' : " << iPort << " : '" << strPath << "'" << std::endl;
|
||||
}
|
||||
// std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPath << "'" << std::endl;
|
||||
|
||||
return bMatch;
|
||||
}
|
||||
|
||||
//
|
||||
// Quality parsing
|
||||
// - integers as is.
|
||||
// - floats multiplied by a billion
|
||||
bool parseQuality(const std::string& strSource, uint32& uQuality)
|
||||
{
|
||||
uQuality = lexical_cast_s<uint32>(strSource);
|
||||
|
||||
if (!uQuality)
|
||||
{
|
||||
float fQuality = lexical_cast_s<float>(strSource);
|
||||
|
||||
if (fQuality)
|
||||
uQuality = (uint32)(QUALITY_ONE*fQuality);
|
||||
}
|
||||
|
||||
return !!uQuality;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE( Utils)
|
||||
|
||||
BOOST_AUTO_TEST_CASE( ParseUrl )
|
||||
{
|
||||
std::string strScheme;
|
||||
std::string strDomain;
|
||||
int iPort;
|
||||
std::string strPath;
|
||||
|
||||
if (!parseUrl("lower://domain", strScheme, strDomain, iPort, strPath))
|
||||
BOOST_FAIL("parseUrl: lower://domain failed");
|
||||
|
||||
if (strScheme != "lower")
|
||||
BOOST_FAIL("parseUrl: lower://domain : scheme failed");
|
||||
|
||||
if (strDomain != "domain")
|
||||
BOOST_FAIL("parseUrl: lower://domain : domain failed");
|
||||
|
||||
if (iPort != -1)
|
||||
BOOST_FAIL("parseUrl: lower://domain : port failed");
|
||||
|
||||
if (strPath != "")
|
||||
BOOST_FAIL("parseUrl: lower://domain : path failed");
|
||||
|
||||
if (!parseUrl("UPPER://domain:234/", strScheme, strDomain, iPort, strPath))
|
||||
BOOST_FAIL("parseUrl: UPPER://domain:234/ failed");
|
||||
|
||||
if (strScheme != "upper")
|
||||
BOOST_FAIL("parseUrl: UPPER://domain:234/ : scheme failed");
|
||||
|
||||
if (iPort != 234)
|
||||
BOOST_FAIL(boost::str(boost::format("parseUrl: UPPER://domain:234/ : port failed: %d") % iPort));
|
||||
|
||||
if (strPath != "/")
|
||||
BOOST_FAIL("parseUrl: UPPER://domain:234/ : path failed");
|
||||
|
||||
if (!parseUrl("Mixed://domain/path", strScheme, strDomain, iPort, strPath))
|
||||
BOOST_FAIL("parseUrl: Mixed://domain/path failed");
|
||||
|
||||
if (strScheme != "mixed")
|
||||
BOOST_FAIL("parseUrl: Mixed://domain/path tolower failed");
|
||||
|
||||
if (strPath != "/path")
|
||||
BOOST_FAIL("parseUrl: Mixed://domain/path path failed");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
// vim:ts=4
|
||||
208
modules/ripple_basics/memory/ripple_StringUtilities.h
Normal file
208
modules/ripple_basics/memory/ripple_StringUtilities.h
Normal file
@@ -0,0 +1,208 @@
|
||||
#ifndef RIPPLE_STRINGUTILITIES_H
|
||||
#define RIPPLE_STRINGUTILITIES_H
|
||||
|
||||
/** String utility functions.
|
||||
*/
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Ripple specific constant used for parsing qualities and other things
|
||||
//
|
||||
#define QUALITY_ONE 1000000000 // 10e9
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Terminal output color codes
|
||||
#define vt_f_black "\033[30m"
|
||||
#define vt_f_red "\033[31m"
|
||||
#define vt_f_green "\033[32m"
|
||||
#define vt_f_yellow "\033[33m"
|
||||
#define vt_f_blue "\033[34m"
|
||||
#define vt_f_megenta "\033[35m"
|
||||
#define vt_f_cyan "\033[36m"
|
||||
#define vt_f_white "\033[37m"
|
||||
#define vt_f_default "\033[39m"
|
||||
|
||||
#define vt_b_black "\033[40m"
|
||||
#define vt_b_red "\033[41m"
|
||||
#define vt_b_green "\033[42m"
|
||||
#define vt_b_yellow "\033[43m"
|
||||
#define vt_b_blue "\033[44m"
|
||||
#define vt_b_megenta "\033[45m"
|
||||
#define vt_b_cyan "\033[46m"
|
||||
#define vt_b_white "\033[47m"
|
||||
#define vt_b_default "\033[49m"
|
||||
|
||||
#define vt_f_bold_black "\033[1m\033[30m"
|
||||
#define vt_f_bold_red "\033[1m\033[31m"
|
||||
#define vt_f_bold_green "\033[1m\033[32m"
|
||||
#define vt_f_bold_yellow "\033[1m\033[33m"
|
||||
#define vt_f_bold_blue "\033[1m\033[34m"
|
||||
#define vt_f_bold_megenta "\033[1m\033[35m"
|
||||
#define vt_f_bold_cyan "\033[1m\033[36m"
|
||||
#define vt_f_bold_white "\033[1m\033[37m"
|
||||
#define vt_f_bold_default "\033[1m\033[39m"
|
||||
|
||||
#define vt_bold "\033[1m"
|
||||
#define vt_dim "\033[2m" // does not work for xterm
|
||||
#define vt_normal "\033[22m" // intensity
|
||||
|
||||
#define vt_n_enable "\033[7m" // negative
|
||||
#define vt_n_disable "\033[27m"
|
||||
|
||||
#define vt_u_single "\033[4m" // underline
|
||||
#define vt_u_double "\033[21m" // does not work for xterm
|
||||
#define vt_u_disable "\033[24m"
|
||||
|
||||
#define vt_reset vt_f_default vt_b_default vt_normal vt_n_disable vt_u_disable
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
extern std::string urlEncode(const std::string& strSrc);
|
||||
|
||||
template<class Iterator>
|
||||
std::string strJoin(Iterator first, Iterator last, std::string strSeperator)
|
||||
{
|
||||
std::ostringstream ossValues;
|
||||
|
||||
for (Iterator start = first; first != last; first++)
|
||||
{
|
||||
ossValues << str(boost::format("%s%s") % (start == first ? "" : strSeperator) % *first);
|
||||
}
|
||||
|
||||
return ossValues.str();
|
||||
}
|
||||
|
||||
char charHex(int iDigit);
|
||||
|
||||
template<class Iterator>
|
||||
std::string strHex(Iterator first, int iSize)
|
||||
{
|
||||
std::string strDst;
|
||||
|
||||
strDst.resize(iSize*2);
|
||||
|
||||
for (int i = 0; i < iSize; i++) {
|
||||
unsigned char c = *first++;
|
||||
|
||||
strDst[i*2] = charHex(c >> 4);
|
||||
strDst[i*2+1] = charHex(c & 15);
|
||||
}
|
||||
|
||||
return strDst;
|
||||
}
|
||||
|
||||
inline const std::string strHex(const std::string& strSrc)
|
||||
{
|
||||
return strHex(strSrc.begin(), strSrc.size());
|
||||
}
|
||||
|
||||
inline std::string strHex(const std::vector<unsigned char>& vucData)
|
||||
{
|
||||
return strHex(vucData.begin(), vucData.size());
|
||||
}
|
||||
|
||||
inline std::string strHex(const uint64 uiHost)
|
||||
{
|
||||
uint64_t uBig = htobe64(uiHost);
|
||||
|
||||
return strHex((unsigned char*) &uBig, sizeof(uBig));
|
||||
}
|
||||
|
||||
inline static std::string sqlEscape(const std::string& strSrc)
|
||||
{
|
||||
static boost::format f("X'%s'");
|
||||
return str(boost::format(f) % strHex(strSrc));
|
||||
}
|
||||
|
||||
inline static std::string sqlEscape(const std::vector<unsigned char>& vecSrc)
|
||||
{
|
||||
size_t size = vecSrc.size();
|
||||
if (size == 0)
|
||||
return "X''";
|
||||
|
||||
std::string j(size * 2 + 3, 0);
|
||||
|
||||
unsigned char *oPtr = reinterpret_cast<unsigned char *>(&*j.begin());
|
||||
const unsigned char *iPtr = &vecSrc[0];
|
||||
|
||||
*oPtr++ = 'X';
|
||||
*oPtr++ = '\'';
|
||||
|
||||
for (int i = size; i != 0; --i)
|
||||
{
|
||||
unsigned char c = *iPtr++;
|
||||
*oPtr++ = charHex(c >> 4);
|
||||
*oPtr++ = charHex(c & 15);
|
||||
}
|
||||
|
||||
*oPtr++ = '\'';
|
||||
return j;
|
||||
}
|
||||
|
||||
template<class Iterator>
|
||||
bool isZero(Iterator first, int iSize)
|
||||
{
|
||||
while (iSize && !*first++)
|
||||
--iSize;
|
||||
|
||||
return !iSize;
|
||||
}
|
||||
|
||||
int charUnHex(char cDigit);
|
||||
int strUnHex(std::string& strDst, const std::string& strSrc);
|
||||
|
||||
uint64_t uintFromHex(const std::string& strSrc);
|
||||
|
||||
std::vector<unsigned char> strUnHex(const std::string& strSrc);
|
||||
|
||||
std::vector<unsigned char> strCopy(const std::string& strSrc);
|
||||
std::string strCopy(const std::vector<unsigned char>& vucSrc);
|
||||
|
||||
bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort);
|
||||
bool parseQuality(const std::string& strSource, uint32& uQuality);
|
||||
|
||||
inline std::string strGetEnv(const std::string& strKey)
|
||||
{
|
||||
return getenv(strKey.c_str()) ? getenv(strKey.c_str()) : "";
|
||||
}
|
||||
|
||||
template<typename T> T lexical_cast_s(const std::string& string)
|
||||
{ // lexically cast a string to the selected type. Does not throw
|
||||
try
|
||||
{
|
||||
return boost::lexical_cast<T>(string);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T> std::string lexical_cast_i(const T& t)
|
||||
{ // lexicaly cast the selected type to a string. Does not throw
|
||||
try
|
||||
{
|
||||
return boost::lexical_cast<std::string>(t);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T> T lexical_cast_st(const std::string& string)
|
||||
{ // lexically cast a string to the selected type. Does throw
|
||||
return boost::lexical_cast<T>(string);
|
||||
}
|
||||
|
||||
template<typename T> std::string lexical_cast_it(const T& t)
|
||||
{ // lexicaly cast the selected type to a string. Does not throw
|
||||
return boost::lexical_cast<std::string>(t);
|
||||
}
|
||||
|
||||
bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, int& iPort, std::string& strPath);
|
||||
|
||||
#endif
|
||||
|
||||
// vim:ts=4
|
||||
@@ -30,6 +30,8 @@
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/asio.hpp> // for stupid parseIpPort
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
// VFALCO: TODO, fix these warnings!
|
||||
#ifdef _MSC_VER
|
||||
@@ -49,6 +51,7 @@
|
||||
#include "Winsock2.h" // for ripple_ByteOrder.cpp
|
||||
#endif
|
||||
#include "memory/ripple_ByteOrder.cpp"
|
||||
#include "memory/ripple_StringUtilities.cpp"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
//#pragma warning (pop)
|
||||
|
||||
@@ -79,6 +79,9 @@ namespace boost {
|
||||
# include <sys/types.h>
|
||||
#endif
|
||||
|
||||
// StringUtilities
|
||||
#include <boost/format.hpp>
|
||||
|
||||
|
||||
|
||||
#include "../ripple_json/ripple_json.h"
|
||||
@@ -93,6 +96,7 @@ namespace boost {
|
||||
#include "containers/ripple_TaggedCache.h"
|
||||
|
||||
#include "memory/ripple_ByteOrder.h"
|
||||
#include "memory/ripple_StringUtilities.h"
|
||||
|
||||
#include "events/ripple_UptimeTimer.h"
|
||||
|
||||
|
||||
@@ -203,6 +203,12 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="modules\ripple_basics\memory\ripple_StringUtilities.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="modules\ripple_basics\ripple_basics.cpp" />
|
||||
<ClCompile Include="modules\ripple_client\ripple_client.cpp" />
|
||||
<ClCompile Include="modules\ripple_db\ripple_db.cpp" />
|
||||
@@ -1188,6 +1194,7 @@
|
||||
<ClInclude Include="modules\ripple_basics\diagnostic\ripple_Log.h" />
|
||||
<ClInclude Include="modules\ripple_basics\events\ripple_UptimeTimer.h" />
|
||||
<ClInclude Include="modules\ripple_basics\memory\ripple_ByteOrder.h" />
|
||||
<ClInclude Include="modules\ripple_basics\memory\ripple_StringUtilities.h" />
|
||||
<ClInclude Include="modules\ripple_basics\ripple_basics.h" />
|
||||
<ClInclude Include="modules\ripple_basics\types\ripple_IntegerTypes.h" />
|
||||
<ClInclude Include="modules\ripple_client\ripple_client.h" />
|
||||
|
||||
@@ -759,6 +759,9 @@
|
||||
<ClCompile Include="modules\ripple_basics\memory\ripple_ByteOrder.cpp">
|
||||
<Filter>1. Modules\ripple_basics\memory</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="modules\ripple_basics\memory\ripple_StringUtilities.cpp">
|
||||
<Filter>1. Modules\ripple_basics\memory</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="database\sqlite3ext.h">
|
||||
@@ -1409,6 +1412,9 @@
|
||||
<ClInclude Include="modules\ripple_basics\memory\ripple_ByteOrder.h">
|
||||
<Filter>1. Modules\ripple_basics\memory</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="modules\ripple_basics\memory\ripple_StringUtilities.h">
|
||||
<Filter>1. Modules\ripple_basics\memory</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="SConstruct" />
|
||||
|
||||
@@ -17,11 +17,11 @@
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
//#include <boost/algorithm/string.hpp>
|
||||
//#include <boost/asio.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/regex.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
//#include <boost/regex.hpp>
|
||||
//#include <boost/test/unit_test.hpp>
|
||||
|
||||
#include <openssl/rand.h>
|
||||
|
||||
@@ -77,148 +77,6 @@ uint64_t utFromSeconds(int iSeconds)
|
||||
return tdDelta.total_seconds();
|
||||
}
|
||||
|
||||
//
|
||||
// Hex suport
|
||||
//
|
||||
|
||||
char charHex(int iDigit)
|
||||
{
|
||||
return iDigit < 10 ? '0' + iDigit : 'A' - 10 + iDigit;
|
||||
}
|
||||
|
||||
int charUnHex(char cDigit)
|
||||
{
|
||||
return cDigit >= '0' && cDigit <= '9'
|
||||
? cDigit - '0'
|
||||
: cDigit >= 'A' && cDigit <= 'F'
|
||||
? cDigit - 'A' + 10
|
||||
: cDigit >= 'a' && cDigit <= 'f'
|
||||
? cDigit - 'a' + 10
|
||||
: -1;
|
||||
}
|
||||
|
||||
int strUnHex(std::string& strDst, const std::string& strSrc)
|
||||
{
|
||||
int iBytes = (strSrc.size()+1)/2;
|
||||
|
||||
strDst.resize(iBytes);
|
||||
|
||||
const char* pSrc = &strSrc[0];
|
||||
char* pDst = &strDst[0];
|
||||
|
||||
if (strSrc.size() & 1)
|
||||
{
|
||||
int c = charUnHex(*pSrc++);
|
||||
|
||||
if (c < 0)
|
||||
{
|
||||
iBytes = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
*pDst++ = c;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i=0; iBytes >= 0 && i != iBytes; i++)
|
||||
{
|
||||
int cHigh = charUnHex(*pSrc++);
|
||||
int cLow = charUnHex(*pSrc++);
|
||||
|
||||
if (cHigh < 0 || cLow < 0)
|
||||
{
|
||||
iBytes = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
strDst[i] = (cHigh << 4) | cLow;
|
||||
}
|
||||
}
|
||||
|
||||
if (iBytes < 0)
|
||||
strDst.clear();
|
||||
|
||||
return iBytes;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> strUnHex(const std::string& strSrc)
|
||||
{
|
||||
std::string strTmp;
|
||||
|
||||
strUnHex(strTmp, strSrc);
|
||||
|
||||
return strCopy(strTmp);
|
||||
}
|
||||
|
||||
uint64_t uintFromHex(const std::string& strSrc)
|
||||
{
|
||||
uint64_t uValue = 0;
|
||||
|
||||
BOOST_FOREACH(char c, strSrc)
|
||||
uValue = (uValue << 4) | charUnHex(c);
|
||||
|
||||
return uValue;
|
||||
}
|
||||
|
||||
//
|
||||
// Misc string
|
||||
//
|
||||
|
||||
std::vector<unsigned char> strCopy(const std::string& strSrc)
|
||||
{
|
||||
std::vector<unsigned char> vucDst;
|
||||
|
||||
vucDst.resize(strSrc.size());
|
||||
|
||||
std::copy(strSrc.begin(), strSrc.end(), vucDst.begin());
|
||||
|
||||
return vucDst;
|
||||
}
|
||||
|
||||
std::string strCopy(const std::vector<unsigned char>& vucSrc)
|
||||
{
|
||||
std::string strDst;
|
||||
|
||||
strDst.resize(vucSrc.size());
|
||||
|
||||
std::copy(vucSrc.begin(), vucSrc.end(), strDst.begin());
|
||||
|
||||
return strDst;
|
||||
|
||||
}
|
||||
|
||||
extern std::string urlEncode(const std::string& strSrc)
|
||||
{
|
||||
std::string strDst;
|
||||
int iOutput = 0;
|
||||
int iSize = strSrc.length();
|
||||
|
||||
strDst.resize(iSize*3);
|
||||
|
||||
for (int iInput = 0; iInput < iSize; iInput++) {
|
||||
unsigned char c = strSrc[iInput];
|
||||
|
||||
if (c == ' ')
|
||||
{
|
||||
strDst[iOutput++] = '+';
|
||||
}
|
||||
else if (isalnum(c))
|
||||
{
|
||||
strDst[iOutput++] = c;
|
||||
}
|
||||
else
|
||||
{
|
||||
strDst[iOutput++] = '%';
|
||||
strDst[iOutput++] = charHex(c >> 4);
|
||||
strDst[iOutput++] = charHex(c & 15);
|
||||
}
|
||||
}
|
||||
|
||||
strDst.resize(iOutput);
|
||||
|
||||
return strDst;
|
||||
}
|
||||
|
||||
//
|
||||
// DH support
|
||||
//
|
||||
@@ -251,100 +109,6 @@ DH* DH_der_load(const std::string& strDer)
|
||||
return d2i_DHparams(NULL, &pbuf, strDer.size());
|
||||
}
|
||||
|
||||
//
|
||||
// IP Port parsing
|
||||
//
|
||||
// <-- iPort: "" = -1
|
||||
bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort)
|
||||
{
|
||||
boost::smatch smMatch;
|
||||
bool bValid = false;
|
||||
|
||||
static boost::regex reEndpoint("\\`\\s*(\\S+)(?:\\s+(\\d+))?\\s*\\'");
|
||||
|
||||
if (boost::regex_match(strSource, smMatch, reEndpoint))
|
||||
{
|
||||
boost::system::error_code err;
|
||||
std::string strIPRaw = smMatch[1];
|
||||
std::string strPortRaw = smMatch[2];
|
||||
|
||||
boost::asio::ip::address addrIP = boost::asio::ip::address::from_string(strIPRaw, err);
|
||||
|
||||
bValid = !err;
|
||||
if (bValid)
|
||||
{
|
||||
strIP = addrIP.to_string();
|
||||
iPort = strPortRaw.empty() ? -1 : boost::lexical_cast<int>(strPortRaw);
|
||||
}
|
||||
}
|
||||
|
||||
return bValid;
|
||||
}
|
||||
|
||||
bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, int& iPort, std::string& strPath)
|
||||
{
|
||||
// scheme://username:password@hostname:port/rest
|
||||
static boost::regex reUrl("(?i)\\`\\s*([[:alpha:]][-+.[:alpha:][:digit:]]*)://([^:/]+)(?::(\\d+))?(/.*)?\\s*?\\'");
|
||||
boost::smatch smMatch;
|
||||
|
||||
bool bMatch = boost::regex_match(strUrl, smMatch, reUrl); // Match status code.
|
||||
|
||||
if (bMatch)
|
||||
{
|
||||
std::string strPort;
|
||||
|
||||
strScheme = smMatch[1];
|
||||
strDomain = smMatch[2];
|
||||
strPort = smMatch[3];
|
||||
strPath = smMatch[4];
|
||||
|
||||
boost::algorithm::to_lower(strScheme);
|
||||
|
||||
iPort = strPort.empty() ? -1 : lexical_cast_s<int>(strPort);
|
||||
// std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPort << "' : " << iPort << " : '" << strPath << "'" << std::endl;
|
||||
}
|
||||
// std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPath << "'" << std::endl;
|
||||
|
||||
return bMatch;
|
||||
}
|
||||
|
||||
//
|
||||
// Quality parsing
|
||||
// - integers as is.
|
||||
// - floats multiplied by a billion
|
||||
bool parseQuality(const std::string& strSource, uint32& uQuality)
|
||||
{
|
||||
uQuality = lexical_cast_s<uint32>(strSource);
|
||||
|
||||
if (!uQuality)
|
||||
{
|
||||
float fQuality = lexical_cast_s<float>(strSource);
|
||||
|
||||
if (fQuality)
|
||||
uQuality = (uint32)(QUALITY_ONE*fQuality);
|
||||
}
|
||||
|
||||
return !!uQuality;
|
||||
}
|
||||
|
||||
/*
|
||||
void intIPtoStr(int ip,std::string& retStr)
|
||||
{
|
||||
unsigned char bytes[4];
|
||||
bytes[0] = ip & 0xFF;
|
||||
bytes[1] = (ip >> 8) & 0xFF;
|
||||
bytes[2] = (ip >> 16) & 0xFF;
|
||||
bytes[3] = (ip >> 24) & 0xFF;
|
||||
|
||||
retStr=str(boost::format("%d.%d.%d.%d") % bytes[3] % bytes[2] % bytes[1] % bytes[0] );
|
||||
}
|
||||
|
||||
int strIPtoInt(std::string& ipStr)
|
||||
{
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
#ifdef PR_SET_NAME
|
||||
#define HAVE_NAME_THREAD
|
||||
extern void NameThread(const char* n)
|
||||
@@ -451,52 +215,4 @@ std::string StopSustain() { return std::string(); }
|
||||
|
||||
#endif
|
||||
|
||||
BOOST_AUTO_TEST_SUITE( Utils)
|
||||
|
||||
BOOST_AUTO_TEST_CASE( ParseUrl )
|
||||
{
|
||||
std::string strScheme;
|
||||
std::string strDomain;
|
||||
int iPort;
|
||||
std::string strPath;
|
||||
|
||||
if (!parseUrl("lower://domain", strScheme, strDomain, iPort, strPath))
|
||||
BOOST_FAIL("parseUrl: lower://domain failed");
|
||||
|
||||
if (strScheme != "lower")
|
||||
BOOST_FAIL("parseUrl: lower://domain : scheme failed");
|
||||
|
||||
if (strDomain != "domain")
|
||||
BOOST_FAIL("parseUrl: lower://domain : domain failed");
|
||||
|
||||
if (iPort != -1)
|
||||
BOOST_FAIL("parseUrl: lower://domain : port failed");
|
||||
|
||||
if (strPath != "")
|
||||
BOOST_FAIL("parseUrl: lower://domain : path failed");
|
||||
|
||||
if (!parseUrl("UPPER://domain:234/", strScheme, strDomain, iPort, strPath))
|
||||
BOOST_FAIL("parseUrl: UPPER://domain:234/ failed");
|
||||
|
||||
if (strScheme != "upper")
|
||||
BOOST_FAIL("parseUrl: UPPER://domain:234/ : scheme failed");
|
||||
|
||||
if (iPort != 234)
|
||||
BOOST_FAIL(boost::str(boost::format("parseUrl: UPPER://domain:234/ : port failed: %d") % iPort));
|
||||
|
||||
if (strPath != "/")
|
||||
BOOST_FAIL("parseUrl: UPPER://domain:234/ : path failed");
|
||||
|
||||
if (!parseUrl("Mixed://domain/path", strScheme, strDomain, iPort, strPath))
|
||||
BOOST_FAIL("parseUrl: Mixed://domain/path failed");
|
||||
|
||||
if (strScheme != "mixed")
|
||||
BOOST_FAIL("parseUrl: Mixed://domain/path tolower failed");
|
||||
|
||||
if (strPath != "/path")
|
||||
BOOST_FAIL("parseUrl: Mixed://domain/path path failed");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
// vim:ts=4
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#define __UTILS__
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include <boost/version.hpp>
|
||||
|
||||
#if BOOST_VERSION < 104700
|
||||
@@ -11,8 +10,6 @@
|
||||
|
||||
#include <openssl/dh.h>
|
||||
|
||||
#define QUALITY_ONE 1000000000 // 10e9
|
||||
|
||||
#define nothing() do {} while (0)
|
||||
#define fallthru() do {} while (0)
|
||||
#define NUMBER(x) (sizeof(x)/sizeof((x)[0]))
|
||||
@@ -21,163 +18,11 @@
|
||||
|
||||
#define isSetBit(x,y) (!!((x) & (y)))
|
||||
|
||||
#define vt_f_black "\033[30m"
|
||||
#define vt_f_red "\033[31m"
|
||||
#define vt_f_green "\033[32m"
|
||||
#define vt_f_yellow "\033[33m"
|
||||
#define vt_f_blue "\033[34m"
|
||||
#define vt_f_megenta "\033[35m"
|
||||
#define vt_f_cyan "\033[36m"
|
||||
#define vt_f_white "\033[37m"
|
||||
#define vt_f_default "\033[39m"
|
||||
|
||||
#define vt_b_black "\033[40m"
|
||||
#define vt_b_red "\033[41m"
|
||||
#define vt_b_green "\033[42m"
|
||||
#define vt_b_yellow "\033[43m"
|
||||
#define vt_b_blue "\033[44m"
|
||||
#define vt_b_megenta "\033[45m"
|
||||
#define vt_b_cyan "\033[46m"
|
||||
#define vt_b_white "\033[47m"
|
||||
#define vt_b_default "\033[49m"
|
||||
|
||||
#define vt_f_bold_black "\033[1m\033[30m"
|
||||
#define vt_f_bold_red "\033[1m\033[31m"
|
||||
#define vt_f_bold_green "\033[1m\033[32m"
|
||||
#define vt_f_bold_yellow "\033[1m\033[33m"
|
||||
#define vt_f_bold_blue "\033[1m\033[34m"
|
||||
#define vt_f_bold_megenta "\033[1m\033[35m"
|
||||
#define vt_f_bold_cyan "\033[1m\033[36m"
|
||||
#define vt_f_bold_white "\033[1m\033[37m"
|
||||
#define vt_f_bold_default "\033[1m\033[39m"
|
||||
|
||||
#define vt_bold "\033[1m"
|
||||
#define vt_dim "\033[2m" // does not work for xterm
|
||||
#define vt_normal "\033[22m" // intensity
|
||||
|
||||
#define vt_n_enable "\033[7m" // negative
|
||||
#define vt_n_disable "\033[27m"
|
||||
|
||||
#define vt_u_single "\033[4m" // underline
|
||||
#define vt_u_double "\033[21m" // does not work for xterm
|
||||
#define vt_u_disable "\033[24m"
|
||||
|
||||
#define vt_reset vt_f_default vt_b_default vt_normal vt_n_disable vt_u_disable
|
||||
|
||||
boost::posix_time::ptime ptEpoch();
|
||||
int iToSeconds(boost::posix_time::ptime ptWhen);
|
||||
boost::posix_time::ptime ptFromSeconds(int iSeconds);
|
||||
uint64_t utFromSeconds(int iSeconds);
|
||||
|
||||
extern std::string urlEncode(const std::string& strSrc);
|
||||
|
||||
/*
|
||||
void intIPtoStr(int ip,std::string& retStr);
|
||||
int strIPtoInt(std::string& ipStr);
|
||||
*/
|
||||
|
||||
template<class Iterator>
|
||||
std::string strJoin(Iterator first, Iterator last, std::string strSeperator)
|
||||
{
|
||||
std::ostringstream ossValues;
|
||||
|
||||
for (Iterator start = first; first != last; first++)
|
||||
{
|
||||
ossValues << str(boost::format("%s%s") % (start == first ? "" : strSeperator) % *first);
|
||||
}
|
||||
|
||||
return ossValues.str();
|
||||
}
|
||||
|
||||
char charHex(int iDigit);
|
||||
|
||||
template<class Iterator>
|
||||
std::string strHex(Iterator first, int iSize)
|
||||
{
|
||||
std::string strDst;
|
||||
|
||||
strDst.resize(iSize*2);
|
||||
|
||||
for (int i = 0; i < iSize; i++) {
|
||||
unsigned char c = *first++;
|
||||
|
||||
strDst[i*2] = charHex(c >> 4);
|
||||
strDst[i*2+1] = charHex(c & 15);
|
||||
}
|
||||
|
||||
return strDst;
|
||||
}
|
||||
|
||||
inline const std::string strHex(const std::string& strSrc)
|
||||
{
|
||||
return strHex(strSrc.begin(), strSrc.size());
|
||||
}
|
||||
|
||||
inline std::string strHex(const std::vector<unsigned char>& vucData)
|
||||
{
|
||||
return strHex(vucData.begin(), vucData.size());
|
||||
}
|
||||
|
||||
inline std::string strHex(const uint64 uiHost)
|
||||
{
|
||||
uint64_t uBig = htobe64(uiHost);
|
||||
|
||||
return strHex((unsigned char*) &uBig, sizeof(uBig));
|
||||
}
|
||||
|
||||
inline static std::string sqlEscape(const std::string& strSrc)
|
||||
{
|
||||
static boost::format f("X'%s'");
|
||||
return str(boost::format(f) % strHex(strSrc));
|
||||
}
|
||||
|
||||
inline static std::string sqlEscape(const std::vector<unsigned char>& vecSrc)
|
||||
{
|
||||
size_t size = vecSrc.size();
|
||||
if (size == 0)
|
||||
return "X''";
|
||||
|
||||
std::string j(size * 2 + 3, 0);
|
||||
|
||||
unsigned char *oPtr = reinterpret_cast<unsigned char *>(&*j.begin());
|
||||
const unsigned char *iPtr = &vecSrc[0];
|
||||
|
||||
*oPtr++ = 'X';
|
||||
*oPtr++ = '\'';
|
||||
|
||||
for (int i = size; i != 0; --i)
|
||||
{
|
||||
unsigned char c = *iPtr++;
|
||||
*oPtr++ = charHex(c >> 4);
|
||||
*oPtr++ = charHex(c & 15);
|
||||
}
|
||||
|
||||
*oPtr++ = '\'';
|
||||
return j;
|
||||
}
|
||||
|
||||
template<class Iterator>
|
||||
bool isZero(Iterator first, int iSize)
|
||||
{
|
||||
while (iSize && !*first++)
|
||||
--iSize;
|
||||
|
||||
return !iSize;
|
||||
}
|
||||
|
||||
int charUnHex(char cDigit);
|
||||
int strUnHex(std::string& strDst, const std::string& strSrc);
|
||||
|
||||
uint64_t uintFromHex(const std::string& strSrc);
|
||||
|
||||
std::vector<unsigned char> strUnHex(const std::string& strSrc);
|
||||
|
||||
std::vector<unsigned char> strCopy(const std::string& strSrc);
|
||||
std::string strCopy(const std::vector<unsigned char>& vucSrc);
|
||||
|
||||
bool parseIpPort(const std::string& strSource, std::string& strIP, int& iPort);
|
||||
bool parseQuality(const std::string& strSource, uint32& uQuality);
|
||||
|
||||
DH* DH_der_load(const std::string& strDer);
|
||||
std::string DH_der_gen(int iKeyLength);
|
||||
|
||||
@@ -185,45 +30,6 @@ void getRand(unsigned char *buf, int num);
|
||||
inline static void getRand(char *buf, int num) { return getRand(reinterpret_cast<unsigned char *>(buf), num); }
|
||||
inline static void getRand(void *buf, int num) { return getRand(reinterpret_cast<unsigned char *>(buf), num); }
|
||||
|
||||
inline std::string strGetEnv(const std::string& strKey)
|
||||
{
|
||||
return getenv(strKey.c_str()) ? getenv(strKey.c_str()) : "";
|
||||
}
|
||||
|
||||
template<typename T> T lexical_cast_s(const std::string& string)
|
||||
{ // lexically cast a string to the selected type. Does not throw
|
||||
try
|
||||
{
|
||||
return boost::lexical_cast<T>(string);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T> std::string lexical_cast_i(const T& t)
|
||||
{ // lexicaly cast the selected type to a string. Does not throw
|
||||
try
|
||||
{
|
||||
return boost::lexical_cast<std::string>(t);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T> T lexical_cast_st(const std::string& string)
|
||||
{ // lexically cast a string to the selected type. Does throw
|
||||
return boost::lexical_cast<T>(string);
|
||||
}
|
||||
|
||||
template<typename T> std::string lexical_cast_it(const T& t)
|
||||
{ // lexicaly cast the selected type to a string. Does not throw
|
||||
return boost::lexical_cast<std::string>(t);
|
||||
}
|
||||
|
||||
template<typename T> T range_check(const T& value, const T& minimum, const T& maximum)
|
||||
{
|
||||
if ((value < minimum) || (value > maximum))
|
||||
@@ -252,8 +58,6 @@ template<typename T, typename U> T range_check_cast(const U& value, const T& min
|
||||
return static_cast<T>(value);
|
||||
}
|
||||
|
||||
bool parseUrl(const std::string& strUrl, std::string& strScheme, std::string& strDomain, int& iPort, std::string& strPath);
|
||||
|
||||
extern void NameThread(const char *);
|
||||
|
||||
extern bool HaveSustain();
|
||||
|
||||
Reference in New Issue
Block a user