X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=sim%2Fo1-interface.git;a=blobdiff_plain;f=ntsimulator%2Fregxstring%2Fgen_regex.cpp;h=42c06602f98671416f13bd46f726f4c7708c744c;hp=e0a4953432bde424b98b58ffa64ba4e0f0fc8901;hb=96526af57d1c3026430e11cfe899e50629a91296;hpb=db05cb943f111d47e9a715f602a1942b925ba675 diff --git a/ntsimulator/regxstring/gen_regex.cpp b/ntsimulator/regxstring/gen_regex.cpp index e0a4953..42c0660 100644 --- a/ntsimulator/regxstring/gen_regex.cpp +++ b/ntsimulator/regxstring/gen_regex.cpp @@ -14,13 +14,30 @@ ***************************************************************************/ #include +#include #include #include +#include +#include #include "regxstring.h" using namespace std; +static char b64_encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/'}; + +static char b64_decoding_table[256] = {0}; + +static int b64_mod_table[] = {0, 2, 1}; + + static string trim(std::string str){ size_t i = 0,e = str.length(); for(;i < e && std::isspace(str[i]);++i); @@ -54,15 +71,69 @@ static void rand_init(void) { srandom(seed); } +static uint8_t *b64_decode(const char *data, size_t input_length, size_t *output_length) { + assert(data); + assert(input_length); + assert(output_length); + + int i, j; + + //one time compute decoding table + if(b64_decoding_table['A'] == 0) { + for(i = 0; i < 64; i++) { + b64_decoding_table[(unsigned char)b64_encoding_table[i]] = i; + } + } + + if(input_length % 4 != 0) { + return 0; + } + + *output_length = input_length / 4 * 3; + if(data[input_length - 1] == '=') { + (*output_length )--; + } + if(data[input_length - 2] == '=') { + (*output_length )--; + } + + uint8_t *decoded_data = (uint8_t*)malloc(*output_length + 1); + if(decoded_data == 0) { + return 0; + } + + for(i = 0, j = 0; i < input_length;) { + uint32_t sextet_a = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]]; + uint32_t sextet_b = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]]; + uint32_t sextet_c = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]]; + uint32_t sextet_d = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]]; + uint32_t triple = ( sextet_a << 3 * 6 ) + ( sextet_b << 2 * 6 ) + ( sextet_c << 1 * 6 ) + ( sextet_d << 0 * 6 ); + + if(j < *output_length) { + decoded_data[j++] = (triple >> 2 * 8) & 0xFF; + } + + if(j < *output_length) { + decoded_data[j++] = (triple >> 1 * 8) & 0xFF; + } + + if(j < *output_length) { + decoded_data[j++] = (triple >> 0 * 8) & 0xFF; + } + } + + return decoded_data; +} + int main(int argc, const char ** argv) { CRegxString regxstr; - string regx = ""; + string regx64 = ""; switch(argc) { case 2: rand_init(); - regx = argv[1]; + regx64 = argv[1]; break; case 3: @@ -75,10 +146,17 @@ int main(int argc, const char ** argv) } srand(pseudo_seed); srandom(pseudo_seed); - regx = argv[2]; + regx64 = argv[2]; break; } + size_t ol; + char *x = (char *)b64_decode(regx64.c_str(), strlen(regx64.c_str()), &ol); + x[ol] = 0; + + string regx = x; + free(x); + regxstr.ParseRegx(pre_handle(regx).c_str()); cout << regxstr.RandString() << endl;