blob: 4873e877a7af831c6e16eac6d170aaf6db19c446 [file] [log] [blame]
Hung Dang0f52b0c2018-12-06 22:42:19 -05001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Hung Dang0f52b0c2018-12-06 22:42:19 -05003 * All rights reserved.
4 *
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.
9 */
10
11/*
Yi Jin04d06ad2018-12-14 18:12:05 -080012 * This header file has common utility functions used in examples.
Hung Dang0f52b0c2018-12-06 22:42:19 -050013 */
Nick Terrell1d0c1702019-04-05 18:11:17 -070014#ifndef COMMON_H
15#define COMMON_H
Hung Dang0f52b0c2018-12-06 22:42:19 -050016
17#include <stdlib.h> // malloc, free, exit
18#include <stdio.h> // fprintf, perror, fopen, etc.
Nick Terrell1d0c1702019-04-05 18:11:17 -070019#include <string.h> // strerror
Hung Dang0f52b0c2018-12-06 22:42:19 -050020#include <errno.h> // errno
21#include <sys/stat.h> // stat
Nick Terrellf5cbee92019-03-22 14:57:23 -070022#include <zstd.h>
Hung Dang0f52b0c2018-12-06 22:42:19 -050023
Elliott Hughes44aba642023-09-12 20:18:59 +000024
25/* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
26#if defined(__GNUC__)
27# define UNUSED_ATTR __attribute__((unused))
28#else
29# define UNUSED_ATTR
30#endif
31
32#define HEADER_FUNCTION static UNUSED_ATTR
33
34
Hung Dang0f52b0c2018-12-06 22:42:19 -050035/*
Yi Jin04d06ad2018-12-14 18:12:05 -080036 * Define the returned error code from utility functions.
Hung Dang0f52b0c2018-12-06 22:42:19 -050037 */
38typedef enum {
39 ERROR_fsize = 1,
40 ERROR_fopen = 2,
41 ERROR_fclose = 3,
42 ERROR_fread = 4,
43 ERROR_fwrite = 5,
44 ERROR_loadFile = 6,
45 ERROR_saveFile = 7,
46 ERROR_malloc = 8,
47 ERROR_largeFile = 9,
Nick Terrell1d0c1702019-04-05 18:11:17 -070048} COMMON_ErrorCode;
49
50/*! CHECK
51 * Check that the condition holds. If it doesn't print a message and die.
52 */
53#define CHECK(cond, ...) \
54 do { \
55 if (!(cond)) { \
56 fprintf(stderr, \
57 "%s:%d CHECK(%s) failed: ", \
58 __FILE__, \
59 __LINE__, \
60 #cond); \
61 fprintf(stderr, "" __VA_ARGS__); \
62 fprintf(stderr, "\n"); \
63 exit(1); \
64 } \
65 } while (0)
66
67/*! CHECK_ZSTD
68 * Check the zstd error code and die if an error occurred after printing a
69 * message.
70 */
Nick Terrell91f58912021-12-01 11:49:58 -080071#define CHECK_ZSTD(fn) \
Nick Terrell1d0c1702019-04-05 18:11:17 -070072 do { \
73 size_t const err = (fn); \
74 CHECK(!ZSTD_isError(err), "%s", ZSTD_getErrorName(err)); \
75 } while (0)
Hung Dang0f52b0c2018-12-06 22:42:19 -050076
Yi Jin04d06ad2018-12-14 18:12:05 -080077/*! fsize_orDie() :
Hung Dang0f52b0c2018-12-06 22:42:19 -050078 * Get the size of a given file path.
Yi Jin04d06ad2018-12-14 18:12:05 -080079 *
Hung Dang0f52b0c2018-12-06 22:42:19 -050080 * @return The size of a given file path.
81 */
Elliott Hughes44aba642023-09-12 20:18:59 +000082HEADER_FUNCTION size_t fsize_orDie(const char *filename)
Hung Dang0f52b0c2018-12-06 22:42:19 -050083{
84 struct stat st;
Yi Jin04d06ad2018-12-14 18:12:05 -080085 if (stat(filename, &st) != 0) {
86 /* error */
87 perror(filename);
88 exit(ERROR_fsize);
89 }
90
91 off_t const fileSize = st.st_size;
92 size_t const size = (size_t)fileSize;
Yi Jinea4859e2018-12-16 20:45:43 -080093 /* 1. fileSize should be non-negative,
94 * 2. if off_t -> size_t type conversion results in discrepancy,
95 * the file size is too large for type size_t.
Yi Jin04d06ad2018-12-14 18:12:05 -080096 */
Yann Colleta1394392019-01-19 23:38:20 -080097 if ((fileSize < 0) || (fileSize != (off_t)size)) {
Yi Jin04d06ad2018-12-14 18:12:05 -080098 fprintf(stderr, "%s : filesize too large \n", filename);
99 exit(ERROR_largeFile);
100 }
101 return size;
Hung Dang0f52b0c2018-12-06 22:42:19 -0500102}
103
Yi Jin04d06ad2018-12-14 18:12:05 -0800104/*! fopen_orDie() :
Hung Dang0f52b0c2018-12-06 22:42:19 -0500105 * Open a file using given file path and open option.
106 *
107 * @return If successful this function will return a FILE pointer to an
108 * opened file otherwise it sends an error to stderr and exits.
109 */
Elliott Hughes44aba642023-09-12 20:18:59 +0000110HEADER_FUNCTION FILE* fopen_orDie(const char *filename, const char *instruction)
Hung Dang0f52b0c2018-12-06 22:42:19 -0500111{
112 FILE* const inFile = fopen(filename, instruction);
113 if (inFile) return inFile;
114 /* error */
115 perror(filename);
116 exit(ERROR_fopen);
117}
118
Yi Jin04d06ad2018-12-14 18:12:05 -0800119/*! fclose_orDie() :
Hung Dang0f52b0c2018-12-06 22:42:19 -0500120 * Close an opened file using given FILE pointer.
121 */
Elliott Hughes44aba642023-09-12 20:18:59 +0000122HEADER_FUNCTION void fclose_orDie(FILE* file)
Hung Dang0f52b0c2018-12-06 22:42:19 -0500123{
124 if (!fclose(file)) { return; };
125 /* error */
126 perror("fclose");
127 exit(ERROR_fclose);
128}
129
Yi Jin04d06ad2018-12-14 18:12:05 -0800130/*! fread_orDie() :
131 *
Hung Dang0f52b0c2018-12-06 22:42:19 -0500132 * Read sizeToRead bytes from a given file, storing them at the
133 * location given by buffer.
Yi Jin04d06ad2018-12-14 18:12:05 -0800134 *
Hung Dang0f52b0c2018-12-06 22:42:19 -0500135 * @return The number of bytes read.
136 */
Elliott Hughes44aba642023-09-12 20:18:59 +0000137HEADER_FUNCTION size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
Hung Dang0f52b0c2018-12-06 22:42:19 -0500138{
139 size_t const readSize = fread(buffer, 1, sizeToRead, file);
140 if (readSize == sizeToRead) return readSize; /* good */
141 if (feof(file)) return readSize; /* good, reached end of file */
142 /* error */
143 perror("fread");
144 exit(ERROR_fread);
145}
146
147/*! fwrite_orDie() :
Yi Jin04d06ad2018-12-14 18:12:05 -0800148 *
Hung Dang0f52b0c2018-12-06 22:42:19 -0500149 * Write sizeToWrite bytes to a file pointed to by file, obtaining
150 * them from a location given by buffer.
151 *
152 * Note: This function will send an error to stderr and exit if it
Yi Jin04d06ad2018-12-14 18:12:05 -0800153 * cannot write data to the given file pointer.
Hung Dang0f52b0c2018-12-06 22:42:19 -0500154 *
155 * @return The number of bytes written.
156 */
Elliott Hughes44aba642023-09-12 20:18:59 +0000157HEADER_FUNCTION size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
Hung Dang0f52b0c2018-12-06 22:42:19 -0500158{
159 size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
160 if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
161 /* error */
162 perror("fwrite");
163 exit(ERROR_fwrite);
164}
165
166/*! malloc_orDie() :
167 * Allocate memory.
Yi Jin04d06ad2018-12-14 18:12:05 -0800168 *
Hung Dang0f52b0c2018-12-06 22:42:19 -0500169 * @return If successful this function returns a pointer to allo-
170 * cated memory. If there is an error, this function will send that
171 * error to stderr and exit.
172 */
Elliott Hughes44aba642023-09-12 20:18:59 +0000173HEADER_FUNCTION void* malloc_orDie(size_t size)
Hung Dang0f52b0c2018-12-06 22:42:19 -0500174{
175 void* const buff = malloc(size);
176 if (buff) return buff;
177 /* error */
178 perror("malloc");
179 exit(ERROR_malloc);
180}
181
182/*! loadFile_orDie() :
Yi Jinbc4dc602018-12-17 16:54:55 -0800183 * load file into buffer (memory).
Yi Jin04d06ad2018-12-14 18:12:05 -0800184 *
Hung Dang0f52b0c2018-12-06 22:42:19 -0500185 * Note: This function will send an error to stderr and exit if it
186 * cannot read data from the given file path.
Yi Jin04d06ad2018-12-14 18:12:05 -0800187 *
Yi Jinbc4dc602018-12-17 16:54:55 -0800188 * @return If successful this function will load file into buffer and
189 * return file size, otherwise it will printout an error to stderr and exit.
Hung Dang0f52b0c2018-12-06 22:42:19 -0500190 */
Elliott Hughes44aba642023-09-12 20:18:59 +0000191HEADER_FUNCTION size_t loadFile_orDie(const char* fileName, void* buffer, size_t bufferSize)
Hung Dang0f52b0c2018-12-06 22:42:19 -0500192{
Yi Jin04d06ad2018-12-14 18:12:05 -0800193 size_t const fileSize = fsize_orDie(fileName);
Nick Terrell1d0c1702019-04-05 18:11:17 -0700194 CHECK(fileSize <= bufferSize, "File too large!");
Yi Jinbc4dc602018-12-17 16:54:55 -0800195
Hung Dang0f52b0c2018-12-06 22:42:19 -0500196 FILE* const inFile = fopen_orDie(fileName, "rb");
Yi Jin04d06ad2018-12-14 18:12:05 -0800197 size_t const readSize = fread(buffer, 1, fileSize, inFile);
198 if (readSize != (size_t)fileSize) {
Hung Dang0f52b0c2018-12-06 22:42:19 -0500199 fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
200 exit(ERROR_fread);
201 }
202 fclose(inFile); /* can't fail, read only */
Yi Jinbc4dc602018-12-17 16:54:55 -0800203 return fileSize;
204}
205
206/*! mallocAndLoadFile_orDie() :
207 * allocate memory buffer and then load file into it.
208 *
209 * Note: This function will send an error to stderr and exit if memory allocation
210 * fails or it cannot read data from the given file path.
211 *
212 * @return If successful this function will return buffer and bufferSize(=fileSize),
213 * otherwise it will printout an error to stderr and exit.
214 */
Elliott Hughes44aba642023-09-12 20:18:59 +0000215HEADER_FUNCTION void* mallocAndLoadFile_orDie(const char* fileName, size_t* bufferSize)
216{
Yi Jinbc4dc602018-12-17 16:54:55 -0800217 size_t const fileSize = fsize_orDie(fileName);
218 *bufferSize = fileSize;
219 void* const buffer = malloc_orDie(*bufferSize);
220 loadFile_orDie(fileName, buffer, *bufferSize);
Hung Dang0f52b0c2018-12-06 22:42:19 -0500221 return buffer;
222}
223
224/*! saveFile_orDie() :
Yi Jin04d06ad2018-12-14 18:12:05 -0800225 *
Hung Dang0f52b0c2018-12-06 22:42:19 -0500226 * Save buffSize bytes to a given file path, obtaining them from a location pointed
227 * to by buff.
Yi Jin04d06ad2018-12-14 18:12:05 -0800228 *
Hung Dang0f52b0c2018-12-06 22:42:19 -0500229 * Note: This function will send an error to stderr and exit if it
230 * cannot write to a given file.
231 */
Elliott Hughes44aba642023-09-12 20:18:59 +0000232HEADER_FUNCTION void saveFile_orDie(const char* fileName, const void* buff, size_t buffSize)
Hung Dang0f52b0c2018-12-06 22:42:19 -0500233{
234 FILE* const oFile = fopen_orDie(fileName, "wb");
235 size_t const wSize = fwrite(buff, 1, buffSize, oFile);
236 if (wSize != (size_t)buffSize) {
237 fprintf(stderr, "fwrite: %s : %s \n", fileName, strerror(errno));
238 exit(ERROR_fwrite);
239 }
240 if (fclose(oFile)) {
241 perror(fileName);
242 exit(ERROR_fclose);
243 }
244}
245
Hung Dang0f52b0c2018-12-06 22:42:19 -0500246#endif