• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     util.h - utility functions
3     Copyright (C) 2016-present, Przemyslaw Skibinski, Yann Collet
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef UTIL_H_MODULE
21 #define UTIL_H_MODULE
22 
23 #if defined (__cplusplus)
24 extern "C" {
25 #endif
26 
27 
28 
29 /*-****************************************
30 *  Dependencies
31 ******************************************/
32 #include "platform.h"     /* PLATFORM_POSIX_VERSION */
33 #include <stddef.h>       /* size_t, ptrdiff_t */
34 #include <stdlib.h>       /* malloc */
35 #include <string.h>       /* strlen, strncpy */
36 #include <stdio.h>        /* fprintf */
37 #include <assert.h>
38 #include <sys/types.h>    /* stat, utime */
39 #include <sys/stat.h>     /* stat */
40 #if defined(_MSC_VER)
41 #  include <sys/utime.h>  /* utime */
42 #  include <io.h>         /* _chmod */
43 #else
44 #  include <unistd.h>     /* chown, stat */
45 #  include <utime.h>      /* utime */
46 #endif
47 #include <time.h>         /* time */
48 #include <limits.h>       /* INT_MAX */
49 #include <errno.h>
50 
51 
52 
53 /*-**************************************************************
54 *  Basic Types
55 *****************************************************************/
56 #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
57 # include <stdint.h>
58   typedef  uint8_t BYTE;
59   typedef uint16_t U16;
60   typedef  int16_t S16;
61   typedef uint32_t U32;
62   typedef  int32_t S32;
63   typedef uint64_t U64;
64   typedef  int64_t S64;
65 #else
66   typedef unsigned char       BYTE;
67   typedef unsigned short      U16;
68   typedef   signed short      S16;
69   typedef unsigned int        U32;
70   typedef   signed int        S32;
71   typedef unsigned long long  U64;
72   typedef   signed long long  S64;
73 #endif
74 
75 
76 /* ************************************************************
77 * Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
78 ***************************************************************/
79 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
80 #   define UTIL_fseek _fseeki64
81 #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
82 #  define UTIL_fseek fseeko
83 #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
84 #   define UTIL_fseek fseeko64
85 #else
86 #   define UTIL_fseek fseek
87 #endif
88 
89 
90 /*-****************************************
91 *  Sleep functions: Windows - Posix - others
92 ******************************************/
93 #if defined(_WIN32)
94 #  include <windows.h>
95 #  define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
96 #  define UTIL_sleep(s) Sleep(1000*s)
97 #  define UTIL_sleepMilli(milli) Sleep(milli)
98 #elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
99 #  include <unistd.h>
100 #  include <sys/resource.h> /* setpriority */
101 #  include <time.h>         /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
102 #  if defined(PRIO_PROCESS)
103 #    define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
104 #  else
105 #    define SET_REALTIME_PRIORITY /* disabled */
106 #  endif
107 #  define UTIL_sleep(s) sleep(s)
108 #  if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L)  /* nanosleep requires POSIX.1-2001 */
109 #      define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
110 #  else
111 #      define UTIL_sleepMilli(milli) /* disabled */
112 #  endif
113 #else
114 #  define SET_REALTIME_PRIORITY      /* disabled */
115 #  define UTIL_sleep(s)          /* disabled */
116 #  define UTIL_sleepMilli(milli) /* disabled */
117 #endif
118 
119 
120 /* *************************************
121 *  Constants
122 ***************************************/
123 #define LIST_SIZE_INCREASE   (8*1024)
124 
125 
126 /*-****************************************
127 *  Compiler specifics
128 ******************************************/
129 #if defined(__INTEL_COMPILER)
130 #  pragma warning(disable : 177)    /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
131 #endif
132 #if defined(__GNUC__)
133 #  define UTIL_STATIC static __attribute__((unused))
134 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
135 #  define UTIL_STATIC static inline
136 #elif defined(_MSC_VER)
137 #  define UTIL_STATIC static __inline
138 #else
139 #  define UTIL_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
140 #endif
141 
142 
143 /*-****************************************
144 *  Time functions
145 ******************************************/
146 #if defined(_WIN32)   /* Windows */
147 
148     typedef LARGE_INTEGER UTIL_time_t;
UTIL_getTime(void)149     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
UTIL_getSpanTimeMicro(UTIL_time_t clockStart,UTIL_time_t clockEnd)150     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
151     {
152         static LARGE_INTEGER ticksPerSecond;
153         static int init = 0;
154         if (!init) {
155             if (!QueryPerformanceFrequency(&ticksPerSecond))
156                 fprintf(stderr, "ERROR: QueryPerformanceFrequency() failure\n");
157             init = 1;
158         }
159         return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
160     }
UTIL_getSpanTimeNano(UTIL_time_t clockStart,UTIL_time_t clockEnd)161     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
162     {
163         static LARGE_INTEGER ticksPerSecond;
164         static int init = 0;
165         if (!init) {
166             if (!QueryPerformanceFrequency(&ticksPerSecond))
167                 fprintf(stderr, "ERROR: QueryPerformanceFrequency() failure\n");
168             init = 1;
169         }
170         return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
171     }
172 
173 #elif defined(__APPLE__) && defined(__MACH__)
174 
175     #include <mach/mach_time.h>
176     typedef U64 UTIL_time_t;
UTIL_getTime(void)177     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
UTIL_getSpanTimeMicro(UTIL_time_t clockStart,UTIL_time_t clockEnd)178     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
179     {
180         static mach_timebase_info_data_t rate;
181         static int init = 0;
182         if (!init) {
183             mach_timebase_info(&rate);
184             init = 1;
185         }
186         return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom)) / 1000ULL;
187     }
UTIL_getSpanTimeNano(UTIL_time_t clockStart,UTIL_time_t clockEnd)188     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
189     {
190         static mach_timebase_info_data_t rate;
191         static int init = 0;
192         if (!init) {
193             mach_timebase_info(&rate);
194             init = 1;
195         }
196         return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom);
197     }
198 
199 #elif (PLATFORM_POSIX_VERSION >= 200112L) && (defined __UCLIBC__ || (defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17) || __GLIBC__ > 2) ) )
200 
201     #include <time.h>
202     typedef struct timespec UTIL_time_t;
UTIL_getTime(void)203     UTIL_STATIC UTIL_time_t UTIL_getTime(void)
204     {
205         UTIL_time_t now;
206         if (clock_gettime(CLOCK_MONOTONIC, &now))
207             fprintf(stderr, "ERROR: Failed to get time\n");   /* we could also exit() */
208         return now;
209     }
UTIL_getSpanTime(UTIL_time_t begin,UTIL_time_t end)210     UTIL_STATIC UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
211     {
212         UTIL_time_t diff;
213         if (end.tv_nsec < begin.tv_nsec) {
214             diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;
215             diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
216         } else {
217             diff.tv_sec = end.tv_sec - begin.tv_sec;
218             diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
219         }
220         return diff;
221     }
UTIL_getSpanTimeMicro(UTIL_time_t begin,UTIL_time_t end)222     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
223     {
224         UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
225         U64 micro = 0;
226         micro += 1000000ULL * diff.tv_sec;
227         micro += diff.tv_nsec / 1000ULL;
228         return micro;
229     }
UTIL_getSpanTimeNano(UTIL_time_t begin,UTIL_time_t end)230     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
231     {
232         UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
233         U64 nano = 0;
234         nano += 1000000000ULL * diff.tv_sec;
235         nano += diff.tv_nsec;
236         return nano;
237     }
238 
239 #else   /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
240 
241     typedef clock_t UTIL_time_t;
UTIL_getTime(void)242     UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return clock(); }
UTIL_getSpanTimeMicro(UTIL_time_t clockStart,UTIL_time_t clockEnd)243     UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
UTIL_getSpanTimeNano(UTIL_time_t clockStart,UTIL_time_t clockEnd)244     UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
245 #endif
246 
247 
248 /* returns time span in microseconds */
UTIL_clockSpanMicro(UTIL_time_t clockStart)249 UTIL_STATIC U64 UTIL_clockSpanMicro(UTIL_time_t clockStart)
250 {
251     UTIL_time_t const clockEnd = UTIL_getTime();
252     return UTIL_getSpanTimeMicro(clockStart, clockEnd);
253 }
254 
255 /* returns time span in nanoseconds */
UTIL_clockSpanNano(UTIL_time_t clockStart)256 UTIL_STATIC U64 UTIL_clockSpanNano(UTIL_time_t clockStart)
257 {
258     UTIL_time_t const clockEnd = UTIL_getTime();
259     return UTIL_getSpanTimeNano(clockStart, clockEnd);
260 }
261 
UTIL_waitForNextTick(void)262 UTIL_STATIC void UTIL_waitForNextTick(void)
263 {
264     UTIL_time_t const clockStart = UTIL_getTime();
265     UTIL_time_t clockEnd;
266     do {
267         clockEnd = UTIL_getTime();
268     } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
269 }
270 
271 
272 
273 /*-****************************************
274 *  File functions
275 ******************************************/
276 #if defined(_MSC_VER)
277     #define chmod _chmod
278     typedef struct __stat64 stat_t;
279 #else
280     typedef struct stat stat_t;
281 #endif
282 
283 
284 UTIL_STATIC int UTIL_isRegFile(const char* infilename);
285 
286 
UTIL_setFileStat(const char * filename,stat_t * statbuf)287 UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
288 {
289     int res = 0;
290     struct utimbuf timebuf;
291 
292     if (!UTIL_isRegFile(filename))
293         return -1;
294 
295     timebuf.actime = time(NULL);
296     timebuf.modtime = statbuf->st_mtime;
297     res += utime(filename, &timebuf);  /* set access and modification times */
298 
299 #if !defined(_WIN32)
300     res += chown(filename, statbuf->st_uid, statbuf->st_gid);  /* Copy ownership */
301 #endif
302 
303     res += chmod(filename, statbuf->st_mode & 07777);  /* Copy file permissions */
304 
305     errno = 0;
306     return -res; /* number of errors is returned */
307 }
308 
309 
UTIL_getFileStat(const char * infilename,stat_t * statbuf)310 UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
311 {
312     int r;
313 #if defined(_MSC_VER)
314     r = _stat64(infilename, statbuf);
315     if (r || !(statbuf->st_mode & S_IFREG)) return 0;   /* No good... */
316 #else
317     r = stat(infilename, statbuf);
318     if (r || !S_ISREG(statbuf->st_mode)) return 0;   /* No good... */
319 #endif
320     return 1;
321 }
322 
323 
UTIL_isRegFile(const char * infilename)324 UTIL_STATIC int UTIL_isRegFile(const char* infilename)
325 {
326     stat_t statbuf;
327     return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
328 }
329 
330 
UTIL_isDirectory(const char * infilename)331 UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
332 {
333     int r;
334     stat_t statbuf;
335 #if defined(_MSC_VER)
336     r = _stat64(infilename, &statbuf);
337     if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
338 #else
339     r = stat(infilename, &statbuf);
340     if (!r && S_ISDIR(statbuf.st_mode)) return 1;
341 #endif
342     return 0;
343 }
344 
345 
UTIL_getFileSize(const char * infilename)346 UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
347 {
348     int r;
349 #if defined(_MSC_VER)
350     struct __stat64 statbuf;
351     r = _stat64(infilename, &statbuf);
352     if (r || !(statbuf.st_mode & S_IFREG)) return 0;   /* No good... */
353 #elif defined(__MINGW32__) && defined (__MSVCRT__)
354     struct _stati64 statbuf;
355     r = _stati64(infilename, &statbuf);
356     if (r || !(statbuf.st_mode & S_IFREG)) return 0;   /* No good... */
357 #else
358     struct stat statbuf;
359     r = stat(infilename, &statbuf);
360     if (r || !S_ISREG(statbuf.st_mode)) return 0;   /* No good... */
361 #endif
362     return (U64)statbuf.st_size;
363 }
364 
365 
UTIL_getTotalFileSize(const char ** fileNamesTable,unsigned nbFiles)366 UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
367 {
368     U64 total = 0;
369     unsigned n;
370     for (n=0; n<nbFiles; n++)
371         total += UTIL_getFileSize(fileNamesTable[n]);
372     return total;
373 }
374 
375 
376 /*
377  * A modified version of realloc().
378  * If UTIL_realloc() fails the original block is freed.
379 */
UTIL_realloc(void * ptr,size_t size)380 UTIL_STATIC void* UTIL_realloc(void* ptr, size_t size)
381 {
382     void* const newptr = realloc(ptr, size);
383     if (newptr) return newptr;
384     free(ptr);
385     return NULL;
386 }
387 
388 
389 #ifdef _WIN32
390 #  define UTIL_HAS_CREATEFILELIST
391 
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd)392 UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd)
393 {
394     char* path;
395     size_t dirLength, nbFiles = 0;
396     WIN32_FIND_DATAA cFile;
397     HANDLE hFile;
398 
399     dirLength = strlen(dirName);
400     path = (char*) malloc(dirLength + 3);
401     if (!path) return 0;
402 
403     memcpy(path, dirName, dirLength);
404     path[dirLength] = '\\';
405     path[dirLength+1] = '*';
406     path[dirLength+2] = 0;
407 
408     hFile=FindFirstFileA(path, &cFile);
409     if (hFile == INVALID_HANDLE_VALUE) {
410         fprintf(stderr, "Cannot open directory '%s'\n", dirName);
411         return 0;
412     }
413     free(path);
414 
415     do {
416         size_t pathLength;
417         int const fnameLength = (int)strlen(cFile.cFileName);
418         path = (char*) malloc(dirLength + fnameLength + 2);
419         if (!path) { FindClose(hFile); return 0; }
420         memcpy(path, dirName, dirLength);
421         path[dirLength] = '\\';
422         memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
423         pathLength = dirLength+1+fnameLength;
424         path[pathLength] = 0;
425         if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
426             if (strcmp (cFile.cFileName, "..") == 0 ||
427                 strcmp (cFile.cFileName, ".") == 0) continue;
428 
429             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd);  /* Recursively call "UTIL_prepareFileList" with the new path. */
430             if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
431         }
432         else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
433             if (*bufStart + *pos + pathLength >= *bufEnd) {
434                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
435                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
436                 *bufEnd = *bufStart + newListSize;
437                 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
438             }
439             if (*bufStart + *pos + pathLength < *bufEnd) {
440                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
441                 *pos += pathLength + 1;
442                 nbFiles++;
443             }
444         }
445         free(path);
446     } while (FindNextFileA(hFile, &cFile));
447 
448     FindClose(hFile);
449     assert(nbFiles < INT_MAX);
450     return (int)nbFiles;
451 }
452 
453 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
454 #  define UTIL_HAS_CREATEFILELIST
455 #  include <dirent.h>       /* opendir, readdir */
456 #  include <string.h>       /* strerror, memcpy */
457 
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd)458 UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd)
459 {
460     DIR* dir;
461     struct dirent * entry;
462     int dirLength, nbFiles = 0;
463 
464     if (!(dir = opendir(dirName))) {
465         fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
466         return 0;
467     }
468 
469     dirLength = (int)strlen(dirName);
470     errno = 0;
471     while ((entry = readdir(dir)) != NULL) {
472         char* path;
473         int fnameLength, pathLength;
474         if (strcmp (entry->d_name, "..") == 0 ||
475             strcmp (entry->d_name, ".") == 0) continue;
476         fnameLength = (int)strlen(entry->d_name);
477         path = (char*) malloc(dirLength + fnameLength + 2);
478         if (!path) { closedir(dir); return 0; }
479         memcpy(path, dirName, dirLength);
480         path[dirLength] = '/';
481         memcpy(path+dirLength+1, entry->d_name, fnameLength);
482         pathLength = dirLength+1+fnameLength;
483         path[pathLength] = 0;
484 
485         if (UTIL_isDirectory(path)) {
486             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd);  /* Recursively call "UTIL_prepareFileList" with the new path. */
487             if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
488         } else {
489             if (*bufStart + *pos + pathLength >= *bufEnd) {
490                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
491                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
492                 *bufEnd = *bufStart + newListSize;
493                 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
494             }
495             if (*bufStart + *pos + pathLength < *bufEnd) {
496                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
497                 *pos += pathLength + 1;
498                 nbFiles++;
499             }
500         }
501         free(path);
502         errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
503     }
504 
505     if (errno != 0) {
506         fprintf(stderr, "readdir(%s) error: %s\n", dirName, strerror(errno));
507         free(*bufStart);
508         *bufStart = NULL;
509     }
510     closedir(dir);
511     return nbFiles;
512 }
513 
514 #else
515 
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd)516 UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd)
517 {
518     (void)bufStart; (void)bufEnd; (void)pos;
519     fprintf(stderr, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
520     return 0;
521 }
522 
523 #endif /* #ifdef _WIN32 */
524 
525 /*
526  * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
527  *                       and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
528  * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
529  * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
530  */
531 UTIL_STATIC const char**
UTIL_createFileList(const char ** inputNames,unsigned inputNamesNb,char ** allocatedBuffer,unsigned * allocatedNamesNb)532 UTIL_createFileList(const char** inputNames, unsigned inputNamesNb,
533                     char** allocatedBuffer, unsigned* allocatedNamesNb)
534 {
535     size_t pos;
536     unsigned i, nbFiles;
537     char* buf = (char*)malloc(LIST_SIZE_INCREASE);
538     size_t bufSize = LIST_SIZE_INCREASE;
539     const char** fileTable;
540 
541     if (!buf) return NULL;
542 
543     for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
544         if (!UTIL_isDirectory(inputNames[i])) {
545             size_t const len = strlen(inputNames[i]);
546             if (pos + len >= bufSize) {
547                 while (pos + len >= bufSize) bufSize += LIST_SIZE_INCREASE;
548                 buf = (char*)UTIL_realloc(buf, bufSize);
549                 if (!buf) return NULL;
550             }
551             assert(pos + len < bufSize);
552             strncpy(buf + pos, inputNames[i], bufSize - pos);
553             pos += len + 1;
554             nbFiles++;
555         } else {
556             char* bufend = buf + bufSize;
557             nbFiles += (unsigned)UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend);
558             if (buf == NULL) return NULL;
559             assert(bufend > buf);
560             bufSize = (size_t)(bufend - buf);
561     }   }
562 
563     if (nbFiles == 0) { free(buf); return NULL; }
564 
565     fileTable = (const char**)malloc(((size_t)nbFiles+1) * sizeof(const char*));
566     if (!fileTable) { free(buf); return NULL; }
567 
568     for (i=0, pos=0; i<nbFiles; i++) {
569         fileTable[i] = buf + pos;
570         pos += strlen(fileTable[i]) + 1;
571     }
572 
573     if (pos > bufSize) {
574         free(buf);
575         free((void*)fileTable);
576         return NULL;
577     }   /* can this happen ? */
578 
579     *allocatedBuffer = buf;
580     *allocatedNamesNb = nbFiles;
581 
582     return fileTable;
583 }
584 
585 
586 UTIL_STATIC void
UTIL_freeFileList(const char ** filenameTable,char * allocatedBuffer)587 UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
588 {
589     if (allocatedBuffer) free(allocatedBuffer);
590     if (filenameTable) free((void*)filenameTable);
591 }
592 
593 
594 #if defined (__cplusplus)
595 }
596 #endif
597 
598 #endif /* UTIL_H_MODULE */
599