-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cc
294 lines (223 loc) · 6.86 KB
/
util.cc
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# include "util.h"
# include "config.h"
/********** Hash stuff **********/
gchar *g_strncpy( gchar *dest, const gchar *src, guint n ) {
strncpy( dest, src, n );
dest[ n - 1 ] = '\0';
return dest;
}
GHashTable* g_hash_new () {
return g_hash_table_new( g_str_hash, g_str_equal );
}
static gboolean g_hash_free_each ( gpointer key, gpointer val, gpointer data ) {
if (key != NULL) g_free(key);
if (val != NULL) g_free(val);
return TRUE;
}
guint g_hash_free (GHashTable *h) {
guint n;
if (h == NULL) return 0;
n = g_hash_table_foreach_remove( h, g_hash_free_each, NULL );
g_hash_table_destroy( h );
return n;
}
gboolean g_hash_delete(GHashTable * h, const gchar *key) {
gpointer k, v;
gchar *kk, *vv;
//g_assert( h != NULL );
//g_assert( key != NULL );
// if (g_hash_table_lookup_extended(h, key, (gpointer *)&k, (gpointer *)&v)) {
if (g_hash_table_lookup_extended(h, key, &k, &v)) {
g_hash_table_remove(h, key);
kk = ((gchar *) k);
vv = ((gchar *) v);
if (kk != NULL) g_free(kk);
if (vv != NULL) g_free(vv);
return TRUE;
}
return FALSE;
}
gboolean g_hash_set(GHashTable *h, const gchar *key, gchar *val) {
// A esta funci'on se le entra con dos punteros gchar a dos strings que representan la key y el val que se quieren insertar en la tabla.
// Primero se garantiza con g_hash_delete que en la tabla no exista una key igual, y despu'es se duplican los strings key y val insert'andose
// estos en la tabla.
gchar *k, *v;
gboolean over;
//g_assert( h != NULL );
//g_assert( key != NULL );
//g_assert( val != NULL );
over = g_hash_delete(h, key);
k = g_strdup(key);
v = g_strdup(val);
g_hash_table_insert(h, k, v);
return over;
}
static void g_hash_dup_each ( gchar *k, gchar *v, GHashTable *dest ) {
g_hash_set( dest, k, v );
}
GHashTable *g_hash_merge( GHashTable *dest, GHashTable *src ) {
g_hash_table_foreach( src, (GHFunc) g_hash_dup_each, dest );
return dest;
}
GHashTable *g_hash_dup( GHashTable *src ) {
GHashTable *dest = g_hash_new();
return g_hash_merge( dest, src );
}
static void g_hash_as_string_each( gchar *k, gchar *v, GString *dest ) {
g_string_sprintfa( dest, "%s=%s\n", k, v );
}
GString* g_hash_as_string( GHashTable *h ) {
GString *dest = g_string_new("");
g_assert( h != NULL );
g_hash_table_foreach( h, (GHFunc) g_hash_as_string_each, dest );
return dest;
}
/********** URL encoding **********/
static int fromhex ( char digit ) {
char d = toupper(digit);
if (!isxdigit(digit)) return 0;
if (isdigit(d)) {
return d - '0';
} else {
return (int) ( d - 'A' ) + 10;
}
}
gchar *url_decode( const gchar *src ) {
gchar *dest, *dest0;
int n;
n = strlen(src) + 1;
dest = dest0 = g_new0(gchar, n);
for (; *src != '\0' && n >= 0; ++dest, ++src, --n )
if ( *src == '%' && n > 2 ) {
*dest = fromhex( *(++src) ) * 0x10;
*dest += fromhex( *(++src) );
n -= 2;
} else if ( *src == '+' )
*dest = ' ';
else
*dest = *src;
*dest = '\0';
return dest0; // g_renew( gchar, dest0, ++n );
}
gchar *url_encode( const gchar *src ) {
char *dest, *dest0;
int n = strlen(src) + 1;
dest = dest0 = g_new0(gchar, n * 3);
for (; *src != '\0' && n >= 0; src++, dest++, n--) {
// g_message( "src: %s dest: %s n: %d", src, dest0, n );
if ( isalnum(*src) || strchr("./-_", *src) )
*dest = *src;
else if ( *src == ' ' )
*dest = '+';
else {
sprintf( dest, "%%%02X", (int) *src & 0xFF );
dest += 2;
}
}
*dest = '\0';
return dest0; // g_renew( gchar, dest0, ++n );
}
static void build_url_each( gchar *k, gchar *v, GString *dest ) {
gchar *val = url_encode( v );
g_string_sprintfa( dest, "%s=%s&", k, val );
g_free( val );
}
GString *build_url( const gchar *uri, GHashTable *query ) {
GString *dest = g_string_new( uri );
//g_assert( query != NULL );
g_string_append( dest, "?" );
g_hash_table_foreach( query, (GHFunc) build_url_each, dest );
g_string_erase( dest, dest->len - 1, 1 );
return dest;
}
/********** I/O stuff **********/
gchar *load_file( const char *path ) {
// A esta funci'on se le pasa un string con el nombre del archivo que se desea cargar en memoria y ella devuelve un puntero a un
// 'area din'amica de memoria donde se ha cargado el contenido del archivo. El 'area din'amica de memoria se deber'a liberar cuando
// se haya terminado de usar el contenido del archivo.
gchar *file;
struct stat s;
void *data;
int fd, r;
g_assert( path != NULL );
fd = open( path, O_RDONLY );
if ( fd == -1 ) {
g_message( "load_file: Can't open %s: %m", path );
return NULL;
}
r = fstat( fd, &s );
//g_assert( r == 0 );
data = mmap( NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0 );
//g_assert( data != MAP_FAILED );
file = g_strndup( (gchar*)data, s.st_size );
//g_assert( file != NULL );
r = munmap( data, s.st_size );
//g_assert( r == 0 );
r = close(fd);
//g_assert( r == 0 );
return file;
}
gchar *parse_template( gchar *src, GHashTable *data1 ) {
GString *dest = g_string_sized_new(strlen(src));
guint n;
gchar *var, *val;
for (; *src != '\0'; src++) {
// Find the text chunk up to the next $,
// and append it to the buffer.
n = strcspn( src, "$" );
if (n) {
g_string_sprintfa( dest, "%.*s", n, src );
src += n;
if ( *src == '\0' )
break;
}
// If the immediately following char is alphabetical...
if (isalpha(*( src + 1 ))) {
// Find the identifier following the $
for (n = 2; isident(src[n]); n++);
// Having found it, copy the variable name out
// and get the corresponding value.
var = g_strndup( src + 1, --n );
val = (gchar*) g_hash_table_lookup( data1, var );
if (val) g_string_append(dest, val);
g_free(var);
src += n;
}
else {
// Otherwise save the $
g_string_append(dest, "$");
}
}
val = g_try_new0 (gchar, strlen(dest->str) + 1);
if (val != NULL){
memcpy(val,dest->str,strlen(dest->str));
}
else g_message("parse_template: could not allocate space for return value");
g_string_free( dest, FALSE );
return val;
}
/**** crypt-type functions *********/
//# ifdef HAVE_LIBCRYPT
static char salt_chars[] =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
gchar *strrand (gchar *dest, int n) {
int i;
for (i = 0; i < n; i++)
dest[i] = salt_chars[ rand() % sizeof(salt_chars) ];
dest[n] = '\0';
return dest;
}
gchar *md5_crypt( const gchar *src, gchar *salt ) {
gchar salt2[12], *hash;
if (salt == NULL) {
strcpy( salt2, "$1$" );
strrand( salt2 + 3, 8 );
} else {
strncpy( salt2, salt, 11 );
}
salt2[11] = '\0';
hash = g_strdup( crypt(src, salt2) );
//hash = NULL;
return hash;
}
//# endif /* HAVE_LIBCRYPT */