2013-03-17 07:45:21 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder General Utility Functions, $Date$, $Revision$
|
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2016-12-26 23:23:16 +08:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2013-03-17 07:45:21 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "zm_utils.h"
|
|
|
|
|
2021-02-04 11:47:28 +08:00
|
|
|
#include "zm_config.h"
|
|
|
|
#include "zm_logger.h"
|
2019-09-17 23:11:22 +08:00
|
|
|
#include <algorithm>
|
2018-09-22 03:57:48 +08:00
|
|
|
#include <fcntl.h> /* Definition of AT_* constants */
|
2021-02-04 11:47:28 +08:00
|
|
|
#include <sstream>
|
2018-09-22 03:57:48 +08:00
|
|
|
#include <sys/stat.h>
|
2021-02-04 11:47:28 +08:00
|
|
|
|
2017-03-20 21:12:28 +08:00
|
|
|
#if defined(__arm__)
|
2017-03-19 08:05:01 +08:00
|
|
|
#include <sys/auxv.h>
|
2017-03-20 21:12:28 +08:00
|
|
|
#endif
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2017-03-20 23:16:21 +08:00
|
|
|
#ifdef HAVE_CURL_CURL_H
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#endif
|
|
|
|
|
2020-07-20 05:50:47 +08:00
|
|
|
unsigned int sse_version = 0;
|
2017-03-19 08:05:01 +08:00
|
|
|
unsigned int neonversion = 0;
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2014-05-05 19:29:12 +08:00
|
|
|
std::string trimSet(std::string str, std::string trimset) {
|
2016-04-04 22:11:48 +08:00
|
|
|
// Trim Both leading and trailing sets
|
|
|
|
size_t startpos = str.find_first_not_of(trimset); // Find the first character position after excluding leading blank spaces
|
|
|
|
size_t endpos = str.find_last_not_of(trimset); // Find the first character position from reverse af
|
2014-05-05 19:29:12 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
// if all spaces or empty return an empty string
|
2020-07-08 06:56:51 +08:00
|
|
|
if ( ( std::string::npos == startpos ) || ( std::string::npos == endpos ) )
|
2016-04-04 22:11:48 +08:00
|
|
|
return std::string("");
|
2020-07-08 04:14:57 +08:00
|
|
|
return str.substr(startpos, endpos-startpos+1);
|
2014-05-05 19:29:12 +08:00
|
|
|
}
|
|
|
|
|
2018-04-12 23:29:35 +08:00
|
|
|
std::string trimSpaces(const std::string &str) {
|
2016-04-04 22:11:48 +08:00
|
|
|
return trimSet(str, " \t");
|
2014-05-05 19:29:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string replaceAll(std::string str, std::string from, std::string to) {
|
2020-07-08 04:14:57 +08:00
|
|
|
if ( from.empty() )
|
2014-05-05 19:29:12 +08:00
|
|
|
return str;
|
2016-04-04 22:11:48 +08:00
|
|
|
size_t start_pos = 0;
|
2020-07-08 04:14:57 +08:00
|
|
|
while ( (start_pos = str.find(from, start_pos)) != std::string::npos ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
str.replace(start_pos, from.length(), to);
|
|
|
|
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
|
|
|
|
}
|
|
|
|
return str;
|
2014-05-05 19:29:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
const std::string stringtf( const char *format, ... ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
va_list ap;
|
|
|
|
char tempBuffer[8192];
|
|
|
|
std::string tempString;
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
va_start(ap, format);
|
|
|
|
vsnprintf(tempBuffer, sizeof(tempBuffer), format , ap);
|
2016-04-04 22:11:48 +08:00
|
|
|
va_end(ap);
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
tempString = tempBuffer;
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
return tempString;
|
2013-03-17 07:45:21 +08:00
|
|
|
}
|
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
const std::string stringtf(const std::string format, ...) {
|
2016-04-04 22:11:48 +08:00
|
|
|
va_list ap;
|
|
|
|
char tempBuffer[8192];
|
|
|
|
std::string tempString;
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
va_start(ap, format);
|
|
|
|
vsnprintf(tempBuffer, sizeof(tempBuffer), format.c_str(), ap);
|
2016-04-04 22:11:48 +08:00
|
|
|
va_end(ap);
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
tempString = tempBuffer;
|
2013-03-17 07:45:21 +08:00
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
return tempString;
|
2013-03-17 07:45:21 +08:00
|
|
|
}
|
|
|
|
|
2018-05-13 05:35:01 +08:00
|
|
|
bool startsWith(const std::string &haystack, const std::string &needle) {
|
2020-07-08 04:14:57 +08:00
|
|
|
return ( haystack.substr(0, needle.length()) == needle );
|
2013-03-17 07:45:21 +08:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:36:34 +08:00
|
|
|
std::vector<std::string> split(const std::string &s, char delim) {
|
|
|
|
std::vector<std::string> elems;
|
|
|
|
std::stringstream ss(s);
|
|
|
|
std::string item;
|
|
|
|
while(std::getline(ss, item, delim)) {
|
|
|
|
elems.push_back(trimSpaces(item));
|
|
|
|
}
|
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
2018-05-13 05:35:01 +08:00
|
|
|
StringVector split(const std::string &string, const std::string &chars, int limit) {
|
2016-04-04 22:11:48 +08:00
|
|
|
StringVector stringVector;
|
|
|
|
std::string tempString = string;
|
|
|
|
std::string::size_type startIndex = 0;
|
|
|
|
std::string::size_type endIndex = 0;
|
|
|
|
|
|
|
|
//Info( "Looking for '%s' in '%s', limit %d", chars.c_str(), string.c_str(), limit );
|
2018-05-13 05:35:01 +08:00
|
|
|
do {
|
2016-04-04 22:11:48 +08:00
|
|
|
// Find delimiters
|
|
|
|
endIndex = string.find_first_of( chars, startIndex );
|
|
|
|
//Info( "Got endIndex at %d", endIndex );
|
2018-05-13 05:35:01 +08:00
|
|
|
if ( endIndex > 0 ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
//Info( "Adding '%s'", string.substr( startIndex, endIndex-startIndex ).c_str() );
|
|
|
|
stringVector.push_back( string.substr( startIndex, endIndex-startIndex ) );
|
|
|
|
}
|
|
|
|
if ( endIndex == std::string::npos )
|
|
|
|
break;
|
|
|
|
// Find non-delimiters
|
|
|
|
startIndex = tempString.find_first_not_of( chars, endIndex );
|
2018-05-13 05:35:01 +08:00
|
|
|
if ( limit && (stringVector.size() == (unsigned int)(limit-1)) ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
stringVector.push_back( string.substr( startIndex ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//Info( "Got new startIndex at %d", startIndex );
|
|
|
|
} while ( startIndex != std::string::npos );
|
|
|
|
//Info( "Finished with %d strings", stringVector.size() );
|
|
|
|
|
2018-05-13 05:35:01 +08:00
|
|
|
return stringVector;
|
2013-03-17 07:45:21 +08:00
|
|
|
}
|
|
|
|
|
2018-05-13 05:35:01 +08:00
|
|
|
const std::string join(const StringVector &v, const char * delim=",") {
|
2016-04-04 22:11:48 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
|
2018-05-13 05:35:01 +08:00
|
|
|
for (size_t i = 0; i < v.size(); ++i) {
|
|
|
|
if ( i != 0 )
|
|
|
|
ss << delim;
|
2016-04-04 22:11:48 +08:00
|
|
|
ss << v[i];
|
|
|
|
}
|
|
|
|
return ss.str();
|
2014-11-06 03:01:48 +08:00
|
|
|
}
|
|
|
|
|
2018-05-13 05:35:01 +08:00
|
|
|
const std::string base64Encode(const std::string &inString) {
|
2016-04-04 22:11:48 +08:00
|
|
|
static char base64_table[64] = { '\0' };
|
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
if ( !base64_table[0] ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
int i = 0;
|
|
|
|
for ( char c = 'A'; c <= 'Z'; c++ )
|
|
|
|
base64_table[i++] = c;
|
|
|
|
for ( char c = 'a'; c <= 'z'; c++ )
|
|
|
|
base64_table[i++] = c;
|
|
|
|
for ( char c = '0'; c <= '9'; c++ )
|
|
|
|
base64_table[i++] = c;
|
|
|
|
base64_table[i++] = '+';
|
|
|
|
base64_table[i++] = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string outString;
|
2020-07-08 04:14:57 +08:00
|
|
|
outString.reserve(2 * inString.size());
|
2016-04-04 22:11:48 +08:00
|
|
|
|
|
|
|
const char *inPtr = inString.c_str();
|
2020-07-08 04:14:57 +08:00
|
|
|
while ( *inPtr ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
unsigned char selection = *inPtr >> 2;
|
|
|
|
unsigned char remainder = (*inPtr++ & 0x03) << 4;
|
|
|
|
outString += base64_table[selection];
|
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
if ( *inPtr ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
selection = remainder | (*inPtr >> 4);
|
|
|
|
remainder = (*inPtr++ & 0x0f) << 2;
|
|
|
|
outString += base64_table[selection];
|
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
if ( *inPtr ) {
|
2016-04-04 22:11:48 +08:00
|
|
|
selection = remainder | (*inPtr >> 6);
|
|
|
|
outString += base64_table[selection];
|
|
|
|
selection = (*inPtr++ & 0x3f);
|
|
|
|
outString += base64_table[selection];
|
2020-07-08 04:14:57 +08:00
|
|
|
} else {
|
2016-04-04 22:11:48 +08:00
|
|
|
outString += base64_table[remainder];
|
|
|
|
outString += '=';
|
|
|
|
}
|
2020-07-08 04:14:57 +08:00
|
|
|
} else {
|
2016-04-04 22:11:48 +08:00
|
|
|
outString += base64_table[remainder];
|
|
|
|
outString += '=';
|
|
|
|
outString += '=';
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 04:14:57 +08:00
|
|
|
return outString;
|
2013-03-17 07:45:21 +08:00
|
|
|
}
|
|
|
|
|
2014-01-08 21:57:17 +08:00
|
|
|
int split(const char* string, const char delim, std::vector<std::string>& items) {
|
2020-08-26 07:45:48 +08:00
|
|
|
if ( string == nullptr )
|
2016-04-04 22:11:48 +08:00
|
|
|
return -1;
|
2014-01-08 21:57:17 +08:00
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
if ( string[0] == 0 )
|
2016-04-04 22:11:48 +08:00
|
|
|
return -2;
|
2014-01-08 21:57:17 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
std::string str(string);
|
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
while ( true ) {
|
2017-11-19 05:00:10 +08:00
|
|
|
size_t pos = str.find(delim);
|
2016-04-04 22:11:48 +08:00
|
|
|
items.push_back(str.substr(0, pos));
|
|
|
|
str.erase(0, pos+1);
|
2014-01-08 21:57:17 +08:00
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
if ( pos == std::string::npos )
|
2016-04-04 22:11:48 +08:00
|
|
|
break;
|
|
|
|
}
|
2014-01-08 21:57:17 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
return items.size();
|
2014-01-08 21:57:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int pairsplit(const char* string, const char delim, std::string& name, std::string& value) {
|
2020-08-26 07:45:48 +08:00
|
|
|
if ( string == nullptr )
|
2016-04-04 22:11:48 +08:00
|
|
|
return -1;
|
2014-01-08 21:57:17 +08:00
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
if ( string[0] == 0 )
|
2016-04-04 22:11:48 +08:00
|
|
|
return -2;
|
2014-01-08 21:57:17 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
std::string str(string);
|
|
|
|
size_t pos = str.find(delim);
|
2014-01-08 21:57:17 +08:00
|
|
|
|
2020-07-08 04:14:57 +08:00
|
|
|
if ( pos == std::string::npos || pos == 0 || pos >= str.length() )
|
2016-04-04 22:11:48 +08:00
|
|
|
return -3;
|
2014-01-08 21:57:17 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
name = str.substr(0, pos);
|
|
|
|
value = str.substr(pos+1, std::string::npos);
|
2014-01-08 21:57:17 +08:00
|
|
|
|
2016-04-04 22:11:48 +08:00
|
|
|
return 0;
|
2014-01-08 21:57:17 +08:00
|
|
|
}
|
|
|
|
|
2017-03-19 08:05:01 +08:00
|
|
|
/* Detect special hardware features, such as SIMD instruction sets */
|
|
|
|
void hwcaps_detect() {
|
|
|
|
neonversion = 0;
|
2020-07-20 05:50:47 +08:00
|
|
|
sse_version = 0;
|
2013-03-17 07:45:21 +08:00
|
|
|
#if (defined(__i386__) || defined(__x86_64__))
|
2020-08-20 04:57:20 +08:00
|
|
|
__builtin_cpu_init();
|
|
|
|
|
2017-03-31 01:35:31 +08:00
|
|
|
|
2020-08-20 04:57:20 +08:00
|
|
|
if ( __builtin_cpu_supports("avx2") ) {
|
2020-07-20 05:50:47 +08:00
|
|
|
sse_version = 52; /* AVX2 */
|
2020-07-08 04:14:57 +08:00
|
|
|
Debug(1, "Detected a x86\\x86-64 processor with AVX2");
|
2020-08-20 04:57:20 +08:00
|
|
|
} else if ( __builtin_cpu_supports("avx") ) {
|
2020-07-20 05:50:47 +08:00
|
|
|
sse_version = 51; /* AVX */
|
2020-07-08 04:14:57 +08:00
|
|
|
Debug(1, "Detected a x86\\x86-64 processor with AVX");
|
2020-08-20 04:57:20 +08:00
|
|
|
} else if ( __builtin_cpu_supports("sse4.2") ) {
|
2020-07-20 05:50:47 +08:00
|
|
|
sse_version = 42; /* SSE4.2 */
|
2020-07-08 04:14:57 +08:00
|
|
|
Debug(1, "Detected a x86\\x86-64 processor with SSE4.2");
|
2020-08-20 04:57:20 +08:00
|
|
|
} else if ( __builtin_cpu_supports("sse4.1") ) {
|
2020-07-20 05:50:47 +08:00
|
|
|
sse_version = 41; /* SSE4.1 */
|
2020-07-08 04:14:57 +08:00
|
|
|
Debug(1, "Detected a x86\\x86-64 processor with SSE4.1");
|
2020-08-20 04:57:20 +08:00
|
|
|
} else if ( __builtin_cpu_supports("ssse3") ) {
|
2020-07-20 05:50:47 +08:00
|
|
|
sse_version = 35; /* SSSE3 */
|
2016-04-04 22:11:48 +08:00
|
|
|
Debug(1,"Detected a x86\\x86-64 processor with SSSE3");
|
2020-08-20 04:57:20 +08:00
|
|
|
} else if ( __builtin_cpu_supports("sse3") ) {
|
2020-07-20 05:50:47 +08:00
|
|
|
sse_version = 30; /* SSE3 */
|
2020-07-08 04:14:57 +08:00
|
|
|
Debug(1, "Detected a x86\\x86-64 processor with SSE3");
|
2020-08-20 04:57:20 +08:00
|
|
|
} else if ( __builtin_cpu_supports("sse2") ) {
|
2020-07-20 05:50:47 +08:00
|
|
|
sse_version = 20; /* SSE2 */
|
2020-07-08 04:14:57 +08:00
|
|
|
Debug(1, "Detected a x86\\x86-64 processor with SSE2");
|
2020-08-20 04:57:20 +08:00
|
|
|
} else if ( __builtin_cpu_supports("sse") ) {
|
2020-07-20 05:50:47 +08:00
|
|
|
sse_version = 10; /* SSE */
|
2020-07-08 04:14:57 +08:00
|
|
|
Debug(1, "Detected a x86\\x86-64 processor with SSE");
|
2016-04-04 22:11:48 +08:00
|
|
|
} else {
|
2020-07-20 05:50:47 +08:00
|
|
|
sse_version = 0;
|
2020-07-08 04:14:57 +08:00
|
|
|
Debug(1, "Detected a x86\\x86-64 processor");
|
2017-03-19 08:05:01 +08:00
|
|
|
}
|
|
|
|
#elif defined(__arm__)
|
2017-05-13 03:43:30 +08:00
|
|
|
// ARM processor in 32bit mode
|
2017-03-19 08:05:01 +08:00
|
|
|
// To see if it supports NEON, we need to get that information from the kernel
|
|
|
|
unsigned long auxval = getauxval(AT_HWCAP);
|
|
|
|
if (auxval & HWCAP_ARM_NEON) {
|
2017-05-13 03:43:30 +08:00
|
|
|
Debug(1,"Detected ARM (AArch32) processor with Neon");
|
2017-03-19 08:05:01 +08:00
|
|
|
neonversion = 1;
|
|
|
|
} else {
|
2017-05-13 03:43:30 +08:00
|
|
|
Debug(1,"Detected ARM (AArch32) processor");
|
2016-04-04 22:11:48 +08:00
|
|
|
}
|
2017-05-13 03:43:30 +08:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
// ARM processor in 64bit mode
|
|
|
|
// Neon is mandatory, no need to check for it
|
|
|
|
neonversion = 1;
|
|
|
|
Debug(1,"Detected ARM (AArch64) processor with Neon");
|
2013-03-17 07:45:21 +08:00
|
|
|
#else
|
2017-03-19 08:05:01 +08:00
|
|
|
// Unknown processor
|
|
|
|
Debug(1,"Detected unknown processor architecture");
|
2013-03-17 07:45:21 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SSE2 aligned memory copy. Useful for big copying of aligned memory like image buffers in ZM */
|
|
|
|
/* For platforms without SSE2 we will use standard x86 asm memcpy or glibc's memcpy() */
|
2014-03-16 23:51:54 +08:00
|
|
|
#if defined(__i386__) || defined(__x86_64__)
|
|
|
|
__attribute__((noinline,__target__("sse2")))
|
|
|
|
#endif
|
|
|
|
void* sse2_aligned_memcpy(void* dest, const void* src, size_t bytes) {
|
2013-03-17 07:45:21 +08:00
|
|
|
#if ((defined(__i386__) || defined(__x86_64__) || defined(ZM_KEEP_SSE)) && !defined(ZM_STRIP_SSE))
|
2020-08-20 04:57:20 +08:00
|
|
|
if(bytes > 128) {
|
2016-04-04 22:11:48 +08:00
|
|
|
unsigned int remainder = bytes % 128;
|
|
|
|
const uint8_t* lastsrc = (uint8_t*)src + (bytes - remainder);
|
|
|
|
|
|
|
|
__asm__ __volatile__(
|
|
|
|
"sse2_copy_iter:\n\t"
|
|
|
|
"movdqa (%0),%%xmm0\n\t"
|
|
|
|
"movdqa 0x10(%0),%%xmm1\n\t"
|
|
|
|
"movdqa 0x20(%0),%%xmm2\n\t"
|
|
|
|
"movdqa 0x30(%0),%%xmm3\n\t"
|
|
|
|
"movdqa 0x40(%0),%%xmm4\n\t"
|
|
|
|
"movdqa 0x50(%0),%%xmm5\n\t"
|
|
|
|
"movdqa 0x60(%0),%%xmm6\n\t"
|
|
|
|
"movdqa 0x70(%0),%%xmm7\n\t"
|
|
|
|
"movntdq %%xmm0,(%1)\n\t"
|
|
|
|
"movntdq %%xmm1,0x10(%1)\n\t"
|
|
|
|
"movntdq %%xmm2,0x20(%1)\n\t"
|
|
|
|
"movntdq %%xmm3,0x30(%1)\n\t"
|
|
|
|
"movntdq %%xmm4,0x40(%1)\n\t"
|
|
|
|
"movntdq %%xmm5,0x50(%1)\n\t"
|
|
|
|
"movntdq %%xmm6,0x60(%1)\n\t"
|
|
|
|
"movntdq %%xmm7,0x70(%1)\n\t"
|
|
|
|
"add $0x80, %0\n\t"
|
|
|
|
"add $0x80, %1\n\t"
|
|
|
|
"cmp %2, %0\n\t"
|
|
|
|
"jb sse2_copy_iter\n\t"
|
|
|
|
"test %3, %3\n\t"
|
|
|
|
"jz sse2_copy_finish\n\t"
|
|
|
|
"cld\n\t"
|
|
|
|
"rep movsb\n\t"
|
|
|
|
"sse2_copy_finish:\n\t"
|
|
|
|
:
|
|
|
|
: "S" (src), "D" (dest), "r" (lastsrc), "c" (remainder)
|
|
|
|
: "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7", "cc", "memory"
|
|
|
|
);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Standard memcpy */
|
|
|
|
__asm__ __volatile__("cld; rep movsb" :: "S"(src), "D"(dest), "c"(bytes) : "cc", "memory");
|
|
|
|
}
|
2013-03-17 07:45:21 +08:00
|
|
|
#else
|
2016-04-04 22:11:48 +08:00
|
|
|
/* Non x86\x86-64 platform, use memcpy */
|
2020-08-20 04:57:20 +08:00
|
|
|
memcpy(dest,src,bytes);
|
2013-03-17 07:45:21 +08:00
|
|
|
#endif
|
2016-04-04 22:11:48 +08:00
|
|
|
return dest;
|
2013-03-17 07:45:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void timespec_diff(struct timespec *start, struct timespec *end, struct timespec *diff) {
|
2016-04-04 22:11:48 +08:00
|
|
|
if (((end->tv_nsec)-(start->tv_nsec))<0) {
|
|
|
|
diff->tv_sec = end->tv_sec-start->tv_sec-1;
|
|
|
|
diff->tv_nsec = 1000000000+end->tv_nsec-start->tv_nsec;
|
|
|
|
} else {
|
|
|
|
diff->tv_sec = end->tv_sec-start->tv_sec;
|
|
|
|
diff->tv_nsec = end->tv_nsec-start->tv_nsec;
|
|
|
|
}
|
2013-03-17 07:45:21 +08:00
|
|
|
}
|
|
|
|
|
2017-04-13 01:40:38 +08:00
|
|
|
char *timeval_to_string( struct timeval tv ) {
|
|
|
|
time_t nowtime;
|
|
|
|
struct tm *nowtm;
|
2020-04-25 04:43:27 +08:00
|
|
|
static char tmbuf[20], buf[28];
|
2017-04-13 01:40:38 +08:00
|
|
|
|
|
|
|
nowtime = tv.tv_sec;
|
|
|
|
nowtm = localtime(&nowtime);
|
|
|
|
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
|
2020-04-25 04:43:27 +08:00
|
|
|
snprintf(buf, sizeof buf-1, "%s.%06ld", tmbuf, tv.tv_usec);
|
2017-04-13 01:40:38 +08:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-03-20 23:16:21 +08:00
|
|
|
std::string UriDecode( const std::string &encoded ) {
|
2020-04-09 00:52:09 +08:00
|
|
|
char a, b;
|
|
|
|
const char *src = encoded.c_str();
|
|
|
|
std::string retbuf;
|
|
|
|
retbuf.resize(encoded.length() + 1);
|
|
|
|
char *dst = &retbuf[0];
|
|
|
|
while (*src) {
|
|
|
|
if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) {
|
|
|
|
if (a >= 'a')
|
|
|
|
a -= 'a'-'A';
|
|
|
|
if (a >= 'A')
|
|
|
|
a -= ('A' - 10);
|
|
|
|
else
|
|
|
|
a -= '0';
|
|
|
|
if (b >= 'a')
|
|
|
|
b -= 'a'-'A';
|
|
|
|
if (b >= 'A')
|
|
|
|
b -= ('A' - 10);
|
|
|
|
else
|
|
|
|
b -= '0';
|
|
|
|
*dst++ = 16*a+b;
|
|
|
|
src+=3;
|
|
|
|
} else if (*src == '+') {
|
|
|
|
*dst++ = ' ';
|
|
|
|
src++;
|
|
|
|
} else {
|
|
|
|
*dst++ = *src++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dst++ = '\0';
|
|
|
|
return retbuf;
|
2017-03-20 23:16:21 +08:00
|
|
|
}
|
|
|
|
|
2019-09-17 23:11:22 +08:00
|
|
|
void string_toupper( std::string& str) {
|
|
|
|
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
|
|
|
|
}
|
|
|
|
|
2018-09-22 03:57:48 +08:00
|
|
|
void touch(const char *pathname) {
|
|
|
|
int fd = open(pathname,
|
|
|
|
O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK,
|
|
|
|
0666);
|
|
|
|
if ( fd < 0 ) {
|
|
|
|
// Couldn't open that path.
|
2019-01-16 00:36:13 +08:00
|
|
|
Error("Couldn't open() path %s in touch", pathname);
|
2018-09-22 03:57:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int rc = utimensat(AT_FDCWD,
|
|
|
|
pathname,
|
|
|
|
nullptr,
|
|
|
|
0);
|
|
|
|
if ( rc ) {
|
|
|
|
Error("Couldn't utimensat() path %s in touch", pathname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|