]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
break loadFile_orDie() into 2: loadFile_orDie() loads file into a pre-allocated memor... 1456/head
authorYi Jin <yijin@fb.com>
Tue, 18 Dec 2018 00:54:55 +0000 (16:54 -0800)
committerYi Jin <yijin@fb.com>
Tue, 18 Dec 2018 00:54:55 +0000 (16:54 -0800)
examples/dictionary_compression.c
examples/dictionary_decompression.c
examples/multiple_simple_compression.c
examples/simple_compression.c
examples/simple_decompression.c
examples/utils.h

index 2a3d61562b20a862a4d20ec7c4aceb41ab5f0567..3c4a5bd1e022ae411224beef49446be68fac86f1 100644 (file)
@@ -21,7 +21,7 @@ static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel)
 {
     size_t dictSize;
     printf("loading dictionary %s \n", dictFileName);
-    void* const dictBuffer = loadFile_orDie(dictFileName, &dictSize, 0, 0);
+    void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
     ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, cLevel);
     if (!cdict) {
         fprintf(stderr, "ZSTD_createCDict error \n");
@@ -35,7 +35,7 @@ static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel)
 static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdict)
 {
     size_t fSize;
-    void* const fBuff = loadFile_orDie(fname, &fSize, 0, 0);
+    void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
     size_t const cBuffSize = ZSTD_compressBound(fSize);
     void* const cBuff = malloc_orDie(cBuffSize);
 
index 36d20c1b19f9bea2844a2b6a3b2cf9ebe3c2d069..243e22236fdbd5a9738a3d159d7ac33d26823231 100644 (file)
@@ -25,7 +25,7 @@ static ZSTD_DDict* createDict_orDie(const char* dictFileName)
 {
     size_t dictSize;
     printf("loading dictionary %s \n", dictFileName);
-    void* const dictBuffer = loadFile_orDie(dictFileName, &dictSize, 0, 0);
+    void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
     ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize);
     if (ddict==NULL) { fprintf(stderr, "ZSTD_createDDict error \n"); exit(5); }
     free(dictBuffer);
@@ -35,7 +35,7 @@ static ZSTD_DDict* createDict_orDie(const char* dictFileName)
 static void decompress(const char* fname, const ZSTD_DDict* ddict)
 {
     size_t cSize;
-    void* const cBuff = loadFile_orDie(fname, &cSize, 0, 0);
+    void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
     unsigned long long const rSize = ZSTD_findDecompressedSize(cBuff, cSize);
     if (rSize==ZSTD_CONTENTSIZE_ERROR) {
         fprintf(stderr, "%s : it was not compressed by zstd.\n", fname);
index 2401a99b64365e2bea7c17f82b42dd1b1c18b98b..65c775bfa1d2aa86fd4197a1e59d519bcdf58751 100644 (file)
@@ -26,9 +26,9 @@ typedef struct {
 
 /*
  * allocate memory for buffers big enough to compress all files
- * as well as memory for output file name (outFilename)
+ * as well as memory for output file name (ofn)
  */
-static resources createResources_orDie(int argc, const char** argv, char **outFilename)
+static resources createResources_orDie(int argc, const char** argv, char **ofn, int* ofnBufferLen)
 {
     size_t maxFilenameLength=0;
     size_t maxFileSize = 0;
@@ -47,7 +47,8 @@ static resources createResources_orDie(int argc, const char** argv, char **outFi
     ress.fBufferSize = maxFileSize;
     ress.cBufferSize = ZSTD_compressBound(maxFileSize);
 
-    *outFilename = (char*)malloc_orDie(maxFilenameLength + 5);
+    *ofnBufferLen = maxFilenameLength + 5;
+    *ofn = (char*)malloc_orDie(*ofnBufferLen);
     ress.fBuffer = malloc_orDie(ress.fBufferSize);
     ress.cBuffer = malloc_orDie(ress.cBufferSize);
     ress.cctx = ZSTD_createCCtx();
@@ -66,8 +67,7 @@ static void freeResources(resources ress, char *outFilename)
 /* compress with pre-allocated context (ZSTD_CCtx) and input/output buffers*/
 static void compressFile_orDie(resources ress, const char* fname, const char* oname)
 {
-    size_t fSize;
-    loadFile_orDie(fname, &fSize, ress.fBuffer, ress.fBufferSize);
+    size_t fSize = loadFile_orDie(fname, ress.fBuffer, ress.fBufferSize);
 
     size_t const cSize = ZSTD_compressCCtx(ress.cctx, ress.cBuffer, ress.cBufferSize, ress.fBuffer, fSize, 1);
     if (ZSTD_isError(cSize)) {
@@ -94,15 +94,17 @@ int main(int argc, const char** argv)
 
     /* memory allocation for outFilename and resources */
     char* outFilename;
-    resources ress = createResources_orDie(argc, argv, &outFilename); 
+    int outFilenameBufferLen;
+    resources const ress = createResources_orDie(argc, argv, &outFilename, &outFilenameBufferLen); 
 
     /* compress files with shared context, input and output buffers */
     int argNb;
     for (argNb = 1; argNb < argc; argNb++) {
         const char* const inFilename = argv[argNb];
-        memset(outFilename, 0, 1);
-        strcat(outFilename, inFilename);
-        strcat(outFilename, ".zst");
+        int inFilenameLen = strlen(inFilename);
+        assert(inFilenameLen + 5 <= outFilenameBufferLen);
+        memcpy(outFilename, inFilename, inFilenameLen);
+        memcpy(outFilename+inFilenameLen, ".zst", 5);
         compressFile_orDie(ress, inFilename, outFilename);
     }
 
index 7f1fa6cc6a0a60bc9d834cfe4fefb834beb6c17e..829cbd7d8a54f9ea9289abbe20d0a4e7f4f49e40 100644 (file)
@@ -19,7 +19,7 @@
 static void compress_orDie(const char* fname, const char* oname)
 {
     size_t fSize;
-    void* const fBuff = loadFile_orDie(fname, &fSize, 0, 0);
+    void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
     size_t const cBuffSize = ZSTD_compressBound(fSize);
     void* const cBuff = malloc_orDie(cBuffSize);
 
index 00e0209549c55c599c6ae4345afca7be2961a602..e7949376cbc770f26b1560f9a033e79ed00fc822 100644 (file)
@@ -20,7 +20,7 @@
 static void decompress(const char* fname)
 {
     size_t cSize;
-    void* const cBuff = loadFile_orDie(fname, &cSize, 0, 0);
+    void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
     unsigned long long const rSize = ZSTD_findDecompressedSize(cBuff, cSize);
     if (rSize==ZSTD_CONTENTSIZE_ERROR) {
         fprintf(stderr, "%s : it was not compressed by zstd.\n", fname);
index 7968d436f2e610027f28561a94463a9ce5b87f3c..6d13604579f94f697768abc10d720ceb2693724e 100644 (file)
@@ -18,6 +18,7 @@
 #include <stdio.h>     // fprintf, perror, fopen, etc.
 #include <string.h>    // strlen, strcat, memset, strerror
 #include <errno.h>     // errno
+#include <assert.h>    // assert
 #include <sys/stat.h>  // stat
 
 /*
@@ -141,33 +142,43 @@ static void* malloc_orDie(size_t size)
 }
 
 /*! loadFile_orDie() :
- * Read size bytes from a file. If buffer is not provided (i.e., buffer == null),
- * malloc will be called to allocate one.
+ * load file into buffer (memory).
  *
  * Note: This function will send an error to stderr and exit if it
  * cannot read data from the given file path.
  *
- * @return If successful this function will return a pointer to read
- * data otherwise it will printout an error to stderr and exit.
+ * @return If successful this function will load file into buffer and
+ * return file size, otherwise it will printout an error to stderr and exit.
  */
-static void* loadFile_orDie(const char* fileName, size_t* size, void* buffer, int bufferSize)
+static size_t loadFile_orDie(const char* fileName, void* buffer, int bufferSize)
 {
     size_t const fileSize = fsize_orDie(fileName);
+    assert(fileSize <= bufferSize);
+
     FILE* const inFile = fopen_orDie(fileName, "rb");
-    if (!buffer) {
-        buffer = malloc_orDie(fileSize);
-    }
-    else if (bufferSize < fileSize) {
-        fprintf(stderr, "%s : filesize bigger than provided buffer.\n", fileName);
-        exit(ERROR_largeFile);
-    }
     size_t const readSize = fread(buffer, 1, fileSize, inFile);
     if (readSize != (size_t)fileSize) {
         fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
         exit(ERROR_fread);
     }
     fclose(inFile);  /* can't fail, read only */
-    *size = fileSize;
+    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);
     return buffer;
 }