xref: /aosp_15_r20/external/lz4/programs/util.h (revision 27162e4e17433d5aa7cb38e7b6a433a09405fc7f)
1 /*
2     util.h - utility functions
3     Copyright (C) 2016-2023, 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, fileno */
37 #include <assert.h>
38 #include <sys/types.h>    /* stat, utime */
39 #include <sys/stat.h>     /* stat */
40 #if defined(_WIN32)
41 #  include <sys/utime.h>  /* utime */
42 #  include <io.h>         /* _chmod */
43 #else
44 #  include <unistd.h>     /* chown, stat */
45 # if PLATFORM_POSIX_VERSION < 200809L
46 #  include <utime.h>      /* utime */
47 # else
48 #  include <fcntl.h>      /* AT_FDCWD */
49 #  include <sys/stat.h>   /* for utimensat */
50 # endif
51 #endif
52 #include <time.h>         /* time */
53 #include <limits.h>       /* INT_MAX */
54 #include <errno.h>
55 
56 
57 
58 /*-**************************************************************
59 *  Basic Types
60 *****************************************************************/
61 #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
62 # include <stdint.h>
63   typedef  uint8_t BYTE;
64   typedef uint16_t U16;
65   typedef  int16_t S16;
66   typedef uint32_t U32;
67   typedef  int32_t S32;
68   typedef uint64_t U64;
69   typedef  int64_t S64;
70 #else
71   typedef unsigned char       BYTE;
72   typedef unsigned short      U16;
73   typedef   signed short      S16;
74   typedef unsigned int        U32;
75   typedef   signed int        S32;
76   typedef unsigned long long  U64;
77   typedef   signed long long  S64;
78 #endif
79 
80 
81 /* ************************************************************
82 * Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
83 ***************************************************************/
84 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
85 #   define UTIL_fseek _fseeki64
86 #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
87 #  define UTIL_fseek fseeko
88 #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
89 #   define UTIL_fseek fseeko64
90 #else
91 #   define UTIL_fseek fseek
92 #endif
93 
94 /*-****************************************
95 *  Local host Core counting
96 ******************************************/
97 int UTIL_countCores(void);
98 
99 /*-****************************************
100 *  Sleep functions: Windows - Posix - others
101 ******************************************/
102 #if defined(_WIN32)
103 #  include <windows.h>
104 #  define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
105 #  define UTIL_sleep(s) Sleep(1000*s)
106 #  define UTIL_sleepMilli(milli) Sleep(milli)
107 #elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
108 #  include <unistd.h>
109 #  include <sys/resource.h> /* setpriority */
110 #  include <time.h>         /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
111 #  if defined(PRIO_PROCESS)
112 #    define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
113 #  else
114 #    define SET_REALTIME_PRIORITY /* disabled */
115 #  endif
116 #  define UTIL_sleep(s) sleep(s)
117 #  if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L)  /* nanosleep requires POSIX.1-2001 */
118 #      define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
119 #  else
120 #      define UTIL_sleepMilli(milli) /* disabled */
121 #  endif
122 #else
123 #  define SET_REALTIME_PRIORITY      /* disabled */
124 #  define UTIL_sleep(s)          /* disabled */
125 #  define UTIL_sleepMilli(milli) /* disabled */
126 #endif
127 
128 
129 /*-****************************************
130 *  stat() functions
131 ******************************************/
132 #if defined(_MSC_VER)
133 #  define UTIL_TYPE_stat __stat64
134 #  define UTIL_stat _stat64
135 #  define UTIL_fstat _fstat64
136 #  define UTIL_STAT_MODE_ISREG(st_mode) ((st_mode) & S_IFREG)
137 #elif   defined(__MINGW32__) && defined (__MSVCRT__)
138 #  define UTIL_TYPE_stat _stati64
139 #  define UTIL_stat _stati64
140 #  define UTIL_fstat _fstati64
141 #  define UTIL_STAT_MODE_ISREG(st_mode) ((st_mode) & S_IFREG)
142 #else
143 #  define UTIL_TYPE_stat stat
144 #  define UTIL_stat stat
145 #  define UTIL_fstat fstat
146 #  define UTIL_STAT_MODE_ISREG(st_mode) (S_ISREG(st_mode))
147 #endif
148 
149 
150 /*-****************************************
151 *  fileno() function
152 ******************************************/
153 #if defined(_MSC_VER)
154 #  define UTIL_fileno _fileno
155 #else
156 #  define UTIL_fileno fileno
157 #endif
158 
159 /* *************************************
160 *  Constants
161 ***************************************/
162 #define LIST_SIZE_INCREASE   (8*1024)
163 
164 
165 /*-****************************************
166 *  Compiler specifics
167 ******************************************/
168 #if defined(__INTEL_COMPILER)
169 #  pragma warning(disable : 177)    /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
170 #endif
171 #if defined(__GNUC__)
172 #  define UTIL_STATIC static __attribute__((unused))
173 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
174 #  define UTIL_STATIC static inline
175 #elif defined(_MSC_VER)
176 #  define UTIL_STATIC static __inline
177 #else
178 #  define UTIL_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
179 #endif
180 
181 
182 
183 /*-****************************************
184 *  Allocation functions
185 ******************************************/
186 /*
187  * A modified version of realloc().
188  * If UTIL_realloc() fails the original block is freed.
189 */
UTIL_realloc(void * ptr,size_t size)190 UTIL_STATIC void* UTIL_realloc(void* ptr, size_t size)
191 {
192     void* const newptr = realloc(ptr, size);
193     if (newptr) return newptr;
194     free(ptr);
195     return NULL;
196 }
197 
198 
199 /*-****************************************
200 *  String functions
201 ******************************************/
202 /* supports a==NULL or b==NULL */
UTIL_sameString(const char * a,const char * b)203 UTIL_STATIC int UTIL_sameString(const char* a, const char* b)
204 {
205     assert(a != NULL || b != NULL);  /* unsupported scenario */
206     if (a==NULL) return 0;
207     if (b==NULL) return 0;
208     return !strcmp(a,b);
209 }
210 
211 
212 
213 /*-****************************************
214 *  File functions
215 ******************************************/
216 #if defined(_MSC_VER)
217     #define chmod _chmod
218     typedef struct __stat64 stat_t;
219 #else
220     typedef struct stat stat_t;
221 #endif
222 
223 
224 UTIL_STATIC int UTIL_isRegFile(const char* infilename);
225 UTIL_STATIC int UTIL_isRegFD(int fd);
226 
227 
UTIL_setFileStat(const char * filename,stat_t * statbuf)228 UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
229 {
230     int res = 0;
231 
232     if (!UTIL_isRegFile(filename))
233         return -1;
234 
235     {
236 #if defined(_WIN32) || (PLATFORM_POSIX_VERSION < 200809L)
237         struct utimbuf timebuf;
238         timebuf.actime = time(NULL);
239         timebuf.modtime = statbuf->st_mtime;
240         res += utime(filename, &timebuf);  /* set access and modification times */
241 #else
242         struct timespec timebuf[2];
243         memset(timebuf, 0, sizeof(timebuf));
244         timebuf[0].tv_nsec = UTIME_NOW;
245         timebuf[1].tv_sec = statbuf->st_mtime;
246         res += utimensat(AT_FDCWD, filename, timebuf, 0);  /* set access and modification times */
247 #endif
248     }
249 
250 #if !defined(_WIN32)
251     res += chown(filename, statbuf->st_uid, statbuf->st_gid);  /* Copy ownership */
252 #endif
253 
254     res += chmod(filename, statbuf->st_mode & 07777);  /* Copy file permissions */
255 
256     errno = 0;
257     return -res; /* number of errors is returned */
258 }
259 
260 
UTIL_getFDStat(int fd,stat_t * statbuf)261 UTIL_STATIC int UTIL_getFDStat(int fd, stat_t *statbuf)
262 {
263     int r;
264 #if defined(_MSC_VER)
265     r = _fstat64(fd, statbuf);
266     if (r || !(statbuf->st_mode & S_IFREG)) return 0;   /* No good... */
267 #else
268     r = fstat(fd, statbuf);
269     if (r || !S_ISREG(statbuf->st_mode)) return 0;   /* No good... */
270 #endif
271     return 1;
272 }
273 
UTIL_getFileStat(const char * infilename,stat_t * statbuf)274 UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
275 {
276     int r;
277 #if defined(_MSC_VER)
278     r = _stat64(infilename, statbuf);
279     if (r || !(statbuf->st_mode & S_IFREG)) return 0;   /* No good... */
280 #else
281     r = stat(infilename, statbuf);
282     if (r || !S_ISREG(statbuf->st_mode)) return 0;   /* No good... */
283 #endif
284     return 1;
285 }
286 
UTIL_isRegFD(int fd)287 UTIL_STATIC int UTIL_isRegFD(int fd)
288 {
289     stat_t statbuf;
290 #ifdef _WIN32
291     /* Windows runtime library always open file descriptors 0, 1 and 2 in text mode, therefore we can't use them for binary I/O */
292     if(fd < 3) return 0;
293 #endif
294     return UTIL_getFDStat(fd, &statbuf); /* Only need to know whether it is a regular file */
295 }
296 
UTIL_isRegFile(const char * infilename)297 UTIL_STATIC int UTIL_isRegFile(const char* infilename)
298 {
299     stat_t statbuf;
300     return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
301 }
302 
UTIL_isDirectory(const char * infilename)303 UTIL_STATIC int UTIL_isDirectory(const char* infilename)
304 {
305     stat_t statbuf;
306     int r;
307 #if defined(_MSC_VER)
308     r = _stat64(infilename, &statbuf);
309     if (r) return 0;
310     return (statbuf.st_mode & S_IFDIR);
311 #else
312     r = stat(infilename, &statbuf);
313     if (r) return 0;
314     return (S_ISDIR(statbuf.st_mode));
315 #endif
316 }
317 
318 
UTIL_getOpenFileSize(FILE * file)319 UTIL_STATIC U64 UTIL_getOpenFileSize(FILE* file)
320 {
321     int r;
322     int fd;
323     struct UTIL_TYPE_stat statbuf;
324 
325     fd = UTIL_fileno(file);
326     if (fd < 0) {
327         perror("fileno");
328         exit(1);
329     }
330     r = UTIL_fstat(fd, &statbuf);
331     if (r || !UTIL_STAT_MODE_ISREG(statbuf.st_mode)) return 0;   /* No good... */
332     return (U64)statbuf.st_size;
333 }
334 
335 
UTIL_getFileSize(const char * infilename)336 UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
337 {
338     int r;
339     struct UTIL_TYPE_stat statbuf;
340 
341     r = UTIL_stat(infilename, &statbuf);
342     if (r || !UTIL_STAT_MODE_ISREG(statbuf.st_mode)) return 0;   /* No good... */
343     return (U64)statbuf.st_size;
344 }
345 
346 
UTIL_getTotalFileSize(const char ** fileNamesTable,unsigned nbFiles)347 UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
348 {
349     U64 total = 0;
350     unsigned n;
351     for (n=0; n<nbFiles; n++)
352         total += UTIL_getFileSize(fileNamesTable[n]);
353     return total;
354 }
355 
356 
357 #ifdef _WIN32
358 #  define UTIL_HAS_CREATEFILELIST
359 
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd)360 UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd)
361 {
362     char* path;
363     size_t dirLength, nbFiles = 0;
364     WIN32_FIND_DATAA cFile;
365     HANDLE hFile;
366 
367     dirLength = strlen(dirName);
368     path = (char*) malloc(dirLength + 3);
369     if (!path) return 0;
370 
371     memcpy(path, dirName, dirLength);
372     path[dirLength] = '\\';
373     path[dirLength+1] = '*';
374     path[dirLength+2] = 0;
375 
376     hFile=FindFirstFileA(path, &cFile);
377     if (hFile == INVALID_HANDLE_VALUE) {
378         fprintf(stderr, "Cannot open directory '%s'\n", dirName);
379         return 0;
380     }
381     free(path);
382 
383     do {
384         size_t pathLength;
385         int const fnameLength = (int)strlen(cFile.cFileName);
386         path = (char*) malloc(dirLength + fnameLength + 2);
387         if (!path) { FindClose(hFile); return 0; }
388         memcpy(path, dirName, dirLength);
389         path[dirLength] = '\\';
390         memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
391         pathLength = dirLength+1+fnameLength;
392         path[pathLength] = 0;
393         if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
394             if (strcmp (cFile.cFileName, "..") == 0 ||
395                 strcmp (cFile.cFileName, ".") == 0) continue;
396 
397             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd);  /* Recursively call "UTIL_prepareFileList" with the new path. */
398             if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
399         }
400         else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
401             if (*bufStart + *pos + pathLength >= *bufEnd) {
402                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
403                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
404                 *bufEnd = *bufStart + newListSize;
405                 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
406             }
407             if (*bufStart + *pos + pathLength < *bufEnd) {
408                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
409                 *pos += pathLength + 1;
410                 nbFiles++;
411             }
412         }
413         free(path);
414     } while (FindNextFileA(hFile, &cFile));
415 
416     FindClose(hFile);
417     assert(nbFiles < INT_MAX);
418     return (int)nbFiles;
419 }
420 
421 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
422 #  define UTIL_HAS_CREATEFILELIST
423 #  include <dirent.h>       /* opendir, readdir */
424 #  include <string.h>       /* strerror, memcpy */
425 
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd)426 UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd)
427 {
428     DIR* dir;
429     struct dirent * entry;
430     size_t dirLength;
431     int nbFiles = 0;
432 
433     if (!(dir = opendir(dirName))) {
434         fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
435         return 0;
436     }
437 
438     dirLength = strlen(dirName);
439     errno = 0;
440     while ((entry = readdir(dir)) != NULL) {
441         char* path;
442         size_t fnameLength, pathLength;
443         if (strcmp (entry->d_name, "..") == 0 ||
444             strcmp (entry->d_name, ".") == 0) continue;
445         fnameLength = strlen(entry->d_name);
446         path = (char*)malloc(dirLength + fnameLength + 2);
447         if (!path) { closedir(dir); return 0; }
448         memcpy(path, dirName, dirLength);
449         path[dirLength] = '/';
450         memcpy(path+dirLength+1, entry->d_name, fnameLength);
451         pathLength = dirLength+1+fnameLength;
452         path[pathLength] = 0;
453 
454         if (UTIL_isDirectory(path)) {
455             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd);  /* Recursively call "UTIL_prepareFileList" with the new path. */
456             if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
457         } else {
458             if (*bufStart + *pos + pathLength >= *bufEnd) {
459                 size_t const newListSize = (size_t)(*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
460                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
461                 *bufEnd = *bufStart + newListSize;
462                 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
463             }
464             if (*bufStart + *pos + pathLength < *bufEnd) {
465                 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
466                 *pos += pathLength + 1;
467                 nbFiles++;
468             }
469         }
470         free(path);
471         errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
472     }
473 
474     if (errno != 0) {
475         fprintf(stderr, "readdir(%s) error: %s\n", dirName, strerror(errno));
476         free(*bufStart);
477         *bufStart = NULL;
478     }
479     closedir(dir);
480     return nbFiles;
481 }
482 
483 #else
484 
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd)485 UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd)
486 {
487     (void)bufStart; (void)bufEnd; (void)pos;
488     fprintf(stderr, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
489     return 0;
490 }
491 
492 #endif /* #ifdef _WIN32 */
493 
494 /*
495  * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
496  *                       and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
497  * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
498  * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
499  */
500 UTIL_STATIC const char**
UTIL_createFileList(const char ** inputNames,unsigned inputNamesNb,char ** allocatedBuffer,unsigned * allocatedNamesNb)501 UTIL_createFileList(const char** inputNames, unsigned inputNamesNb,
502                     char** allocatedBuffer, unsigned* allocatedNamesNb)
503 {
504     size_t pos;
505     unsigned i, nbFiles;
506     char* buf = (char*)malloc(LIST_SIZE_INCREASE);
507     size_t bufSize = LIST_SIZE_INCREASE;
508     const char** fileTable;
509 
510     if (!buf) return NULL;
511 
512     for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
513         if (!UTIL_isDirectory(inputNames[i])) {
514             size_t const len = strlen(inputNames[i]) + 1;  /* include nul char */
515             if (pos + len >= bufSize) {
516                 while (pos + len >= bufSize) bufSize += LIST_SIZE_INCREASE;
517                 buf = (char*)UTIL_realloc(buf, bufSize);
518                 if (!buf) return NULL;
519             }
520             assert(pos + len < bufSize);
521             memcpy(buf + pos, inputNames[i], len);
522             pos += len;
523             nbFiles++;
524         } else {
525             char* bufend = buf + bufSize;
526             nbFiles += (unsigned)UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend);
527             if (buf == NULL) return NULL;
528             assert(bufend > buf);
529             bufSize = (size_t)(bufend - buf);
530     }   }
531 
532     if (nbFiles == 0) { free(buf); return NULL; }
533 
534     fileTable = (const char**)malloc(((size_t)nbFiles+1) * sizeof(const char*));
535     if (!fileTable) { free(buf); return NULL; }
536 
537     for (i=0, pos=0; i<nbFiles; i++) {
538         fileTable[i] = buf + pos;
539         pos += strlen(fileTable[i]) + 1;
540     }
541 
542     if (pos > bufSize) {
543         free(buf);
544         free((void*)fileTable);
545         return NULL;
546     }   /* can this happen ? */
547 
548     *allocatedBuffer = buf;
549     *allocatedNamesNb = nbFiles;
550 
551     return fileTable;
552 }
553 
554 
555 UTIL_STATIC void
UTIL_freeFileList(const char ** filenameTable,char * allocatedBuffer)556 UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
557 {
558     free(allocatedBuffer);
559     free((void*)filenameTable);
560 }
561 
562 
563 #if defined (__cplusplus)
564 }
565 #endif
566 
567 #endif /* UTIL_H_MODULE */
568