fork/slippc
git clone https://git.julia.blue/fork/slippc
27331b7
0.4.0
1#ifndef UTIL_H_ 2#define UTIL_H_ 3 4#include <iomanip> 5#include <iostream> 6#include <sstream> 7#include <fstream> 8#include <codecvt> 9#include <algorithm> //std::find 10 11namespace slip { 12 13//Indent Level 14#define ILEV 1 15 16//Strings for various indentation amounts 17const std ::string SPACE [10 ]= { 18"" , 19" " , 20" " , 21" " , 22" " , 23" " , 24" " , 25" " , 26" " , 27" " , 28}; 29 30static const std ::string base64_chars = 31"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 32"abcdefghijklmnopqrstuvwxyz" 33"0123456789+/" ; 34 35 36static inline bool is_base64 (unsigned char c ) { 37return (isalnum (c )|| (c == '+' )|| (c == '/' )); 38} 39 40//Hex Output stuff 41 42struct HexCharStruct { 43unsigned char c ; 44HexCharStruct (unsigned char _c ) :c (_c ) { } 45}; 46 47inline std ::ostream & operator <<(std ::ostream & o ,const HexCharStruct & hs ) { 48return (o << "0 x " <<std ::hex << (int )hs .c ) <<std ::dec ; 49} 50 51inline HexCharStruct hex (unsigned char _c ) { 52return HexCharStruct (_c ); 53} 54 55inline void printBytes (char * buff ,unsigned nchars ) { 56for (unsigned i = 0 ;i < nchars ;++ i ) { 57std ::cout <<hex (buff [i ]) <<" " ; 58 } 59std ::cout <<std ::dec <<std ::endl ; 60} 61 62//Bitwise comparison of 8-bytes array with uint_64 63inline bool same8 (char * array ,uint64_t other ) { 64return ((* ((uint64_t * )array )) ^other )== 0 ; 65} 66//Bitwise comparison of 4-bytes array with uint_32 67inline bool same4 (char * array ,uint32_t other ) { 68return ((* ((uint32_t * )array )) ^other )== 0 ; 69} 70//Load a big-endian 32-bit unsigned int from an array 71inline uint32_t readBE4U (char * array ) {return __builtin_bswap32 (* ((uint32_t * )array )); } 72//Load a big-endian 16-bit unsigned int from an array 73inline uint16_t readBE2U (char * array ) {return __builtin_bswap16 (* ((uint16_t * )array )); } 74//Load a big-endian 32-bit int from an array 75inline int32_t readBE4S (char * array ) {return __builtin_bswap32 (* ((int32_t * )array )); } 76//Load a big-endian 16-bit int from an array 77inline int16_t readBE2S (char * array ) {return __builtin_bswap16 (* ((int16_t * )array )); } 78//Load a big-endian float from an array 79inline float readBE4F (char * array ) { 80float r ; 81char * rc = (char * )& r ; 82rc [0 ]= array [3 ]; 83rc [1 ]= array [2 ]; 84rc [2 ]= array [1 ]; 85rc [3 ]= array [0 ]; 86return r ; 87} 88 89//Base64 encoder / decoder -> https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp 90inline std ::string base64_encode (unsigned char const * bytes_to_encode ,unsigned int in_len ) { 91std ::string ret ; 92int i = 0 ; 93int j = 0 ; 94unsigned char char_array_3 [3 ]; 95unsigned char char_array_4 [4 ]; 96 97while (in_len -- ) { 98char_array_3 [i ++ ]= * (bytes_to_encode ++ ); 99if (i == 3 ) { 100char_array_4 [0 ]= (char_array_3 [0 ]& 0xfc ) >>2 ; 101char_array_4 [1 ]= ((char_array_3 [0 ]& 0x03 ) <<4 )+ ((char_array_3 [1 ]& 0xf0 ) >>4 ); 102char_array_4 [2 ]= ((char_array_3 [1 ]& 0x0f ) <<2 )+ ((char_array_3 [2 ]& 0xc0 ) >>6 ); 103char_array_4 [3 ]= char_array_3 [2 ]& 0x3f ; 104 105for (i = 0 ; (i < 4 ) ;i ++ ) 106ret += base64_chars [char_array_4 [i ]]; 107i = 0 ; 108 } 109 } 110 111if (i ) 112 { 113for (j = i ;j < 3 ;j ++ ) 114char_array_3 [j ]= '\0' ; 115 116char_array_4 [0 ]= (char_array_3 [0 ]& 0xfc ) >>2 ; 117char_array_4 [1 ]= ((char_array_3 [0 ]& 0x03 ) <<4 )+ ((char_array_3 [1 ]& 0xf0 ) >>4 ); 118char_array_4 [2 ]= ((char_array_3 [1 ]& 0x0f ) <<2 )+ ((char_array_3 [2 ]& 0xc0 ) >>6 ); 119 120for (j = 0 ; (j < i + 1 );j ++ ) 121ret += base64_chars [char_array_4 [j ]]; 122 123while ((i ++ < 3 )) 124ret += '=' ; 125 126 } 127 128return ret ; 129 130} 131 132//Base64 encoder / decoder -> https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp 133inline std ::string base64_decode (std ::string const & encoded_string ) { 134int in_len = encoded_string .size (); 135int i = 0 ; 136int j = 0 ; 137int in_ = 0 ; 138unsigned char char_array_4 [4 ],char_array_3 [3 ]; 139std ::string ret ; 140 141while (in_len -- && (encoded_string [in_ ]!= '=' )&& is_base64 (encoded_string [in_ ])) { 142char_array_4 [i ++ ]= encoded_string [in_ ];in_ ++ ; 143if (i == 4 ) { 144for (i = 0 ;i < 4 ;i ++ ) 145char_array_4 [i ]= base64_chars .find (char_array_4 [i ]); 146 147char_array_3 [0 ]= (char_array_4 [0 ] <<2 )+ ((char_array_4 [1 ]& 0x30 ) >>4 ); 148char_array_3 [1 ]= ((char_array_4 [1 ]& 0xf ) <<4 )+ ((char_array_4 [2 ]& 0x3c ) >>2 ); 149char_array_3 [2 ]= ((char_array_4 [2 ]& 0x3 ) <<6 )+ char_array_4 [3 ]; 150 151for (i = 0 ; (i < 3 );i ++ ) 152ret += char_array_3 [i ]; 153i = 0 ; 154 } 155 } 156 157if (i ) { 158for (j = 0 ;j < i ;j ++ ) 159char_array_4 [j ]= base64_chars .find (char_array_4 [j ]); 160 161char_array_3 [0 ]= (char_array_4 [0 ] <<2 )+ ((char_array_4 [1 ]& 0x30 ) >>4 ); 162char_array_3 [1 ]= ((char_array_4 [1 ]& 0xf ) <<4 )+ ((char_array_4 [2 ]& 0x3c ) >>2 ); 163 164for (j = 0 ; (j < i - 1 );j ++ )ret += char_array_3 [j ]; 165 } 166 167return ret ; 168} 169 170// https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784 171inline std ::string escape_json (const std ::string & s ) { 172std ::ostringstream o ; 173for (autoc = s. cbegin (); c != s. cend (); c ++ ) { 174if ( * c == '"' || * c == '\\' || ( '\x00' <= * c && * c <= '\x1f' )) { 175o << "\\u" 176<< std::hex << std:: setw ( 4 ) << std:: setfill ( '0' ) << ( int ) * c; 177} else { 178o << * c; 179} 180} 181return o. str (); 182} 183 184inline std ::string to_utf8 ( const std ::u16string & s) { 185std::wstring_convert < std::codecvt_utf8 < char16_t > , char16_t > conv; 186std :: string u = conv. to_bytes (s); 187u. erase (std:: find (u. begin (), u. end (), '\0' ), u. end ()); 188return u; 189} 190 191} 192 193#endif /* UTIL_H_ */