2018-12-07 03:42:19 +00:00
|
|
|
/*
|
2020-03-26 22:19:05 +00:00
|
|
|
* Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
|
2018-12-07 03:42:19 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2018-12-15 02:12:05 +00:00
|
|
|
* This header file has common utility functions used in examples.
|
2018-12-07 03:42:19 +00:00
|
|
|
*/
|
2019-04-06 01:11:17 +00:00
|
|
|
#ifndef COMMON_H
|
|
|
|
#define COMMON_H
|
2018-12-07 03:42:19 +00:00
|
|
|
|
|
|
|
#include <stdlib.h> // malloc, free, exit
|
|
|
|
#include <stdio.h> // fprintf, perror, fopen, etc.
|
2019-04-06 01:11:17 +00:00
|
|
|
#include <string.h> // strerror
|
2018-12-07 03:42:19 +00:00
|
|
|
#include <errno.h> // errno
|
|
|
|
#include <sys/stat.h> // stat
|
2019-03-22 21:57:23 +00:00
|
|
|
#include <zstd.h>
|
2018-12-07 03:42:19 +00:00
|
|
|
|
|
|
|
/*
|
2018-12-15 02:12:05 +00:00
|
|
|
* Define the returned error code from utility functions.
|
2018-12-07 03:42:19 +00:00
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
ERROR_fsize = 1,
|
|
|
|
ERROR_fopen = 2,
|
|
|
|
ERROR_fclose = 3,
|
|
|
|
ERROR_fread = 4,
|
|
|
|
ERROR_fwrite = 5,
|
|
|
|
ERROR_loadFile = 6,
|
|
|
|
ERROR_saveFile = 7,
|
|
|
|
ERROR_malloc = 8,
|
|
|
|
ERROR_largeFile = 9,
|
2019-04-06 01:11:17 +00:00
|
|
|
} COMMON_ErrorCode;
|
|
|
|
|
|
|
|
/*! CHECK
|
|
|
|
* Check that the condition holds. If it doesn't print a message and die.
|
|
|
|
*/
|
|
|
|
#define CHECK(cond, ...) \
|
|
|
|
do { \
|
|
|
|
if (!(cond)) { \
|
|
|
|
fprintf(stderr, \
|
|
|
|
"%s:%d CHECK(%s) failed: ", \
|
|
|
|
__FILE__, \
|
|
|
|
__LINE__, \
|
|
|
|
#cond); \
|
|
|
|
fprintf(stderr, "" __VA_ARGS__); \
|
|
|
|
fprintf(stderr, "\n"); \
|
|
|
|
exit(1); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*! CHECK_ZSTD
|
|
|
|
* Check the zstd error code and die if an error occurred after printing a
|
|
|
|
* message.
|
|
|
|
*/
|
|
|
|
#define CHECK_ZSTD(fn, ...) \
|
|
|
|
do { \
|
|
|
|
size_t const err = (fn); \
|
|
|
|
CHECK(!ZSTD_isError(err), "%s", ZSTD_getErrorName(err)); \
|
|
|
|
} while (0)
|
2018-12-07 03:42:19 +00:00
|
|
|
|
2018-12-15 02:12:05 +00:00
|
|
|
/*! fsize_orDie() :
|
2018-12-07 03:42:19 +00:00
|
|
|
* Get the size of a given file path.
|
2018-12-15 02:12:05 +00:00
|
|
|
*
|
2018-12-07 03:42:19 +00:00
|
|
|
* @return The size of a given file path.
|
|
|
|
*/
|
2018-12-15 02:12:05 +00:00
|
|
|
static size_t fsize_orDie(const char *filename)
|
2018-12-07 03:42:19 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
2018-12-15 02:12:05 +00:00
|
|
|
if (stat(filename, &st) != 0) {
|
|
|
|
/* error */
|
|
|
|
perror(filename);
|
|
|
|
exit(ERROR_fsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t const fileSize = st.st_size;
|
|
|
|
size_t const size = (size_t)fileSize;
|
2018-12-17 04:45:43 +00:00
|
|
|
/* 1. fileSize should be non-negative,
|
|
|
|
* 2. if off_t -> size_t type conversion results in discrepancy,
|
|
|
|
* the file size is too large for type size_t.
|
2018-12-15 02:12:05 +00:00
|
|
|
*/
|
2019-01-20 07:38:20 +00:00
|
|
|
if ((fileSize < 0) || (fileSize != (off_t)size)) {
|
2018-12-15 02:12:05 +00:00
|
|
|
fprintf(stderr, "%s : filesize too large \n", filename);
|
|
|
|
exit(ERROR_largeFile);
|
|
|
|
}
|
|
|
|
return size;
|
2018-12-07 03:42:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-15 02:12:05 +00:00
|
|
|
/*! fopen_orDie() :
|
2018-12-07 03:42:19 +00:00
|
|
|
* Open a file using given file path and open option.
|
|
|
|
*
|
|
|
|
* @return If successful this function will return a FILE pointer to an
|
|
|
|
* opened file otherwise it sends an error to stderr and exits.
|
|
|
|
*/
|
|
|
|
static FILE* fopen_orDie(const char *filename, const char *instruction)
|
|
|
|
{
|
|
|
|
FILE* const inFile = fopen(filename, instruction);
|
|
|
|
if (inFile) return inFile;
|
|
|
|
/* error */
|
|
|
|
perror(filename);
|
|
|
|
exit(ERROR_fopen);
|
|
|
|
}
|
|
|
|
|
2018-12-15 02:12:05 +00:00
|
|
|
/*! fclose_orDie() :
|
2018-12-07 03:42:19 +00:00
|
|
|
* Close an opened file using given FILE pointer.
|
|
|
|
*/
|
|
|
|
static void fclose_orDie(FILE* file)
|
|
|
|
{
|
|
|
|
if (!fclose(file)) { return; };
|
|
|
|
/* error */
|
|
|
|
perror("fclose");
|
|
|
|
exit(ERROR_fclose);
|
|
|
|
}
|
|
|
|
|
2018-12-15 02:12:05 +00:00
|
|
|
/*! fread_orDie() :
|
|
|
|
*
|
2018-12-07 03:42:19 +00:00
|
|
|
* Read sizeToRead bytes from a given file, storing them at the
|
|
|
|
* location given by buffer.
|
2018-12-15 02:12:05 +00:00
|
|
|
*
|
2018-12-07 03:42:19 +00:00
|
|
|
* @return The number of bytes read.
|
|
|
|
*/
|
|
|
|
static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
|
|
|
|
{
|
|
|
|
size_t const readSize = fread(buffer, 1, sizeToRead, file);
|
|
|
|
if (readSize == sizeToRead) return readSize; /* good */
|
|
|
|
if (feof(file)) return readSize; /* good, reached end of file */
|
|
|
|
/* error */
|
|
|
|
perror("fread");
|
|
|
|
exit(ERROR_fread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! fwrite_orDie() :
|
2018-12-15 02:12:05 +00:00
|
|
|
*
|
2018-12-07 03:42:19 +00:00
|
|
|
* Write sizeToWrite bytes to a file pointed to by file, obtaining
|
|
|
|
* them from a location given by buffer.
|
|
|
|
*
|
|
|
|
* Note: This function will send an error to stderr and exit if it
|
2018-12-15 02:12:05 +00:00
|
|
|
* cannot write data to the given file pointer.
|
2018-12-07 03:42:19 +00:00
|
|
|
*
|
|
|
|
* @return The number of bytes written.
|
|
|
|
*/
|
|
|
|
static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
|
|
|
|
{
|
|
|
|
size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
|
|
|
|
if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
|
|
|
|
/* error */
|
|
|
|
perror("fwrite");
|
|
|
|
exit(ERROR_fwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! malloc_orDie() :
|
|
|
|
* Allocate memory.
|
2018-12-15 02:12:05 +00:00
|
|
|
*
|
2018-12-07 03:42:19 +00:00
|
|
|
* @return If successful this function returns a pointer to allo-
|
|
|
|
* cated memory. If there is an error, this function will send that
|
|
|
|
* error to stderr and exit.
|
|
|
|
*/
|
|
|
|
static void* malloc_orDie(size_t size)
|
|
|
|
{
|
|
|
|
void* const buff = malloc(size);
|
|
|
|
if (buff) return buff;
|
|
|
|
/* error */
|
|
|
|
perror("malloc");
|
|
|
|
exit(ERROR_malloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! loadFile_orDie() :
|
2018-12-18 00:54:55 +00:00
|
|
|
* load file into buffer (memory).
|
2018-12-15 02:12:05 +00:00
|
|
|
*
|
2018-12-07 03:42:19 +00:00
|
|
|
* Note: This function will send an error to stderr and exit if it
|
|
|
|
* cannot read data from the given file path.
|
2018-12-15 02:12:05 +00:00
|
|
|
*
|
2018-12-18 00:54:55 +00:00
|
|
|
* @return If successful this function will load file into buffer and
|
|
|
|
* return file size, otherwise it will printout an error to stderr and exit.
|
2018-12-07 03:42:19 +00:00
|
|
|
*/
|
2019-01-20 07:38:20 +00:00
|
|
|
static size_t loadFile_orDie(const char* fileName, void* buffer, size_t bufferSize)
|
2018-12-07 03:42:19 +00:00
|
|
|
{
|
2018-12-15 02:12:05 +00:00
|
|
|
size_t const fileSize = fsize_orDie(fileName);
|
2019-04-06 01:11:17 +00:00
|
|
|
CHECK(fileSize <= bufferSize, "File too large!");
|
2018-12-18 00:54:55 +00:00
|
|
|
|
2018-12-07 03:42:19 +00:00
|
|
|
FILE* const inFile = fopen_orDie(fileName, "rb");
|
2018-12-15 02:12:05 +00:00
|
|
|
size_t const readSize = fread(buffer, 1, fileSize, inFile);
|
|
|
|
if (readSize != (size_t)fileSize) {
|
2018-12-07 03:42:19 +00:00
|
|
|
fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
|
|
|
|
exit(ERROR_fread);
|
|
|
|
}
|
|
|
|
fclose(inFile); /* can't fail, read only */
|
2018-12-18 00:54:55 +00:00
|
|
|
return fileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! mallocAndLoadFile_orDie() :
|
|
|
|
* allocate memory buffer and then load file into it.
|
|
|
|
*
|
|
|
|
* Note: This function will send an error to stderr and exit if memory allocation
|
|
|
|
* fails or it cannot read data from the given file path.
|
|
|
|
*
|
|
|
|
* @return If successful this function will return buffer and bufferSize(=fileSize),
|
|
|
|
* otherwise it will printout an error to stderr and exit.
|
|
|
|
*/
|
|
|
|
static void* mallocAndLoadFile_orDie(const char* fileName, size_t* bufferSize) {
|
|
|
|
size_t const fileSize = fsize_orDie(fileName);
|
|
|
|
*bufferSize = fileSize;
|
|
|
|
void* const buffer = malloc_orDie(*bufferSize);
|
|
|
|
loadFile_orDie(fileName, buffer, *bufferSize);
|
2018-12-07 03:42:19 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! saveFile_orDie() :
|
2018-12-15 02:12:05 +00:00
|
|
|
*
|
2018-12-07 03:42:19 +00:00
|
|
|
* Save buffSize bytes to a given file path, obtaining them from a location pointed
|
|
|
|
* to by buff.
|
2018-12-15 02:12:05 +00:00
|
|
|
*
|
2018-12-07 03:42:19 +00:00
|
|
|
* Note: This function will send an error to stderr and exit if it
|
|
|
|
* cannot write to a given file.
|
|
|
|
*/
|
|
|
|
static void saveFile_orDie(const char* fileName, const void* buff, size_t buffSize)
|
|
|
|
{
|
|
|
|
FILE* const oFile = fopen_orDie(fileName, "wb");
|
|
|
|
size_t const wSize = fwrite(buff, 1, buffSize, oFile);
|
|
|
|
if (wSize != (size_t)buffSize) {
|
|
|
|
fprintf(stderr, "fwrite: %s : %s \n", fileName, strerror(errno));
|
|
|
|
exit(ERROR_fwrite);
|
|
|
|
}
|
|
|
|
if (fclose(oFile)) {
|
|
|
|
perror(fileName);
|
|
|
|
exit(ERROR_fclose);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|