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