fork/slippc
git clone https://git.julia.blue/fork/slippc
802ebe1
0.8.1
1#ifndef UTIL_H_ 2#define UTIL_H_ 3 4#if defined(__GNUC__ )|| defined(__GNUG__ ) 5#define swap32 __builtin_bswap32 6#define swap16 __builtin_bswap16 7#elif defined(_MSC_VER ) 8#define swap32 _byteswap_ulong 9#define swap16 _byteswap_ushort 10#include <intrin.h> 11#endif 12 13#include <string.h> 14#include <iomanip> 15#include <iostream> 16#include <sstream> 17#include <fstream> 18#include <codecvt> 19#include <algorithm> //std::find 20#include <sys/stat.h> //std::find 21#include <filesystem> 22 23#include "lzma.h" 24#include "picohash.h" 25#include "shiftjis.h" 26 27#define BYTE8 (b1 ,b2 ,b3 ,b4 ,b5 ,b6 ,b7 ,b8 ) (*((uint64_t*)(uint8_t[]){b1,b2,b3,b4,b5,b6,b7,b8})) 28#define BYTE4 (b1 ,b2 ,b3 ,b4 ) (*((uint32_t*)(uint8_t[]){b1,b2,b3,b4})) 29#define BYTE2 (b1 ,b2 ) (*((uint16_t*)(uint8_t[]){b1,b2})) 30 31const uint64_t SLP_HEADER = BYTE8 (0x7b ,0x55 ,0x03 ,0x72 ,0x61 ,0x77 ,0x5b ,0x24 );// {U.raw[$ 32const uint32_t LZMA_HEADER = BYTE4 (0xfd ,0x37 ,0x7a ,0x58 ); 33 34const unsigned N_HEADER_BYTES = 15 ;//Header is always 15 bytes 35const unsigned MIN_EV_PAYLOAD_SIZE = 14 ;//Payloads, game start, pre frame, post frame, game end always defined 36const unsigned MIN_GAME_START_SIZE = 353 ;//Minimum size for game start event (necessary for all replays) 37const unsigned MIN_REPLAY_LENGTH = N_HEADER_BYTES + MIN_EV_PAYLOAD_SIZE + MIN_GAME_START_SIZE ; 38 39// Version convenience macros 40#define MIN_VERSION (maj ,min ,rev ) (_slippi_maj > (maj)) || (_slippi_maj == (maj) && ( (_slippi_min > (min)) || (_slippi_min == (min) && _slippi_rev >= (rev)) )) 41#define MAX_VERSION (maj ,min ,rev ) (_slippi_maj < (maj)) || (_slippi_maj == (maj) && ( (_slippi_min < (min)) || (_slippi_min == (min) && _slippi_rev < (rev)) )) 42#define GET_VERSION () (std::to_string(int(_slippi_maj))+"."+std::to_string(int(_slippi_min))+"."+std::to_string(int(_slippi_rev))) 43#define ENCODE_VERSION_MIN (min ) (_encode_ver == 0 || _encode_ver >= min) //also allows unencoded files 44 45// Convenience pseudo-typedefs 46#define PATH std::filesystem::path 47 48// Variable checking whether error log has been initialized 49static bool errlog_init = false; 50 51//Debug output convenience macros 52#define DOUT1 (s ) if (_debug >= 1) { std::cerr << " " << BLU << "DEBUG 1: " << BLN << s << std::endl; } 53#define DOUT2 (s ) if (_debug >= 2) { std::cerr << " " << BLU << "DEBUG 2: " << BLN << s << std::endl; } 54#define DOUT3 (s ) if (_debug >= 3) { std::cerr << " " << BLU << "DEBUG 3: " << BLN << s << std::endl; } 55#define INFO (e ) std::cerr << " " << GRN << " INFO: " << BLN << e << std::endl; 56#define WARN (e ) std::cerr << " " << YLW << "WARNING: " << BLN << e << std::endl; 57#define FAIL (e ) std::cerr << " " << RED << " ERROR: " << BLN << e << std::endl; 58#define YIKES (e ) std::cerr << " " << CRT << " YIKES: " << BLN << e << std::endl; 59#define WARN_CORRUPT (e ) std::cerr << " " << YLW << "WARNING: " << BLN << e << "; replay may be corrupt" << std::endl; 60#define FAIL_CORRUPT (e ) std::cerr << " " << RED << " ERROR: " << BLN << e << "; cannot continue parsing" << std::endl; 61#define _LOG (e ) \ 62 std::cerr << " " << MGN << " LOG: " << BLN << e << std::endl; 63#define LOG (e ,f ) {\ 64 std::cerr << " " << MGN << " LOG: " << BLN << e << std::endl; \ 65 std::ofstream log(f, std::ios_base::app | std::ios_base::out); \ 66 log << " [" << timestamp() << "] " << e << std::endl; \ 67 log.close(); }; 68#define ERRLOG (path ,e ) \ 69 PATH errorpath = (path / "_errors.txt"); \ 70 if (!errlog_init) { \ 71 errlog_init = true; \ 72 _LOG("Logging errors to " << MGN << errorpath << BLN); \ 73 LOG("Log opened",errorpath); \ 74 } \ 75 LOG(e,errorpath); 76 77// ANSI color codes don't work on Windows (I think?) 78#ifdef _WIN32 79#define BLK "" 80#define RED "" 81#define GRN "" 82#define YLW "" 83#define BLU "" 84#define MGN "" 85#define CYN "" 86#define WHT "" 87#define CRT "" 88#define BLN "" 89#else 90#define BLK "\u001b[30m" 91#define RED "\u001b[31m" 92#define GRN "\u001b[32m" 93#define YLW "\u001b[33m" 94#define BLU "\u001b[34m" 95#define MGN "\u001b[35m" 96#define CYN "\u001b[36m" 97#define WHT "\u001b[37m" 98#define CRT "\u001b[41m" 99#define BLN "\u001b[0m" 100#endif 101 102namespace slip { 103 104//Indent Level 105#define ILEV 1 106 107//Strings for various indentation amounts 108const std ::string SPACE [10 ]= { 109"" , 110" " , 111" " , 112" " , 113" " , 114" " , 115" " , 116" " , 117" " , 118" " , 119}; 120 121static const std ::string base64_chars = 122"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 123"abcdefghijklmnopqrstuvwxyz" 124"0123456789+/" ; 125 126 127static inline bool is_base64 (unsigned char c ) { 128return (isalnum (c )|| (c == '+' )|| (c == '/' )); 129} 130 131//Hex Output stuff 132 133struct HexCharStruct { 134unsigned char c ; 135HexCharStruct (unsigned char _c ) :c (_c ) { } 136}; 137 138inline std ::ostream & operator <<(std ::ostream & o ,const HexCharStruct & hs ) { 139return (o << "0 x " <<std ::hex << (int )hs .c ) <<std ::dec ; 140} 141 142inline HexCharStruct hex (unsigned char _c ) { 143return HexCharStruct (_c ); 144} 145 146inline void printBytes (char * buff ,unsigned nchars ) { 147for (unsigned i = 0 ;i < nchars ;++ i ) { 148std ::cout <<hex (buff [i ]) <<" " ; 149 } 150std ::cout <<std ::dec <<std ::endl ; 151} 152 153//Bitwise comparison of 8-bytes array with uint_64 154inline bool same8 (char * array ,uint64_t other ) { 155return ((* ((uint64_t * )array )) ^other )== 0 ; 156} 157//Bitwise comparison of 4-bytes array with uint_32 158inline bool same4 (char * array ,uint32_t other ) { 159return ((* ((uint32_t * )array )) ^other )== 0 ; 160} 161//Load a big-endian 32-bit unsigned int from an array 162inline uint32_t readBE4U (char * array ) {return swap32 (* ((uint32_t * )array )); } 163//Load a big-endian 16-bit unsigned int from an array 164inline uint16_t readBE2U (char * array ) {return swap16 (* ((uint16_t * )array )); } 165//Load a big-endian 32-bit int from an array 166inline int32_t readBE4S (char * array ) {return swap32 (* ((int32_t * )array )); } 167//Load a big-endian 16-bit int from an array 168inline int16_t readBE2S (char * array ) {return swap16 (* ((int16_t * )array )); } 169//Load a big-endian float from an array 170inline float readBE4F (char * array ) { 171float r ; 172char * rc = (char * )& r ; 173rc [0 ]= array [3 ]; 174rc [1 ]= array [2 ]; 175rc [2 ]= array [1 ]; 176rc [3 ]= array [0 ]; 177return r ; 178} 179 180//Write a big-endian 32-bit unsigned int to an array 181inline void writeBE4F (float f ,char * a ) { 182char * wc = (char * )& f ; 183a [3 ]= wc [0 ]; 184a [2 ]= wc [1 ]; 185a [1 ]= wc [2 ]; 186a [0 ]= wc [3 ]; 187} 188 189//Write a big-endian 16-bit unsigned int to an array 190inline void writeBE2U (uint16_t i ,char * a ) { 191a [1 ]= i & 0xff ; 192a [0 ]= (i >>8 )& 0xff ; 193} 194 195//Write a big-endian 32-bit unsigned int to an array 196inline void writeBE4U (uint32_t i ,char * a ) { 197a [3 ]= i & 0xff ; 198a [2 ]= (i >>8 )& 0xff ; 199a [1 ]= (i >>16 )& 0xff ; 200a [0 ]= (i >>24 )& 0xff ; 201} 202 203//Write a big-endian 32-bit signed int to an array 204inline void writeBE4S (int32_t i ,char * a ) { 205a [3 ]= i & 0xff ; 206a [2 ]= (i >>8 )& 0xff ; 207a [1 ]= (i >>16 )& 0xff ; 208a [0 ]= (i >>24 )& 0xff ; 209} 210 211//Base64 encoder / decoder -> https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp 212inline std ::string base64_encode (unsigned char const * bytes_to_encode ,unsigned int in_len ) { 213std ::string ret ; 214int i = 0 ; 215int j = 0 ; 216unsigned char char_array_3 [3 ]; 217unsigned char char_array_4 [4 ]; 218 219while (in_len -- ) { 220char_array_3 [i ++ ]= * (bytes_to_encode ++ ); 221if (i == 3 ) { 222char_array_4 [0 ]= (char_array_3 [0 ]& 0xfc ) >>2 ; 223char_array_4 [1 ]= ((char_array_3 [0 ]& 0x03 ) <<4 )+ ((char_array_3 [1 ]& 0xf0 ) >>4 ); 224char_array_4 [2 ]= ((char_array_3 [1 ]& 0x0f ) <<2 )+ ((char_array_3 [2 ]& 0xc0 ) >>6 ); 225char_array_4 [3 ]= char_array_3 [2 ]& 0x3f ; 226 227for (i = 0 ; (i < 4 ) ;i ++ ) 228ret += base64_chars [char_array_4 [i ]]; 229i = 0 ; 230 } 231 } 232 233if (i ) 234 { 235for (j = i ;j < 3 ;j ++ ) 236char_array_3 [j ]= '\0' ; 237 238char_array_4 [0 ]= (char_array_3 [0 ]& 0xfc ) >>2 ; 239char_array_4 [1 ]= ((char_array_3 [0 ]& 0x03 ) <<4 )+ ((char_array_3 [1 ]& 0xf0 ) >>4 ); 240char_array_4 [2 ]= ((char_array_3 [1 ]& 0x0f ) <<2 )+ ((char_array_3 [2 ]& 0xc0 ) >>6 ); 241 242for (j = 0 ; (j < i + 1 );j ++ ) 243ret += base64_chars [char_array_4 [j ]]; 244 245while ((i ++ < 3 )) 246ret += '=' ; 247 248 } 249 250return ret ; 251 252} 253 254//Base64 encoder / decoder -> https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp 255inline std ::string base64_decode (std ::string const & encoded_string ) { 256int in_len = encoded_string .size (); 257int i = 0 ; 258int j = 0 ; 259int in_ = 0 ; 260unsigned char char_array_4 [4 ],char_array_3 [3 ]; 261std ::string ret ; 262 263while (in_len -- && (encoded_string [in_ ]!= '=' )&& is_base64 (encoded_string [in_ ])) { 264char_array_4 [i ++ ]= encoded_string [in_ ];in_ ++ ; 265if (i == 4 ) { 266for (i = 0 ;i < 4 ;i ++ ) 267char_array_4 [i ]= base64_chars .find (char_array_4 [i ]); 268 269char_array_3 [0 ]= (char_array_4 [0 ] <<2 )+ ((char_array_4 [1 ]& 0x30 ) >>4 ); 270char_array_3 [1 ]= ((char_array_4 [1 ]& 0xf ) <<4 )+ ((char_array_4 [2 ]& 0x3c ) >>2 ); 271char_array_3 [2 ]= ((char_array_4 [2 ]& 0x3 ) <<6 )+ char_array_4 [3 ]; 272 273for (i = 0 ; (i < 3 );i ++ ) 274ret += char_array_3 [i ]; 275i = 0 ; 276 } 277 } 278 279if (i ) { 280for (j = 0 ;j < i ;j ++ ) 281char_array_4 [j ]= base64_chars .find (char_array_4 [j ]); 282 283char_array_3 [0 ]= (char_array_4 [0 ] <<2 )+ ((char_array_4 [1 ]& 0x30 ) >>4 ); 284char_array_3 [1 ]= ((char_array_4 [1 ]& 0xf ) <<4 )+ ((char_array_4 [2 ]& 0x3c ) >>2 ); 285 286for (j = 0 ; (j < i - 1 );j ++ )ret += char_array_3 [j ]; 287 } 288 289return ret ; 290} 291 292// https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784 293inline std ::string escape_json (const std ::string & s ) { 294std ::ostringstream o ; 295for (autoc = s. cbegin (); c != s. cend (); c ++ ) { 296switch ( * c) { 297case '"' : o << "\\\"" ; break ; 298case '\\' : o << "\\\\" ; break ; 299case '\b' : o << "\\b" ; break ; 300case '\f' : o << "\\f" ; break ; 301case '\n' : o << "\\n" ; break ; 302case '\r' : o << "\\r" ; break ; 303case '\t' : o << "\\t" ; break ; 304default : 305if ( '\x00' <= * c && * c <= '\x1f' ) { 306o << "\\u" 307<< std::hex << std:: setw ( 4 ) << std:: setfill ( '0' ) << ( int ) * c; 308} else { 309o << * c; 310} 311} 312} 313return o. str (); 314} 315 316inline std ::string to_utf8 ( const std ::u16string & s) { 317std::wstring_convert < std::codecvt_utf8 < char16_t > , char16_t > conv; 318std :: string u = conv. to_bytes (s); 319u. erase (std:: find (u. begin (), u. end (), '\0' ), u. end ()); 320return u; 321} 322 323inline std ::string decode_shift_jis ( char * addr) { 324std :: u16string tag; 325for ( unsigned n = 0 ; n < 16 ; n += 2 ) { 326uint16_t b = ( readBE2U (addr + n)); 327if ((b>> 8 ) == 0x82 ) { 328if ( (b & 0xff ) < 0x59 ) { //Roman numerals 329b -= 0x821f ; 330} else if ( (b & 0xff ) < 0x80 ) { //Roman letters 331b -= 0x81ff ; 332} else { //Hiragana 333b -= 0x525e ; 334} 335} else if ((b>> 8 ) == 0x83 ) { 336if ( (b & 0xff ) < 0x80 ) { //Katakana, part 1 337b -= 0x529f ; 338} else { //Katakana, part 2 339b -= 0x52a0 ; 340} 341} else if ((b>> 8 ) == 0x81 ) { //Miscellaneous characters 342switch (b & 0xff ) { 343case ( 0x81 ): b = 0x003D ; break ; // = 344case ( 0x5b ): b = 0x30FC ; break ; // Hiragana Wave Dash (lol) 345case ( 0x7c ): b = 0x002D ; break ; // - 346case ( 0x7b ): b = 0x002B ; break ; // + 347case ( 0x49 ): b = 0x0021 ; break ; // ! 348case ( 0x48 ): b = 0x003F ; break ; // ? 349case ( 0x97 ): b = 0x0040 ; break ; // @ 350case ( 0x93 ): b = 0x0025 ; break ; // % 351case ( 0x95 ): b = 0x0026 ; break ; // & 352case ( 0x90 ): b = 0x0024 ; break ; // $ 353case ( 0x44 ): b = 0x002E ; break ; // . 354case ( 0x60 ): b = 0x301C ; break ; // Japanese tilde 355case ( 0x42 ): b = 0x3002 ; break ; // Japanese period 356case ( 0x41 ): b = 0x3001 ; break ; // Japanese comma 357case ( 0x40 ): b = 0x3000 ; break ; // Space 358default : 359// DOUT1(" Encountered unknown character in tag: " << b << std::endl); 360b = 0 ; 361break ; 362} 363} else { 364if (b != 0 ) { 365// DOUT1(" Encountered unknown character in tag: " << b << std::endl); 366} 367b = 0 ; 368} 369tag += b; 370if (b == 0 ) { 371break ; 372} 373} 374return to_utf8 (tag); 375} 376 377// From: https://stackoverflow.com/questions/33165171/c-shiftjis-to-utf8-conversion 378inline std ::string sj2utf8 ( const std ::string & input) { 379std :: string output( 3 * input. length (), ' ' ); //ShiftJis won't give 4byte UTF8, so max. 3 byte per input char are needed 380size_t indexInput = 0 , indexOutput = 0 ; 381 382while (indexInput < input. length ()) 383{ 384char arraySection = (( uint8_t )input[indexInput]) >> 4 ; 385 386size_t arrayOffset; 387if (arraySection == 0x8 ) arrayOffset = 0x100 ; //these are two-byte shiftjis 388else if (arraySection == 0x9 ) arrayOffset = 0x1100 ; 389else if (arraySection == 0xE ) arrayOffset = 0x2100 ; 390else arrayOffset = 0 ; //this is one byte shiftjis 391 392//determining real array offset 393if (arrayOffset) 394{ 395arrayOffset += ((( uint8_t )input[indexInput]) & 0xf ) << 8 ; 396indexInput ++ ; 397if (indexInput >= input. length ()) break ; 398} 399arrayOffset += ( uint8_t )input[indexInput ++ ]; 400arrayOffset <<= 1 ; 401 402//unicode number is... 403uint16_t unicodeValue = (shiftJIS_convTable[arrayOffset] << 8 ) | shiftJIS_convTable[arrayOffset + 1 ]; 404 405//converting to UTF8 406if (unicodeValue < 0x80 ) 407{ 408output[indexOutput ++ ] = unicodeValue; 409} 410else if (unicodeValue < 0x800 ) 411{ 412output[indexOutput ++ ] = 0xC0 | (unicodeValue >> 6 ); 413output[indexOutput ++ ] = 0x80 | (unicodeValue & 0x3f ); 414} 415else 416{ 417output[indexOutput ++ ] = 0xE0 | (unicodeValue >> 12 ); 418output[indexOutput ++ ] = 0x80 | ((unicodeValue & 0xfff ) >> 6 ); 419output[indexOutput ++ ] = 0x80 | (unicodeValue & 0x3f ); 420} 421} 422 423output. resize (indexOutput); //remove the unnecessary bytes 424return output; 425} 426 427inline std ::string parseConnCode ( const std ::string & input) { 428std :: string s = sj2utf8 (input); 429size_t index = s. find ( "#" , 0 ); //repace 2-byte shift-jis # with ascii one 430if (index != std::string::npos) { 431s. replace (index, 3 , "#" ); 432} 433return s; 434} 435 436inline std ::string floatToBinary ( float f) { 437union { float f ; uint32_t i ; } u; 438u. f = f; 439std :: string s; 440for ( int i = 0 ; i < 32 ; i ++ ) { 441if (u. i % 2 ) s. push_back ( '1' ); 442else s. push_back ( '0' ); 443u. i >>= 1 ; 444} 445// Reverse the string since now it's backwards 446std:: string temp(s. rbegin (), s. rend ()); 447return temp; 448} 449 450// http://ptspts.blogspot.com/2011/11/how-to-simply-compress-c-string-with.html 451inline std ::string compressWithLzma ( const char * in, const size_t inlen, int level = 6 ) { 452std ::string result; 453result. resize (inlen + (inlen >> 2 ) + 128 ); 454size_t out_pos = 0 ; 455if ( LZMA_OK != lzma_easy_buffer_encode ( 456level, LZMA_CHECK_CRC32 , NULL , 457reinterpret_cast < const uint8_t *> (in), inlen, 458reinterpret_cast < uint8_t *> ( & result[ 0 ]), & out_pos, result. size ())) 459abort (); 460result. resize (out_pos); 461return result; 462} 463 464inline std ::string decompressWithLzma ( const uint8_t * in, const size_t inlen) { 465static const size_t kMemLimit = 1 << 30 ; // 1 GB. 466lzma_stream strm = LZMA_STREAM_INIT ; 467std :: string result; 468result. resize ( 8192 ); 469size_t result_used = 0 ; 470lzma_ret ret; 471ret = lzma_stream_decoder ( & strm, kMemLimit, LZMA_CONCATENATED ); 472if (ret != LZMA_OK ) 473abort (); 474size_t avail0 = result. size (); 475strm. next_in = in; 476strm. avail_in = inlen; 477strm. next_out = reinterpret_cast < uint8_t *> ( & result[ 0 ]); 478strm. avail_out = avail0; 479while (true) { 480ret = lzma_code ( & strm, strm. avail_in == 0 ? LZMA_FINISH : LZMA_RUN ); 481if (ret == LZMA_STREAM_END ) { 482result_used += avail0 - strm. avail_out ; 483if ( 0 != strm. avail_in ) // Guaranteed by lzma_stream_decoder(). 484abort (); 485result. resize (result_used); 486lzma_end ( & strm); 487return result; 488} 489if (ret != LZMA_OK ) 490abort (); 491if (strm. avail_out == 0 ) { 492result_used += avail0 - strm. avail_out ; 493result. resize (result. size () << 1 ); 494strm. next_out = reinterpret_cast < uint8_t *> ( & result[ 0 ] + result_used); 495strm. avail_out = avail0 = result. size () - result_used; 496} 497} 498} 499 500inline std ::string decompressWithLzma ( const char * in, const size_t inlen) { 501return decompressWithLzma (reinterpret_cast < const uint8_t *> ( & in[ 0 ]),inlen); 502} 503 504inline bool fileExists ( std ::string fname) { 505std :: ifstream i ( fname . c_str ()); 506return i. good (); 507} 508 509// https://stackoverflow.com/questions/667183/padding-stl-strings-in-c 510inline std ::string padString ( std ::string str, const size_t num, const char paddingChar = ' ' ) { 511if (num > str. size ()) 512str. insert ( 0 , num - str. size (), paddingChar); 513return str; 514} 515 516inline std ::string padString ( float f, const size_t num, const char paddingChar = ' ' ) { 517return padString (std:: to_string (f),num,paddingChar); 518} 519 520inline std::string padString ( int i, const size_t num, const char paddingChar = ' ' ) { 521return padString (std:: to_string (i),num,paddingChar); 522} 523 524 525inline int diffBits ( uint16_t u1, uint16_t u2) { 526int count = 0 ; 527u1 ^= u2; 528for ( int i = 0 ; i < 16 ; i ++ ) { 529if ((u1 >> i) & 1 ) { 530++ count; 531} 532} 533return count; 534} 535 536inline int countBits ( uint16_t u1) { 537return diffBits (u1, 0 ); 538} 539 540inline int diffBits ( float f1, float f2) { 541union { float f ; uint32_t u ; } n1, n2; 542n1. f = f1; 543n2. f = f2; 544int count = 0 ; 545n1. u ^= n2. u ; 546for ( int i = 0 ; i < 32 ; i ++ ) { 547if ((n1. u >> i) & 1 ) { 548++ count; 549} 550} 551return count; 552} 553 554inline std ::string getFileBase ( std ::string f) { 555int extpos = f. find_last_of ( "." ); 556return f. substr ( 0 ,extpos); 557} 558 559 560inline std ::string getFileExt ( std ::string f) { 561int extpos = f. find_last_of ( "." ); 562return f. substr (extpos + 1 ,f. size () - (extpos + 1 )); 563} 564 565inline void stringtoChars ( std ::string s, char ** c) { 566* c = new char[s. size () + 1 ]; 567strcpy ( * c, s. c_str ()); 568} 569 570inline std ::string md5tostring ( unsigned char * digest) { 571std :: stringstream ss; 572ss << std::hex << std:: setfill ( '0' ); 573for ( int i = 0 ; i < 16 ; ++ i) { 574ss << std:: setw ( 2 ) << static_cast < unsigned > (digest[i]); 575} 576return ss. str (); 577} 578 579inline std ::string md5data ( unsigned char * buffer, size_t length) { 580picohash_ctx_t ctx; 581unsigned char digest[ PICOHASH_MD5_DIGEST_LENGTH ]; 582picohash_init_md5 ( & ctx); 583picohash_update ( & ctx, buffer, length); 584picohash_final ( & ctx, digest); 585 586return md5tostring (digest); 587} 588 589inline std ::string md5file ( std ::string fname, bool compressed = false) { 590FILE * f = fopen (fname. c_str (), "r" ); 591fseek (f, 0 , SEEK_END ); 592size_t length = ftell (f); 593fseek (f, 0 , SEEK_SET ); 594unsigned char * b = ( unsigned char * ) malloc (length); 595fread (b, 1 ,length,f); 596fclose (f); 597 598if (compressed) { 599std :: string decomp = decompressWithLzma (b, length); 600// Get the new file size 601length = decomp. size (); 602// Delete the old read buffer 603free (b); 604// Reallocate it with more spce 605b = ( unsigned char * ) malloc (length); 606// Copy buffer from the decompressed string 607memcpy (b,decomp. c_str (),length); 608} 609 610std :: string m = md5data (b, length); 611 612free (b); 613 614return m; 615} 616 617inline std ::string md5compressed ( std ::string fname) { 618return md5file (fname,true); 619} 620 621inline bool isDirectory ( const char * path) { 622struct stat s; 623if ( stat (path, & s) == 0 ) { 624if ( s. st_mode & S_IFDIR ) { 625return true; 626} 627} 628return false; 629} 630 631inline bool makeDirectoryIfNotExists ( const char * path) { 632struct stat s; 633if ( stat (path, & s) == 0 ) { 634if ( s. st_mode & S_IFDIR ) { 635return true; //path exists and is a directory 636} 637return false; //path exists but is not a directory 638} 639return std::filesystem:: create_directories (path); 640} 641 642inline auto timestamp() { 643std :: time_t time_now = std:: time ( nullptr ); 644return std:: put_time (std:: localtime ( & time_now), "%Y-%m-%d %OH:%OM:%OS" ); 645} 646 647inline bool ensureExt ( const char * ext, const char * fname) { 648std :: string fs ( fname ); 649return (fs. substr (fs. length () - 4 , 4 ). compare (ext) == 0 ); 650} 651 652} 653 654#endif /* UTIL_H_ */