1 /*************************************************************************
\r
3 * Licensed under the Apache License, Version 2.0 (the "License");
\r
4 * you may not use this file except in compliance with the License.
\r
5 * You may obtain a copy of the License at
\r
7 * http://www.apache.org/licenses/LICENSE-2.0
\r
9 * Unless required by applicable law or agreed to in writing, software
\r
10 * distributed under the License is distributed on an "AS IS" BASIS,
\r
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 * See the License for the specific language governing permissions and
\r
13 * limitations under the License.
\r
14 ***************************************************************************/
\r
23 #include "regxstring.h"
\r
25 using namespace std;
\r
27 static char b64_encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
\r
28 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
\r
29 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
\r
30 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
\r
31 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
\r
32 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
\r
33 'w', 'x', 'y', 'z', '0', '1', '2', '3',
\r
34 '4', '5', '6', '7', '8', '9', '+', '/'};
\r
36 static char b64_decoding_table[256] = {0};
\r
38 static int b64_mod_table[] = {0, 2, 1};
\r
41 static string trim(std::string str){
\r
42 size_t i = 0,e = str.length();
\r
43 for(;i < e && std::isspace(str[i]);++i);
\r
45 for(;j > i && std::isspace(str[j - 1]);--j);
\r
46 return (i < j ? str.substr(i,j - i) : "");
\r
49 static string pre_handle(const string & str)
\r
51 string ret = trim(str);
\r
54 ret.insert(ret.begin(),'^');
\r
57 if(ret[ret.size() - 1] != '$') {
\r
64 static void rand_init(void) {
\r
66 FILE* urandom = fopen("/dev/urandom", "r");
\r
67 size_t ret = fread(&seed, sizeof(int), 1, urandom);
\r
74 static uint8_t *b64_decode(const char *data, size_t input_length, size_t *output_length) {
\r
76 assert(input_length);
\r
77 assert(output_length);
\r
81 //one time compute decoding table
\r
82 if(b64_decoding_table['A'] == 0) {
\r
83 for(i = 0; i < 64; i++) {
\r
84 b64_decoding_table[(unsigned char)b64_encoding_table[i]] = i;
\r
88 if(input_length % 4 != 0) {
\r
92 *output_length = input_length / 4 * 3;
\r
93 if(data[input_length - 1] == '=') {
\r
94 (*output_length )--;
\r
96 if(data[input_length - 2] == '=') {
\r
97 (*output_length )--;
\r
100 uint8_t *decoded_data = (uint8_t*)malloc(*output_length + 1);
\r
101 if(decoded_data == 0) {
\r
105 for(i = 0, j = 0; i < input_length;) {
\r
106 uint32_t sextet_a = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
\r
107 uint32_t sextet_b = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
\r
108 uint32_t sextet_c = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
\r
109 uint32_t sextet_d = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
\r
110 uint32_t triple = ( sextet_a << 3 * 6 ) + ( sextet_b << 2 * 6 ) + ( sextet_c << 1 * 6 ) + ( sextet_d << 0 * 6 );
\r
112 if(j < *output_length) {
\r
113 decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
\r
116 if(j < *output_length) {
\r
117 decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
\r
120 if(j < *output_length) {
\r
121 decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
\r
125 return decoded_data;
\r
128 int main(int argc, const char ** argv)
\r
130 CRegxString regxstr;
\r
131 string regx64 = "";
\r
140 int pseudo_seed = 0;
\r
142 while(argv[1][i]) {
\r
144 pseudo_seed += argv[1][i] - '0';
\r
147 srand(pseudo_seed);
\r
148 srandom(pseudo_seed);
\r
154 char *x = (char *)b64_decode(regx64.c_str(), strlen(regx64.c_str()), &ol);
\r
160 regxstr.ParseRegx(pre_handle(regx).c_str());
\r
161 cout << regxstr.RandString() << endl;
\r