1 |
|
|
#include <errno.h> /* errno */ |
2 |
|
|
#include <stdio.h> /* printf, fflush, fprintf, fopen, fclose */ |
3 |
|
|
#include <stdlib.h> /* atoi, malloc, free, realloc, calloc */ |
4 |
|
|
#include <string.h> /* strerror, memset, strncpy, memcpy, strlen, |
5 |
|
|
strtok, strtok_r */ |
6 |
|
|
#include "str.h" |
7 |
|
|
#include "alloc.h" |
8 |
|
|
#include "log.h" |
9 |
|
|
#include "predict.h" |
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* Function that converts a binary representation into a hexidecimal one. |
13 |
|
|
* |
14 |
|
|
* @param bin [const unsigned char *] "The binary value" |
15 |
|
|
* @param len [size_t] "The length of the binary value" |
16 |
|
|
* @return [char *] "The hexidecimal representation of the binary value in a new memory block" |
17 |
|
|
*/ |
18 |
|
3 |
char *bin2hex(const unsigned char *bin, size_t len) |
19 |
|
|
{ |
20 |
|
3 |
char *out; |
21 |
|
3 |
size_t i; |
22 |
|
|
|
23 |
✓✗ |
3 |
if ( unlikely(NULL == (out = (char *) WSS_malloc(len*2+1))) ) { |
24 |
|
|
return NULL; |
25 |
|
|
} |
26 |
|
|
|
27 |
✓✓ |
28 |
for (i = 0; likely(i < len); i++) { |
28 |
|
25 |
out[i*2] = "0123456789ABCDEF"[bin[i] >> 4]; |
29 |
|
25 |
out[i*2+1] = "0123456789ABCDEF"[bin[i] & 0x0F]; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
3 |
out[len*2] = '\0'; |
33 |
|
|
|
34 |
|
3 |
return out; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
/** |
38 |
|
|
* Function that looks for a value in a char * array. |
39 |
|
|
* |
40 |
|
|
* A value is said to be found if the prefix of the needle is contained in the |
41 |
|
|
* whole array value. |
42 |
|
|
* |
43 |
|
|
* E.g |
44 |
|
|
* |
45 |
|
|
* haystack[0] = "testing" |
46 |
|
|
* needle = "testing123" |
47 |
|
|
* |
48 |
|
|
* will return 0, since all of the string in haystack[0] is present in needle. |
49 |
|
|
* |
50 |
|
|
* @param needle [const char *] "The value to look for" |
51 |
|
|
* @param haystack [const char **] "The array to look in" |
52 |
|
|
* @param size [size_t] "The amount of values in the array" |
53 |
|
|
* @return [int] "Will return 0 if present and -1 if not" |
54 |
|
|
*/ |
55 |
|
230 |
int strinarray(const char *needle, const char **haystack, size_t size) { |
56 |
✓✓ |
230 |
if ( unlikely(NULL == needle) ) { |
57 |
|
|
return -1; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
224 |
int i; |
61 |
|
224 |
unsigned long length = strlen(needle); |
62 |
|
|
|
63 |
✓✓ |
1301 |
for (i = 0; likely(i < (int) size); i++) { |
64 |
✓✗✓✓ ✓✓ |
1277 |
if (NULL != haystack[i] && length == strlen(haystack[i]) && strncmp(needle, haystack[i], length) == 0) { |
65 |
|
|
return 0; |
66 |
|
|
} |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
return -1; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
/** |
73 |
|
|
* Function that loads the content of a file into memory. |
74 |
|
|
* |
75 |
|
|
* @param path [char *] "The path to the subprotocols folder" |
76 |
|
|
* @param str [char **] "Will be filled with the content of the file" |
77 |
|
|
* @return [size_t] "The length of the string in memory" |
78 |
|
|
*/ |
79 |
|
88 |
size_t strload(char *path, char **str) { |
80 |
|
88 |
int n; |
81 |
|
88 |
long size; |
82 |
|
88 |
FILE *file; |
83 |
|
|
|
84 |
✓✓ |
88 |
if ( unlikely(NULL == (file = fopen(path, "rb"))) ) { |
85 |
|
2 |
WSS_log_error("Unable to open file: %s", strerror(errno)); |
86 |
|
2 |
*str = NULL; |
87 |
|
2 |
return 0; |
88 |
|
|
} |
89 |
|
|
|
90 |
✗✓ |
86 |
if ( unlikely(fseek(file, 0, SEEK_END) != 0) ) { |
91 |
|
|
WSS_log_error("Unable to seek to the end of file: %s", strerror(errno)); |
92 |
|
|
fclose(file); |
93 |
|
|
*str = NULL; |
94 |
|
|
return 0; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
86 |
size = ftell(file); |
98 |
|
|
|
99 |
✗✓ |
86 |
if ( unlikely(fseek(file, 0, SEEK_SET)) ) { |
100 |
|
|
WSS_log_error("Unable to seek back to the start of file: %s", strerror(errno)); |
101 |
|
|
fclose(file); |
102 |
|
|
*str = NULL; |
103 |
|
|
return 0; |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
|
107 |
✗✓ |
86 |
if ( unlikely(NULL == (*str = (char *)WSS_malloc(size + 1))) ) { |
108 |
|
|
WSS_log_error("Unable to allocate for string"); |
109 |
|
|
fclose(file); |
110 |
|
|
*str = NULL; |
111 |
|
|
return 0; |
112 |
|
|
} |
113 |
|
|
|
114 |
✗✓✗✓
|
172 |
if ( unlikely(fread(*str, sizeof(char), size, file) != (unsigned long)size) ) { |
115 |
|
|
WSS_log_error("Didn't read what was expected"); |
116 |
|
|
fclose(file); |
117 |
|
|
WSS_free((void **) &str); |
118 |
|
|
return 0; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
86 |
n = fclose(file); |
122 |
✗✓ |
86 |
if ( unlikely(n != 0) ) { |
123 |
|
|
WSS_log_error("Unable to close file: %s", strerror(errno)); |
124 |
|
|
WSS_free((void **) &str); |
125 |
|
|
return 0; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
return size; |
129 |
|
|
} |