Bug fix for NTS YANGs.
[sim/o1-interface.git] / ntsimulator / regxstring / gen_regex.cpp
1 /*************************************************************************\r
2 *\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
6 *\r
7 *     http://www.apache.org/licenses/LICENSE-2.0\r
8 *\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
15 \r
16 #include <string>\r
17 #include <cstring>\r
18 #include <cstdlib>\r
19 #include <iostream>\r
20 #include <cstdio>\r
21 #include <assert.h>\r
22 \r
23 #include "regxstring.h"\r
24 \r
25 using namespace std;\r
26 \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
35 \r
36 static char b64_decoding_table[256] = {0};\r
37 \r
38 static int b64_mod_table[] = {0, 2, 1};\r
39 \r
40 \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
44     size_t j = e;\r
45     for(;j > i && std::isspace(str[j - 1]);--j);\r
46     return (i < j ? str.substr(i,j - i) : "");\r
47 }\r
48 \r
49 static string pre_handle(const string & str)\r
50 {\r
51     string ret = trim(str);\r
52     if(!ret.empty()) {\r
53         if(ret[0] != '^') {\r
54             ret.insert(ret.begin(),'^');\r
55         }\r
56 \r
57         if(ret[ret.size() - 1] != '$') {\r
58             ret.push_back('$');\r
59         }\r
60     }\r
61     return ret;\r
62 }\r
63 \r
64 static void rand_init(void) {\r
65     unsigned int seed;\r
66     FILE* urandom = fopen("/dev/urandom", "r");\r
67     size_t ret = fread(&seed, sizeof(int), 1, urandom);\r
68     (void)ret;\r
69     fclose(urandom);\r
70     srand(seed);\r
71     srandom(seed);\r
72 }\r
73 \r
74 static uint8_t *b64_decode(const char *data, size_t input_length, size_t *output_length) {\r
75     assert(data);\r
76     assert(input_length);\r
77     assert(output_length);\r
78 \r
79     int i, j;\r
80 \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
85         }\r
86     }\r
87 \r
88     if(input_length % 4 != 0) {\r
89         return 0;\r
90     }\r
91 \r
92     *output_length = input_length / 4 * 3;\r
93     if(data[input_length - 1] == '=') {\r
94         (*output_length )--;\r
95     }\r
96     if(data[input_length - 2] == '=') {\r
97         (*output_length )--;\r
98     }\r
99 \r
100     uint8_t *decoded_data = (uint8_t*)malloc(*output_length + 1);\r
101     if(decoded_data == 0) {\r
102         return 0;\r
103     }\r
104 \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
111 \r
112         if(j < *output_length) {\r
113             decoded_data[j++] = (triple >> 2 * 8) & 0xFF;\r
114         }\r
115 \r
116         if(j < *output_length) {\r
117             decoded_data[j++] = (triple >> 1 * 8) & 0xFF;\r
118         }\r
119 \r
120         if(j < *output_length) {\r
121             decoded_data[j++] = (triple >> 0 * 8) & 0xFF;\r
122         }\r
123     }\r
124 \r
125     return decoded_data;\r
126 }\r
127 \r
128 int main(int argc, const char ** argv)\r
129 {\r
130     CRegxString regxstr;\r
131     string regx64 = "";\r
132 \r
133     switch(argc) {\r
134         case 2:\r
135             rand_init();\r
136             regx64 = argv[1];\r
137             break;\r
138 \r
139         case 3:\r
140             int pseudo_seed = 0;\r
141             int i = 0;\r
142             while(argv[1][i]) {\r
143                 pseudo_seed *= 10;\r
144                 pseudo_seed += argv[1][i] - '0';\r
145                 i++;\r
146             }\r
147             srand(pseudo_seed);\r
148             srandom(pseudo_seed);\r
149             regx64 = argv[2];\r
150             break;\r
151     }\r
152 \r
153     size_t ol;\r
154     char *x = (char *)b64_decode(regx64.c_str(), strlen(regx64.c_str()), &ol);\r
155     x[ol] = 0;\r
156 \r
157     string regx = x;\r
158     free(x);\r
159 \r
160     regxstr.ParseRegx(pre_handle(regx).c_str());\r
161     cout << regxstr.RandString() << endl;\r
162     \r
163     return 0;\r
164 }\r