-
Notifications
You must be signed in to change notification settings - Fork 427
/
util.c
178 lines (157 loc) · 3.36 KB
/
util.c
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
175
176
177
178
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include "util.h"
char *ltrim(char *str, const char *seps) {
size_t totrim;
if (seps == NULL) {
seps = "\t\n\v\f\r ";
}
totrim = strspn(str, seps);
if (totrim > 0) {
size_t len = strlen(str);
if (totrim == len) {
str[0] = '\0';
}
else {
memmove(str, str + totrim, len + 1 - totrim);
}
}
return str;
}
char *rtrim(char *str, const char *seps) {
int i;
if (seps == NULL) {
seps = "\t\n\v\f\r ";
}
i = strlen(str) - 1;
while (i >= 0 && strchr(seps, str[i]) != NULL) {
str[i] = '\0';
i--;
}
return str;
}
char *trim(char *str, const char *seps) {
return ltrim(rtrim(str, seps), seps);
}
int indexOf(char *s,const char **array,int length_array) {
int index = -1,i,continuar = 1;
for(i = 0; i <length_array && continuar; i++) {
if(strcmp(s,array[i]) == 0) {
index = i;
continuar = 0;
}
}
return index;
}
char *nextToken(Tokenizer *t) {
if(t->current < t->n) {
t->current++;
return t->tokens[t->current-1];
}
else {
return NULL;
}
}
int hasMoreTokens(Tokenizer *t) {
return (t->current < t->n);
}
void stringtokenizer(char *data,Tokenizer *t) {
char *token;
t->tokens = NULL;
t->n = 0;
t->current = 0;
trim(data,"\t\n\r :");
token = strtok(data," \t:");
while(token != NULL) {
t->n++;
t->tokens = (char**) realloc(t->tokens,sizeof(char*)*t->n);
if(t->tokens == NULL) {
printf("Out of memory\n");
exit(0);
}
t->tokens[t->n - 1] = token;
token = strtok(NULL," \t:");
}
}
void freetokenizer(Tokenizer *t) {
if(t->n > 0) {
free(t->tokens);
}
memset(t,0,sizeof(Tokenizer));
}
/*
Aux function to get the hexvalues of the data
*/
char *tohex(char *ptr,int length){
char *buffer;
int offset = 0;
unsigned char c;
buffer = (char *) malloc((length * 2)+1);
for (int i = 0; i <length; i++) {
c = ptr[i];
sprintf((char*) (buffer + offset),"%.2x",c);
offset+=2;
}
buffer[length*2] = 0;
return buffer;
}
void tohex_dst(char *ptr,int length,char *dst) {
int offset = 0;
unsigned char c;
for (int i = 0; i <length; i++) {
c = ptr[i];
sprintf((char*) (dst + offset),"%.2x",c);
offset+=2;
}
dst[length*2] = 0;
}
int hexs2bin(char *hex, unsigned char *out) {
int len;
char b1;
char b2;
int i;
if (hex == NULL || *hex == '\0' || out == NULL)
return 0;
len = strlen(hex);
if (len % 2 != 0)
return 0;
len /= 2;
memset(out, 'A', len);
for (i=0; i<len; i++) {
if (!hexchr2bin(hex[i*2], &b1) || !hexchr2bin(hex[i*2+1], &b2)) {
return 0;
}
out[i] = (b1 << 4) | b2;
}
return len;
}
int hexchr2bin(const char hex, char *out) {
if (out == NULL)
return 0;
if (hex >= '0' && hex <= '9') {
*out = hex - '0';
} else if (hex >= 'A' && hex <= 'F') {
*out = hex - 'A' + 10;
} else if (hex >= 'a' && hex <= 'f') {
*out = hex - 'a' + 10;
} else {
return 0;
}
return 1;
}
void addItemList(char *data, List *l) {
l->data = (char**) realloc(l->data,sizeof(char*)* (l->n +1));
l->data[l->n] = data;
l->n++;
}
int isValidHex(char *data) {
char c;
int len,i,valid = 1;
len = strlen(data);
for(i = 0 ; i < len && valid ;i++ ) {
c = data[i];
valid = ( (c >= '0' && c <='9') || (c >= 'A' && c <='F' ) || (c >= 'a' && c <='f' ) );
}
return valid;
}