Example #1
0
static int lz4_msendv(struct msock_vfs *mvfs,
      const struct iovec *iov, size_t iovlen, int64_t deadline) {
    struct lz4_sock *obj = dsock_cont(mvfs, struct lz4_sock, mvfs);
    /* Adjust the buffer size as needed. */
    size_t len = iov_size(iov, iovlen);
    size_t maxlen = LZ4F_compressFrameBound(len, NULL);
    if(obj->outlen < maxlen) {
        uint8_t *newbuf = realloc(obj->outbuf, maxlen);
        if(dsock_slow(!newbuf)) {errno = ENOMEM; return -1;}
        obj->outbuf = newbuf;
        obj->outlen = maxlen;
    }
    /* Compress the data. */
    /* TODO: Avoid the extra allocations and copies. */
    uint8_t *buf = malloc(len);
    if(dsock_slow(!buf)) {errno = ENOMEM; return -1;}
    iov_copyallfrom(buf, iov, iovlen);
    LZ4F_preferences_t prefs = {0};
    prefs.frameInfo.contentSize = len;
    size_t dstlen = LZ4F_compressFrame(obj->outbuf, obj->outlen,
        buf, len, &prefs);
    dsock_assert(!LZ4F_isError(dstlen));
    dsock_assert(dstlen <= obj->outlen);
    free(buf);
    /* Send the compressed frame. */
    return msend(obj->s, obj->outbuf, dstlen, deadline);
}
Example #2
0
static PyObject *py_lz4f_compressFrame(PyObject *self, PyObject *args) {
    PyObject *result;
    const char* source;
    char* dest;
    int src_size;
    size_t dest_size;
    size_t final_size;
    size_t ssrc_size;

    (void)self;
    if (!PyArg_ParseTuple(args, "s#", &source, &src_size)) {
        return NULL;
    }

    ssrc_size = (size_t)src_size;
    dest_size = LZ4F_compressFrameBound(ssrc_size, NULL);
    dest = (char*)malloc(dest_size);

    final_size = LZ4F_compressFrame(dest, dest_size, source, ssrc_size, NULL);
    result = PyBytes_FromStringAndSize(dest, final_size);

    free(dest);

    return result;
}
Example #3
0
    static bool writeFile(llvm::StringRef file_name, llvm::StringRef data) {
        std::error_code error_code;
        llvm::raw_fd_ostream file(file_name, error_code, llvm::sys::fs::F_RW);
        if (error_code)
            return false;

        int uncompressed_size = data.size();
        // Write the uncompressed size to the beginning of the file as a simple checksum.
        // It looks like each lz4 block has its own data checksum, but we need to also
        // make sure that we have all the blocks that we expected.
        // In particular, without this, an empty file seems to be a valid lz4 stream.
        file.write(reinterpret_cast<const char*>(&uncompressed_size), 4);

        LZ4F_preferences_t preferences;
        memset(&preferences, 0, sizeof(preferences));
        preferences.frameInfo.contentChecksumFlag = contentChecksumEnabled;
        preferences.frameInfo.contentSize = data.size();

        std::vector<char> compressed;
        size_t max_size = LZ4F_compressFrameBound(data.size(), &preferences);
        compressed.resize(max_size);
        size_t compressed_size = LZ4F_compressFrame(&compressed[0], max_size, data.data(), data.size(), &preferences);
        if (LZ4F_isError(compressed_size))
            return false;
        file.write(compressed.data(), compressed_size);
        return true;
    }
Example #4
0
/* lz4_compress support function
 * @param src constant input memory buffer of size size to be compressed
 * @param srz_size input memory buffer size
 * @param dest ouput memory buffer of size size in which to place results
 * @param dest_size pointer to a variable that contains the size of dest on input
 *        and is updated to contain the size of compressed data in dest on out
 * @param level compression level from 1 (low/fast) to 9 (high/slow)
 * Returns an error code, 0 for success non-zero otherwise.
 */
int
lz4_compress (void *src, size_t src_size, void *dest, size_t * dest_size,
              int level)
{
  size_t n, len = 0;
  LZ4F_errorCode_t err;
  LZ4F_preferences_t prefs;
  LZ4F_compressionContext_t ctx;
  int retval = 0;

  /* Set compression parameters */
  memset (&prefs, 0, sizeof (prefs));
  prefs.autoFlush = 1;
  prefs.compressionLevel = level;       /* 0...16 */
  prefs.frameInfo.blockMode = 0;        /* blockLinked, blockIndependent ; 0 == default */
  prefs.frameInfo.blockSizeID = 0;      /* max64KB, max256KB, max1MB, max4MB ; 0 == default */
  prefs.frameInfo.contentChecksumFlag = 1;      /* noContentChecksum, contentChecksumEnabled ; 0 == default  */
  prefs.frameInfo.contentSize = (long long)src_size;  /* for reference */

  /* create context */
  err = LZ4F_createCompressionContext (&ctx, LZ4F_VERSION);
  if (LZ4F_isError (err))
    return -1;

  n = LZ4F_compressFrame (dest, *dest_size, src, src_size, &prefs);
  if (LZ4F_isError (n))
    {
      retval = -2;
      goto cleanup;
    }
  /* update the compressed buffer size */
  *dest_size = n;

cleanup:
  if (ctx)
    {
      err = LZ4F_freeCompressionContext (ctx);
      if (LZ4F_isError (err))
        return -5;
    }

  return retval;
}