static int compress_lz4(const void *in, void *out, size_t inlen, size_t outlen) { int ret; ret = lz4_compress(in, inlen, out, &outlen, workspace); if (ret) { pr_err("lz4_compress error, ret = %d!\n", ret); return -EIO; } return outlen; }
static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen) { struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); size_t tmp_len = *dlen; int err; err = lz4_compress(src, slen, dst, &tmp_len, ctx->lz4_comp_mem); if (err < 0) return -EINVAL; *dlen = tmp_len; return 0; }
SEXP do_lzCompress (SEXP FROM, SEXP LEVEL) { char *buf; int ret; SEXP ans; int level = *(INTEGER(LEVEL)); size_t input_size = (size_t) xlength(FROM); size_t buffer_size = input_size*1.1 + 100; if(TYPEOF(FROM) != RAWSXP) error("'from' must be raw or character"); buf = (char *) R_alloc(buffer_size, sizeof(char)); ret = lz4_compress((void *)RAW(FROM), input_size, (void *)buf, &buffer_size, level); if(ret<0) error("internal error %d in lz4_compress",ret); ans = allocVector(RAWSXP, buffer_size); memcpy(RAW(ans), buf, buffer_size); return ans; }
int adapt_compress(void *src, uint64_t srclen, void *dst, uint64_t *dstlen, int level, uchar_t chdr, int btype, void *data) { struct adapt_data *adat = (struct adapt_data *)(data); uchar_t *src1 = (uchar_t *)src; int rv = 0, bsc_type = 0; if (btype == TYPE_UNKNOWN) { uint64_t i, tot8b, tag1, tag2, tag3; double tagcnt, pct_tag; uchar_t cur_byte, prev_byte; /* * Count number of 8-bit binary bytes and XML tags in source. */ tot8b = 0; tag1 = 0; tag2 = 0; tag3 = 0; prev_byte = cur_byte = 0; for (i = 0; i < srclen; i++) { cur_byte = src1[i]; tot8b += (cur_byte & 0x80); // This way for possible auto-vectorization tag1 += (cur_byte == '<'); tag2 += (cur_byte == '>'); tag3 += ((prev_byte == '<') & (cur_byte == '/')); tag3 += ((prev_byte == '/') & (cur_byte == '>')); if (cur_byte != ' ') prev_byte = cur_byte; } tot8b /= 0x80; tagcnt = tag1 + tag2 + tag3; pct_tag = tagcnt / (double)srclen; if (adat->adapt_mode == 2 && tot8b > FORTY_PCT(srclen)) { btype = TYPE_BINARY; } else if (adat->adapt_mode == 1 && tot8b > FIFTY_PCT(srclen)) { btype = TYPE_BINARY; } else { btype = TYPE_TEXT; if (tag1 > tag2 - 4 && tag1 < tag2 + 4 && tag3 > (double)tag1 * 0.40 && tagcnt > (double)srclen * 0.001) btype |= TYPE_MARKUP; } } /* * Use PPMd if some percentage of source is 7-bit textual bytes, otherwise * use Bzip2 or LZMA. For totally incompressible data we always use LZ4. There * is no point trying to compress such data, like Jpegs. However some archive headers * and zero paddings can exist which LZ4 can easily take care of very fast. */ #ifdef ENABLE_PC_LIBBSC bsc_type = is_bsc_type(btype); #endif if (is_incompressible(btype)) { rv = lz4_compress(src, srclen, dst, dstlen, level, chdr, btype, adat->lz4_data); if (rv < 0) return (rv); rv = ADAPT_COMPRESS_LZ4; lz4_count++; } else if (adat->adapt_mode == 2 && PC_TYPE(btype) == TYPE_BINARY && !bsc_type) { rv = lzma_compress(src, srclen, dst, dstlen, level, chdr, btype, adat->lzma_data); if (rv < 0) return (rv); rv = ADAPT_COMPRESS_LZMA; lzma_count++; } else if (adat->adapt_mode == 1 && PC_TYPE(btype) == TYPE_BINARY && !bsc_type) { rv = bzip2_compress(src, srclen, dst, dstlen, level, chdr, btype, NULL); if (rv < 0) return (rv); rv = ADAPT_COMPRESS_BZIP2; bzip2_count++; } else { #ifdef ENABLE_PC_LIBBSC if (adat->bsc_data && bsc_type) { rv = libbsc_compress(src, srclen, dst, dstlen, level, chdr, btype, adat->bsc_data); if (rv < 0) return (rv); rv = ADAPT_COMPRESS_BSC; bsc_count++; } else { #endif rv = ppmd_alloc(adat->ppmd_data); if (rv < 0) return (rv); rv = ppmd_compress(src, srclen, dst, dstlen, level, chdr, btype, adat->ppmd_data); ppmd_free(adat->ppmd_data); if (rv < 0) return (rv); rv = ADAPT_COMPRESS_PPMD; ppmd_count++; #ifdef ENABLE_PC_LIBBSC } #endif } return (rv); }