forked from lcatro/vuln_javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaselib_string.cpp
174 lines (155 loc) · 5.68 KB
/
baselib_string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "disable_warning.h"
#include "global_setting.h"
#include <stdlib.h>
#include <string.h>
#include <string>
using std::string;
HANDLE heap_handle=NULL;
void trim(string& input_string) {
for (string::iterator index =input_string.begin();
index!=input_string.end();
) {
if (' '==*index) {
input_string.erase(index);
index=input_string.begin();
} else {
break;
}
}
for (string::reverse_iterator rindex =input_string.rbegin();
rindex!=input_string.rend();
) {
if (' '==*rindex) {
input_string.erase((++rindex).base());
rindex=input_string.rbegin();
} else {
break;
}
}
if (input_string[input_string.length()-1]==' ')
input_string.erase((++input_string.rbegin()).base());
}
long hex_string_to_number(const string& input_string) {
char* end_point=NULL;
return strtol(input_string.c_str(),&end_point,16);
}
long hex_string_to_number(const char* input_string) {
char* end_point=NULL;
return strtol(input_string,&end_point,16);
}
bool check_string(const char* input_string1,const char* input_string2) {
unsigned long input_string1_length=strlen(input_string1);
for (unsigned long input_string1_index=0;
input_string1_index<input_string1_length;
++input_string1_index)
if (*(char*)(input_string1+input_string1_index)!=
*(char*)(input_string2+input_string1_index))
return false;
return true;
}
unsigned long get_matching_outside_right_bracket(string& express,unsigned long call_index=0) {
for (unsigned long index=0;index<express.length();++index) {
if ('('==express[index]) {
unsigned long right_bracket_index=get_matching_outside_right_bracket(express.substr(index+1),call_index+1)+index+1;
if (!call_index)
return right_bracket_index;
index=right_bracket_index;
} else if (')'==express[index]) {
return index;
}
}
return 0;
}
unsigned long get_matching_outside_right_brace(string& express,unsigned long call_index=0) {
for (unsigned long index=0;index<express.length();++index) {
if ('{'==express[index]) {
unsigned long right_bracket_index=get_matching_outside_right_brace(express.substr(index+1),call_index+1)+index+1;
if (!call_index)
return right_bracket_index;
index=right_bracket_index;
} else if ('}'==express[index]) {
return index;
}
}
return 0;
}
void filter_useless_char(string& express) {
for (string::iterator express_index=express.begin();
express_index!=express.end();
++express_index) {
char express_char=*express_index;
if ('\t'==express_char || '\n'==express_char || '\r'==express_char)
*express_index=' ';
}
}
bool init_heap(void) {
#ifdef HEAP_ALLOC
#ifdef HEAP_EXECUTE_PROTECT
heap_handle=HeapCreate(NULL,HEAP_SIZE,0);
#else
heap_handle=HeapCreate(HEAP_CREATE_ENABLE_EXECUTE,HEAP_SIZE,0);
#endif
if (NULL!=heap_handle)
return true;
return false;
#else
return true;
#endif
}
void* alloc_memory(unsigned long alloc_length) {
alloc_length=(alloc_length<4)?4:alloc_length;
void* alloc_address=NULL;
#ifdef HEAP_ALLOC
alloc_address=HeapAlloc(heap_handle,HEAP_ZERO_MEMORY,alloc_length);
#else
#ifdef HEAP_EXECUTE_PROTECT
alloc_address=VirtualAlloc(NULL,alloc_length,NULL,PAGE_READWRITE);
#else
alloc_address=VirtualAlloc(NULL,alloc_length,NULL,PAGE_EXECUTE_READWRITE);
#endif
#endif
if (NULL==alloc_address)
alloc_address=malloc(alloc_length);
if (NULL!=alloc_address)
memset(alloc_address,0,alloc_length);
return alloc_address;
}
void free_memory(void* alloc_buffer) {
#ifdef HEAP_ALLOC
HeapFree(heap_handle,NULL,alloc_buffer);
#else
VirtualFree(alloc_buffer,NULL,NULL);
#endif
}
unsigned long conver_coding(char* input_string) {
unsigned long input_string_length=0;
if ((unsigned long)input_string==*(unsigned long*)(input_string-4)) // check this is not string object ,because if your string conver %u0000%u1234 ,strlen() will get a error length ..
input_string_length=*(unsigned long*)(input_string-8);
else
input_string_length=strlen(input_string);
unsigned long return_new_length=0;
for (unsigned long input_string_index=0;
input_string_index<input_string_length;
++input_string_index) {
if ('%'==input_string[input_string_index] &&
'u'==input_string[input_string_index+1]) {
if (input_string_index+6<=input_string_length) {
char bit1_string[3]={input_string[input_string_index+2],input_string[input_string_index+3],0};
char bit2_string[3]={input_string[input_string_index+4],input_string[input_string_index+5],0};
char bit1=(char)hex_string_to_number(bit1_string);
char bit2=(char)hex_string_to_number(bit2_string);
input_string[input_string_index]=bit2;
input_string[input_string_index+1]=bit1;
memcpy(&input_string[input_string_index+2],&input_string[input_string_index+6],input_string_length-input_string_index-6+1); // +1 for \0
} else {
return -1;
}
return_new_length+=2;
input_string_length-=4;
++input_string_index;
} else {
++return_new_length;
}
}
return return_new_length;
}