Rizin
unix-like reverse engineering framework and cli tools
util.h File Reference
#include "platform.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
#include <time.h>
#include <limits.h>
#include <errno.h>

Go to the source code of this file.

Macros

#define UTIL_fseek   fseek
 
#define SET_REALTIME_PRIORITY   /* disabled */
 
#define UTIL_sleep(s)   /* disabled */
 
#define UTIL_sleepMilli(milli)   /* disabled */
 
#define UTIL_TYPE_stat   stat
 
#define UTIL_stat   stat
 
#define UTIL_fstat   fstat
 
#define UTIL_STAT_MODE_ISREG(st_mode)   (S_ISREG(st_mode))
 
#define UTIL_fileno   fileno
 
#define LIST_SIZE_INCREASE   (8*1024)
 
#define UTIL_STATIC   static /* this version may generate warnings for unused static functions; disable the relevant warning */
 

Typedefs

typedef unsigned char BYTE
 
typedef unsigned short U16
 
typedef signed short S16
 
typedef unsigned int U32
 
typedef signed int S32
 
typedef unsigned long long U64
 
typedef signed long long S64
 
typedef clock_t UTIL_time_t
 
typedef struct stat stat_t
 

Functions

UTIL_STATIC UTIL_time_t UTIL_getTime (void)
 
UTIL_STATIC U64 UTIL_getSpanTimeMicro (UTIL_time_t clockStart, UTIL_time_t clockEnd)
 
UTIL_STATIC U64 UTIL_getSpanTimeNano (UTIL_time_t clockStart, UTIL_time_t clockEnd)
 
UTIL_STATIC U64 UTIL_clockSpanMicro (UTIL_time_t clockStart)
 
UTIL_STATIC U64 UTIL_clockSpanNano (UTIL_time_t clockStart)
 
UTIL_STATIC void UTIL_waitForNextTick (void)
 
UTIL_STATIC int UTIL_isRegFile (const char *infilename)
 
UTIL_STATIC int UTIL_setFileStat (const char *filename, stat_t *statbuf)
 
UTIL_STATIC int UTIL_getFileStat (const char *infilename, stat_t *statbuf)
 
UTIL_STATIC U32 UTIL_isDirectory (const char *infilename)
 
UTIL_STATIC U64 UTIL_getOpenFileSize (FILE *file)
 
UTIL_STATIC U64 UTIL_getFileSize (const char *infilename)
 
UTIL_STATIC U64 UTIL_getTotalFileSize (const char **fileNamesTable, unsigned nbFiles)
 
UTIL_STATIC void * UTIL_realloc (void *ptr, size_t size)
 
UTIL_STATIC int UTIL_prepareFileList (const char *dirName, char **bufStart, size_t *pos, char **bufEnd)
 
UTIL_STATIC const char ** UTIL_createFileList (const char **inputNames, unsigned inputNamesNb, char **allocatedBuffer, unsigned *allocatedNamesNb)
 
UTIL_STATIC void UTIL_freeFileList (const char **filenameTable, char *allocatedBuffer)
 

Macro Definition Documentation

◆ LIST_SIZE_INCREASE

#define LIST_SIZE_INCREASE   (8*1024)

Definition at line 158 of file util.h.

◆ SET_REALTIME_PRIORITY

#define SET_REALTIME_PRIORITY   /* disabled */

Definition at line 119 of file util.h.

◆ UTIL_fileno

#define UTIL_fileno   fileno

Definition at line 152 of file util.h.

◆ UTIL_fseek

#define UTIL_fseek   fseek

Definition at line 91 of file util.h.

◆ UTIL_fstat

#define UTIL_fstat   fstat

Definition at line 141 of file util.h.

◆ UTIL_sleep

#define UTIL_sleep (   s)    /* disabled */

Definition at line 120 of file util.h.

◆ UTIL_sleepMilli

#define UTIL_sleepMilli (   milli)    /* disabled */

Definition at line 121 of file util.h.

◆ UTIL_stat

#define UTIL_stat   stat

Definition at line 140 of file util.h.

◆ UTIL_STAT_MODE_ISREG

#define UTIL_STAT_MODE_ISREG (   st_mode)    (S_ISREG(st_mode))

Definition at line 142 of file util.h.

◆ UTIL_STATIC

#define UTIL_STATIC   static /* this version may generate warnings for unused static functions; disable the relevant warning */

Definition at line 174 of file util.h.

◆ UTIL_TYPE_stat

#define UTIL_TYPE_stat   stat

Definition at line 139 of file util.h.

Typedef Documentation

◆ BYTE

typedef unsigned char BYTE

Definition at line 71 of file util.h.

◆ S16

typedef signed short S16

Definition at line 73 of file util.h.

◆ S32

typedef signed int S32

Definition at line 75 of file util.h.

◆ S64

typedef signed long long S64

Definition at line 77 of file util.h.

◆ stat_t

typedef struct stat stat_t

Definition at line 297 of file util.h.

◆ U16

typedef unsigned short U16

Definition at line 72 of file util.h.

◆ U32

typedef unsigned int U32

Definition at line 74 of file util.h.

◆ U64

typedef unsigned long long U64

Definition at line 76 of file util.h.

◆ UTIL_time_t

Definition at line 276 of file util.h.

Function Documentation

◆ UTIL_clockSpanMicro()

UTIL_STATIC U64 UTIL_clockSpanMicro ( UTIL_time_t  clockStart)

Definition at line 284 of file util.h.

285 {
286  UTIL_time_t const clockEnd = UTIL_getTime();
287  return UTIL_getSpanTimeMicro(clockStart, clockEnd);
288 }
UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
Definition: util.h:278
UTIL_STATIC UTIL_time_t UTIL_getTime(void)
Definition: util.h:277
clock_t UTIL_time_t
Definition: util.h:276

References UTIL_getSpanTimeMicro(), and UTIL_getTime().

Referenced by BMK_benchMem().

◆ UTIL_clockSpanNano()

UTIL_STATIC U64 UTIL_clockSpanNano ( UTIL_time_t  clockStart)

Definition at line 291 of file util.h.

292 {
293  UTIL_time_t const clockEnd = UTIL_getTime();
294  return UTIL_getSpanTimeNano(clockStart, clockEnd);
295 }
UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
Definition: util.h:279

References UTIL_getSpanTimeNano(), and UTIL_getTime().

Referenced by BMK_benchMem(), and LZ4IO_compressFilename().

◆ UTIL_createFileList()

UTIL_STATIC const char** UTIL_createFileList ( const char **  inputNames,
unsigned  inputNamesNb,
char **  allocatedBuffer,
unsigned allocatedNamesNb 
)

Definition at line 584 of file util.h.

586 {
587  size_t pos;
588  unsigned i, nbFiles;
589  char* buf = (char*)malloc(LIST_SIZE_INCREASE);
590  size_t bufSize = LIST_SIZE_INCREASE;
591  const char** fileTable;
592 
593  if (!buf) return NULL;
594 
595  for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
596  if (!UTIL_isDirectory(inputNames[i])) {
597  size_t const len = strlen(inputNames[i]) + 1; /* include nul char */
598  if (pos + len >= bufSize) {
599  while (pos + len >= bufSize) bufSize += LIST_SIZE_INCREASE;
600  buf = (char*)UTIL_realloc(buf, bufSize);
601  if (!buf) return NULL;
602  }
603  assert(pos + len < bufSize);
604  memcpy(buf + pos, inputNames[i], len);
605  pos += len;
606  nbFiles++;
607  } else {
608  char* bufend = buf + bufSize;
609  nbFiles += (unsigned)UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend);
610  if (buf == NULL) return NULL;
611  assert(bufend > buf);
612  bufSize = (size_t)(bufend - buf);
613  } }
614 
615  if (nbFiles == 0) { free(buf); return NULL; }
616 
617  fileTable = (const char**)malloc(((size_t)nbFiles+1) * sizeof(const char*));
618  if (!fileTable) { free(buf); return NULL; }
619 
620  for (i=0, pos=0; i<nbFiles; i++) {
621  fileTable[i] = buf + pos;
622  pos += strlen(fileTable[i]) + 1;
623  }
624 
625  if (pos > bufSize) {
626  free(buf);
627  free((void*)fileTable);
628  return NULL;
629  } /* can this happen ? */
630 
631  *allocatedBuffer = buf;
632  *allocatedNamesNb = nbFiles;
633 
634  return fileTable;
635 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
static void struct sockaddr socklen_t static fromlen static backlog static fork char char char static envp int struct rusage static rusage struct utsname static buf struct sembuf unsigned
Definition: sflib.h:97
UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char **bufStart, size_t *pos, char **bufEnd)
Definition: util.h:568
UTIL_STATIC U32 UTIL_isDirectory(const char *infilename)
Definition: util.h:375
UTIL_STATIC void * UTIL_realloc(void *ptr, size_t size)
Definition: util.h:432
#define LIST_SIZE_INCREASE
Definition: util.h:158
assert(limit<=UINT32_MAX/2)
int size_t
Definition: sftypes.h:40
int pos
Definition: main.c:11

References assert(), free(), i, len, LIST_SIZE_INCREASE, malloc(), memcpy(), NULL, pos, unsigned, UTIL_isDirectory(), UTIL_prepareFileList(), and UTIL_realloc().

Referenced by main().

◆ UTIL_freeFileList()

UTIL_STATIC void UTIL_freeFileList ( const char **  filenameTable,
char *  allocatedBuffer 
)

Definition at line 639 of file util.h.

640 {
641  if (allocatedBuffer) free(allocatedBuffer);
642  if (filenameTable) free((void*)filenameTable);
643 }

References free().

Referenced by main().

◆ UTIL_getFileSize()

UTIL_STATIC U64 UTIL_getFileSize ( const char *  infilename)

Definition at line 407 of file util.h.

408 {
409  int r;
410  struct UTIL_TYPE_stat statbuf;
411 
412  r = UTIL_stat(infilename, &statbuf);
413  if (r || !UTIL_STAT_MODE_ISREG(statbuf.st_mode)) return 0; /* No good... */
414  return (U64)statbuf.st_size;
415 }
#define r
Definition: crypto_rc6.c:12
#define UTIL_STAT_MODE_ISREG(st_mode)
Definition: util.h:142
#define UTIL_stat
Definition: util.h:140
#define UTIL_TYPE_stat
Definition: util.h:139
unsigned long long U64
Definition: lz4.c:290

References r, UTIL_stat, UTIL_STAT_MODE_ISREG, and UTIL_TYPE_stat.

Referenced by BMK_benchFiles(), BMK_loadFiles(), fullSpeedBench(), and UTIL_getTotalFileSize().

◆ UTIL_getFileStat()

UTIL_STATIC int UTIL_getFileStat ( const char *  infilename,
stat_t statbuf 
)

Definition at line 354 of file util.h.

355 {
356  int r;
357 #if defined(_MSC_VER)
358  r = _stat64(infilename, statbuf);
359  if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */
360 #else
361  r = stat(infilename, statbuf);
362  if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */
363 #endif
364  return 1;
365 }
#define S_ISREG(mode)
Definition: compat.h:191
static stat
Definition: sflib.h:131

References r, S_ISREG, and stat.

Referenced by LZ4IO_compressFilename_extRess(), LZ4IO_decompressDstFile(), and UTIL_isRegFile().

◆ UTIL_getOpenFileSize()

UTIL_STATIC U64 UTIL_getOpenFileSize ( FILE *  file)

Definition at line 390 of file util.h.

391 {
392  int r;
393  int fd;
394  struct UTIL_TYPE_stat statbuf;
395 
396  fd = UTIL_fileno(file);
397  if (fd < 0) {
398  perror("fileno");
399  exit(1);
400  }
401  r = UTIL_fstat(fd, &statbuf);
402  if (r || !UTIL_STAT_MODE_ISREG(statbuf.st_mode)) return 0; /* No good... */
403  return (U64)statbuf.st_size;
404 }
#define UTIL_fstat
Definition: util.h:141
#define UTIL_fileno
Definition: util.h:152
Definition: gzappend.c:170
static const z80_opcode fd[]
Definition: z80_tab.h:997

References test-lz4-list::exit, fd, r, UTIL_fileno, UTIL_fstat, UTIL_STAT_MODE_ISREG, and UTIL_TYPE_stat.

Referenced by LZ4IO_compressFilename_extRess(), and LZ4IO_getCompressedFileInfo().

◆ UTIL_getSpanTimeMicro()

UTIL_STATIC U64 UTIL_getSpanTimeMicro ( UTIL_time_t  clockStart,
UTIL_time_t  clockEnd 
)

Definition at line 278 of file util.h.

278 { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }

Referenced by UTIL_clockSpanMicro().

◆ UTIL_getSpanTimeNano()

UTIL_STATIC U64 UTIL_getSpanTimeNano ( UTIL_time_t  clockStart,
UTIL_time_t  clockEnd 
)

Definition at line 279 of file util.h.

279 { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }

Referenced by UTIL_clockSpanNano().

◆ UTIL_getTime()

UTIL_STATIC UTIL_time_t UTIL_getTime ( void  )

Definition at line 277 of file util.h.

277 { return clock(); }

Referenced by BMK_benchMem(), LZ4IO_compressFilename(), UTIL_clockSpanMicro(), and UTIL_clockSpanNano().

◆ UTIL_getTotalFileSize()

UTIL_STATIC U64 UTIL_getTotalFileSize ( const char **  fileNamesTable,
unsigned  nbFiles 
)

Definition at line 418 of file util.h.

419 {
420  U64 total = 0;
421  unsigned n;
422  for (n=0; n<nbFiles; n++)
423  total += UTIL_getFileSize(fileNamesTable[n]);
424  return total;
425 }
UTIL_STATIC U64 UTIL_getFileSize(const char *infilename)
Definition: util.h:407
int n
Definition: mipsasm.c:19

References n, and UTIL_getFileSize().

Referenced by BMK_benchFileTable().

◆ UTIL_isDirectory()

UTIL_STATIC U32 UTIL_isDirectory ( const char *  infilename)

Definition at line 375 of file util.h.

376 {
377  int r;
378  stat_t statbuf;
379 #if defined(_MSC_VER)
380  r = _stat64(infilename, &statbuf);
381  if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
382 #else
383  r = stat(infilename, &statbuf);
384  if (!r && S_ISDIR(statbuf.st_mode)) return 1;
385 #endif
386  return 0;
387 }
#define S_ISDIR(mode)
Definition: compat.h:187
Definition: sftypes.h:80

References r, S_ISDIR, and stat.

Referenced by BMK_loadFiles(), and UTIL_createFileList().

◆ UTIL_isRegFile()

UTIL_STATIC int UTIL_isRegFile ( const char *  infilename)

Definition at line 368 of file util.h.

369 {
370  stat_t statbuf;
371  return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
372 }
UTIL_STATIC int UTIL_getFileStat(const char *infilename, stat_t *statbuf)
Definition: util.h:354

References UTIL_getFileStat().

Referenced by LZ4IO_displayCompressedFilesInfo(), main(), and UTIL_setFileStat().

◆ UTIL_prepareFileList()

UTIL_STATIC int UTIL_prepareFileList ( const char *  dirName,
char **  bufStart,
size_t pos,
char **  bufEnd 
)

Definition at line 568 of file util.h.

569 {
570  (void)bufStart; (void)bufEnd; (void)pos;
571  fprintf(stderr, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
572  return 0;
573 }

References pos.

Referenced by UTIL_createFileList().

◆ UTIL_realloc()

UTIL_STATIC void* UTIL_realloc ( void *  ptr,
size_t  size 
)

Definition at line 432 of file util.h.

433 {
434  void* const newptr = realloc(ptr, size);
435  if (newptr) return newptr;
436  free(ptr);
437  return NULL;
438 }
voidpf void uLong size
Definition: ioapi.h:138
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144

References free(), NULL, and realloc().

Referenced by UTIL_createFileList().

◆ UTIL_setFileStat()

UTIL_STATIC int UTIL_setFileStat ( const char *  filename,
stat_t statbuf 
)

Definition at line 322 of file util.h.

323 {
324  int res = 0;
325 
326  if (!UTIL_isRegFile(filename))
327  return -1;
328 
329  {
330 #if defined(_WIN32) || (PLATFORM_POSIX_VERSION < 200809L)
331  struct utimbuf timebuf;
332  timebuf.actime = time(NULL);
333  timebuf.modtime = statbuf->st_mtime;
334  res += utime(filename, &timebuf); /* set access and modification times */
335 #else
336  struct timespec timebuf[2] = {};
337  timebuf[0].tv_nsec = UTIME_NOW;
338  timebuf[1].tv_sec = statbuf->st_mtime;
339  res += utimensat(AT_FDCWD, filename, timebuf, 0); /* set access and modification times */
340 #endif
341  }
342 
343 #if !defined(_WIN32)
344  res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
345 #endif
346 
347  res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */
348 
349  errno = 0;
350  return -res; /* number of errors is returned */
351 }
static static fork const void static count static fd const char const char static newpath const char static path chmod
Definition: sflib.h:35
const char * filename
Definition: ioapi.h:137
static static fork const void static count static fd const char const char static newpath char char char static envp time
Definition: sflib.h:42
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds utime
Definition: sflib.h:57
static const char struct stat static buf struct stat static buf static vhangup int struct rusage static rusage struct sysinfo static info unsigned static __unused struct utsname static buf const char static size const char static name static pid unsigned static persona static fsgid const void static flags const struct iovec static count static fd const void static len static munlockall struct sched_param static p static sched_yield static policy const struct timespec struct timespec static rem uid_t uid_t uid_t static suid struct pollfd unsigned static timeout chown
Definition: sflib.h:210
UTIL_STATIC int UTIL_isRegFile(const char *infilename)
Definition: util.h:368
long tv_nsec
Definition: sftypes.h:90
time_t tv_sec
Definition: sftypes.h:89

References chmod, chown, NULL, time, UTIL_isRegFile(), and utime.

Referenced by LZ4IO_compressFilename_extRess(), and LZ4IO_decompressDstFile().

◆ UTIL_waitForNextTick()

UTIL_STATIC void UTIL_waitForNextTick ( void  )

Definition at line 297 of file util.h.

298 {
299  UTIL_time_t const clockStart = UTIL_getTime();
300  UTIL_time_t clockEnd;
301  do {
302  clockEnd = UTIL_getTime();
303  } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
304 }

Referenced by BMK_benchMem().