fork/slippc
git clone https://git.julia.blue/fork/slippc
70622c9
0.7.0
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 22#include "lzma.h" 23 24#define BYTE8 (b1 ,b2 ,b3 ,b4 ,b5 ,b6 ,b7 ,b8 ) (*((uint64_t*)(uint8_t[]){b1,b2,b3,b4,b5,b6,b7,b8})) 25#define BYTE4 (b1 ,b2 ,b3 ,b4 ) (*((uint32_t*)(uint8_t[]){b1,b2,b3,b4})) 26#define BYTE2 (b1 ,b2 ) (*((uint16_t*)(uint8_t[]){b1,b2})) 27 28const uint64_t SLP_HEADER = BYTE8 (0x7b ,0x55 ,0x03 ,0x72 ,0x61 ,0x77 ,0x5b ,0x24 );// {U.raw[$ 29const uint32_t LZMA_HEADER = BYTE4 (0xfd ,0x37 ,0x7a ,0x58 ); 30// const uint32_t LZMA_HEADER = BYTE4(0x37,0xfd,0x58,0x7a); 31 32 33const unsigned N_HEADER_BYTES = 15 ;//Header is always 15 bytes 34const unsigned MIN_EV_PAYLOAD_SIZE = 14 ;//Payloads, game start, pre frame, post frame, game end always defined 35const unsigned MIN_GAME_START_SIZE = 353 ;//Minimum size for game start event (necessary for all replays) 36const unsigned MIN_REPLAY_LENGTH = N_HEADER_BYTES + MIN_EV_PAYLOAD_SIZE + MIN_GAME_START_SIZE ; 37 38// Version convenience macros 39#define MIN_VERSION (maj ,min ,rev ) (_slippi_maj > (maj)) || (_slippi_maj == (maj) && ( (_slippi_min > (min)) || (_slippi_min == (min) && _slippi_rev >= (rev)) )) 40#define MAX_VERSION (maj ,min ,rev ) (_slippi_maj < (maj)) || (_slippi_maj == (maj) && ( (_slippi_min < (min)) || (_slippi_min == (min) && _slippi_rev < (rev)) )) 41 42namespace slip { 43 44//Indent Level 45#define ILEV 1 46 47//Strings for various indentation amounts 48const std ::string SPACE [10 ]= { 49"" , 50" " , 51" " , 52" " , 53" " , 54" " , 55" " , 56" " , 57" " , 58" " , 59}; 60 61static const std ::string base64_chars = 62"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 63"abcdefghijklmnopqrstuvwxyz" 64"0123456789+/" ; 65 66 67static inline bool is_base64 (unsigned char c ) { 68return (isalnum (c )|| (c == '+' )|| (c == '/' )); 69} 70 71//Hex Output stuff 72 73struct HexCharStruct { 74unsigned char c ; 75HexCharStruct (unsigned char _c ) :c (_c ) { } 76}; 77 78inline std ::ostream & operator <<(std ::ostream & o ,const HexCharStruct & hs ) { 79return (o << "0 x " <<std ::hex << (int )hs .c ) <<std ::dec ; 80} 81 82inline HexCharStruct hex (unsigned char _c ) { 83return HexCharStruct (_c ); 84} 85 86inline void printBytes (char * buff ,unsigned nchars ) { 87for (unsigned i = 0 ;i < nchars ;++ i ) { 88std ::cout <<hex (buff [i ]) <<" " ; 89 } 90std ::cout <<std ::dec <<std ::endl ; 91} 92 93//Bitwise comparison of 8-bytes array with uint_64 94inline bool same8 (char * array ,uint64_t other ) { 95return ((* ((uint64_t * )array )) ^other )== 0 ; 96} 97//Bitwise comparison of 4-bytes array with uint_32 98inline bool same4 (char * array ,uint32_t other ) { 99return ((* ((uint32_t * )array )) ^other )== 0 ; 100} 101//Load a big-endian 32-bit unsigned int from an array 102inline uint32_t readBE4U (char * array ) {return swap32 (* ((uint32_t * )array )); } 103//Load a big-endian 16-bit unsigned int from an array 104inline uint16_t readBE2U (char * array ) {return swap16 (* ((uint16_t * )array )); } 105//Load a big-endian 32-bit int from an array 106inline int32_t readBE4S (char * array ) {return swap32 (* ((int32_t * )array )); } 107//Load a big-endian 16-bit int from an array 108inline int16_t readBE2S (char * array ) {return swap16 (* ((int16_t * )array )); } 109//Load a big-endian float from an array 110inline float readBE4F (char * array ) { 111float r ; 112char * rc = (char * )& r ; 113rc [0 ]= array [3 ]; 114rc [1 ]= array [2 ]; 115rc [2 ]= array [1 ]; 116rc [3 ]= array [0 ]; 117return r ; 118} 119 120//Write a big-endian 32-bit unsigned int to an array 121inline void writeBE4F (float f ,char * a ) { 122char * wc = (char * )& f ; 123a [3 ]= wc [0 ]; 124a [2 ]= wc [1 ]; 125a [1 ]= wc [2 ]; 126a [0 ]= wc [3 ]; 127} 128 129//Write a big-endian 16-bit unsigned int to an array 130inline void writeBE2U (uint16_t i ,char * a ) { 131a [1 ]= i & 0xff ; 132a [0 ]= (i >>8 )& 0xff ; 133} 134 135//Write a big-endian 32-bit unsigned int to an array 136inline void writeBE4U (uint32_t i ,char * a ) { 137a [3 ]= i & 0xff ; 138a [2 ]= (i >>8 )& 0xff ; 139a [1 ]= (i >>16 )& 0xff ; 140a [0 ]= (i >>24 )& 0xff ; 141} 142 143//Write a big-endian 32-bit signed int to an array 144inline void writeBE4S (int32_t i ,char * a ) { 145a [3 ]= i & 0xff ; 146a [2 ]= (i >>8 )& 0xff ; 147a [1 ]= (i >>16 )& 0xff ; 148a [0 ]= (i >>24 )& 0xff ; 149} 150 151//Base64 encoder / decoder -> https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp 152inline std ::string base64_encode (unsigned char const * bytes_to_encode ,unsigned int in_len ) { 153std ::string ret ; 154int i = 0 ; 155int j = 0 ; 156unsigned char char_array_3 [3 ]; 157unsigned char char_array_4 [4 ]; 158 159while (in_len -- ) { 160char_array_3 [i ++ ]= * (bytes_to_encode ++ ); 161if (i == 3 ) { 162char_array_4 [0 ]= (char_array_3 [0 ]& 0xfc ) >>2 ; 163char_array_4 [1 ]= ((char_array_3 [0 ]& 0x03 ) <<4 )+ ((char_array_3 [1 ]& 0xf0 ) >>4 ); 164char_array_4 [2 ]= ((char_array_3 [1 ]& 0x0f ) <<2 )+ ((char_array_3 [2 ]& 0xc0 ) >>6 ); 165char_array_4 [3 ]= char_array_3 [2 ]& 0x3f ; 166 167for (i = 0 ; (i < 4 ) ;i ++ ) 168ret += base64_chars [char_array_4 [i ]]; 169i = 0 ; 170 } 171 } 172 173if (i ) 174 { 175for (j = i ;j < 3 ;j ++ ) 176char_array_3 [j ]= '\0' ; 177 178char_array_4 [0 ]= (char_array_3 [0 ]& 0xfc ) >>2 ; 179char_array_4 [1 ]= ((char_array_3 [0 ]& 0x03 ) <<4 )+ ((char_array_3 [1 ]& 0xf0 ) >>4 ); 180char_array_4 [2 ]= ((char_array_3 [1 ]& 0x0f ) <<2 )+ ((char_array_3 [2 ]& 0xc0 ) >>6 ); 181 182for (j = 0 ; (j < i + 1 );j ++ ) 183ret += base64_chars [char_array_4 [j ]]; 184 185while ((i ++ < 3 )) 186ret += '=' ; 187 188 } 189 190return ret ; 191 192} 193 194//Base64 encoder / decoder -> https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp 195inline std ::string base64_decode (std ::string const & encoded_string ) { 196int in_len = encoded_string .size (); 197int i = 0 ; 198int j = 0 ; 199int in_ = 0 ; 200unsigned char char_array_4 [4 ],char_array_3 [3 ]; 201std ::string ret ; 202 203while (in_len -- && (encoded_string [in_ ]!= '=' )&& is_base64 (encoded_string [in_ ])) { 204char_array_4 [i ++ ]= encoded_string [in_ ];in_ ++ ; 205if (i == 4 ) { 206for (i = 0 ;i < 4 ;i ++ ) 207char_array_4 [i ]= base64_chars .find (char_array_4 [i ]); 208 209char_array_3 [0 ]= (char_array_4 [0 ] <<2 )+ ((char_array_4 [1 ]& 0x30 ) >>4 ); 210char_array_3 [1 ]= ((char_array_4 [1 ]& 0xf ) <<4 )+ ((char_array_4 [2 ]& 0x3c ) >>2 ); 211char_array_3 [2 ]= ((char_array_4 [2 ]& 0x3 ) <<6 )+ char_array_4 [3 ]; 212 213for (i = 0 ; (i < 3 );i ++ ) 214ret += char_array_3 [i ]; 215i = 0 ; 216 } 217 } 218 219if (i ) { 220for (j = 0 ;j < i ;j ++ ) 221char_array_4 [j ]= base64_chars .find (char_array_4 [j ]); 222 223char_array_3 [0 ]= (char_array_4 [0 ] <<2 )+ ((char_array_4 [1 ]& 0x30 ) >>4 ); 224char_array_3 [1 ]= ((char_array_4 [1 ]& 0xf ) <<4 )+ ((char_array_4 [2 ]& 0x3c ) >>2 ); 225 226for (j = 0 ; (j < i - 1 );j ++ )ret += char_array_3 [j ]; 227 } 228 229return ret ; 230} 231 232// https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784 233inline std ::string escape_json (const std ::string & s ) { 234std ::ostringstream o ; 235for (autoc = s. cbegin (); c != s. cend (); c ++ ) { 236if ( * c == '"' || * c == '\\' || ( '\x00' <= * c && * c <= '\x1f' )) { 237o << "\\u" 238<< std::hex << std:: setw ( 4 ) << std:: setfill ( '0' ) << ( int ) * c; 239} else { 240o << * c; 241} 242} 243return o. str (); 244} 245 246inline std ::string to_utf8 ( const std ::u16string & s) { 247std::wstring_convert < std::codecvt_utf8 < char16_t > , char16_t > conv; 248std :: string u = conv. to_bytes (s); 249u. erase (std:: find (u. begin (), u. end (), '\0' ), u. end ()); 250return u; 251} 252 253inline std ::string decode_shift_jis ( char * addr) { 254std :: u16string tag; 255for ( unsigned n = 0 ; n < 16 ; n += 2 ) { 256uint16_t b = ( readBE2U (addr + n)); 257if ((b>> 8 ) == 0x82 ) { 258if ( (b & 0xff ) < 0x59 ) { //Roman numerals 259b -= 0x821f ; 260} else if ( (b & 0xff ) < 0x80 ) { //Roman letters 261b -= 0x81ff ; 262} else { //Hiragana 263b -= 0x525e ; 264} 265} else if ((b>> 8 ) == 0x83 ) { 266if ( (b & 0xff ) < 0x80 ) { //Katakana, part 1 267b -= 0x529f ; 268} else { //Katakana, part 2 269b -= 0x52a0 ; 270} 271} else if ((b>> 8 ) == 0x81 ) { //Miscellaneous characters 272switch (b & 0xff ) { 273case ( 0x81 ): b = 0x003D ; break ; // = 274case ( 0x5b ): b = 0x30FC ; break ; // Hiragana Wave Dash (lol) 275case ( 0x7c ): b = 0x002D ; break ; // - 276case ( 0x7b ): b = 0x002B ; break ; // + 277case ( 0x49 ): b = 0x0021 ; break ; // ! 278case ( 0x48 ): b = 0x003F ; break ; // ? 279case ( 0x97 ): b = 0x0040 ; break ; // @ 280case ( 0x93 ): b = 0x0025 ; break ; // % 281case ( 0x95 ): b = 0x0026 ; break ; // & 282case ( 0x90 ): b = 0x0024 ; break ; // $ 283case ( 0x44 ): b = 0x002E ; break ; // . 284case ( 0x60 ): b = 0x301C ; break ; // Japanese tilde 285case ( 0x42 ): b = 0x3002 ; break ; // Japanese period 286case ( 0x41 ): b = 0x3001 ; break ; // Japanese comma 287case ( 0x40 ): b = 0x3000 ; break ; // Space 288default : 289// DOUT1(" Encountered unknown character in tag: " << b << std::endl); 290b = 0 ; 291break ; 292} 293} else { 294if (b != 0 ) { 295// DOUT1(" Encountered unknown character in tag: " << b << std::endl); 296} 297b = 0 ; 298} 299tag += b; 300if (b == 0 ) { 301break ; 302} 303} 304return to_utf8 (tag); 305} 306 307inline std ::string floatToBinary ( float f) { 308union { float f ; uint32_t i ; } u; 309u. f = f; 310std :: string s; 311for ( int i = 0 ; i < 32 ; i ++ ) { 312if (u. i % 2 ) s. push_back ( '1' ); 313else s. push_back ( '0' ); 314u. i >>= 1 ; 315} 316// Reverse the string since now it's backwards 317std:: string temp(s. rbegin (), s. rend ()); 318return temp; 319} 320 321// http://ptspts.blogspot.com/2011/11/how-to-simply-compress-c-string-with.html 322inline std ::string compressWithLzma ( const std ::string & in, int level = 6 ) { 323std ::string result; 324result. resize (in. size () + (in. size () >> 2 ) + 128 ); 325size_t out_pos = 0 ; 326if ( LZMA_OK != lzma_easy_buffer_encode ( 327level, LZMA_CHECK_CRC32 , NULL , 328reinterpret_cast < uint8_t *> (const_cast < char *> (in. data ())), in. size (), 329reinterpret_cast < uint8_t *> ( & result[ 0 ]), & out_pos, result. size ())) 330abort (); 331result. resize (out_pos); 332return result; 333} 334 335inline std ::string decompressWithLzma ( const std ::string & in) { 336static const size_t kMemLimit = 1 << 30 ; // 1 GB. 337lzma_stream strm = LZMA_STREAM_INIT ; 338std :: string result; 339result. resize ( 8192 ); 340size_t result_used = 0 ; 341lzma_ret ret; 342ret = lzma_stream_decoder ( & strm, kMemLimit, LZMA_CONCATENATED ); 343if (ret != LZMA_OK ) 344abort (); 345size_t avail0 = result. size (); 346strm. next_in = reinterpret_cast < const uint8_t *> (in. data ()); 347strm. avail_in = in. size (); 348strm. next_out = reinterpret_cast < uint8_t *> ( & result[ 0 ]); 349strm. avail_out = avail0; 350while (true) { 351ret = lzma_code ( & strm, strm. avail_in == 0 ? LZMA_FINISH : LZMA_RUN ); 352if (ret == LZMA_STREAM_END ) { 353result_used += avail0 - strm. avail_out ; 354if ( 0 != strm. avail_in ) // Guaranteed by lzma_stream_decoder(). 355abort (); 356result. resize (result_used); 357lzma_end ( & strm); 358return result; 359} 360if (ret != LZMA_OK ) 361abort (); 362if (strm. avail_out == 0 ) { 363result_used += avail0 - strm. avail_out ; 364result. resize (result. size () << 1 ); 365strm. next_out = reinterpret_cast < uint8_t *> ( & result[ 0 ] + result_used); 366strm. avail_out = avail0 = result. size () - result_used; 367} 368} 369} 370 371inline bool fileExists ( std ::string fname) { 372std :: ifstream i ( fname . c_str ()); 373return i. good (); 374} 375 376// https://stackoverflow.com/questions/667183/padding-stl-strings-in-c 377inline std ::string padString ( std ::string str, const size_t num, const char paddingChar = ' ' ) { 378if (num > str. size ()) 379str. insert ( 0 , num - str. size (), paddingChar); 380return str; 381} 382 383inline std ::string padString ( float f, const size_t num, const char paddingChar = ' ' ) { 384return padString (std:: to_string (f),num,paddingChar); 385} 386 387inline std::string padString ( int i, const size_t num, const char paddingChar = ' ' ) { 388return padString (std:: to_string (i),num,paddingChar); 389} 390 391 392inline int diffBits ( uint16_t u1, uint16_t u2) { 393int count = 0 ; 394u1 ^= u2; 395for ( int i = 0 ; i < 16 ; i ++ ) { 396if ((u1 >> i) & 1 ) { 397++ count; 398} 399} 400return count; 401} 402 403inline int countBits ( uint16_t u1) { 404return diffBits (u1, 0 ); 405} 406 407inline int diffBits ( float f1, float f2) { 408union { float f ; uint32_t u ; } n1, n2; 409n1. f = f1; 410n2. f = f2; 411int count = 0 ; 412n1. u ^= n2. u ; 413for ( int i = 0 ; i < 32 ; i ++ ) { 414if ((n1. u >> i) & 1 ) { 415++ count; 416} 417} 418return count; 419} 420 421inline std ::string getFileBase ( std ::string f) { 422int extpos = f. find_last_of ( "." ); 423return f. substr ( 0 ,extpos); 424} 425 426 427inline std ::string getFileExt ( std ::string f) { 428int extpos = f. find_last_of ( "." ); 429return f. substr (extpos + 1 ,f. size () - (extpos + 1 )); 430} 431 432inline void stringtoChars ( std ::string s, char ** c) { 433* c = new char[s. size () + 1 ]; 434strcpy ( * c, s. c_str ()); 435} 436 437} 438 439#endif /* UTIL_H_ */