2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
12 * This header file has common utility functions used in examples.
17 #include <stdlib.h> // malloc, free, exit
18 #include <stdio.h> // fprintf, perror, fopen, etc.
19 #include <string.h> // strerror
20 #include <errno.h> // errno
21 #include <sys/stat.h> // stat
25 * Define the returned error code from utility functions.
40 * Check that the condition holds. If it doesn't print a message and die.
42 #define CHECK(cond, ...) \
46 "%s:%d CHECK(%s) failed: ", \
50 fprintf(stderr, "" __VA_ARGS__); \
51 fprintf(stderr, "\n"); \
57 * Check the zstd error code and die if an error occurred after printing a
60 #define CHECK_ZSTD(fn, ...) \
62 size_t const err = (fn); \
63 CHECK(!ZSTD_isError(err), "%s", ZSTD_getErrorName(err)); \
67 * Get the size of a given file path.
69 * @return The size of a given file path.
71 static size_t fsize_orDie(const char *filename)
74 if (stat(filename, &st) != 0) {
80 off_t const fileSize = st.st_size;
81 size_t const size = (size_t)fileSize;
82 /* 1. fileSize should be non-negative,
83 * 2. if off_t -> size_t type conversion results in discrepancy,
84 * the file size is too large for type size_t.
86 if ((fileSize < 0) || (fileSize != (off_t)size)) {
87 fprintf(stderr, "%s : filesize too large \n", filename);
88 exit(ERROR_largeFile);
94 * Open a file using given file path and open option.
96 * @return If successful this function will return a FILE pointer to an
97 * opened file otherwise it sends an error to stderr and exits.
99 static FILE* fopen_orDie(const char *filename, const char *instruction)
101 FILE* const inFile = fopen(filename, instruction);
102 if (inFile) return inFile;
109 * Close an opened file using given FILE pointer.
111 static void fclose_orDie(FILE* file)
113 if (!fclose(file)) { return; };
121 * Read sizeToRead bytes from a given file, storing them at the
122 * location given by buffer.
124 * @return The number of bytes read.
126 static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
128 size_t const readSize = fread(buffer, 1, sizeToRead, file);
129 if (readSize == sizeToRead) return readSize; /* good */
130 if (feof(file)) return readSize; /* good, reached end of file */
138 * Write sizeToWrite bytes to a file pointed to by file, obtaining
139 * them from a location given by buffer.
141 * Note: This function will send an error to stderr and exit if it
142 * cannot write data to the given file pointer.
144 * @return The number of bytes written.
146 static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
148 size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
149 if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
158 * @return If successful this function returns a pointer to allo-
159 * cated memory. If there is an error, this function will send that
160 * error to stderr and exit.
162 static void* malloc_orDie(size_t size)
164 void* const buff = malloc(size);
165 if (buff) return buff;
171 /*! loadFile_orDie() :
172 * load file into buffer (memory).
174 * Note: This function will send an error to stderr and exit if it
175 * cannot read data from the given file path.
177 * @return If successful this function will load file into buffer and
178 * return file size, otherwise it will printout an error to stderr and exit.
180 static size_t loadFile_orDie(const char* fileName, void* buffer, size_t bufferSize)
182 size_t const fileSize = fsize_orDie(fileName);
183 CHECK(fileSize <= bufferSize, "File too large!");
185 FILE* const inFile = fopen_orDie(fileName, "rb");
186 size_t const readSize = fread(buffer, 1, fileSize, inFile);
187 if (readSize != (size_t)fileSize) {
188 fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
191 fclose(inFile); /* can't fail, read only */
195 /*! mallocAndLoadFile_orDie() :
196 * allocate memory buffer and then load file into it.
198 * Note: This function will send an error to stderr and exit if memory allocation
199 * fails or it cannot read data from the given file path.
201 * @return If successful this function will return buffer and bufferSize(=fileSize),
202 * otherwise it will printout an error to stderr and exit.
204 static void* mallocAndLoadFile_orDie(const char* fileName, size_t* bufferSize) {
205 size_t const fileSize = fsize_orDie(fileName);
206 *bufferSize = fileSize;
207 void* const buffer = malloc_orDie(*bufferSize);
208 loadFile_orDie(fileName, buffer, *bufferSize);
212 /*! saveFile_orDie() :
214 * Save buffSize bytes to a given file path, obtaining them from a location pointed
217 * Note: This function will send an error to stderr and exit if it
218 * cannot write to a given file.
220 static void saveFile_orDie(const char* fileName, const void* buff, size_t buffSize)
222 FILE* const oFile = fopen_orDie(fileName, "wb");
223 size_t const wSize = fwrite(buff, 1, buffSize, oFile);
224 if (wSize != (size_t)buffSize) {
225 fprintf(stderr, "fwrite: %s : %s \n", fileName, strerror(errno));