Browse code

bb12284 - Fix to prevent path traversal when using cli_genfname() to generate filenames that may retain path and filename information. Changed scanrar so that it will no longer retain path information for extracted files.

Micah Snyder authored on 2019/03/03 03:05:17
Showing 10 changed files
... ...
@@ -116,6 +116,7 @@ CLAMAV_PRIVATE {
116 116
     cli_memstr;
117 117
     cli_strdup;
118 118
     cli_strndup;
119
+    cli_strnstr;
119 120
     cli_realloc;
120 121
     cli_ctime;
121 122
     tableCreate;
... ...
@@ -257,6 +258,7 @@ CLAMAV_PRIVATE {
257 257
     cl_get_pkey_file;
258 258
     cl_base64_decode;
259 259
     cl_base64_encode;
260
+    cli_sanitize_filepath;
260 261
   local:
261 262
     *;
262 263
 };
... ...
@@ -751,6 +751,15 @@ int cli_writen(int fd, const void *buff, unsigned int count);
751 751
 const char *cli_gettmpdir(void);
752 752
 
753 753
 /**
754
+ * @brief Sanitize a relative path, so it cannot have a negative depth.
755
+ *
756
+ * Caller is responsible for freeing the filename.
757
+ *
758
+ * @return char* filename or NULL.
759
+ */
760
+char *cli_sanitize_filepath(const char *filepath, size_t filepath_len);
761
+
762
+/**
754 763
  * @brief Generate tempfile filename (no path) with a random MD5 hash.
755 764
  *
756 765
  * Caller is responsible for freeing the filename.
... ...
@@ -29,49 +29,50 @@
29 29
 #include <string.h>
30 30
 #include <stdlib.h>
31 31
 #include <ctype.h>
32
-#ifdef	HAVE_UNISTD_H
32
+#ifdef HAVE_UNISTD_H
33 33
 #include <unistd.h>
34 34
 #endif
35 35
 #include <sys/types.h>
36 36
 #include <sys/stat.h>
37 37
 #include <dirent.h>
38
-#ifndef	_WIN32
38
+#ifndef _WIN32
39 39
 #include <sys/wait.h>
40 40
 #include <sys/time.h>
41 41
 #endif
42 42
 #include <time.h>
43 43
 #include <fcntl.h>
44
-#ifdef	HAVE_PWD_H
44
+#ifdef HAVE_PWD_H
45 45
 #include <pwd.h>
46 46
 #endif
47 47
 #include <errno.h>
48 48
 #include "target.h"
49
-#ifdef	HAVE_SYS_PARAM_H
49
+#ifdef HAVE_SYS_PARAM_H
50 50
 #include <sys/param.h>
51 51
 #endif
52
-#ifdef	HAVE_MALLOC_H
52
+#ifdef HAVE_MALLOC_H
53 53
 #include <malloc.h>
54 54
 #endif
55 55
 
56 56
 #include "clamav.h"
57 57
 #include "others.h"
58
+#include "platform.h"
58 59
 #include "regex/regex.h"
59 60
 #include "ltdl.h"
60 61
 #include "matcher-ac.h"
61 62
 
62
-static unsigned char name_salt[16] = { 16, 38, 97, 12, 8, 4, 72, 196, 217, 144, 33, 124, 18, 11, 17, 253 };
63
+static unsigned char name_salt[16] = {16, 38, 97, 12, 8, 4, 72, 196, 217, 144, 33, 124, 18, 11, 17, 253};
63 64
 
64 65
 #ifdef CL_NOTHREADS
65 66
 #undef CL_THREAD_SAFE
66 67
 #endif
67 68
 
68 69
 #ifdef CL_THREAD_SAFE
69
-#  include <pthread.h>
70
+#include <pthread.h>
70 71
 
71 72
 static pthread_mutex_t cli_gentemp_mutex = PTHREAD_MUTEX_INITIALIZER;
72
-# ifndef HAVE_CTIME_R
73
+#ifndef HAVE_CTIME_R
73 74
 static pthread_mutex_t cli_ctime_mutex = PTHREAD_MUTEX_INITIALIZER;
74
-# endif
75
+#endif
75 76
 static pthread_mutex_t cli_strerror_mutex = PTHREAD_MUTEX_INITIALIZER;
76 77
 static pthread_key_t cli_ctx_tls_key;
77 78
 static pthread_once_t cli_ctx_tls_key_once = PTHREAD_ONCE_INIT;
... ...
@@ -81,7 +82,7 @@ static void cli_ctx_tls_key_alloc(void)
81 81
     pthread_key_create(&cli_ctx_tls_key, NULL);
82 82
 }
83 83
 
84
-void cli_logg_setup(const cli_ctx *ctx)
84
+void cli_logg_setup(const cli_ctx* ctx)
85 85
 {
86 86
     pthread_once(&cli_ctx_tls_key_once, cli_ctx_tls_key_alloc);
87 87
     pthread_setspecific(cli_ctx_tls_key, ctx);
... ...
@@ -92,22 +93,22 @@ void cli_logg_unsetup(void)
92 92
     pthread_setspecific(cli_ctx_tls_key, NULL);
93 93
 }
94 94
 
95
-static inline void *cli_getctx(void)
95
+static inline void* cli_getctx(void)
96 96
 {
97
-    cli_ctx *ctx;
97
+    cli_ctx* ctx;
98 98
     pthread_once(&cli_ctx_tls_key_once, cli_ctx_tls_key_alloc);
99 99
     ctx = pthread_getspecific(cli_ctx_tls_key);
100 100
     return ctx ? ctx->cb_ctx : NULL;
101 101
 }
102 102
 #else
103 103
 
104
-static const cli_ctx *current_ctx = NULL;
105
-void cli_logg_setup(const cli_ctx *ctx)
104
+static const cli_ctx* current_ctx = NULL;
105
+void cli_logg_setup(const cli_ctx* ctx)
106 106
 {
107 107
     current_ctx = ctx;
108 108
 }
109 109
 
110
-static inline void *cli_getctx(void)
110
+static inline void* cli_getctx(void)
111 111
 {
112 112
     return current_ctx ? current_ctx->cb_ctx : NULL;
113 113
 }
... ...
@@ -117,10 +118,10 @@ void cli_logg_unsetup(void)
117 117
 }
118 118
 #endif
119 119
 
120
-uint8_t cli_debug_flag = 0;
120
+uint8_t cli_debug_flag              = 0;
121 121
 uint8_t cli_always_gen_section_hash = 0;
122 122
 
123
-static void fputs_callback(enum cl_msg severity, const char *fullmsg, const char *msg, void *context)
123
+static void fputs_callback(enum cl_msg severity, const char* fullmsg, const char* msg, void* context)
124 124
 {
125 125
     UNUSEDPARAM(severity);
126 126
     UNUSEDPARAM(msg);
... ...
@@ -135,138 +136,136 @@ void cl_set_clcb_msg(clcb_msg callback)
135 135
     msg_callback = callback;
136 136
 }
137 137
 
138
-#define MSGCODE(buff, len, x)				    \
139
-	va_list args;					    \
140
-	size_t len = sizeof(x) - 1;			    \
141
-	char buff[BUFSIZ];				    \
142
-    strncpy(buff, x, len);				    \
143
-    va_start(args, str);				    \
144
-    vsnprintf(buff + len, sizeof(buff) - len, str, args);   \
145
-    buff[sizeof(buff) - 1] = '\0';			    \
138
+#define MSGCODE(buff, len, x)                             \
139
+    va_list args;                                         \
140
+    size_t len = sizeof(x) - 1;                           \
141
+    char buff[BUFSIZ];                                    \
142
+    strncpy(buff, x, len);                                \
143
+    va_start(args, str);                                  \
144
+    vsnprintf(buff + len, sizeof(buff) - len, str, args); \
145
+    buff[sizeof(buff) - 1] = '\0';                        \
146 146
     va_end(args)
147 147
 
148
-void cli_warnmsg(const char *str, ...)
148
+void cli_warnmsg(const char* str, ...)
149 149
 {
150 150
     MSGCODE(buff, len, "LibClamAV Warning: ");
151
-    msg_callback(CL_MSG_WARN, buff, buff+len, cli_getctx());
151
+    msg_callback(CL_MSG_WARN, buff, buff + len, cli_getctx());
152 152
 }
153 153
 
154
-void cli_errmsg(const char *str, ...)
154
+void cli_errmsg(const char* str, ...)
155 155
 {
156 156
     MSGCODE(buff, len, "LibClamAV Error: ");
157
-    msg_callback(CL_MSG_ERROR, buff, buff+len, cli_getctx());
157
+    msg_callback(CL_MSG_ERROR, buff, buff + len, cli_getctx());
158 158
 }
159 159
 
160
-void cli_infomsg(const cli_ctx* ctx, const char *str, ...)
160
+void cli_infomsg(const cli_ctx* ctx, const char* str, ...)
161 161
 {
162 162
     MSGCODE(buff, len, "LibClamAV info: ");
163
-    msg_callback(CL_MSG_INFO_VERBOSE, buff, buff+len, ctx ? ctx->cb_ctx : NULL);
163
+    msg_callback(CL_MSG_INFO_VERBOSE, buff, buff + len, ctx ? ctx->cb_ctx : NULL);
164 164
 }
165 165
 
166
-void cli_dbgmsg_internal(const char *str, ...)
166
+void cli_dbgmsg_internal(const char* str, ...)
167 167
 {
168 168
     MSGCODE(buff, len, "LibClamAV debug: ");
169 169
     fputs(buff, stderr);
170 170
 }
171 171
 
172
-int cli_matchregex(const char *str, const char *regex)
172
+int cli_matchregex(const char* str, const char* regex)
173 173
 {
174
-	regex_t reg;
175
-	int match, flags = REG_EXTENDED | REG_NOSUB;
174
+    regex_t reg;
175
+    int match, flags = REG_EXTENDED | REG_NOSUB;
176 176
 #ifdef _WIN32
177 177
     flags |= REG_ICASE;
178 178
 #endif
179 179
     if(cli_regcomp(&reg, regex, flags) == 0) {
180
-	match = (cli_regexec(&reg, str, 0, NULL, 0) == REG_NOMATCH) ? 0 : 1;
181
-	cli_regfree(&reg);
182
-	return match;
180
+        match = (cli_regexec(&reg, str, 0, NULL, 0) == REG_NOMATCH) ? 0 : 1;
181
+        cli_regfree(&reg);
182
+        return match;
183 183
     }
184 184
 
185 185
     return 0;
186 186
 }
187
-void *cli_malloc(size_t size)
187
+void* cli_malloc(size_t size)
188 188
 {
189
-	void *alloc;
190
-
189
+    void* alloc;
191 190
 
192 191
     if(!size || size > CLI_MAX_ALLOCATION) {
193
-	cli_errmsg("cli_malloc(): Attempt to allocate %lu bytes. Please report to https://bugzilla.clamav.net\n", (unsigned long int) size);
194
-	return NULL;
192
+        cli_errmsg("cli_malloc(): Attempt to allocate %lu bytes. Please report to https://bugzilla.clamav.net\n", (unsigned long int)size);
193
+        return NULL;
195 194
     }
196 195
 
197 196
     alloc = malloc(size);
198 197
 
199 198
     if(!alloc) {
200
-	perror("malloc_problem");
201
-	cli_errmsg("cli_malloc(): Can't allocate memory (%lu bytes).\n", (unsigned long int) size);
202
-	return NULL;
203
-    } else return alloc;
199
+        perror("malloc_problem");
200
+        cli_errmsg("cli_malloc(): Can't allocate memory (%lu bytes).\n", (unsigned long int)size);
201
+        return NULL;
202
+    } else
203
+        return alloc;
204 204
 }
205 205
 
206
-void *cli_calloc(size_t nmemb, size_t size)
206
+void* cli_calloc(size_t nmemb, size_t size)
207 207
 {
208
-	void *alloc;
209
-
208
+    void* alloc;
210 209
 
211
-    if(!nmemb || !size || size > CLI_MAX_ALLOCATION || nmemb > CLI_MAX_ALLOCATION
212
-        || (nmemb*size > CLI_MAX_ALLOCATION)) {
213
-	cli_errmsg("cli_calloc(): Attempt to allocate %lu bytes. Please report to https://bugzilla.clamav.net\n", (unsigned long int) nmemb*size);
214
-	return NULL;
210
+    if(!nmemb || !size || size > CLI_MAX_ALLOCATION || nmemb > CLI_MAX_ALLOCATION || (nmemb * size > CLI_MAX_ALLOCATION)) {
211
+        cli_errmsg("cli_calloc(): Attempt to allocate %lu bytes. Please report to https://bugzilla.clamav.net\n", (unsigned long int)nmemb * size);
212
+        return NULL;
215 213
     }
216 214
 
217 215
     alloc = calloc(nmemb, size);
218 216
 
219 217
     if(!alloc) {
220
-	perror("calloc_problem");
221
-	cli_errmsg("cli_calloc(): Can't allocate memory (%lu bytes).\n", (unsigned long int) (nmemb * size));
222
-	return NULL;
223
-    } else return alloc;
218
+        perror("calloc_problem");
219
+        cli_errmsg("cli_calloc(): Can't allocate memory (%lu bytes).\n", (unsigned long int)(nmemb * size));
220
+        return NULL;
221
+    } else
222
+        return alloc;
224 223
 }
225 224
 
226
-void *cli_realloc(void *ptr, size_t size)
225
+void* cli_realloc(void* ptr, size_t size)
227 226
 {
228
-	void *alloc;
229
-
227
+    void* alloc;
230 228
 
231 229
     if(!size || size > CLI_MAX_ALLOCATION) {
232
-	cli_errmsg("cli_realloc(): Attempt to allocate %lu bytes. Please report to https://bugzilla.clamav.net\n", (unsigned long int) size);
233
-	return NULL;
230
+        cli_errmsg("cli_realloc(): Attempt to allocate %lu bytes. Please report to https://bugzilla.clamav.net\n", (unsigned long int)size);
231
+        return NULL;
234 232
     }
235 233
 
236 234
     alloc = realloc(ptr, size);
237 235
 
238 236
     if(!alloc) {
239
-	perror("realloc_problem");
240
-	cli_errmsg("cli_realloc(): Can't re-allocate memory to %lu bytes.\n", (unsigned long int) size);
241
-	return NULL;
242
-    } else return alloc;
237
+        perror("realloc_problem");
238
+        cli_errmsg("cli_realloc(): Can't re-allocate memory to %lu bytes.\n", (unsigned long int)size);
239
+        return NULL;
240
+    } else
241
+        return alloc;
243 242
 }
244 243
 
245
-void *cli_realloc2(void *ptr, size_t size)
244
+void* cli_realloc2(void* ptr, size_t size)
246 245
 {
247
-	void *alloc;
248
-
246
+    void* alloc;
249 247
 
250 248
     if(!size || size > CLI_MAX_ALLOCATION) {
251
-	cli_errmsg("cli_realloc2(): Attempt to allocate %lu bytes. Please report to https://bugzilla.clamav.net\n", (unsigned long int) size);
252
-	return NULL;
249
+        cli_errmsg("cli_realloc2(): Attempt to allocate %lu bytes. Please report to https://bugzilla.clamav.net\n", (unsigned long int)size);
250
+        return NULL;
253 251
     }
254 252
 
255 253
     alloc = realloc(ptr, size);
256 254
 
257 255
     if(!alloc) {
258
-	perror("realloc_problem");
259
-	cli_errmsg("cli_realloc2(): Can't re-allocate memory to %lu bytes.\n", (unsigned long int) size);
260
-	if(ptr)
261
-	    free(ptr);
262
-	return NULL;
263
-    } else return alloc;
256
+        perror("realloc_problem");
257
+        cli_errmsg("cli_realloc2(): Can't re-allocate memory to %lu bytes.\n", (unsigned long int)size);
258
+        if(ptr)
259
+            free(ptr);
260
+        return NULL;
261
+    } else
262
+        return alloc;
264 263
 }
265 264
 
266
-char *cli_strdup(const char *s)
265
+char* cli_strdup(const char* s)
267 266
 {
268
-        char *alloc;
269
-
267
+    char* alloc;
270 268
 
271 269
     if(s == NULL) {
272 270
         cli_errmsg("cli_strdup(): s == NULL. Please report to https://bugzilla.clamav.net\n");
... ...
@@ -277,7 +276,7 @@ char *cli_strdup(const char *s)
277 277
 
278 278
     if(!alloc) {
279 279
         perror("strdup_problem");
280
-        cli_errmsg("cli_strdup(): Can't allocate memory (%u bytes).\n", (unsigned int) strlen(s));
280
+        cli_errmsg("cli_strdup(): Can't allocate memory (%u bytes).\n", (unsigned int)strlen(s));
281 281
         return NULL;
282 282
     }
283 283
 
... ...
@@ -285,142 +284,137 @@ char *cli_strdup(const char *s)
285 285
 }
286 286
 
287 287
 /* returns converted timestamp, in case of error the returned string contains at least one character */
288
-const char* cli_ctime(const time_t *timep, char *buf, const size_t bufsize)
288
+const char* cli_ctime(const time_t* timep, char* buf, const size_t bufsize)
289 289
 {
290
-	const char *ret;
291
-	if(bufsize < 26) {
292
-		/* standard says we must have at least 26 bytes buffer */
293
-		cli_warnmsg("buffer too small for ctime\n");
294
-		return " ";
295
-	}
296
-	if((uint32_t)(*timep) > 0x7fffffff) {
297
-		/* some systems can consider these timestamps invalid */
298
-		strncpy(buf, "invalid timestamp", bufsize-1);
299
-		buf[bufsize-1] = '\0';
300
-		return buf;
301
-	}
302
-
303
-#ifdef HAVE_CTIME_R	
304
-# ifdef HAVE_CTIME_R_2
305
-	ret = ctime_r(timep, buf);
306
-# else
307
-	ret = ctime_r(timep, buf, bufsize);
308
-# endif
290
+    const char* ret;
291
+    if(bufsize < 26) {
292
+        /* standard says we must have at least 26 bytes buffer */
293
+        cli_warnmsg("buffer too small for ctime\n");
294
+        return " ";
295
+    }
296
+    if((uint32_t)(*timep) > 0x7fffffff) {
297
+        /* some systems can consider these timestamps invalid */
298
+        strncpy(buf, "invalid timestamp", bufsize - 1);
299
+        buf[bufsize - 1] = '\0';
300
+        return buf;
301
+    }
302
+
303
+#ifdef HAVE_CTIME_R
304
+#ifdef HAVE_CTIME_R_2
305
+    ret = ctime_r(timep, buf);
306
+#else
307
+    ret = ctime_r(timep, buf, bufsize);
308
+#endif
309 309
 #else /* no ctime_r */
310 310
 
311
-# ifdef CL_THREAD_SAFE
312
-	pthread_mutex_lock(&cli_ctime_mutex);
313
-# endif
314
-	ret = ctime(timep);
315
-	if(ret) {
316
-		strncpy(buf, ret, bufsize-1);
317
-		buf[bufsize-1] = '\0';
318
-		ret = buf;
319
-	}
320
-# ifdef CL_THREAD_SAFE
321
-	pthread_mutex_unlock(&cli_ctime_mutex);
322
-# endif
311
+#ifdef CL_THREAD_SAFE
312
+    pthread_mutex_lock(&cli_ctime_mutex);
313
+#endif
314
+    ret = ctime(timep);
315
+    if(ret) {
316
+        strncpy(buf, ret, bufsize - 1);
317
+        buf[bufsize - 1] = '\0';
318
+        ret              = buf;
319
+    }
320
+#ifdef CL_THREAD_SAFE
321
+    pthread_mutex_unlock(&cli_ctime_mutex);
323 322
 #endif
324
-	/* common */
325
-	if(!ret) {
326
-		buf[0] = ' ';
327
-		buf[1] = '\0';
328
-		return buf;
329
-	}
330
-	return ret;
323
+#endif
324
+    /* common */
325
+    if(!ret) {
326
+        buf[0] = ' ';
327
+        buf[1] = '\0';
328
+        return buf;
329
+    }
330
+    return ret;
331 331
 }
332 332
 
333 333
 /* Function: readn
334 334
         Try hard to read the requested number of bytes
335 335
 */
336
-int cli_readn(int fd, void *buff, unsigned int count)
336
+int cli_readn(int fd, void* buff, unsigned int count)
337 337
 {
338
-        int retval;
339
-        unsigned int todo;
340
-        unsigned char *current;
341
-
338
+    int retval;
339
+    unsigned int todo;
340
+    unsigned char* current;
342 341
 
343
-        todo = count;
344
-        current = (unsigned char *) buff;
345
-
346
-        do {
347
-                retval = read(fd, current, todo);
348
-                if (retval == 0) {
349
-                        return (count - todo);
350
-                }
351
-                if (retval < 0) {
352
-			char err[128];
353
-			if (errno == EINTR) {
354
-				continue;
355
-			}
356
-			cli_errmsg("cli_readn: read error: %s\n", cli_strerror(errno, err, sizeof(err)));
357
-                        return -1;
358
-                }
359
-                todo -= retval;
360
-                current += retval;
361
-        } while (todo > 0);
342
+    todo    = count;
343
+    current = (unsigned char*)buff;
362 344
 
345
+    do {
346
+        retval = read(fd, current, todo);
347
+        if(retval == 0) {
348
+            return (count - todo);
349
+        }
350
+        if(retval < 0) {
351
+            char err[128];
352
+            if(errno == EINTR) {
353
+                continue;
354
+            }
355
+            cli_errmsg("cli_readn: read error: %s\n", cli_strerror(errno, err, sizeof(err)));
356
+            return -1;
357
+        }
358
+        todo -= retval;
359
+        current += retval;
360
+    } while(todo > 0);
363 361
 
364
-        return count;
362
+    return count;
365 363
 }
366 364
 
367 365
 /* Function: writen
368 366
         Try hard to write the specified number of bytes
369 367
 */
370
-int cli_writen(int fd, const void *buff, unsigned int count)
368
+int cli_writen(int fd, const void* buff, unsigned int count)
371 369
 {
372
-        int retval;
373
-        unsigned int todo;
374
-        const unsigned char *current;
375
-
376
-
377
-        todo = count;
378
-        current = (const unsigned char *) buff;
379
-
380
-        do {
381
-                retval = write(fd, current, todo);
382
-                if (retval < 0) {
383
-			char err[128];
384
-			if (errno == EINTR) {
385
-				continue;
386
-			}
387
-			cli_errmsg("cli_writen: write error: %s\n", cli_strerror(errno, err, sizeof(err)));
388
-                        return -1;
389
-                }
390
-                todo -= retval;
391
-                current += retval;
392
-        } while (todo > 0);
393
-
370
+    int retval;
371
+    unsigned int todo;
372
+    const unsigned char* current;
373
+
374
+    todo    = count;
375
+    current = (const unsigned char*)buff;
376
+
377
+    do {
378
+        retval = write(fd, current, todo);
379
+        if(retval < 0) {
380
+            char err[128];
381
+            if(errno == EINTR) {
382
+                continue;
383
+            }
384
+            cli_errmsg("cli_writen: write error: %s\n", cli_strerror(errno, err, sizeof(err)));
385
+            return -1;
386
+        }
387
+        todo -= retval;
388
+        current += retval;
389
+    } while(todo > 0);
394 390
 
395
-        return count;
391
+    return count;
396 392
 }
397 393
 
398
-int cli_filecopy(const char *src, const char *dest)
394
+int cli_filecopy(const char* src, const char* dest)
399 395
 {
400 396
 
401 397
 #ifdef _WIN32
402 398
     return CopyFileA(src, dest, 0) ? 0 : -1;
403 399
 #else
404
-	char *buffer;
405
-	int s, d, bytes;
400
+    char* buffer;
401
+    int s, d, bytes;
406 402
 
403
+    if((s = open(src, O_RDONLY | O_BINARY)) == -1)
404
+        return -1;
407 405
 
408
-    if((s = open(src, O_RDONLY|O_BINARY)) == -1)
409
-	return -1;
410
-
411
-    if((d = open(dest, O_CREAT|O_WRONLY|O_TRUNC|O_BINARY, S_IRWXU)) == -1) {
412
-	close(s);
413
-	return -1;
406
+    if((d = open(dest, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, S_IRWXU)) == -1) {
407
+        close(s);
408
+        return -1;
414 409
     }
415 410
 
416 411
     if(!(buffer = cli_malloc(FILEBUFF))) {
417
-	close(s);
418
-	close(d);
419
-	return -1;
412
+        close(s);
413
+        close(d);
414
+        return -1;
420 415
     }
421 416
 
422 417
     while((bytes = cli_readn(s, buffer, FILEBUFF)) > 0)
423
-	cli_writen(d, buffer, bytes);
418
+        cli_writen(d, buffer, bytes);
424 419
 
425 420
     free(buffer);
426 421
     close(s);
... ...
@@ -437,39 +431,40 @@ int cli_filecopy(const char *src, const char *dest)
437 437
 #endif /* _WIN32 */
438 438
 #endif /* P_tmpdir */
439 439
 
440
-const char *cli_gettmpdir(void) {
441
-	const char *tmpdir;
440
+const char* cli_gettmpdir(void)
441
+{
442
+    const char* tmpdir;
442 443
     unsigned int i;
443 444
 
444 445
 #ifdef _WIN32
445
-    char *envs[] = { "TEMP", "TMP", NULL };
446
+    char* envs[] = {"TEMP", "TMP", NULL};
446 447
 #else
447
-    char *envs[] = { "TMPDIR", NULL };
448
+    char* envs[] = {"TMPDIR", NULL};
448 449
 #endif
449 450
 
450
-    for (i=0; envs[i] != NULL; i++)
451
-        if ((tmpdir = getenv(envs[i])))
451
+    for(i = 0; envs[i] != NULL; i++)
452
+        if((tmpdir = getenv(envs[i])))
452 453
             return tmpdir;
453 454
 
454 455
     return P_tmpdir;
455 456
 }
456 457
 
457 458
 struct dirent_data {
458
-    char *filename;
459
-    const char *dirname;
460
-    STATBUF *statbuf;
461
-    long  ino; /* -1: inode not available */
462
-    int   is_dir;/* 0 - no, 1 - yes */
459
+    char* filename;
460
+    const char* dirname;
461
+    STATBUF* statbuf;
462
+    long ino;   /* -1: inode not available */
463
+    int is_dir; /* 0 - no, 1 - yes */
463 464
 };
464 465
 
465 466
 /* sort files before directories, and lower inodes before higher inodes */
466
-static int ftw_compare(const void *a, const void *b)
467
+static int ftw_compare(const void* a, const void* b)
467 468
 {
468
-    const struct dirent_data *da = a;
469
-    const struct dirent_data *db = b;
470
-    long diff = da->is_dir - db->is_dir;
471
-    if (!diff) {
472
-	diff = da->ino - db->ino;
469
+    const struct dirent_data* da = a;
470
+    const struct dirent_data* db = b;
471
+    long diff                    = da->is_dir - db->is_dir;
472
+    if(!diff) {
473
+        diff = da->ino - db->ino;
473 474
     }
474 475
     return diff;
475 476
 }
... ...
@@ -489,100 +484,98 @@ static inline int ft_skipped(enum filetype ft)
489 489
 }
490 490
 
491 491
 #define FOLLOW_SYMLINK_MASK (CLI_FTW_FOLLOW_FILE_SYMLINK | CLI_FTW_FOLLOW_DIR_SYMLINK)
492
-static int get_filetype(const char *fname, int flags, int need_stat,
493
-			 STATBUF *statbuf, enum filetype *ft)
492
+static int get_filetype(const char* fname, int flags, int need_stat,
493
+                        STATBUF* statbuf, enum filetype* ft)
494 494
 {
495 495
     int stated = 0;
496 496
 
497
-    if (*ft == ft_unknown || *ft == ft_link) {
498
-	need_stat = 1;
497
+    if(*ft == ft_unknown || *ft == ft_link) {
498
+        need_stat = 1;
499 499
 
500
-	if ((flags & FOLLOW_SYMLINK_MASK) != FOLLOW_SYMLINK_MASK) {
501
-	    /* Following only one of directory/file symlinks, or none, may
500
+        if((flags & FOLLOW_SYMLINK_MASK) != FOLLOW_SYMLINK_MASK) {
501
+            /* Following only one of directory/file symlinks, or none, may
502 502
 	     * need to lstat.
503 503
 	     * If we're following both file and directory symlinks, we don't need
504 504
 	     * to lstat(), we can just stat() directly.*/
505
-	    if (*ft != ft_link) {
506
-		/* need to lstat to determine if it is a symlink */
507
-		if (LSTAT(fname, statbuf) == -1)
508
-		    return -1;
509
-		if (S_ISLNK(statbuf->st_mode)) {
510
-		    *ft = ft_link;
511
-		} else {
512
-		    /* It was not a symlink, stat() not needed */
513
-		    need_stat = 0;
514
-		    stated = 1;
515
-		}
516
-	    }
517
-	    if (*ft == ft_link && !(flags & FOLLOW_SYMLINK_MASK)) {
518
-		/* This is a symlink, but we don't follow any symlinks */
519
-		*ft = ft_skipped_link;
520
-		return 0;
521
-	    }
522
-	}
505
+            if(*ft != ft_link) {
506
+                /* need to lstat to determine if it is a symlink */
507
+                if(LSTAT(fname, statbuf) == -1)
508
+                    return -1;
509
+                if(S_ISLNK(statbuf->st_mode)) {
510
+                    *ft = ft_link;
511
+                } else {
512
+                    /* It was not a symlink, stat() not needed */
513
+                    need_stat = 0;
514
+                    stated    = 1;
515
+                }
516
+            }
517
+            if(*ft == ft_link && !(flags & FOLLOW_SYMLINK_MASK)) {
518
+                /* This is a symlink, but we don't follow any symlinks */
519
+                *ft = ft_skipped_link;
520
+                return 0;
521
+            }
522
+        }
523 523
     }
524 524
 
525
-    if (need_stat) {
526
-	if (CLAMSTAT(fname, statbuf) == -1)
527
-	    return -1;
528
-	stated = 1;
525
+    if(need_stat) {
526
+        if(CLAMSTAT(fname, statbuf) == -1)
527
+            return -1;
528
+        stated = 1;
529 529
     }
530 530
 
531
-    if (*ft == ft_unknown || *ft == ft_link) {
532
-	if (S_ISDIR(statbuf->st_mode) &&
533
-	    (*ft != ft_link || (flags & CLI_FTW_FOLLOW_DIR_SYMLINK))) {
534
-	    /* A directory, or (a symlink to a directory and we're following dir
531
+    if(*ft == ft_unknown || *ft == ft_link) {
532
+        if(S_ISDIR(statbuf->st_mode) &&
533
+           (*ft != ft_link || (flags & CLI_FTW_FOLLOW_DIR_SYMLINK))) {
534
+            /* A directory, or (a symlink to a directory and we're following dir
535 535
 	     * symlinks) */
536
-	    *ft = ft_directory;
537
-	} else if (S_ISREG(statbuf->st_mode) &&
538
-		   (*ft != ft_link || (flags & CLI_FTW_FOLLOW_FILE_SYMLINK))) {
539
-	    /* A file, or (a symlink to a file and we're following file symlinks) */
540
-	    *ft = ft_regular;
541
-	} else {
542
-	    /* default: skipped */
543
-	    *ft = S_ISLNK(statbuf->st_mode) ?
544
-		ft_skipped_link : ft_skipped_special;
545
-	}
536
+            *ft = ft_directory;
537
+        } else if(S_ISREG(statbuf->st_mode) &&
538
+                  (*ft != ft_link || (flags & CLI_FTW_FOLLOW_FILE_SYMLINK))) {
539
+            /* A file, or (a symlink to a file and we're following file symlinks) */
540
+            *ft = ft_regular;
541
+        } else {
542
+            /* default: skipped */
543
+            *ft = S_ISLNK(statbuf->st_mode) ? ft_skipped_link : ft_skipped_special;
544
+        }
546 545
     }
547 546
     return stated;
548 547
 }
549 548
 
550
-static int handle_filetype(const char *fname, int flags,
551
-			   STATBUF *statbuf, int *stated, enum filetype *ft,
552
-			   cli_ftw_cb callback, struct cli_ftw_cbdata *data)
549
+static int handle_filetype(const char* fname, int flags,
550
+                           STATBUF* statbuf, int* stated, enum filetype* ft,
551
+                           cli_ftw_cb callback, struct cli_ftw_cbdata* data)
553 552
 {
554 553
     int ret;
555 554
 
556
-    *stated = get_filetype(fname, flags, flags & CLI_FTW_NEED_STAT , statbuf, ft);
557
-
558
-    if (*stated == -1) {
559
-	/*  we failed a stat() or lstat() */
560
-	ret = callback(NULL, NULL, fname, error_stat, data);
561
-	if (ret != CL_SUCCESS)
562
-	    return ret;
563
-	*ft = ft_unknown;
564
-    } else if (*ft == ft_skipped_link || *ft == ft_skipped_special) {
565
-	/* skipped filetype */
566
-	ret = callback(stated ? statbuf : NULL, NULL, fname,
567
-		       *ft == ft_skipped_link ?
568
-		       warning_skipped_link : warning_skipped_special, data);
569
-	if (ret != CL_SUCCESS)
570
-	    return ret;
555
+    *stated = get_filetype(fname, flags, flags & CLI_FTW_NEED_STAT, statbuf, ft);
556
+
557
+    if(*stated == -1) {
558
+        /*  we failed a stat() or lstat() */
559
+        ret = callback(NULL, NULL, fname, error_stat, data);
560
+        if(ret != CL_SUCCESS)
561
+            return ret;
562
+        *ft = ft_unknown;
563
+    } else if(*ft == ft_skipped_link || *ft == ft_skipped_special) {
564
+        /* skipped filetype */
565
+        ret = callback(stated ? statbuf : NULL, NULL, fname,
566
+                       *ft == ft_skipped_link ? warning_skipped_link : warning_skipped_special, data);
567
+        if(ret != CL_SUCCESS)
568
+            return ret;
571 569
     }
572 570
     return CL_SUCCESS;
573 571
 }
574 572
 
575
-static int cli_ftw_dir(const char *dirname, int flags, int maxdepth, cli_ftw_cb callback, struct cli_ftw_cbdata *data, cli_ftw_pathchk pathchk);
576
-static int handle_entry(struct dirent_data *entry, int flags, int maxdepth, cli_ftw_cb callback, struct cli_ftw_cbdata *data, cli_ftw_pathchk pathchk)
573
+static int cli_ftw_dir(const char* dirname, int flags, int maxdepth, cli_ftw_cb callback, struct cli_ftw_cbdata* data, cli_ftw_pathchk pathchk);
574
+static int handle_entry(struct dirent_data* entry, int flags, int maxdepth, cli_ftw_cb callback, struct cli_ftw_cbdata* data, cli_ftw_pathchk pathchk)
577 575
 {
578
-    if (!entry->is_dir) {
579
-	return callback(entry->statbuf, entry->filename, entry->filename, visit_file, data);
576
+    if(!entry->is_dir) {
577
+        return callback(entry->statbuf, entry->filename, entry->filename, visit_file, data);
580 578
     } else {
581
-	return cli_ftw_dir(entry->dirname, flags, maxdepth, callback, data, pathchk);
579
+        return cli_ftw_dir(entry->dirname, flags, maxdepth, callback, data, pathchk);
582 580
     }
583 581
 }
584 582
 
585
-int cli_ftw(char *path, int flags, int maxdepth, cli_ftw_cb callback, struct cli_ftw_cbdata *data, cli_ftw_pathchk pathchk)
583
+int cli_ftw(char* path, int flags, int maxdepth, cli_ftw_cb callback, struct cli_ftw_cbdata* data, cli_ftw_pathchk pathchk)
586 584
 {
587 585
     STATBUF statbuf;
588 586
     enum filetype ft = ft_unknown;
... ...
@@ -590,215 +583,215 @@ int cli_ftw(char *path, int flags, int maxdepth, cli_ftw_cb callback, struct cli
590 590
     int stated = 0;
591 591
     int ret;
592 592
 
593
-    if (((flags & CLI_FTW_TRIM_SLASHES) || pathchk) && path[0] && path[1]) {
594
-	char *pathend;
595
-	/* trim slashes so that dir and dir/ behave the same when
593
+    if(((flags & CLI_FTW_TRIM_SLASHES) || pathchk) && path[0] && path[1]) {
594
+        char* pathend;
595
+        /* trim slashes so that dir and dir/ behave the same when
596 596
 	 * they are symlinks, and we are not following symlinks */
597 597
 #ifndef _WIN32
598
-	while (path[0] == *PATHSEP && path[1] == *PATHSEP) path++;
598
+        while(path[0] == *PATHSEP && path[1] == *PATHSEP) path++;
599 599
 #endif
600
-	pathend = path + strlen(path);
601
-	while (pathend > path && pathend[-1] == *PATHSEP) --pathend;
602
-	*pathend = '\0';
600
+        pathend = path + strlen(path);
601
+        while(pathend > path && pathend[-1] == *PATHSEP) --pathend;
602
+        *pathend = '\0';
603 603
     }
604 604
     if(pathchk && pathchk(path, data) == 1)
605
-	return CL_SUCCESS;
605
+        return CL_SUCCESS;
606 606
     ret = handle_filetype(path, flags, &statbuf, &stated, &ft, callback, data);
607
-    if (ret != CL_SUCCESS)
608
-	return ret;
609
-    if (ft_skipped(ft))
610
-	return CL_SUCCESS;
611
-    entry.statbuf = stated ? &statbuf : NULL;
612
-    entry.is_dir = ft == ft_directory;
607
+    if(ret != CL_SUCCESS)
608
+        return ret;
609
+    if(ft_skipped(ft))
610
+        return CL_SUCCESS;
611
+    entry.statbuf  = stated ? &statbuf : NULL;
612
+    entry.is_dir   = ft == ft_directory;
613 613
     entry.filename = entry.is_dir ? NULL : strdup(path);
614
-    entry.dirname = entry.is_dir ? path : NULL;
615
-    if (entry.is_dir) {
616
-	ret = callback(entry.statbuf, NULL, path, visit_directory_toplev, data);
617
-	if (ret != CL_SUCCESS)
618
-	    return ret;
614
+    entry.dirname  = entry.is_dir ? path : NULL;
615
+    if(entry.is_dir) {
616
+        ret = callback(entry.statbuf, NULL, path, visit_directory_toplev, data);
617
+        if(ret != CL_SUCCESS)
618
+            return ret;
619 619
     }
620 620
     return handle_entry(&entry, flags, maxdepth, callback, data, pathchk);
621 621
 }
622 622
 
623
-static int cli_ftw_dir(const char *dirname, int flags, int maxdepth, cli_ftw_cb callback, struct cli_ftw_cbdata *data, cli_ftw_pathchk pathchk)
623
+static int cli_ftw_dir(const char* dirname, int flags, int maxdepth, cli_ftw_cb callback, struct cli_ftw_cbdata* data, cli_ftw_pathchk pathchk)
624 624
 {
625
-    DIR *dd;
625
+    DIR* dd;
626 626
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
627 627
     union {
628
-	struct dirent d;
629
-	char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
628
+        struct dirent d;
629
+        char b[offsetof(struct dirent, d_name) + NAME_MAX + 1];
630 630
     } result;
631 631
 #endif
632
-    struct dirent_data *entries = NULL;
632
+    struct dirent_data* entries = NULL;
633 633
     size_t i, entries_cnt = 0;
634 634
     int ret;
635 635
 
636
-    if (maxdepth < 0) {
637
-	/* exceeded recursion limit */
638
-	ret = callback(NULL, NULL, dirname, warning_skipped_dir, data);
639
-	return ret;
636
+    if(maxdepth < 0) {
637
+        /* exceeded recursion limit */
638
+        ret = callback(NULL, NULL, dirname, warning_skipped_dir, data);
639
+        return ret;
640 640
     }
641 641
 
642 642
     if((dd = opendir(dirname)) != NULL) {
643
-	struct dirent *dent;
644
-	int err;
645
-	errno = 0;
646
-	ret = CL_SUCCESS;
643
+        struct dirent* dent;
644
+        int err;
645
+        errno = 0;
646
+        ret   = CL_SUCCESS;
647 647
 #ifdef HAVE_READDIR_R_3
648
-	while(!(err = readdir_r(dd, &result.d, &dent)) && dent) {
648
+        while(!(err = readdir_r(dd, &result.d, &dent)) && dent) {
649 649
 #elif defined(HAVE_READDIR_R_2)
650
-	while((dent = (struct dirent *) readdir_r(dd, &result.d))) {
650
+        while((dent = (struct dirent*)readdir_r(dd, &result.d))) {
651 651
 #else
652
-	while((dent = readdir(dd))) {
652
+        while((dent = readdir(dd))) {
653 653
 #endif
654
-	    int stated = 0;
655
-	    enum filetype ft;
656
-	    char *fname;
657
-	    STATBUF statbuf;
658
-	    STATBUF *statbufp;
659
-
660
-	    if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
661
-		continue;
654
+            int stated = 0;
655
+            enum filetype ft;
656
+            char* fname;
657
+            STATBUF statbuf;
658
+            STATBUF* statbufp;
659
+
660
+            if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
661
+                continue;
662 662
 #ifdef _DIRENT_HAVE_D_TYPE
663
-	    switch (dent->d_type) {
664
-		case DT_DIR:
665
-		    ft = ft_directory;
666
-		    break;
667
-		case DT_LNK:
668
-		    if (!(flags & FOLLOW_SYMLINK_MASK)) {
669
-			/* we don't follow symlinks, don't bother
663
+            switch(dent->d_type) {
664
+            case DT_DIR:
665
+                ft = ft_directory;
666
+                break;
667
+            case DT_LNK:
668
+                if(!(flags & FOLLOW_SYMLINK_MASK)) {
669
+                    /* we don't follow symlinks, don't bother
670 670
 			 * stating it */
671
-			errno = 0;
672
-			continue;
673
-		    }
674
-		    ft = ft_link;
675
-		    break;
676
-		case DT_REG:
677
-		    ft = ft_regular;
678
-		    break;
679
-		case DT_UNKNOWN:
680
-		    ft = ft_unknown;
681
-		    break;
682
-		default:
683
-		    ft = ft_skipped_special;
684
-		    break;
685
-	    }
671
+                    errno = 0;
672
+                    continue;
673
+                }
674
+                ft = ft_link;
675
+                break;
676
+            case DT_REG:
677
+                ft = ft_regular;
678
+                break;
679
+            case DT_UNKNOWN:
680
+                ft = ft_unknown;
681
+                break;
682
+            default:
683
+                ft = ft_skipped_special;
684
+                break;
685
+            }
686 686
 #else
687
-	    ft = ft_unknown;
687
+            ft = ft_unknown;
688 688
 #endif
689
-	    fname = (char *) cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
690
-	    if(!fname) {
691
-		ret = callback(NULL, NULL, dirname, error_mem, data);
692
-		if (ret != CL_SUCCESS)
693
-		    break;
694
-		continue; /* have to skip this one if continuing after error */
695
-	    }
689
+            fname = (char*)cli_malloc(strlen(dirname) + strlen(dent->d_name) + 2);
690
+            if(!fname) {
691
+                ret = callback(NULL, NULL, dirname, error_mem, data);
692
+                if(ret != CL_SUCCESS)
693
+                    break;
694
+                continue; /* have to skip this one if continuing after error */
695
+            }
696 696
             if(!strcmp(dirname, PATHSEP))
697
-		sprintf(fname, PATHSEP"%s", dent->d_name);
698
-	    else
699
-		sprintf(fname, "%s"PATHSEP"%s", dirname, dent->d_name);
700
-
701
-	    if(pathchk && pathchk(fname, data) == 1) {
702
-		free(fname);
703
-		continue;
704
-	    }
705
-
706
-	    ret = handle_filetype(fname, flags, &statbuf, &stated, &ft, callback, data);
707
-	    if (ret != CL_SUCCESS) {
708
-		free(fname);
709
-		break;
710
-	    }
711
-
712
-	    if (ft_skipped(ft)) { /* skip */
713
-		free(fname);
714
-		errno = 0;
715
-		continue;
716
-	    }
717
-
718
-	    if (stated && (flags & CLI_FTW_NEED_STAT)) {
719
-		statbufp = cli_malloc(sizeof(*statbufp));
720
-		if (!statbufp) {
721
-		    ret = callback(stated ? &statbuf : NULL, NULL, fname, error_mem, data);
722
-		    free(fname);
723
-		    if (ret != CL_SUCCESS)
724
-			break;
725
-		    else {
726
-			errno = 0;
727
-			continue;
728
-		    }
729
-		}
730
-		memcpy(statbufp, &statbuf, sizeof(statbuf));
731
-	    } else {
732
-		statbufp = 0;
733
-	    }
734
-
735
-	    entries_cnt++;
736
-	    entries = cli_realloc(entries, entries_cnt*sizeof(*entries));
737
-	    if (!entries) {
738
-		ret = callback(stated ? &statbuf : NULL, NULL, fname, error_mem, data);
739
-		free(fname);
740
-		if (statbufp)
741
-		    free(statbufp);
742
-		break;
743
-	    } else {
744
-		struct dirent_data *entry = &entries[entries_cnt-1];
745
-		entry->filename = fname;
746
-		entry->statbuf = statbufp;
747
-		entry->is_dir = ft == ft_directory;
748
-		entry->dirname = entry->is_dir ? fname : NULL;
697
+                sprintf(fname, PATHSEP "%s", dent->d_name);
698
+            else
699
+                sprintf(fname, "%s" PATHSEP "%s", dirname, dent->d_name);
700
+
701
+            if(pathchk && pathchk(fname, data) == 1) {
702
+                free(fname);
703
+                continue;
704
+            }
705
+
706
+            ret = handle_filetype(fname, flags, &statbuf, &stated, &ft, callback, data);
707
+            if(ret != CL_SUCCESS) {
708
+                free(fname);
709
+                break;
710
+            }
711
+
712
+            if(ft_skipped(ft)) { /* skip */
713
+                free(fname);
714
+                errno = 0;
715
+                continue;
716
+            }
717
+
718
+            if(stated && (flags & CLI_FTW_NEED_STAT)) {
719
+                statbufp = cli_malloc(sizeof(*statbufp));
720
+                if(!statbufp) {
721
+                    ret = callback(stated ? &statbuf : NULL, NULL, fname, error_mem, data);
722
+                    free(fname);
723
+                    if(ret != CL_SUCCESS)
724
+                        break;
725
+                    else {
726
+                        errno = 0;
727
+                        continue;
728
+                    }
729
+                }
730
+                memcpy(statbufp, &statbuf, sizeof(statbuf));
731
+            } else {
732
+                statbufp = 0;
733
+            }
734
+
735
+            entries_cnt++;
736
+            entries = cli_realloc(entries, entries_cnt * sizeof(*entries));
737
+            if(!entries) {
738
+                ret = callback(stated ? &statbuf : NULL, NULL, fname, error_mem, data);
739
+                free(fname);
740
+                if(statbufp)
741
+                    free(statbufp);
742
+                break;
743
+            } else {
744
+                struct dirent_data* entry = &entries[entries_cnt - 1];
745
+                entry->filename           = fname;
746
+                entry->statbuf            = statbufp;
747
+                entry->is_dir             = ft == ft_directory;
748
+                entry->dirname            = entry->is_dir ? fname : NULL;
749 749
 #ifdef _XOPEN_UNIX
750
-		entry->ino = dent->d_ino;
750
+                entry->ino = dent->d_ino;
751 751
 #else
752
-		entry->ino = -1;
752
+                entry->ino = -1;
753 753
 #endif
754
-	    }
755
-	    errno = 0;
756
-	}
754
+            }
755
+            errno = 0;
756
+        }
757 757
 #ifndef HAVE_READDIR_R_3
758
-	err = errno;
758
+        err = errno;
759 759
 #endif
760
-	closedir(dd);
761
-	ret = CL_SUCCESS;
762
-	if (err) {
763
-	    char errs[128];
764
-	    cli_errmsg("Unable to readdir() directory %s: %s\n", dirname,
765
-		       cli_strerror(errno, errs, sizeof(errs)));
766
-	    /* report error to callback using error_stat */
767
-	    ret = callback(NULL, NULL, dirname, error_stat, data);
768
-	    if (ret != CL_SUCCESS) {
769
-		if (entries) {
770
-		    for (i=0;i<entries_cnt;i++) {
771
-			struct dirent_data *entry = &entries[i];
772
-			free(entry->filename);
773
-			free(entry->statbuf);
774
-		    }
775
-		    free(entries);
776
-		}
777
-		return ret;
778
-	    }
779
-	}
780
-
781
-	if (entries) {
782
-	    cli_qsort(entries, entries_cnt, sizeof(*entries), ftw_compare);
783
-	    for (i = 0; i < entries_cnt; i++) {
784
-		struct dirent_data *entry = &entries[i];
785
-		ret = handle_entry(entry, flags, maxdepth-1, callback, data, pathchk);
786
-		if (entry->is_dir)
787
-		    free(entry->filename);
788
-		if (entry->statbuf)
789
-		    free(entry->statbuf);
790
-		if (ret != CL_SUCCESS)
791
-		    break;
792
-	    }
793
-	    for (i++;i<entries_cnt;i++) {
794
-		struct dirent_data *entry = &entries[i];
795
-		free(entry->filename);
796
-		free(entry->statbuf);
797
-	    }
798
-	    free(entries);
799
-	}
760
+        closedir(dd);
761
+        ret = CL_SUCCESS;
762
+        if(err) {
763
+            char errs[128];
764
+            cli_errmsg("Unable to readdir() directory %s: %s\n", dirname,
765
+                       cli_strerror(errno, errs, sizeof(errs)));
766
+            /* report error to callback using error_stat */
767
+            ret = callback(NULL, NULL, dirname, error_stat, data);
768
+            if(ret != CL_SUCCESS) {
769
+                if(entries) {
770
+                    for(i = 0; i < entries_cnt; i++) {
771
+                        struct dirent_data* entry = &entries[i];
772
+                        free(entry->filename);
773
+                        free(entry->statbuf);
774
+                    }
775
+                    free(entries);
776
+                }
777
+                return ret;
778
+            }
779
+        }
780
+
781
+        if(entries) {
782
+            cli_qsort(entries, entries_cnt, sizeof(*entries), ftw_compare);
783
+            for(i = 0; i < entries_cnt; i++) {
784
+                struct dirent_data* entry = &entries[i];
785
+                ret                       = handle_entry(entry, flags, maxdepth - 1, callback, data, pathchk);
786
+                if(entry->is_dir)
787
+                    free(entry->filename);
788
+                if(entry->statbuf)
789
+                    free(entry->statbuf);
790
+                if(ret != CL_SUCCESS)
791
+                    break;
792
+            }
793
+            for(i++; i < entries_cnt; i++) {
794
+                struct dirent_data* entry = &entries[i];
795
+                free(entry->filename);
796
+                free(entry->statbuf);
797
+            }
798
+            free(entries);
799
+        }
800 800
     } else {
801
-	ret = callback(NULL, NULL, dirname, error_stat, data);
801
+        ret = callback(NULL, NULL, dirname, error_stat, data);
802 802
     }
803 803
     return ret;
804 804
 }
... ...
@@ -806,39 +799,39 @@ static int cli_ftw_dir(const char *dirname, int flags, int maxdepth, cli_ftw_cb
806 806
 /* strerror_r is not available everywhere, (and when it is there are two variants,
807 807
  * the XSI, and the GNU one, so provide a wrapper to make sure correct one is
808 808
  * used */
809
-const char* cli_strerror(int errnum, char *buf, size_t len)
809
+const char* cli_strerror(int errnum, char* buf, size_t len)
810 810
 {
811
-    char *err;
812
-# ifdef CL_THREAD_SAFE
811
+    char* err;
812
+#ifdef CL_THREAD_SAFE
813 813
     pthread_mutex_lock(&cli_strerror_mutex);
814 814
 #endif
815 815
     err = strerror(errnum);
816 816
     strncpy(buf, err, len);
817
-    buf[len-1] = '\0'; /* just in case */
818
-# ifdef CL_THREAD_SAFE
817
+    buf[len - 1] = '\0'; /* just in case */
818
+#ifdef CL_THREAD_SAFE
819 819
     pthread_mutex_unlock(&cli_strerror_mutex);
820 820
 #endif
821 821
     return buf;
822 822
 }
823 823
 
824
-static char *cli_md5buff(const unsigned char *buffer, unsigned int len, unsigned char *dig)
824
+static char* cli_md5buff(const unsigned char* buffer, unsigned int len, unsigned char* dig)
825 825
 {
826
-	unsigned char digest[16];
827
-	char *md5str, *pt;
828
-	int i;
826
+    unsigned char digest[16];
827
+    char *md5str, *pt;
828
+    int i;
829 829
 
830 830
     cl_hash_data("md5", buffer, len, digest, NULL);
831 831
 
832 832
     if(dig)
833
-	memcpy(dig, digest, 16);
833
+        memcpy(dig, digest, 16);
834 834
 
835
-    if(!(md5str = (char *) cli_calloc(32 + 1, sizeof(char))))
836
-	return NULL;
835
+    if(!(md5str = (char*)cli_calloc(32 + 1, sizeof(char))))
836
+        return NULL;
837 837
 
838 838
     pt = md5str;
839 839
     for(i = 0; i < 16; i++) {
840
-	sprintf(pt, "%02x", digest[i]);
841
-	pt += 2;
840
+        sprintf(pt, "%02x", digest[i]);
841
+        pt += 2;
842 842
     }
843 843
 
844 844
     return md5str;
... ...
@@ -847,30 +840,148 @@ static char *cli_md5buff(const unsigned char *buffer, unsigned int len, unsigned
847 847
 unsigned int cli_rndnum(unsigned int max)
848 848
 {
849 849
     if(name_salt[0] == 16) { /* minimizes re-seeding after the first call to cli_gentemp() */
850
-	    struct timeval tv;
851
-	gettimeofday(&tv, (struct timezone *) 0);
852
-	srand(tv.tv_usec+clock()+rand());
850
+        struct timeval tv;
851
+        gettimeofday(&tv, (struct timezone*)0);
852
+        srand(tv.tv_usec + clock() + rand());
853 853
     }
854 854
 
855
-    return 1 + (unsigned int) (max * (rand() / (1.0 + RAND_MAX)));
855
+    return 1 + (unsigned int)(max * (rand() / (1.0 + RAND_MAX)));
856 856
 }
857 857
 
858
-char* cli_genfname(const char * prefix)
858
+char* cli_sanitize_filepath(const char* filepath, size_t filepath_len)
859 859
 {
860
-	char* fname;
860
+    uint32_t depth           = 0;
861
+    size_t index             = 0;
862
+    size_t sanitized_index   = 0;
863
+    char* sanitized_filepath = NULL;
864
+
865
+    if((NULL == filepath) || (0 == filepath_len) || (MAX_PATH < filepath_len)) {
866
+        goto done;
867
+    }
868
+
869
+    sanitized_filepath = cli_calloc(filepath_len + 1, sizeof(unsigned char));
870
+    if(NULL == sanitized_filepath) {
871
+        cli_dbgmsg("cli_sanitize_filepath: out of memory\n");
872
+        goto done;
873
+    }
874
+
875
+    while(index < filepath_len) {
876
+        char* next_pathsep = NULL;
877
+
878
+        if(0 == strncmp(filepath + index, PATHSEP, strlen(PATHSEP))) {
879
+            /*
880
+             * Is "/" (or "\\" on Windows)
881
+             */
882
+            /* Skip leading pathsep in absolute path, or extra pathsep) */
883
+            index += strlen(PATHSEP);
884
+            continue;
885
+        } else if(0 == strncmp(filepath + index, "." PATHSEP, strlen("." PATHSEP))) {
886
+            /*
887
+             * Is "./" (or ".\\" on Windows)
888
+             */
889
+            /* Current directory indicator is meaningless and should not add to the depth. Skip it. */
890
+            index += strlen("." PATHSEP);
891
+            continue;
892
+        } else if(0 == strncmp(filepath + index, ".." PATHSEP, strlen(".." PATHSEP))) {
893
+            /*
894
+             * Is "../" (or "..\\" on Windows)
895
+             */
896
+            if(depth == 0) {
897
+                /* Relative path would traverse parent directory. Skip it. */
898
+                index += strlen(".." PATHSEP);
899
+                continue;
900
+            } else {
901
+                /* Relative path is safe. Allow it. */
902
+                strncpy(sanitized_filepath + sanitized_index, filepath + index, strlen(".." PATHSEP));
903
+                sanitized_index += strlen(".." PATHSEP);
904
+                index += strlen(".." PATHSEP);
905
+                depth--;
906
+            }
907
+        }
908
+#ifdef _WIN32
909
+        /*
910
+         * Windows' POSIX style API's accept both "/" and "\\" style path separators.
911
+         * The following checks using POSIX style path separators on Windows.
912
+         */
913
+        else if(0 == strncmp(filepath + index, "/", strlen("/"))) {
914
+            /*
915
+             * Is "/".
916
+             */
917
+            /* Skip leading pathsep in absolute path, or extra pathsep) */
918
+            index += strlen("/");
919
+            continue;
920
+        } else if(0 == strncmp(filepath + index, "./", strlen("./"))) {
921
+            /*
922
+             * Is "./"
923
+             */
924
+            /* Current directory indicator is meaningless and should not add to the depth. Skip it. */
925
+            index += strlen("./");
926
+            continue;
927
+        } else if(0 == strncmp(filepath + index, "../", strlen("../"))) {
928
+            /*
929
+             * Is "../"
930
+             */
931
+            if(depth == 0) {
932
+                /* Relative path would traverse parent directory. Skip it. */
933
+                index += strlen("../");
934
+                continue;
935
+            } else {
936
+                /* Relative path is safe. Allow it. */
937
+                strncpy(sanitized_filepath + sanitized_index, filepath + index, strlen("../"));
938
+                sanitized_index += strlen("../");
939
+                index += strlen("../");
940
+                depth--;
941
+            }
942
+        }
943
+#endif
944
+        else {
945
+            /*
946
+             * Is not "/", "./", or "../".
947
+             */
948
+            /* Find the next path separator. */
949
+            next_pathsep = cli_strnstr(filepath + index, PATHSEP, filepath_len - index);
950
+            if(NULL == next_pathsep) {
951
+                /* No more path separators, copy the rest (filename) into the sanitized path */
952
+                strncpy(sanitized_filepath + sanitized_index, filepath + index, filepath_len - index);
953
+                break;
954
+            }
955
+            next_pathsep += strlen(PATHSEP); /* Include the path separator in the copy */
956
+
957
+            /* Copy next directory name into the sanitized path */
958
+            strncpy(sanitized_filepath + sanitized_index, filepath + index, next_pathsep - (filepath + index));
959
+            sanitized_index += next_pathsep - (filepath + index);
960
+            index += next_pathsep - (filepath + index);
961
+            depth++;
962
+        }
963
+    }
964
+
965
+done:
966
+    if((NULL != sanitized_filepath) && (0 == strlen(sanitized_filepath))) {
967
+        free(sanitized_filepath);
968
+        sanitized_filepath = NULL;
969
+    }
970
+
971
+    return sanitized_filepath;
972
+}
973
+
974
+char* cli_genfname(const char* prefix)
975
+{
976
+    char* sanitized_prefix = NULL;
977
+    char* fname            = NULL;
861 978
     unsigned char salt[16 + 32];
862 979
     char* tmp;
863 980
     int i;
864
-	size_t len;
981
+    size_t len;
865 982
 
866
-	if (prefix && (strlen(prefix) > 0)) {
867
-    	len = strlen(prefix) + 1 + 5 + 1;  /* {prefix}.{5}\0 */
868
-	} else {
869
-    	len = 6 + 1 + 48 + 4 + 1;  /* clamav-{48}.tmp\0 */
870
-	}
983
+    if(prefix && (strlen(prefix) > 0)) {
984
+        sanitized_prefix = cli_sanitize_filepath(prefix, strlen(prefix));
985
+        len              = strlen(sanitized_prefix) + 1 + 5 + 1; /* {prefix}.{5}\0 */
986
+    } else {
987
+        len = 6 + 1 + 48 + 4 + 1; /* clamav-{48}.tmp\0 */
988
+    }
871 989
 
872 990
     fname = (char*)cli_calloc(len, sizeof(char));
873
-    if (!fname) {
991
+    if(!fname) {
874 992
         cli_dbgmsg("cli_genfname: out of memory\n");
875 993
         return NULL;
876 994
     }
... ...
@@ -881,7 +992,7 @@ char* cli_genfname(const char * prefix)
881 881
 
882 882
     memcpy(salt, name_salt, 16);
883 883
 
884
-    for (i = 16; i < 48; i++)
884
+    for(i = 16; i < 48; i++)
885 885
         salt[i] = cli_rndnum(255);
886 886
 
887 887
     tmp = cli_md5buff(salt, 48, name_salt);
... ...
@@ -890,18 +1001,19 @@ char* cli_genfname(const char * prefix)
890 890
     pthread_mutex_unlock(&cli_gentemp_mutex);
891 891
 #endif
892 892
 
893
-    if (!tmp) {
893
+    if(!tmp) {
894 894
         free(fname);
895 895
         cli_dbgmsg("cli_genfname: out of memory\n");
896 896
         return NULL;
897 897
     }
898 898
 
899
-	if (prefix && (strlen(prefix) > 0)) {
900
-		fname[5] = '\0';
901
-    	snprintf(fname, len, "%s.%s", prefix, tmp);
902
-	} else {
903
-    	snprintf(fname, len, "clamav-%s.tmp", tmp);
904
-	}
899
+    if(sanitized_prefix && (strlen(sanitized_prefix) > 0)) {
900
+        fname[5] = '\0';
901
+        snprintf(fname, len, "%s.%s", sanitized_prefix, tmp);
902
+        free(sanitized_prefix);
903
+    } else {
904
+        snprintf(fname, len, "clamav-%s.tmp", tmp);
905
+    }
905 906
 
906 907
     free(tmp);
907 908
 
... ...
@@ -910,7 +1022,7 @@ char* cli_genfname(const char * prefix)
910 910
 
911 911
 char* cli_gentemp_with_prefix(const char* dir, const char* prefix)
912 912
 {
913
-	char* fname;
913
+    char* fname;
914 914
     char* fullpath;
915 915
     const char* mdir;
916 916
     int i;
... ...
@@ -918,32 +1030,32 @@ char* cli_gentemp_with_prefix(const char* dir, const char* prefix)
918 918
 
919 919
     mdir = dir ? dir : cli_gettmpdir();
920 920
 
921
-	fname = cli_genfname(prefix);
922
-    if (!fname) {
921
+    fname = cli_genfname(prefix);
922
+    if(!fname) {
923 923
         cli_dbgmsg("cli_gentemp('%s'): out of memory\n", mdir);
924 924
         return NULL;
925 925
     }
926 926
 
927
-    len = strlen(mdir) + strlen(PATHSEP) + strlen(fname) + 1; /* mdir/fname\0 */
927
+    len      = strlen(mdir) + strlen(PATHSEP) + strlen(fname) + 1; /* mdir/fname\0 */
928 928
     fullpath = (char*)cli_calloc(len, sizeof(char));
929
-    if (!fullpath) {
929
+    if(!fullpath) {
930 930
         free(fname);
931 931
         cli_dbgmsg("cli_gentemp('%s'): out of memory\n", mdir);
932 932
         return NULL;
933 933
     }
934 934
 
935 935
     snprintf(fullpath, len, "%s" PATHSEP "%s", mdir, fname);
936
-	free(fname);
936
+    free(fname);
937 937
 
938 938
     return (fullpath);
939 939
 }
940 940
 
941 941
 char* cli_gentemp(const char* dir)
942 942
 {
943
-	return cli_gentemp_with_prefix(dir, NULL);
943
+    return cli_gentemp_with_prefix(dir, NULL);
944 944
 }
945 945
 
946
-cl_error_t cli_gentempfd(const char *dir, char **name, int *fd)
946
+cl_error_t cli_gentempfd(const char* dir, char** name, int* fd)
947 947
 {
948 948
     return cli_gentempfd_with_prefix(dir, NULL, name, fd);
949 949
 }
... ...
@@ -951,7 +1063,7 @@ cl_error_t cli_gentempfd(const char *dir, char **name, int *fd)
951 951
 cl_error_t cli_gentempfd_with_prefix(const char* dir, char* prefix, char** name, int* fd)
952 952
 {
953 953
     *name = cli_gentemp_with_prefix(dir, prefix);
954
-    if (!*name)
954
+    if(!*name)
955 955
         return CL_EMEM;
956 956
 
957 957
     *fd = open(*name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, S_IRWXU);
... ...
@@ -959,15 +1071,15 @@ cl_error_t cli_gentempfd_with_prefix(const char* dir, char* prefix, char** name,
959 959
      * EEXIST is almost impossible to occur, so we just treat it as other
960 960
      * errors
961 961
      */
962
-    if (*fd == -1) {
963
-        if ((EILSEQ == errno) || (EINVAL == errno) || (ENAMETOOLONG == errno)) {
962
+    if(*fd == -1) {
963
+        if((EILSEQ == errno) || (EINVAL == errno) || (ENAMETOOLONG == errno)) {
964 964
             cli_dbgmsg("cli_gentempfd_with_prefix: Can't create temp file using prefix. Using a randomly generated name instead.\n");
965 965
             free(*name);
966 966
             *name = cli_gentemp(dir);
967
-            if (!*name)
967
+            if(!*name)
968 968
                 return CL_EMEM;
969 969
             *fd = open(*name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, S_IRWXU);
970
-            if (*fd == -1) {
970
+            if(*fd == -1) {
971 971
                 cli_errmsg("cli_gentempfd_with_prefix: Can't create temporary file %s: %s\n", *name, strerror(errno));
972 972
                 free(*name);
973 973
                 *name = NULL;
... ...
@@ -984,11 +1096,11 @@ cl_error_t cli_gentempfd_with_prefix(const char* dir, char* prefix, char** name,
984 984
     return CL_SUCCESS;
985 985
 }
986 986
 
987
-int cli_regcomp(regex_t *preg, const char *pattern, int cflags)
987
+int cli_regcomp(regex_t* preg, const char* pattern, int cflags)
988 988
 {
989
-    if (!strncmp(pattern, "(?i)", 4)) {
990
-	pattern += 4;
991
-	cflags |= REG_ICASE;
989
+    if(!strncmp(pattern, "(?i)", 4)) {
990
+        pattern += 4;
991
+        cflags |= REG_ICASE;
992 992
     }
993 993
     return cli_regcomp_real(preg, pattern, cflags);
994 994
 }
... ...
@@ -997,7 +1109,7 @@ cl_error_t cli_get_filepath_from_filedesc(int desc, char** filepath)
997 997
 {
998 998
     cl_error_t status = CL_EARG;
999 999
 
1000
-    if (NULL == filepath) {
1000
+    if(NULL == filepath) {
1001 1001
         cli_errmsg("cli_get_filepath_from_filedesc: Invalid args.\n");
1002 1002
         goto done;
1003 1003
     }
... ...
@@ -1013,77 +1125,77 @@ cl_error_t cli_get_filepath_from_filedesc(int desc, char** filepath)
1013 1013
     snprintf(link, sizeof(link), "/proc/self/fd/%u", desc);
1014 1014
     link[sizeof(link) - 1] = '\0';
1015 1015
 
1016
-    if (-1 == (linksz = readlink(link, fname, PATH_MAX - 1))) {
1016
+    if(-1 == (linksz = readlink(link, fname, PATH_MAX - 1))) {
1017 1017
         cli_errmsg("cli_get_filepath_from_filedesc: Failed to resolve filename for descriptor %d (%s)\n", desc, link);
1018 1018
         status = CL_EOPEN;
1019 1019
         goto done;
1020 1020
     }
1021 1021
 
1022
-	/* Success. Add null terminator */
1023
-	fname[linksz] = '\0';
1022
+    /* Success. Add null terminator */
1023
+    fname[linksz] = '\0';
1024 1024
 
1025 1025
     *filepath = cli_strndup(fname, cli_strnlen(fname, PATH_MAX));
1026
-	if (NULL == *filepath) {
1027
-		cli_errmsg("cli_get_filepath_from_filedesc: Failed to allocate memory to store filename\n");
1028
-		status = CL_EMEM;
1029
-		goto done;
1030
-	}
1026
+    if(NULL == *filepath) {
1027
+        cli_errmsg("cli_get_filepath_from_filedesc: Failed to allocate memory to store filename\n");
1028
+        status = CL_EMEM;
1029
+        goto done;
1030
+    }
1031 1031
 
1032 1032
 #elif __APPLE__
1033 1033
     char fname[PATH_MAX];
1034 1034
     memset(&fname, 0, PATH_MAX);
1035 1035
 
1036
-    if (fcntl(desc, F_GETPATH, &fname) < 0) {
1036
+    if(fcntl(desc, F_GETPATH, &fname) < 0) {
1037 1037
         printf("cli_get_filepath_from_filedesc: Failed to resolve filename for descriptor %d\n", desc);
1038 1038
         status = CL_EOPEN;
1039 1039
         goto done;
1040 1040
     }
1041 1041
 
1042 1042
     *filepath = cli_strndup(fname, cli_strnlen(fname, PATH_MAX));
1043
-	if (NULL == *filepath) {
1044
-		cli_errmsg("cli_get_filepath_from_filedesc: Failed to allocate memory to store filename\n");
1045
-		status = CL_EMEM;
1046
-		goto done;
1047
-	}
1043
+    if(NULL == *filepath) {
1044
+        cli_errmsg("cli_get_filepath_from_filedesc: Failed to allocate memory to store filename\n");
1045
+        status = CL_EMEM;
1046
+        goto done;
1047
+    }
1048 1048
 
1049 1049
 #elif _WIN32
1050 1050
     DWORD dwRet = 0;
1051 1051
     intptr_t hFile = _get_osfhandle(desc);
1052 1052
 
1053 1053
     dwRet = GetFinalPathNameByHandleA((HANDLE)hFile, NULL, 0, VOLUME_NAME_NT);
1054
-    if (dwRet == 0) {
1054
+    if(dwRet == 0) {
1055 1055
         cli_errmsg("cli_get_filepath_from_filedesc: Failed to resolve filename for descriptor %d\n", desc);
1056
-		status = CL_EOPEN;
1057
-		goto done;
1056
+        status = CL_EOPEN;
1057
+        goto done;
1058 1058
     }
1059 1059
 
1060
-	*filepath = calloc(dwRet + 1, 1);
1061
-	if (NULL == *filepath) {
1062
-		cli_errmsg("cli_get_filepath_from_filedesc: Failed to allocate %u bytes to store filename\n", dwRet + 1);
1063
-		status = CL_EMEM;
1064
-		goto done;
1065
-	}
1066
-
1067
-	dwRet = GetFinalPathNameByHandleA((HANDLE)hFile, *filepath, dwRet + 1, VOLUME_NAME_NT);
1068
-	if (dwRet == 0) {
1069
-		cli_errmsg("cli_get_filepath_from_filedesc: Failed to resolve filename for descriptor %d\n", desc);
1070
-		free(*filepath);
1071
-		*filepath = NULL;
1072
-		status = CL_EOPEN;
1073
-		goto done;
1074
-	}
1060
+    *filepath = calloc(dwRet + 1, 1);
1061
+    if(NULL == *filepath) {
1062
+        cli_errmsg("cli_get_filepath_from_filedesc: Failed to allocate %u bytes to store filename\n", dwRet + 1);
1063
+        status = CL_EMEM;
1064
+        goto done;
1065
+    }
1066
+
1067
+    dwRet = GetFinalPathNameByHandleA((HANDLE)hFile, *filepath, dwRet + 1, VOLUME_NAME_NT);
1068
+    if(dwRet == 0) {
1069
+        cli_errmsg("cli_get_filepath_from_filedesc: Failed to resolve filename for descriptor %d\n", desc);
1070
+        free(*filepath);
1071
+        *filepath = NULL;
1072
+        status = CL_EOPEN;
1073
+        goto done;
1074
+    }
1075 1075
 
1076 1076
 #else
1077 1077
 
1078
-	cli_dbgmsg("cli_get_filepath_from_filedesc: No mechanism implemented to determine filename from file descriptor.\n");
1079
-	*filepath = NULL;
1080
-	status = CL_BREAK;
1081
-	goto done;
1078
+    cli_dbgmsg("cli_get_filepath_from_filedesc: No mechanism implemented to determine filename from file descriptor.\n");
1079
+    *filepath = NULL;
1080
+    status    = CL_BREAK;
1081
+    goto done;
1082 1082
 
1083 1083
 #endif
1084 1084
 
1085
-	cli_dbgmsg("cli_get_filepath_from_filedesc: File path for fd [%d] is: %s\n", desc, *filepath);
1086
-	status = CL_SUCCESS;
1085
+    cli_dbgmsg("cli_get_filepath_from_filedesc: File path for fd [%d] is: %s\n", desc, *filepath);
1086
+    status = CL_SUCCESS;
1087 1087
 
1088 1088
 done:
1089 1089
 
... ...
@@ -449,9 +449,9 @@ static cl_error_t cli_scanrar(const char *filepath, int desc, cli_ctx* ctx)
449 449
                 }
450 450
             } else {
451 451
                 /*
452
-                * Extract the file...
453
-                */
454
-                extract_fullpath = cli_gentemp_with_prefix(extract_dir, metadata.filename);
452
+                 * Extract the file...
453
+                 */
454
+                extract_fullpath = cli_gentemp(extract_dir);
455 455
                 if (NULL == extract_fullpath) {
456 456
                     cli_dbgmsg("RAR: Memory error allocating filename for extracted file.");
457 457
                     status = CL_EMEM;
... ...
@@ -496,6 +496,48 @@ char *cli_strndup(const char *s, size_t n)
496 496
 }
497 497
 #endif
498 498
 
499
+#if !defined(HAVE_STRNSTR) || defined(HAVE_STRNI)
500
+/*
501
+ * @brief Find the first occurrence of find in s.
502
+ *
503
+ * The search is limited to the first slen characters of s.
504
+ *
505
+ * Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org>
506
+ * Copyright (c) 1990, 1993
507
+ * The Regents of the University of California.  All rights reserved.
508
+ *
509
+ * This code is derived from software contributed to Berkeley by
510
+ * Chris Torek.
511
+ *
512
+ * Copyright (c) 1990 The Regents of the University of California.
513
+ * All rights reserved.
514
+ *
515
+ * @param s      haystack
516
+ * @param find   needle
517
+ * @param slen   haystack length
518
+ * @return char* Address of the needle, if found, else NULL.
519
+ */
520
+char *cli_strnstr(const char *s, const char *find, size_t slen)
521
+{
522
+    char c, sc;
523
+    size_t len;
524
+
525
+    if ((c = *find++) != '\0') {
526
+        len = strlen(find);
527
+        do {
528
+            do {
529
+                if (slen-- < 1 || (sc = *s++) == '\0')
530
+                    return (NULL);
531
+            } while (sc != c);
532
+            if (len > slen)
533
+                return (NULL);
534
+        } while (strncmp(s, find, len) != 0);
535
+        s--;
536
+    }
537
+    return ((char *)s);
538
+}
539
+#endif
540
+
499 541
 size_t cli_strtokenize(char *buffer, const char delim, const size_t token_count, const char **tokens)
500 542
 {
501 543
 	size_t tokens_found, i;
... ...
@@ -52,6 +52,12 @@ char *cli_strndup(const char *s, size_t n);
52 52
 size_t cli_strnlen(const char *s, size_t n);
53 53
 #endif
54 54
 
55
+#if defined(HAVE_STRNSTR) && !defined(HAVE_STRNI)
56
+#define cli_strnstr strnstr
57
+#else
58
+char *cli_strnstr(const char *s, const char *find, size_t slen);
59
+#endif
60
+
55 61
 #include <stdio.h>
56 62
 #define cli_nocase(val) tolower(val)
57 63
 #define cli_nocasei(val) toupper(val)
... ...
@@ -5,6 +5,7 @@ AC_SEARCH_LIBS([gethostent],[nsl], [(LIBS="$LIBS -lnsl"; CLAMAV_MILTER_LIBS="$CL
5 5
 AC_CHECK_FUNCS_ONCE([poll setsid memcpy snprintf vsnprintf strerror_r strlcpy strlcat strcasestr inet_ntop setgroups initgroups ctime_r mkstemp mallinfo madvise getnameinfo])
6 6
 AC_CHECK_FUNCS([strndup])
7 7
 AC_CHECK_FUNCS([strnlen])
8
+AC_CHECK_FUNCS([strnstr])
8 9
 AC_FUNC_FSEEKO
9 10
 
10 11
 dnl Check if anon maps are available, check if we can determine the page size
... ...
@@ -23,15 +23,16 @@
23 23
 #include "../libclamav/version.h"
24 24
 #include "../libclamav/dsig.h"
25 25
 #include "../libclamav/fpu.h"
26
+#include "../platform.h"
26 27
 #include "checks.h"
27 28
 
28
-static int fpu_words  = FPU_ENDIAN_INITME;
29
+static int fpu_words = FPU_ENDIAN_INITME;
29 30
 #define NO_FPU_ENDIAN (fpu_words == FPU_ENDIAN_UNKNOWN)
30 31
 #define EA06_SCAN strstr(file, "clam.ea06.exe")
31 32
 #define FALSE_NEGATIVE (EA06_SCAN && NO_FPU_ENDIAN)
32 33
 
33 34
 /* extern void cl_free(struct cl_engine *engine); */
34
-START_TEST (test_cl_free)
35
+START_TEST(test_cl_free)
35 36
 /*
36 37
     struct cl_engine *engine = NULL;
37 38
     cl_free(NULL);
... ...
@@ -39,16 +40,16 @@ START_TEST (test_cl_free)
39 39
 END_TEST
40 40
 
41 41
 /* extern struct cl_engine *cl_dup(struct cl_engine *engine); */
42
-START_TEST (test_cl_dup)
43
-    /*
42
+START_TEST(test_cl_dup)
43
+/*
44 44
     struct cl_engine *engine;
45 45
     fail_unless(NULL == cl_dup(NULL), "cl_dup null pointer");
46 46
     */
47 47
 END_TEST
48 48
 
49 49
 /* extern int cl_build(struct cl_engine *engine); */
50
-START_TEST (test_cl_build)
51
-    /*
50
+START_TEST(test_cl_build)
51
+/*
52 52
     struct cl_engine *engine;
53 53
     fail_unless(CL_ENULLARG == cl_build(NULL), "cl_build null pointer");
54 54
     engine = calloc(sizeof(struct cl_engine),1);
... ...
@@ -59,7 +60,7 @@ START_TEST (test_cl_build)
59 59
 END_TEST
60 60
 
61 61
 /* extern void cl_debug(void); */
62
-START_TEST (test_cl_debug)
62
+START_TEST(test_cl_debug)
63 63
 {
64 64
     int old_status = cli_debug_flag;
65 65
     cli_debug_flag = 0;
... ...
@@ -74,8 +75,8 @@ START_TEST (test_cl_debug)
74 74
 END_TEST
75 75
 
76 76
 /* extern const char *cl_retdbdir(void); */
77
-START_TEST (test_cl_retdbdir)
78
-    fail_unless(!strcmp(DATADIR, cl_retdbdir()), "cl_retdbdir");
77
+START_TEST(test_cl_retdbdir)
78
+fail_unless(!strcmp(DATADIR, cl_retdbdir()), "cl_retdbdir");
79 79
 END_TEST
80 80
 
81 81
 #ifndef REPO_VERSION
... ...
@@ -83,17 +84,17 @@ END_TEST
83 83
 #endif
84 84
 
85 85
 /* extern const char *cl_retver(void); */
86
-START_TEST (test_cl_retver)
86
+START_TEST(test_cl_retver)
87 87
 {
88
-    const char *ver = cl_retver();
89
-    fail_unless(!strcmp(REPO_VERSION""VERSION_SUFFIX, ver),"cl_retver");
90
-    fail_unless(strcspn(ver,"012345789") < strlen(ver),
91
-		    "cl_retver must have a number");
88
+    const char* ver = cl_retver();
89
+    fail_unless(!strcmp(REPO_VERSION "" VERSION_SUFFIX, ver), "cl_retver");
90
+    fail_unless(strcspn(ver, "012345789") < strlen(ver),
91
+                "cl_retver must have a number");
92 92
 }
93 93
 END_TEST
94 94
 
95 95
 /* extern void cl_cvdfree(struct cl_cvd *cvd); */
96
-START_TEST (test_cl_cvdfree)
96
+START_TEST(test_cl_cvdfree)
97 97
 /*
98 98
     struct cl_cvd *cvd1, *cvd2;
99 99
 
... ...
@@ -117,15 +118,15 @@ START_TEST (test_cl_cvdfree)
117 117
 END_TEST
118 118
 
119 119
 /* extern int cl_statfree(struct cl_stat *dbstat); */
120
-START_TEST (test_cl_statfree)
120
+START_TEST(test_cl_statfree)
121 121
 /*
122 122
     struct cl_stat *stat;
123 123
     fail_unless(CL_ENULLARG == cl_statfree(NULL), "cl_statfree(NULL)");
124
-    
124
+
125 125
     stat = malloc(sizeof(struct cl_stat));
126 126
     fail_unless(NULL != stat, "malloc");
127 127
     fail_unless(CL_SUCCESS == cl_statfree(stat), "cl_statfree(empty_struct)");
128
-    
128
+
129 129
     stat = malloc(sizeof(struct cl_stat));
130 130
     fail_unless(NULL != stat, "malloc");
131 131
     stat->stattab = strdup("test");
... ...
@@ -140,30 +141,30 @@ START_TEST (test_cl_statfree)
140 140
 END_TEST
141 141
 
142 142
 /* extern unsigned int cl_retflevel(void); */
143
-START_TEST (test_cl_retflevel)
144
-END_TEST    
143
+START_TEST(test_cl_retflevel)
144
+END_TEST
145 145
 
146 146
 /* extern struct cl_cvd *cl_cvdhead(const char *file); */
147
-START_TEST (test_cl_cvdhead)
147
+START_TEST(test_cl_cvdhead)
148 148
 /*
149 149
     fail_unless(NULL == cl_cvdhead(NULL), "cl_cvdhead(null)");
150 150
     fail_unless(NULL == cl_cvdhead("input/cl_cvdhead/1.txt"), "cl_cvdhead(515 byte file, all nulls)");
151 151
 */
152
-    /* the data read from the file is passed to cl_cvdparse, test cases for that are separate */
152
+/* the data read from the file is passed to cl_cvdparse, test cases for that are separate */
153 153
 END_TEST
154 154
 
155 155
 /* extern struct cl_cvd *cl_cvdparse(const char *head); */
156
-START_TEST (test_cl_cvdparse)
156
+START_TEST(test_cl_cvdparse)
157 157
 END_TEST
158 158
 
159
-static int get_test_file(int i, char *file, unsigned fsize, unsigned long *size);
160
-static struct cl_engine *g_engine;
159
+static int get_test_file(int i, char* file, unsigned fsize, unsigned long* size);
160
+static struct cl_engine* g_engine;
161 161
 
162 162
 #ifdef CHECK_HAVE_LOOPS
163 163
 /* int cl_scandesc(int desc, const char **virname, unsigned long int *scanned, const struct cl_engine *engine, const struct cl_limits *limits, struct cl_scan_options* options) */
164
-START_TEST (test_cl_scandesc)
164
+START_TEST(test_cl_scandesc)
165 165
 {
166
-    const char *virname = NULL;
166
+    const char* virname = NULL;
167 167
     char file[256];
168 168
     unsigned long size;
169 169
     unsigned long int scanned = 0;
... ...
@@ -178,17 +179,17 @@ START_TEST (test_cl_scandesc)
178 178
     ret = cl_scandesc(fd, file, &virname, &scanned, g_engine, &options);
179 179
     cli_dbgmsg("scan end (scandesc) %s\n", file);
180 180
 
181
-    if (!FALSE_NEGATIVE) {
182
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scandesc failed for %s: %s", file, cl_strerror(ret));
183
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
181
+    if(!FALSE_NEGATIVE) {
182
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scandesc failed for %s: %s", file, cl_strerror(ret));
183
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
184 184
     }
185 185
     close(fd);
186 186
 }
187 187
 END_TEST
188 188
 
189
-START_TEST (test_cl_scandesc_allscan)
189
+START_TEST(test_cl_scandesc_allscan)
190 190
 {
191
-    const char *virname = NULL;
191
+    const char* virname = NULL;
192 192
     char file[256];
193 193
     unsigned long size;
194 194
     unsigned long int scanned = 0;
... ...
@@ -205,18 +206,18 @@ START_TEST (test_cl_scandesc_allscan)
205 205
 
206 206
     cli_dbgmsg("scan end (scandesc) %s\n", file);
207 207
 
208
-    if (!FALSE_NEGATIVE) {
209
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scandesc_allscan failed for %s: %s", file, cl_strerror(ret));
210
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
208
+    if(!FALSE_NEGATIVE) {
209
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scandesc_allscan failed for %s: %s", file, cl_strerror(ret));
210
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
211 211
     }
212 212
     close(fd);
213 213
 }
214 214
 END_TEST
215 215
 
216 216
 //* int cl_scanfile(const char *filename, const char **virname, unsigned long int *scanned, const struct cl_engine *engine, const struct cl_limits *limits, unsigned int options) */
217
-START_TEST (test_cl_scanfile)
217
+START_TEST(test_cl_scanfile)
218 218
 {
219
-    const char *virname = NULL;
219
+    const char* virname = NULL;
220 220
     char file[256];
221 221
     unsigned long size;
222 222
     unsigned long int scanned = 0;
... ...
@@ -233,16 +234,16 @@ START_TEST (test_cl_scanfile)
233 233
     ret = cl_scanfile(file, &virname, &scanned, g_engine, &options);
234 234
     cli_dbgmsg("scan end (scanfile) %s\n", file);
235 235
 
236
-    if (!FALSE_NEGATIVE) {
237
-      fail_unless_fmt(ret == CL_VIRUS , "cl_scanfile failed for %s: %s", file, cl_strerror(ret));
238
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
236
+    if(!FALSE_NEGATIVE) {
237
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile failed for %s: %s", file, cl_strerror(ret));
238
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
239 239
     }
240 240
 }
241 241
 END_TEST
242 242
 
243
-START_TEST (test_cl_scanfile_allscan)
243
+START_TEST(test_cl_scanfile_allscan)
244 244
 {
245
-    const char *virname = NULL;
245
+    const char* virname = NULL;
246 246
     char file[256];
247 247
     unsigned long size;
248 248
     unsigned long int scanned = 0;
... ...
@@ -260,16 +261,16 @@ START_TEST (test_cl_scanfile_allscan)
260 260
     ret = cl_scanfile(file, &virname, &scanned, g_engine, &options);
261 261
     cli_dbgmsg("scan end (scanfile_allscan) %s\n", file);
262 262
 
263
-    if (!FALSE_NEGATIVE) {
264
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_allscan failed for %s: %s", file, cl_strerror(ret));
265
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
263
+    if(!FALSE_NEGATIVE) {
264
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_allscan failed for %s: %s", file, cl_strerror(ret));
265
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
266 266
     }
267 267
 }
268 268
 END_TEST
269 269
 
270
-START_TEST (test_cl_scanfile_callback)
270
+START_TEST(test_cl_scanfile_callback)
271 271
 {
272
-    const char *virname = NULL;
272
+    const char* virname = NULL;
273 273
     char file[256];
274 274
     unsigned long size;
275 275
     unsigned long int scanned = 0;
... ...
@@ -287,16 +288,16 @@ START_TEST (test_cl_scanfile_callback)
287 287
     ret = cl_scanfile_callback(file, &virname, &scanned, g_engine, &options, NULL);
288 288
     cli_dbgmsg("scan end (scanfile_cb) %s\n", file);
289 289
 
290
-    if (!FALSE_NEGATIVE) {
291
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_cb failed for %s: %s", file, cl_strerror(ret));
292
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
290
+    if(!FALSE_NEGATIVE) {
291
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_cb failed for %s: %s", file, cl_strerror(ret));
292
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
293 293
     }
294 294
 }
295 295
 END_TEST
296 296
 
297
-START_TEST (test_cl_scanfile_callback_allscan)
297
+START_TEST(test_cl_scanfile_callback_allscan)
298 298
 {
299
-    const char *virname = NULL;
299
+    const char* virname = NULL;
300 300
     char file[256];
301 301
     unsigned long size;
302 302
     unsigned long int scanned = 0;
... ...
@@ -315,16 +316,16 @@ START_TEST (test_cl_scanfile_callback_allscan)
315 315
     ret = cl_scanfile_callback(file, &virname, &scanned, g_engine, &options, NULL);
316 316
     cli_dbgmsg("scan end (scanfile_cb_allscan) %s\n", file);
317 317
 
318
-    if (!FALSE_NEGATIVE) {
319
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_cb_allscan failed for %s: %s", file, cl_strerror(ret));
320
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
318
+    if(!FALSE_NEGATIVE) {
319
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_cb_allscan failed for %s: %s", file, cl_strerror(ret));
320
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
321 321
     }
322 322
 }
323 323
 END_TEST
324 324
 
325
-START_TEST (test_cl_scandesc_callback)
325
+START_TEST(test_cl_scandesc_callback)
326 326
 {
327
-    const char *virname = NULL;
327
+    const char* virname = NULL;
328 328
     char file[256];
329 329
     unsigned long size;
330 330
     unsigned long int scanned = 0;
... ...
@@ -341,17 +342,17 @@ START_TEST (test_cl_scandesc_callback)
341 341
     ret = cl_scandesc_callback(fd, file, &virname, &scanned, g_engine, &options, NULL);
342 342
     cli_dbgmsg("scan end (scandesc_cb) %s\n", file);
343 343
 
344
-    if (!FALSE_NEGATIVE) {
345
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile failed for %s: %s", file, cl_strerror(ret));
346
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
344
+    if(!FALSE_NEGATIVE) {
345
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile failed for %s: %s", file, cl_strerror(ret));
346
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
347 347
     }
348 348
     close(fd);
349 349
 }
350 350
 END_TEST
351 351
 
352
-START_TEST (test_cl_scandesc_callback_allscan)
352
+START_TEST(test_cl_scandesc_callback_allscan)
353 353
 {
354
-    const char *virname = NULL;
354
+    const char* virname = NULL;
355 355
     char file[256];
356 356
     unsigned long size;
357 357
     unsigned long int scanned = 0;
... ...
@@ -369,9 +370,9 @@ START_TEST (test_cl_scandesc_callback_allscan)
369 369
     ret = cl_scandesc_callback(fd, file, &virname, &scanned, g_engine, &options, NULL);
370 370
     cli_dbgmsg("scan end (scandesc_cb_allscan) %s\n", file);
371 371
 
372
-    if (!FALSE_NEGATIVE) {
373
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_allscan failed for %s: %s", file, cl_strerror(ret));
374
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
372
+    if(!FALSE_NEGATIVE) {
373
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scanfile_allscan failed for %s: %s", file, cl_strerror(ret));
374
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
375 375
     }
376 376
     close(fd);
377 377
 }
... ...
@@ -380,30 +381,30 @@ END_TEST
380 380
 #endif
381 381
 
382 382
 /* int cl_load(const char *path, struct cl_engine **engine, unsigned int *signo, unsigned int options) */
383
-START_TEST (test_cl_load)
383
+START_TEST(test_cl_load)
384 384
 END_TEST
385 385
 
386 386
 /* int cl_cvdverify(const char *file) */
387
-START_TEST (test_cl_cvdverify)
387
+START_TEST(test_cl_cvdverify)
388 388
 END_TEST
389 389
 
390 390
 /* int cl_statinidir(const char *dirname, struct cl_stat *dbstat) */
391
-START_TEST (test_cl_statinidir)
391
+START_TEST(test_cl_statinidir)
392 392
 END_TEST
393 393
 
394 394
 /* int cl_statchkdir(const struct cl_stat *dbstat) */
395
-START_TEST (test_cl_statchkdir)
395
+START_TEST(test_cl_statchkdir)
396 396
 END_TEST
397 397
 
398 398
 /* void cl_settempdir(const char *dir, short leavetemps) */
399
-START_TEST (test_cl_settempdir)
399
+START_TEST(test_cl_settempdir)
400 400
 END_TEST
401 401
 
402 402
 /* const char *cl_strerror(int clerror) */
403
-START_TEST (test_cl_strerror)
403
+START_TEST(test_cl_strerror)
404 404
 END_TEST
405 405
 
406
-static char **testfiles = NULL;
406
+static char** testfiles     = NULL;
407 407
 static unsigned testfiles_n = 0;
408 408
 
409 409
 static const int expected_testfiles = 48;
... ...
@@ -413,8 +414,8 @@ static unsigned skip_files(void)
413 413
     unsigned skipped = 0;
414 414
 
415 415
     /* skip .rar files if unrar is disabled */
416
-    const char *s = getenv("unrar_disabled");
417
-    if (s && !strcmp(s, "1")) {
416
+    const char* s = getenv("unrar_disabled");
417
+    if(s && !strcmp(s, "1")) {
418 418
         skipped += 2;
419 419
     }
420 420
 
... ...
@@ -435,26 +436,26 @@ static unsigned skip_files(void)
435 435
 
436 436
 static void init_testfiles(void)
437 437
 {
438
-    struct dirent *dirent;
438
+    struct dirent* dirent;
439 439
     unsigned i = 0;
440 440
     int expect = expected_testfiles;
441 441
 
442
-    DIR *d = opendir(OBJDIR"/../test");
442
+    DIR* d = opendir(OBJDIR "/../test");
443 443
     fail_unless(!!d, "opendir");
444
-    if (!d)
445
-	return;
446
-    testfiles = NULL;
444
+    if(!d)
445
+        return;
446
+    testfiles   = NULL;
447 447
     testfiles_n = 0;
448
-    while ((dirent = readdir(d))) {
449
-	if (strncmp(dirent->d_name, "clam", 4))
450
-	    continue;
448
+    while((dirent = readdir(d))) {
449
+        if(strncmp(dirent->d_name, "clam", 4))
450
+            continue;
451 451
         i++;
452
-	testfiles = cli_realloc(testfiles, i*sizeof(*testfiles));
453
-	fail_unless(!!testfiles, "cli_realloc");
454
-	testfiles[i-1] = strdup(dirent->d_name);
452
+        testfiles = cli_realloc(testfiles, i * sizeof(*testfiles));
453
+        fail_unless(!!testfiles, "cli_realloc");
454
+        testfiles[i - 1] = strdup(dirent->d_name);
455 455
     }
456 456
     testfiles_n = i;
457
-    if (get_fpu_endian() == FPU_ENDIAN_UNKNOWN)
457
+    if(get_fpu_endian() == FPU_ENDIAN_UNKNOWN)
458 458
         expect--;
459 459
     expect -= skip_files();
460 460
     fail_unless_fmt(testfiles_n == expect, "testfiles: %d != %d", testfiles_n, expect);
... ...
@@ -465,11 +466,11 @@ static void init_testfiles(void)
465 465
 static void free_testfiles(void)
466 466
 {
467 467
     unsigned i;
468
-    for (i=0;i<testfiles_n;i++) {
469
-	free(testfiles[i]);
468
+    for(i = 0; i < testfiles_n; i++) {
469
+        free(testfiles[i]);
470 470
     }
471 471
     free(testfiles);
472
-    testfiles = NULL;
472
+    testfiles   = NULL;
473 473
     testfiles_n = 0;
474 474
 }
475 475
 
... ...
@@ -478,12 +479,12 @@ static int inited = 0;
478 478
 static void engine_setup(void)
479 479
 {
480 480
     unsigned int sigs = 0;
481
-    const char *hdb = OBJDIR"/clamav.hdb";
481
+    const char* hdb   = OBJDIR "/clamav.hdb";
482 482
 
483 483
     init_testfiles();
484
-    if (!inited)
485
-	fail_unless(cl_init(CL_INIT_DEFAULT) == 0, "cl_init");
486
-    inited = 1;
484
+    if(!inited)
485
+        fail_unless(cl_init(CL_INIT_DEFAULT) == 0, "cl_init");
486
+    inited   = 1;
487 487
     g_engine = cl_engine_new();
488 488
     fail_unless(!!g_engine, "engine");
489 489
     fail_unless_fmt(cl_load(hdb, g_engine, &sigs, CL_DB_STDOPT) == 0, "cl_load %s", hdb);
... ...
@@ -497,13 +498,13 @@ static void engine_teardown(void)
497 497
     cl_engine_free(g_engine);
498 498
 }
499 499
 
500
-static int get_test_file(int i, char *file, unsigned fsize, unsigned long *size)
500
+static int get_test_file(int i, char* file, unsigned fsize, unsigned long* size)
501 501
 {
502 502
     int fd;
503 503
     STATBUF st;
504 504
 
505 505
     fail_unless(i < testfiles_n, "%i < %i %s", i, testfiles_n, file);
506
-    snprintf(file, fsize, OBJDIR"/../test/%s", testfiles[i]);
506
+    snprintf(file, fsize, OBJDIR "/../test/%s", testfiles[i]);
507 507
 
508 508
     fd = open(file, O_RDONLY);
509 509
     fail_unless(fd > 0, "open");
... ...
@@ -513,16 +514,16 @@ static int get_test_file(int i, char *file, unsigned fsize, unsigned long *size)
513 513
 }
514 514
 #ifdef CHECK_HAVE_LOOPS
515 515
 
516
-static off_t pread_cb(void *handle, void *buf, size_t count, off_t offset)
516
+static off_t pread_cb(void* handle, void* buf, size_t count, off_t offset)
517 517
 {
518 518
     return pread(*((int*)handle), buf, count, offset);
519 519
 }
520 520
 
521
-START_TEST (test_cl_scanmap_callback_handle)
521
+START_TEST(test_cl_scanmap_callback_handle)
522 522
 {
523
-    const char *virname = NULL;
523
+    const char* virname       = NULL;
524 524
     unsigned long int scanned = 0;
525
-    cl_fmap_t *map;
525
+    cl_fmap_t* map;
526 526
     int ret;
527 527
     char file[256];
528 528
     unsigned long size;
... ...
@@ -540,19 +541,19 @@ START_TEST (test_cl_scanmap_callback_handle)
540 540
     ret = cl_scanmap_callback(map, file, &virname, &scanned, g_engine, &options, NULL);
541 541
     cli_dbgmsg("scan end (handle) %s\n", file);
542 542
 
543
-    if (!FALSE_NEGATIVE) {
544
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback failed for %s: %s", file, cl_strerror(ret));
545
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
543
+    if(!FALSE_NEGATIVE) {
544
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback failed for %s: %s", file, cl_strerror(ret));
545
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
546 546
     }
547 547
     close(fd);
548 548
 }
549 549
 END_TEST
550 550
 
551
-START_TEST (test_cl_scanmap_callback_handle_allscan)
551
+START_TEST(test_cl_scanmap_callback_handle_allscan)
552 552
 {
553
-    const char *virname = NULL;
553
+    const char* virname       = NULL;
554 554
     unsigned long int scanned = 0;
555
-    cl_fmap_t *map;
555
+    cl_fmap_t* map;
556 556
     int ret;
557 557
     char file[256];
558 558
     unsigned long size;
... ...
@@ -571,21 +572,21 @@ START_TEST (test_cl_scanmap_callback_handle_allscan)
571 571
     ret = cl_scanmap_callback(map, file, &virname, &scanned, g_engine, &options, NULL);
572 572
     cli_dbgmsg("scan end (handle) allscan %s\n", file);
573 573
 
574
-    if (!FALSE_NEGATIVE) {
575
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback allscan failed for %s: %s", file, cl_strerror(ret));
576
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
574
+    if(!FALSE_NEGATIVE) {
575
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback allscan failed for %s: %s", file, cl_strerror(ret));
576
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s", virname);
577 577
     }
578 578
     close(fd);
579 579
 }
580 580
 END_TEST
581 581
 
582
-START_TEST (test_cl_scanmap_callback_mem)
582
+START_TEST(test_cl_scanmap_callback_mem)
583 583
 {
584
-    const char *virname = NULL;
584
+    const char* virname       = NULL;
585 585
     unsigned long int scanned = 0;
586
-    cl_fmap_t *map;
586
+    cl_fmap_t* map;
587 587
     int ret;
588
-    void *mem;
588
+    void* mem;
589 589
     unsigned long size;
590 590
     char file[256];
591 591
     struct cl_scan_options options;
... ...
@@ -605,9 +606,9 @@ START_TEST (test_cl_scanmap_callback_mem)
605 605
     cli_dbgmsg("scanning (mem) %s\n", file);
606 606
     ret = cl_scanmap_callback(map, file, &virname, &scanned, g_engine, &options, NULL);
607 607
     cli_dbgmsg("scan end (mem) %s\n", file);
608
-    if (!FALSE_NEGATIVE) {
609
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback failed for %s: %s", file, cl_strerror(ret));
610
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s for %s", virname, file);
608
+    if(!FALSE_NEGATIVE) {
609
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback failed for %s: %s", file, cl_strerror(ret));
610
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s for %s", virname, file);
611 611
     }
612 612
     close(fd);
613 613
     cl_fmap_close(map);
... ...
@@ -616,13 +617,13 @@ START_TEST (test_cl_scanmap_callback_mem)
616 616
 }
617 617
 END_TEST
618 618
 
619
-START_TEST (test_cl_scanmap_callback_mem_allscan)
619
+START_TEST(test_cl_scanmap_callback_mem_allscan)
620 620
 {
621
-    const char *virname = NULL;
621
+    const char* virname       = NULL;
622 622
     unsigned long int scanned = 0;
623
-    cl_fmap_t *map;
623
+    cl_fmap_t* map;
624 624
     int ret;
625
-    void *mem;
625
+    void* mem;
626 626
     unsigned long size;
627 627
     char file[256];
628 628
     struct cl_scan_options options;
... ...
@@ -643,9 +644,9 @@ START_TEST (test_cl_scanmap_callback_mem_allscan)
643 643
     cli_dbgmsg("scanning (mem) allscan %s\n", file);
644 644
     ret = cl_scanmap_callback(map, file, &virname, &scanned, g_engine, &options, NULL);
645 645
     cli_dbgmsg("scan end (mem) allscan %s\n", file);
646
-    if (!FALSE_NEGATIVE) {
647
-      fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback allscan failed for %s: %s", file, cl_strerror(ret));
648
-      fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s for %s", virname, file);
646
+    if(!FALSE_NEGATIVE) {
647
+        fail_unless_fmt(ret == CL_VIRUS, "cl_scanmap_callback allscan failed for %s: %s", file, cl_strerror(ret));
648
+        fail_unless_fmt(virname && !strcmp(virname, "ClamAV-Test-File.UNOFFICIAL"), "virusname: %s for %s", virname, file);
649 649
     }
650 650
     close(fd);
651 651
     cl_fmap_close(map);
... ...
@@ -654,14 +655,14 @@ START_TEST (test_cl_scanmap_callback_mem_allscan)
654 654
 END_TEST
655 655
 #endif
656 656
 
657
-static Suite *test_cl_suite(void)
657
+static Suite* test_cl_suite(void)
658 658
 {
659
-    Suite *s = suite_create("cl_api");
660
-    TCase *tc_cl = tcase_create("cl_dup");
661
-    TCase *tc_cl_scan = tcase_create("cl_scan");
662
-    char *user_timeout = NULL;
663
-    int expect = expected_testfiles;
664
-    suite_add_tcase (s, tc_cl);
659
+    Suite* s           = suite_create("cl_api");
660
+    TCase* tc_cl       = tcase_create("cl_dup");
661
+    TCase* tc_cl_scan  = tcase_create("cl_scan");
662
+    char* user_timeout = NULL;
663
+    int expect         = expected_testfiles;
664
+    suite_add_tcase(s, tc_cl);
665 665
     tcase_add_test(tc_cl, test_cl_free);
666 666
     tcase_add_test(tc_cl, test_cl_dup);
667 667
     tcase_add_test(tc_cl, test_cl_build);
... ...
@@ -681,9 +682,9 @@ static Suite *test_cl_suite(void)
681 681
     tcase_add_test(tc_cl, test_cl_strerror);
682 682
 
683 683
     suite_add_tcase(s, tc_cl_scan);
684
-    tcase_add_checked_fixture (tc_cl_scan, engine_setup, engine_teardown);
684
+    tcase_add_checked_fixture(tc_cl_scan, engine_setup, engine_teardown);
685 685
 #ifdef CHECK_HAVE_LOOPS
686
-    if (get_fpu_endian() == FPU_ENDIAN_UNKNOWN)
686
+    if(get_fpu_endian() == FPU_ENDIAN_UNKNOWN)
687 687
         expect--;
688 688
     expect -= skip_files();
689 689
     tcase_add_loop_test(tc_cl_scan, test_cl_scandesc, 0, expect);
... ...
@@ -700,307 +701,402 @@ static Suite *test_cl_suite(void)
700 700
     tcase_add_loop_test(tc_cl_scan, test_cl_scanmap_callback_mem_allscan, 0, expect);
701 701
 
702 702
     user_timeout = getenv("T");
703
-    if (user_timeout) {
703
+    if(user_timeout) {
704 704
         int timeout = atoi(user_timeout);
705 705
         tcase_set_timeout(tc_cl_scan, timeout);
706
-	printf("Using test case timeout of %d seconds set by user\n", timeout);
706
+        printf("Using test case timeout of %d seconds set by user\n", timeout);
707 707
     } else {
708
-	printf("Using default test timeout; alter by setting 'T' env var (in seconds)\n");
708
+        printf("Using default test timeout; alter by setting 'T' env var (in seconds)\n");
709 709
     }
710 710
 #endif
711 711
     return s;
712 712
 }
713 713
 
714
-static uint8_t le_data[4] = {0x67,0x45,0x23,0x01};
715
-static int32_t le_expected[4] = { 0x01234567, 0x67012345, 0x45670123, 0x23456701};
716
-uint8_t *data = NULL;
717
-uint8_t *data2 = NULL;
714
+static uint8_t le_data[4]     = {0x67, 0x45, 0x23, 0x01};
715
+static int32_t le_expected[4] = {0x01234567, 0x67012345, 0x45670123, 0x23456701};
716
+uint8_t* data                 = NULL;
717
+uint8_t* data2                = NULL;
718 718
 #define DATA_REP 100
719 719
 
720 720
 static void data_setup(void)
721 721
 {
722
-        uint8_t *p;
723
-        size_t i;
724
-
725
-	data = malloc(sizeof(le_data)*DATA_REP);
726
-	data2 = malloc(sizeof(le_data)*DATA_REP);
727
-	fail_unless(!!data, "unable to allocate memory for fixture");
728
-        fail_unless(!!data2, "unable to allocate memory for fixture");
729
-        p = data;
730
-        /* make multiple copies of le_data, we need to run readint tests in a loop, so we need
722
+    uint8_t* p;
723
+    size_t i;
724
+
725
+    data  = malloc(sizeof(le_data) * DATA_REP);
726
+    data2 = malloc(sizeof(le_data) * DATA_REP);
727
+    fail_unless(!!data, "unable to allocate memory for fixture");
728
+    fail_unless(!!data2, "unable to allocate memory for fixture");
729
+    p = data;
730
+    /* make multiple copies of le_data, we need to run readint tests in a loop, so we need
731 731
          * to give it some data to run it on */
732
-        for(i=0; i<DATA_REP;i++) {
733
-                memcpy(p, le_data, sizeof(le_data));
734
-                p += sizeof(le_data);
735
-        }
736
-        memset(data2, 0, DATA_REP*sizeof(le_data));
732
+    for(i = 0; i < DATA_REP; i++) {
733
+        memcpy(p, le_data, sizeof(le_data));
734
+        p += sizeof(le_data);
735
+    }
736
+    memset(data2, 0, DATA_REP * sizeof(le_data));
737 737
 }
738 738
 
739 739
 static void data_teardown(void)
740 740
 {
741
-        free(data);
742
-	free(data2);
741
+    free(data);
742
+    free(data2);
743 743
 }
744 744
 
745 745
 #ifdef CHECK_HAVE_LOOPS
746 746
 /* test reading with different alignments, _i is parameter from tcase_add_loop_test */
747
-START_TEST (test_cli_readint16)
747
+START_TEST(test_cli_readint16)
748 748
 {
749 749
     size_t j;
750 750
     int16_t value;
751 751
     /* read 2 bytes apart, start is not always aligned*/
752
-    for(j=_i;j <= DATA_REP*sizeof(le_data)-2;j += 2) {
753
-        value = le_expected[j&3];
752
+    for(j = _i; j <= DATA_REP * sizeof(le_data) - 2; j += 2) {
753
+        value = le_expected[j & 3];
754 754
         fail_unless(cli_readint16(&data[j]) == value, "(1) data read must be little endian");
755 755
     }
756 756
     /* read 2 bytes apart, always aligned*/
757
-    for(j=0;j <= DATA_REP*sizeof(le_data)-2;j += 2) {
758
-        value = le_expected[j&3];
757
+    for(j = 0; j <= DATA_REP * sizeof(le_data) - 2; j += 2) {
758
+        value = le_expected[j & 3];
759 759
         fail_unless(cli_readint16(&data[j]) == value, "(2) data read must be little endian");
760 760
     }
761 761
 }
762 762
 END_TEST
763 763
 
764 764
 /* test reading with different alignments, _i is parameter from tcase_add_loop_test */
765
-START_TEST (test_cli_readint32)
765
+START_TEST(test_cli_readint32)
766 766
 {
767 767
     size_t j;
768
-    int32_t value = le_expected[_i&3];
768
+    int32_t value = le_expected[_i & 3];
769 769
     /* read 4 bytes apart, start is not always aligned*/
770
-    for(j=_i;j < DATA_REP*sizeof(le_data)-4;j += 4) {
770
+    for(j = _i; j < DATA_REP * sizeof(le_data) - 4; j += 4) {
771 771
         fail_unless(cli_readint32(&data[j]) == value, "(1) data read must be little endian");
772 772
     }
773 773
     value = le_expected[0];
774 774
     /* read 4 bytes apart, always aligned*/
775
-    for(j=0;j < DATA_REP*sizeof(le_data)-4;j += 4) {
775
+    for(j = 0; j < DATA_REP * sizeof(le_data) - 4; j += 4) {
776 776
         fail_unless(cli_readint32(&data[j]) == value, "(2) data read must be little endian");
777 777
     }
778 778
 }
779 779
 END_TEST
780 780
 
781 781
 /* test writing with different alignments, _i is parameter from tcase_add_loop_test */
782
-START_TEST (test_cli_writeint32)
782
+START_TEST(test_cli_writeint32)
783 783
 {
784 784
     size_t j;
785 785
     /* write 4 bytes apart, start is not always aligned*/
786
-    for(j=_i;j < DATA_REP*sizeof(le_data) - 4;j += 4) {
786
+    for(j = _i; j < DATA_REP * sizeof(le_data) - 4; j += 4) {
787 787
         cli_writeint32(&data2[j], 0x12345678);
788 788
     }
789
-    for(j=_i;j < DATA_REP*sizeof(le_data) - 4;j += 4) {
789
+    for(j = _i; j < DATA_REP * sizeof(le_data) - 4; j += 4) {
790 790
         fail_unless(cli_readint32(&data2[j]) == 0x12345678, "write/read mismatch");
791 791
     }
792 792
     /* write 4 bytes apart, always aligned*/
793
-    for(j=0;j < DATA_REP*sizeof(le_data) - 4;j += 4) {
793
+    for(j = 0; j < DATA_REP * sizeof(le_data) - 4; j += 4) {
794 794
         cli_writeint32(&data2[j], 0x12345678);
795 795
     }
796
-    for(j=0;j < DATA_REP*sizeof(le_data) - 4;j += 4) {
796
+    for(j = 0; j < DATA_REP * sizeof(le_data) - 4; j += 4) {
797 797
         fail_unless(cli_readint32(&data2[j]) == 0x12345678, "write/read mismatch");
798 798
     }
799 799
 }
800 800
 END_TEST
801 801
 
802 802
 static struct dsig_test {
803
-    const char *md5;
804
-    const char *dsig;
803
+    const char* md5;
804
+    const char* dsig;
805 805
     int result;
806
-} dsig_tests [] = {
807
-    {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe", 
808
-	CL_SUCCESS},
806
+} dsig_tests[] = {
807
+    {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
808
+     CL_SUCCESS},
809 809
     {"96b7feb3b2a863846438809fe481906f", "Zh5gmf09Zfj6V4gmRKu/NURzhFiE9VloI7w1G33BgDdGSs0Xhscx6sjPUpFSCPsjOalyS4L8q7RS+NdGvNCsLymiIH6RYItlOZsygFhcGuH4jt15KAaAkvEg2TwmqR8z41nUaMlZ0c8q1MXYCLvQJyFARsfzIxS3PAoN2Y3HPoe",
810
-	CL_SUCCESS},
810
+     CL_SUCCESS},
811 811
     {"ae307614434715274c60854c931a26de", "Zh5gmf09Zfj6V4gmRKu/NURzhFiE9VloI7w1G33BgDdGSs0Xhscx6sjPUpFSCPsjOalyS4L8q7RS+NdGvNCsLymiIH6RYItlOZsygFhcGuH4jt15KAaAkvEg2TwmqR8z41nUaMlZ0c8q1MXYCLvQJyFARsfzIxS3PAoN2Y3HPoe",
812
-	CL_EVERIFY},
812
+     CL_EVERIFY},
813 813
     {"96b7feb3b2a863846438809fe481906f", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
814
-	CL_EVERIFY},
814
+     CL_EVERIFY},
815 815
     {"ae307614434715274060854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
816
-	CL_EVERIFY},
816
+     CL_EVERIFY},
817 817
     {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaatinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
818
-	CL_EVERIFY},
818
+     CL_EVERIFY},
819 819
     {"96b7feb3b2a863846438809fe481906f", "Zh5gmf09Zfj6V4gmRKu/NURzhFiE9VloI7w1G33BgDdGSs0Xhscx6sjPUpFSCPsjOalyS4L8q7RS+NdGvNCsLymiIH6RYItlOZsygFhcGuH4jt15KAaAkvEg2TwmqR8z41nUaMlZ0c8q1MYYCLvQJyFARsfzIxS3PAoN2Y3HPoe",
820
-	CL_EVERIFY},
821
-    {"ge307614434715274c60854c931a26dee","60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
822
-	CL_EVERIFY},
823
-    {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGee", 
824
-	CL_EVERIFY},
825
-    {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+", 
826
-	CL_EVERIFY}
827
-};
828
-
829
-static const size_t dsig_tests_cnt = sizeof(dsig_tests)/sizeof(dsig_tests[0]);
830
-
831
-START_TEST (test_cli_dsig)
820
+     CL_EVERIFY},
821
+    {"ge307614434715274c60854c931a26dee", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGe",
822
+     CL_EVERIFY},
823
+    {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+60VhQcuXfb0iV1O+sCEyMiRXt/iYF6vXtPXHVd6DiuZ4Gfrry7sVQqNTt3o1/KwU1rc0l5FHgX/nC99fdr/fjaFtinMtRnUXHLeu0j8e6HK+7JLBpD37fZ60GC9YY86EclYGee",
824
+     CL_EVERIFY},
825
+    {"ae307614434715274c60854c931a26de", "60uhCFmiN48J8r6c7coBv9Q1mehAWEGh6GPYA+",
826
+     CL_EVERIFY}};
827
+
828
+static const size_t dsig_tests_cnt = sizeof(dsig_tests) / sizeof(dsig_tests[0]);
829
+
830
+START_TEST(test_cli_dsig)
832 831
 {
833 832
     fail_unless(cli_versig(dsig_tests[_i].md5, dsig_tests[_i].dsig) == dsig_tests[_i].result,
834
-		"digital signature verification test failed");
833
+                "digital signature verification test failed");
835 834
 }
836 835
 END_TEST
837 836
 
838 837
 static uint8_t tv1[3] = {
839
-  0x61, 0x62, 0x63
840
-};
838
+    0x61, 0x62, 0x63};
841 839
 
842 840
 static uint8_t tv2[56] = {
843
-  0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65,
844
-  0x63, 0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67,
845
-  0x65, 0x66, 0x67, 0x68, 0x66, 0x67, 0x68, 0x69,
846
-  0x67, 0x68, 0x69, 0x6a, 0x68, 0x69, 0x6a, 0x6b,
847
-  0x69, 0x6a, 0x6b, 0x6c, 0x6a, 0x6b, 0x6c, 0x6d,
848
-  0x6b, 0x6c, 0x6d, 0x6e, 0x6c, 0x6d, 0x6e, 0x6f,
849
-  0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, 0x70, 0x71
850
-};
841
+    0x61, 0x62, 0x63, 0x64, 0x62, 0x63, 0x64, 0x65,
842
+    0x63, 0x64, 0x65, 0x66, 0x64, 0x65, 0x66, 0x67,
843
+    0x65, 0x66, 0x67, 0x68, 0x66, 0x67, 0x68, 0x69,
844
+    0x67, 0x68, 0x69, 0x6a, 0x68, 0x69, 0x6a, 0x6b,
845
+    0x69, 0x6a, 0x6b, 0x6c, 0x6a, 0x6b, 0x6c, 0x6d,
846
+    0x6b, 0x6c, 0x6d, 0x6e, 0x6c, 0x6d, 0x6e, 0x6f,
847
+    0x6d, 0x6e, 0x6f, 0x70, 0x6e, 0x6f, 0x70, 0x71};
851 848
 
852 849
 static uint8_t res256[3][SHA256_HASH_SIZE] = {
853
-  { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde,
854
-    0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
855
-    0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad },
856
-  { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93,
857
-    0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
858
-    0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 },
859
-  { 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7, 0xe2,
860
-    0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
861
-    0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0 }
862
-};
863
-
864
-START_TEST (test_sha256)
850
+    {0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde,
851
+     0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
852
+     0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad},
853
+    {0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93,
854
+     0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
855
+     0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1},
856
+    {0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7, 0xe2,
857
+     0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e,
858
+     0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0}};
859
+
860
+START_TEST(test_sha256)
865 861
 {
866
-    void *sha256;
862
+    void* sha256;
867 863
     uint8_t hsha256[SHA256_HASH_SIZE];
868 864
     uint8_t buf[1000];
869 865
     int i;
870 866
 
871
-    memset (buf, 0x61, sizeof (buf));
867
+    memset(buf, 0x61, sizeof(buf));
872 868
 
873 869
     cl_sha256(tv1, sizeof(tv1), hsha256, NULL);
874
-    fail_unless(!memcmp (hsha256, res256[0], sizeof (hsha256)), "sha256 test vector #1 failed");
870
+    fail_unless(!memcmp(hsha256, res256[0], sizeof(hsha256)), "sha256 test vector #1 failed");
875 871
 
876 872
     cl_sha256(tv2, sizeof(tv2), hsha256, NULL);
877
-    fail_unless(!memcmp (hsha256, res256[1], sizeof (hsha256)), "sha256 test vector #2 failed");
873
+    fail_unless(!memcmp(hsha256, res256[1], sizeof(hsha256)), "sha256 test vector #2 failed");
878 874
 
879 875
     sha256 = cl_hash_init("sha256");
880 876
     fail_unless(sha256 != NULL, "Could not create EVP_MD_CTX for sha256");
881 877
 
882
-    for (i = 0; i < 1000; i++)
883
-        cl_update_hash (sha256, buf, sizeof (buf));
878
+    for(i = 0; i < 1000; i++)
879
+        cl_update_hash(sha256, buf, sizeof(buf));
884 880
     cl_finish_hash(sha256, hsha256);
885
-    fail_unless(!memcmp (hsha256, res256[2], sizeof (hsha256)), "sha256 test vector #3 failed");
881
+    fail_unless(!memcmp(hsha256, res256[2], sizeof(hsha256)), "sha256 test vector #3 failed");
882
+}
883
+END_TEST
884
+
885
+START_TEST(test_sanitize_path)
886
+{
887
+    char* sanitized         = NULL;
888
+    const char* unsanitized = NULL;
889
+
890
+    unsanitized = "";
891
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
892
+    fail_if(NULL != sanitized, "sanitize_path: Empty path test failed");
893
+
894
+    unsanitized = NULL;
895
+    sanitized   = cli_sanitize_filepath(unsanitized, 0);
896
+    fail_if(NULL != sanitized, "sanitize_path: NULL path #1 test failed");
897
+
898
+    unsanitized = NULL;
899
+    sanitized   = cli_sanitize_filepath(unsanitized, 50);
900
+    fail_if(NULL != sanitized, "sanitize_path: NULL path #2 test failed");
901
+
902
+    unsanitized = "badlen";
903
+    sanitized   = cli_sanitize_filepath(unsanitized, 0);
904
+    fail_if(NULL != sanitized, "sanitize_path: Zero/bad path length test failed");
905
+
906
+    unsanitized = ".." PATHSEP;
907
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
908
+    fail_if(NULL != sanitized, "sanitize_path: sanitized path should have been NULL");
909
+
910
+    unsanitized = "." PATHSEP;
911
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
912
+    fail_if(NULL != sanitized, "sanitize_path: sanitized path should have been NULL (2)");
913
+
914
+    unsanitized = PATHSEP;
915
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
916
+    fail_if(NULL != sanitized, "sanitize_path: sanitized path should have been NULL (3)");
917
+
918
+    unsanitized = ".." PATHSEP "relative_bad_1";
919
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
920
+    fail_if(NULL == sanitized);
921
+    fail_unless(!strcmp(sanitized, "relative_bad_1"), "sanitize_path: bad relative path test #1 failed");
922
+    free(sanitized);
923
+
924
+    unsanitized = "relative" PATHSEP ".." PATHSEP "good";
925
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
926
+    fail_if(NULL == sanitized);
927
+    fail_unless(!strcmp(sanitized, "relative" PATHSEP ".." PATHSEP "good"), "sanitize_path: good relative path test failed");
928
+    free(sanitized);
929
+
930
+    unsanitized = "relative" PATHSEP ".." PATHSEP ".." PATHSEP "bad_2";
931
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
932
+    fail_if(NULL == sanitized);
933
+    fail_unless(!strcmp(sanitized, "relative" PATHSEP ".." PATHSEP "bad_2"), "sanitize_path: bad relative path test failed");
934
+    free(sanitized);
935
+
936
+    unsanitized = "relative" PATHSEP "." PATHSEP ".." PATHSEP ".." PATHSEP "bad_current";
937
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
938
+    fail_if(NULL == sanitized);
939
+    fail_unless(!strcmp(sanitized, "relative" PATHSEP ".." PATHSEP "bad_current"), "sanitize_path: bad relative current path test failed");
940
+    free(sanitized);
941
+
942
+    unsanitized = "relative/../../bad_win_posix_path";
943
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
944
+    fail_if(NULL == sanitized);
945
+    fail_unless(!strcmp(sanitized, "relative/../bad_win_posix_path"), "sanitize_path: bad relative win posix path test failed");
946
+    free(sanitized);
947
+
948
+    unsanitized = "" PATHSEP "absolute" PATHSEP ".." PATHSEP ".." PATHSEP "bad";
949
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
950
+    fail_if(NULL == sanitized);
951
+    fail_unless(!strcmp(sanitized, "absolute" PATHSEP ".." PATHSEP "bad"), "sanitize_path: bad absolute path test failed");
952
+    free(sanitized);
953
+
954
+    unsanitized = "" PATHSEP "absolute" PATHSEP ".." PATHSEP "good";
955
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
956
+    fail_if(NULL == sanitized);
957
+    fail_unless(!strcmp(sanitized, "absolute" PATHSEP ".." PATHSEP "good"), "sanitize_path: good absolute path test failed");
958
+    free(sanitized);
959
+
960
+    unsanitized = "relative" PATHSEP "normal";
961
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
962
+    fail_if(NULL == sanitized);
963
+    fail_unless(!strcmp(sanitized, "relative" PATHSEP "normal"), "sanitize_path: relative normal path test failed");
964
+    free(sanitized);
965
+
966
+    unsanitized = "relative" PATHSEP PATHSEP "doublesep";
967
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
968
+    fail_if(NULL == sanitized);
969
+    fail_unless(!strcmp(sanitized, "relative" PATHSEP "doublesep"), "sanitize_path: relative double sep path test failed");
970
+    free(sanitized);
971
+
972
+    unsanitized = "relative" PATHSEP "shortname" PATHSEP "1";
973
+    sanitized   = cli_sanitize_filepath(unsanitized, strlen(unsanitized));
974
+    fail_if(NULL == sanitized);
975
+    fail_unless(!strcmp(sanitized, "relative" PATHSEP "shortname" PATHSEP "1"), "sanitize_path: relative short name path test failed");
976
+    free(sanitized);
886 977
 }
887 978
 END_TEST
888 979
 
889
-static Suite *test_cli_suite(void)
980
+static Suite* test_cli_suite(void)
890 981
 {
891
-    Suite *s = suite_create("cli");
892
-    TCase *tc_cli_others = tcase_create("byteorder_macros");
893
-    TCase *tc_cli_dsig = tcase_create("digital signatures");
982
+    Suite* s               = suite_create("cli");
983
+    TCase* tc_cli_others   = tcase_create("byteorder_macros");
984
+    TCase* tc_cli_dsig     = tcase_create("digital signatures");
985
+    TCase* tc_cli_assorted = tcase_create("assorted functions");
894 986
 
895
-    suite_add_tcase (s, tc_cli_others);
896
-    tcase_add_checked_fixture (tc_cli_others, data_setup, data_teardown);
987
+    suite_add_tcase(s, tc_cli_others);
988
+    tcase_add_checked_fixture(tc_cli_others, data_setup, data_teardown);
897 989
     tcase_add_loop_test(tc_cli_others, test_cli_readint32, 0, 16);
898 990
     tcase_add_loop_test(tc_cli_others, test_cli_readint16, 0, 16);
899 991
     tcase_add_loop_test(tc_cli_others, test_cli_writeint32, 0, 16);
900 992
 
901
-    suite_add_tcase (s, tc_cli_dsig);
993
+    suite_add_tcase(s, tc_cli_dsig);
902 994
     tcase_add_loop_test(tc_cli_dsig, test_cli_dsig, 0, dsig_tests_cnt);
903 995
     tcase_add_test(tc_cli_dsig, test_sha256);
904 996
 
997
+    suite_add_tcase(s, tc_cli_assorted);
998
+    tcase_add_test(tc_cli_assorted, test_sanitize_path);
999
+
905 1000
     return s;
906 1001
 }
907 1002
 #endif /* CHECK_HAVE_LOOPS */
908 1003
 
909 1004
 void errmsg_expected(void)
910 1005
 {
911
-	fputs("cli_errmsg() expected here\n", stderr);
1006
+    fputs("cli_errmsg() expected here\n", stderr);
912 1007
 }
913 1008
 
914
-int open_testfile(const char *name)
1009
+int open_testfile(const char* name)
915 1010
 {
916
-	int fd;
917
-	const char * srcdir = getenv("srcdir");
918
-	char *str;
919
-
920
-	if(!srcdir) {
921
-		/* when run from automake srcdir is set, but if run manually then not */
922
-		srcdir = SRCDIR;
923
-	}
924
-
925
-	str = cli_malloc(strlen(name)+strlen(srcdir)+2);
926
-	fail_unless(!!str, "cli_malloc");
927
-	sprintf(str, "%s/%s", srcdir, name);
928
-
929
-	fd = open(str, O_RDONLY);
930
-	fail_unless_fmt(fd >= 0, "open() failed: %s", str);
931
-	free(str);
932
-	return fd;
1011
+    int fd;
1012
+    const char* srcdir = getenv("srcdir");
1013
+    char* str;
1014
+
1015
+    if(!srcdir) {
1016
+        /* when run from automake srcdir is set, but if run manually then not */
1017
+        srcdir = SRCDIR;
1018
+    }
1019
+
1020
+    str = cli_malloc(strlen(name) + strlen(srcdir) + 2);
1021
+    fail_unless(!!str, "cli_malloc");
1022
+    sprintf(str, "%s/%s", srcdir, name);
1023
+
1024
+    fd = open(str, O_RDONLY);
1025
+    fail_unless_fmt(fd >= 0, "open() failed: %s", str);
1026
+    free(str);
1027
+    return fd;
933 1028
 }
934 1029
 
935
-void diff_file_mem(int fd, const char *ref, size_t len)
1030
+void diff_file_mem(int fd, const char* ref, size_t len)
936 1031
 {
937
-	char c1,c2;
938
-	size_t p, reflen = len;
939
-	char *buf = cli_malloc(len);
940
-
941
-	fail_unless_fmt(!!buf, "unable to malloc buffer: %d", len);
942
-	p = read(fd, buf, len);
943
-	fail_unless_fmt(p == len,  "file is smaller: %lu, expected: %lu", p, len);
944
-	p = 0;
945
-	while(len > 0) {
946
-		c1 = ref[p];
947
-		c2 = buf[p];
948
-		if(c1 != c2)
949
-			break;
950
-		p++;
951
-		len--;
952
-	}
953
-	if (len > 0)
954
-		fail_unless_fmt(c1 == c2, "file contents mismatch at byte: %lu, was: %c, expected: %c", p, c2, c1);
955
-	free(buf);
956
-	p = lseek(fd, 0, SEEK_END);
957
-        fail_unless_fmt(p == reflen, "trailing garbage, file size: %ld, expected: %ld", p, reflen);
958
-	close(fd);
1032
+    char c1, c2;
1033
+    size_t p, reflen = len;
1034
+    char* buf = cli_malloc(len);
1035
+
1036
+    fail_unless_fmt(!!buf, "unable to malloc buffer: %d", len);
1037
+    p = read(fd, buf, len);
1038
+    fail_unless_fmt(p == len, "file is smaller: %lu, expected: %lu", p, len);
1039
+    p = 0;
1040
+    while(len > 0) {
1041
+        c1 = ref[p];
1042
+        c2 = buf[p];
1043
+        if(c1 != c2)
1044
+            break;
1045
+        p++;
1046
+        len--;
1047
+    }
1048
+    if(len > 0)
1049
+        fail_unless_fmt(c1 == c2, "file contents mismatch at byte: %lu, was: %c, expected: %c", p, c2, c1);
1050
+    free(buf);
1051
+    p = lseek(fd, 0, SEEK_END);
1052
+    fail_unless_fmt(p == reflen, "trailing garbage, file size: %ld, expected: %ld", p, reflen);
1053
+    close(fd);
959 1054
 }
960 1055
 
961 1056
 void diff_files(int fd, int ref_fd)
962 1057
 {
963
-	char *ref;
964
-	ssize_t nread;
965
-	off_t siz = lseek(ref_fd, 0, SEEK_END);
966
-	fail_unless_fmt(siz != -1, "lseek failed");
967
-
968
-	ref = cli_malloc(siz);
969
-	fail_unless_fmt(!!ref, "unable to malloc buffer: %d", siz);
970
-
971
-	fail_unless_fmt(lseek(ref_fd, 0, SEEK_SET) == 0,"lseek failed");
972
-	nread = read(ref_fd, ref, siz);
973
-        fail_unless_fmt(nread == siz, "short read, expected: %ld, was: %ld", siz, nread);
974
-	close(ref_fd);
975
-	diff_file_mem(fd, ref, siz);
976
-	free(ref);
1058
+    char* ref;
1059
+    ssize_t nread;
1060
+    off_t siz = lseek(ref_fd, 0, SEEK_END);
1061
+    fail_unless_fmt(siz != -1, "lseek failed");
1062
+
1063
+    ref = cli_malloc(siz);
1064
+    fail_unless_fmt(!!ref, "unable to malloc buffer: %d", siz);
1065
+
1066
+    fail_unless_fmt(lseek(ref_fd, 0, SEEK_SET) == 0, "lseek failed");
1067
+    nread = read(ref_fd, ref, siz);
1068
+    fail_unless_fmt(nread == siz, "short read, expected: %ld, was: %ld", siz, nread);
1069
+    close(ref_fd);
1070
+    diff_file_mem(fd, ref, siz);
1071
+    free(ref);
977 1072
 }
978 1073
 
979 1074
 #ifdef USE_MPOOL
980
-static mpool_t *pool;
1075
+static mpool_t* pool;
981 1076
 #else
982
-static void *pool;
1077
+static void* pool;
983 1078
 #endif
984
-struct cli_dconf *dconf;
1079
+struct cli_dconf* dconf;
985 1080
 
986 1081
 void dconf_setup(void)
987 1082
 {
988
-	pool = NULL;
989
-	dconf = NULL;
1083
+    pool  = NULL;
1084
+    dconf = NULL;
990 1085
 #ifdef USE_MPOOL
991
-	pool = mpool_create();
992
-	fail_unless(!!pool, "unable to create pool");
1086
+    pool = mpool_create();
1087
+    fail_unless(!!pool, "unable to create pool");
993 1088
 #endif
994
-	dconf = cli_mpool_dconf_init(pool);
995
-	fail_unless(!!dconf, "failed to init dconf");
1089
+    dconf = cli_mpool_dconf_init(pool);
1090
+    fail_unless(!!dconf, "failed to init dconf");
996 1091
 }
997 1092
 
998 1093
 void dconf_teardown(void)
999 1094
 {
1000
-	mpool_free(pool, dconf);
1095
+    mpool_free(pool, dconf);
1001 1096
 #ifdef USE_MPOOL
1002
-	if (pool)
1003
-		mpool_destroy(pool);
1097
+    if(pool)
1098
+        mpool_destroy(pool);
1004 1099
 #endif
1005 1100
 }
1006 1101
 
... ...
@@ -1009,35 +1105,35 @@ static void check_version_compatible()
1009 1009
     /* check 0.9.8 is not ABI compatible with 0.9.6,
1010 1010
      * if by accident you compile with check 0.9.6 header
1011 1011
      * and link with 0.9.8 then check will hang/crash. */
1012
-    if ((check_major_version != CHECK_MAJOR_VERSION) ||
1013
-	(check_minor_version != CHECK_MINOR_VERSION) ||
1014
-	(check_micro_version != CHECK_MICRO_VERSION)) {
1015
-	fprintf(stderr, "ERROR: check version mismatch!\n"
1016
-		"\tVersion from header: %u.%u.%u\n"
1017
-		"\tVersion from library: %u.%u.%u\n"
1018
-		"\tMake sure check.h and -lcheck are same version!\n",
1019
-		CHECK_MAJOR_VERSION,
1020
-		CHECK_MINOR_VERSION,
1021
-		CHECK_MICRO_VERSION,
1022
-		check_major_version,
1023
-		check_minor_version,
1024
-		check_micro_version);
1025
-	exit(EXIT_FAILURE);
1012
+    if((check_major_version != CHECK_MAJOR_VERSION) ||
1013
+       (check_minor_version != CHECK_MINOR_VERSION) ||
1014
+       (check_micro_version != CHECK_MICRO_VERSION)) {
1015
+        fprintf(stderr, "ERROR: check version mismatch!\n"
1016
+                        "\tVersion from header: %u.%u.%u\n"
1017
+                        "\tVersion from library: %u.%u.%u\n"
1018
+                        "\tMake sure check.h and -lcheck are same version!\n",
1019
+                CHECK_MAJOR_VERSION,
1020
+                CHECK_MINOR_VERSION,
1021
+                CHECK_MICRO_VERSION,
1022
+                check_major_version,
1023
+                check_minor_version,
1024
+                check_micro_version);
1025
+        exit(EXIT_FAILURE);
1026 1026
     }
1027 1027
 }
1028 1028
 
1029 1029
 int main(void)
1030 1030
 {
1031 1031
     int nf;
1032
-    Suite *s;
1033
-    SRunner *sr;
1032
+    Suite* s;
1033
+    SRunner* sr;
1034 1034
 
1035 1035
     cl_initialize_crypto();
1036 1036
 
1037
-    fpu_words  = get_fpu_endian();
1038
-  
1037
+    fpu_words = get_fpu_endian();
1038
+
1039 1039
     check_version_compatible();
1040
-    s = test_cl_suite();
1040
+    s  = test_cl_suite();
1041 1041
     sr = srunner_create(s);
1042 1042
 #ifdef CHECK_HAVE_LOOPS
1043 1043
     srunner_add_suite(sr, test_cli_suite());
... ...
@@ -1053,17 +1149,16 @@ int main(void)
1053 1053
     srunner_add_suite(sr, test_htmlnorm_suite());
1054 1054
     srunner_add_suite(sr, test_bytecode_suite());
1055 1055
 
1056
-
1057 1056
     srunner_set_log(sr, "test.log");
1058
-    if(freopen("test-stderr.log","w+",stderr) == NULL) {
1059
-	    fputs("Unable to redirect stderr!\n",stderr);
1057
+    if(freopen("test-stderr.log", "w+", stderr) == NULL) {
1058
+        fputs("Unable to redirect stderr!\n", stderr);
1060 1059
     }
1061 1060
     cl_debug();
1062 1061
 
1063 1062
     srunner_run_all(sr, CK_NORMAL);
1064 1063
     nf = srunner_ntests_failed(sr);
1065
-    if (nf)
1066
-	printf("NOTICE: Use the 'T' environment variable to adjust testcase timeout\n");
1064
+    if(nf)
1065
+        printf("NOTICE: Use the 'T' environment variable to adjust testcase timeout\n");
1067 1066
     srunner_free(sr);
1068 1067
 
1069 1068
 #if HAVE_LIBXML2
... ...
@@ -361,6 +361,12 @@
361 361
 /* Define to 1 if you have the `strlcpy' function. */
362 362
 /* #undef HAVE_STRLCPY */
363 363
 
364
+/* Define to 1 if you have the `strndup' function. */
365
+/* #undef HAVE_STRNDUP */
366
+
367
+/* Define to 1 if you have the `strnstr' function. */
368
+/* #undef HAVE_STRNSTR */
369
+
364 370
 /* Define to 1 if sysconf(_SC_PAGESIZE) is available */
365 371
 /* #undef HAVE_SYSCONF_SC_PAGESIZE */
366 372
 
... ...
@@ -135,6 +135,8 @@ my %CONF = (
135 135
     'HAVE_STRING_H' => '1',
136 136
     'HAVE_STRLCAT' => -1,
137 137
     'HAVE_STRLCPY' => -1,
138
+    'HAVE_STRNDUP' => -1,
139
+    'HAVE_STRNSTR' => -1,
138 140
     'HAVE_SYSCONF_SC_PAGESIZE' => -1,
139 141
     'HAVE_SYSTEM_TOMMATH' => -1,
140 142
     'HAVE_SYS_DL_H' => -1,