kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

lz4.c (1305B)


      1 #include "lz4.h"
      2 
      3 #include <limits.h>
      4 
      5 #define LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION 1
      6 #define LZ4LIB_VISIBILITY
      7 #include "lz4/lz4.c"
      8 
      9 size_t dist_lz4_compress_bound(size_t raw_len) {
     10   int bound;
     11   if (raw_len > (size_t)LZ4_MAX_INPUT_SIZE) return 0;
     12   bound = LZ4_compressBound((int)raw_len);
     13   if (bound <= 0) return 0;
     14   return (size_t)bound;
     15 }
     16 
     17 int dist_lz4_compress_block(uint8_t* dst, size_t dst_cap, size_t* dst_len,
     18                             const uint8_t* src, size_t src_len) {
     19   int n;
     20   if (!dst || !dst_len || (!src && src_len != 0)) return DIST_ERR;
     21   if (src_len > (size_t)LZ4_MAX_INPUT_SIZE || dst_cap > (size_t)INT_MAX)
     22     return DIST_ERR;
     23   n = LZ4_compress_default((const char*)src, (char*)dst, (int)src_len,
     24                            (int)dst_cap);
     25   if (n <= 0) return DIST_ERR;
     26   *dst_len = (size_t)n;
     27   return DIST_OK;
     28 }
     29 
     30 int dist_lz4_decompress_block(uint8_t* dst, size_t dst_len, const uint8_t* src,
     31                               size_t src_len) {
     32   int n;
     33   if (!dst || (!src && src_len != 0)) return DIST_ERR;
     34   if (dst_len > (size_t)INT_MAX || src_len > (size_t)INT_MAX) return DIST_ERR;
     35   n = LZ4_decompress_safe((const char*)src, (char*)dst, (int)src_len,
     36                           (int)dst_len);
     37   if (n != (int)dst_len) return DIST_ERR;
     38   return DIST_OK;
     39 }