]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
made separate API for dictionary management
authorPaul Cruz <paulcruz74@fb.com>
Sat, 12 Aug 2017 01:40:19 +0000 (18:40 -0700)
committerPaul Cruz <paulcruz74@fb.com>
Sat, 12 Aug 2017 01:40:19 +0000 (18:40 -0700)
doc/educational_decoder/harness.c
doc/educational_decoder/zstd_decompress.c
doc/educational_decoder/zstd_decompress.h

index 31e1c292432c99c62903d943ffcad3663ce13f76..43d6df7fc0457f5f9d0fa274b8fc71c36996f6b1 100644 (file)
@@ -106,9 +106,15 @@ int main(int argc, char **argv) {
         return 1;
     }
 
+    dictionary_t* parsed_dict = create_dictionary();
+    if (dict) {
+        parse_dictionary(parsed_dict, dict, dict_size);
+    }
     size_t decompressed =
         ZSTD_decompress_with_dict(output, decompressed_size,
-                                  input, input_size, dict, dict_size);
+                                  input, input_size, parsed_dict);
+
+    free_dictionary(parsed_dict);
 
     write_file(argv[2], output, decompressed);
 
index fe87709247419b1d6bf06606cbc83bf949249692..e22df1c1034e9871a3df7eb644b412b8cf844b09 100644 (file)
@@ -357,19 +357,16 @@ static void execute_sequences(frame_context_t *const ctx, ostream_t *const out,
 
 size_t ZSTD_decompress(void *const dst, const size_t dst_len,
                        const void *const src, const size_t src_len) {
-    return ZSTD_decompress_with_dict(dst, dst_len, src, src_len, NULL, 0);
+    dictionary_t* uninit_dict = create_dictionary();
+    size_t const decomp_size = ZSTD_decompress_with_dict(dst, dst_len, src,
+                                                         src_len, uninit_dict);
+    free_dictionary(uninit_dict);
+    return decomp_size;
 }
 
 size_t ZSTD_decompress_with_dict(void *const dst, const size_t dst_len,
                                  const void *const src, const size_t src_len,
-                                 const void *const dict,
-                                 const size_t dict_len) {
-    dictionary_t parsed_dict;
-    memset(&parsed_dict, 0, sizeof(dictionary_t));
-    // dict_len < 8 is not a valid dictionary
-    if (dict && dict_len > 8) {
-        parse_dictionary(&parsed_dict, dict, dict_len);
-    }
+                                 dictionary_t* parsed_dict) {
 
     istream_t in = IO_make_istream(src, src_len);
     ostream_t out = IO_make_ostream(dst, dst_len);
@@ -380,9 +377,7 @@ size_t ZSTD_decompress_with_dict(void *const dst, const size_t dst_len,
     // parameters which tells the decoder how to decompress it."
 
     /* this decoder assumes decompression of a single frame */
-    decode_frame(&out, &in, &parsed_dict);
-
-    free_dictionary(&parsed_dict);
+    decode_frame(&out, &in, parsed_dict);
 
     return out.ptr - (u8 *)dst;
 }
@@ -1408,6 +1403,16 @@ size_t ZSTD_get_decompressed_size(const void *src, const size_t src_len) {
 /******* END OUTPUT SIZE COUNTING *********************************************/
 
 /******* DICTIONARY PARSING ***************************************************/
+#define DICT_SIZE_ERROR() ERROR("Dictionary size cannot be less than 8 bytes")
+
+dictionary_t* create_dictionary() {
+    dictionary_t* dict = calloc(1, sizeof(dictionary_t));
+    if (!dict) {
+        BAD_ALLOC();
+    }
+    return dict;
+}
+
 static void init_dictionary_content(dictionary_t *const dict,
                                     istream_t *const in);
 
@@ -1416,7 +1421,7 @@ void parse_dictionary(dictionary_t *const dict, const void *src,
     const u8 *byte_src = (const u8 *)src;
     memset(dict, 0, sizeof(dictionary_t));
     if (src_len < 8) {
-        INP_SIZE();
+        DICT_SIZE_ERROR();
     }
 
     istream_t in = IO_make_istream(byte_src, src_len);
index 2ac903075b053bddd0da3bddc68a838468e48aec..41009909bfa124106b4824629a22f04b3c2c5474 100644 (file)
@@ -7,6 +7,13 @@
  * of patent rights can be found in the PATENTS file in the same directory.
  */
 
+/******* EXPOSED TYPES ********************************************************/
+/*
+* Contains the parsed contents of a dictionary
+* This includes Huffman and FSE tables used for decoding and data on offsets
+*/
+typedef struct dictionary_s dictionary_t;
+/******* END EXPOSED TYPES ****************************************************/
 
 /******* DECOMPRESSION FUNCTIONS **********************************************/
 /// Zstandard decompression functions.
@@ -18,7 +25,7 @@ size_t ZSTD_decompress(void *const dst, const size_t dst_len,
 /// `ZSTD_decompress` but uses the provided dict
 size_t ZSTD_decompress_with_dict(void *const dst, const size_t dst_len,
                               const void *const src, const size_t src_len,
-                              const void *const dict, const size_t dict_len);
+                              dictionary_t* parsed_dict);
 
 /// Get the decompressed size of an input stream so memory can be allocated in
 /// advance
@@ -29,10 +36,10 @@ size_t ZSTD_get_decompressed_size(const void *const src, const size_t src_len);
 
 /******* DICTIONARY MANAGEMENT ***********************************************/
 /*
- * Contains the parsed contents of a dictionary
- * This includes Huffman and FSE tables used for decoding and data on offsets
+ * Return a valid dictionary_t pointer for use with dictionary initialization
+ * or decompression
  */
-typedef struct dictionary_s dictionary_t;
+dictionary_t* create_dictionary();
 
 /*
  * Parse a provided dictionary blob for use in decompression
@@ -43,6 +50,7 @@ typedef struct dictionary_s dictionary_t;
  */
 void parse_dictionary(dictionary_t *const dict, const void *src,
                              size_t src_len);
+
 /*
  * Free internal Huffman tables, FSE tables, and dictionary content
  */