Skip to content

Commit d3370f3

Browse files
committed
Cleanup: unify util.c a bit with libshout.
1 parent 2ff8c50 commit d3370f3

File tree

1 file changed

+55
-52
lines changed

1 file changed

+55
-52
lines changed

src/util.c

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,58 @@
5454

5555
#include "logging.h"
5656

57+
/* first all static tables, then the code */
58+
59+
static const char hexchars[16] = {
60+
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
61+
};
62+
63+
static const char safechars[256] = {
64+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
68+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
69+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
70+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
71+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
72+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80+
};
81+
82+
static const char base64table[64] = {
83+
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
84+
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
85+
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
86+
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
87+
};
88+
89+
static const signed char base64decode[256] = {
90+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
91+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
92+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
93+
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -1, -2, -2,
94+
-2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
95+
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
96+
-2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
97+
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
98+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
99+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
100+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
101+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
102+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
103+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
104+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
105+
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
106+
};
107+
108+
57109
/* Abstract out an interface to use either poll or select depending on which
58110
* is available (poll is preferred) to watch a single fd.
59111
*
@@ -109,7 +161,6 @@ int util_read_header(sock_t sock, char *buff, unsigned long len, int entire)
109161
read_bytes = 0;
110162

111163
if (util_timed_wait_for_fd(sock, header_timeout*1000) > 0) {
112-
113164
if ((read_bytes = recv(sock, &c, 1, 0))) {
114165
if (c != '\r') buff[pos++] = c;
115166
if (entire) {
@@ -245,29 +296,6 @@ char *util_get_path_from_normalised_uri(const char *uri) {
245296
return fullpath;
246297
}
247298

248-
static const char hexchars[16] = {
249-
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
250-
};
251-
252-
static const char safechars[256] = {
253-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
254-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
256-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
257-
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
258-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
259-
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
260-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
261-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
262-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
263-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
264-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
265-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
266-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
267-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
268-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
269-
};
270-
271299
char *util_url_escape (const char *src)
272300
{
273301
size_t len;
@@ -372,32 +400,6 @@ char *util_normalise_uri(const char *uri) {
372400
}
373401
}
374402

375-
static const char base64table[64] = {
376-
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
377-
'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
378-
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
379-
'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
380-
};
381-
382-
static const signed char base64decode[256] = {
383-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
384-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
385-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
386-
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -1, -2, -2,
387-
-2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
388-
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
389-
-2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
390-
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
391-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
392-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
393-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
394-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
395-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
396-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
397-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
398-
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
399-
};
400-
401403
char *util_bin_to_hex(unsigned char *data, int len)
402404
{
403405
char *hex = malloc(len*2 + 1);
@@ -420,9 +422,10 @@ char *util_base64_encode(const char *data, size_t len) {
420422
size_t chunk;
421423

422424
while(len > 0) {
423-
chunk = len > 3 ? 3 : len;
425+
chunk = (len > 3) ? 3 : len;
424426
*out++ = base64table[(*data & 0xFC)>>2];
425427
*out++ = base64table[((*data & 0x03)<<4) | ((*(data+1) & 0xF0) >> 4)];
428+
426429
switch(chunk) {
427430
case 3:
428431
*out++ = base64table[((*(data+1) & 0x0F)<<2) | ((*(data+2) & 0xC0)>>6)];
@@ -771,7 +774,7 @@ void util_dict_free(util_dict *dict)
771774
const char *util_dict_get(util_dict *dict, const char *key)
772775
{
773776
while (dict) {
774-
if (!strcmp(key, dict->key))
777+
if (dict->key && !strcmp(key, dict->key))
775778
return dict->val;
776779
dict = dict->next;
777780
}

0 commit comments

Comments
 (0)