int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
		  int type)
{
	int ret;

	if (in_len < UBIFS_MIN_COMPR_LEN) {
		no_compress(in_buf, in_len, out_buf, out_len);
		return MKFS_UBIFS_COMPR_NONE;
	}

	if (c->favor_lzo)
		ret = favor_lzo_compress(in_buf, in_len, out_buf, out_len, &type);
	else {
		switch (type) {
		case MKFS_UBIFS_COMPR_LZO:
			ret = lzo_compress(in_buf, in_len, out_buf, out_len);
			break;
		case MKFS_UBIFS_COMPR_ZLIB:
			ret = zlib_deflate(in_buf, in_len, out_buf, out_len);
			break;
		case MKFS_UBIFS_COMPR_NONE:
			ret = 1;
			break;
		default:
			errcnt += 1;
			ret = 1;
			break;
		}
	}
	if (ret || *out_len >= in_len) {
		no_compress(in_buf, in_len, out_buf, out_len);
		return MKFS_UBIFS_COMPR_NONE;
	}
	return type;
}
Example #2
0
/* delete = 1 Do delete dbdata
   delete = 0 Do not delete dbdta */
unsigned int file_commit_block(unsigned char *dbdata, INOBNO inobno, 
									off_t offset)
{
    unsigned char *stiger;
    compr *compressed;
    INUSE *inuse;
	OFFHASH *offhash;
    unsigned int ret = 0;
#ifndef SHA3
    word64 res[3];
#endif

    FUNC;
#ifdef SHA3
    stiger=sha_binhash(dbdata, BLKSIZE);
#else
    binhash(dbdata, BLKSIZE, res);
    stiger=(unsigned char *)&res;
#endif
#ifdef LZO
    compressed = lzo_compress((unsigned char *) dbdata, BLKSIZE);
#else
    compressed = clz_compress((unsigned char *) dbdata, BLKSIZE);
#endif
    ret = compressed->size;
    inuse = file_get_inuse(stiger);
    if (NULL == inuse) {
        loghash("commit_block : write hash with file_qdta", stiger);
        inuse = s_malloc(sizeof(INUSE));
        inuse->inuse = 0;
        inuse->offset = get_offset(compressed->size);
        inuse->size = compressed->size;
        file_qdta(&inobno, stiger, compressed->data, compressed->size,
                  inuse->offset);
    } else
        loghash("commit_block : only updated inuse for hash ", stiger);
    inuse->inuse++;
    file_update_inuse(stiger, inuse);
    comprfree(compressed);
	offhash = s_malloc(sizeof(OFFHASH));
	offhash->offset = offset;
	memcpy(offhash->stiger, stiger, config->hashlen);
    write_dbb_to_cache(&inobno, offhash);
#ifdef SHA3
    free(stiger);
#endif
    free(inuse);
    return (ret);
}
static int favor_lzo_compress(void *in_buf, size_t in_len, void *out_buf,
			       size_t *out_len, int *type)
{
	int lzo_ret, zlib_ret;
	size_t lzo_len, zlib_len;

	lzo_len = zlib_len = *out_len;
	lzo_ret = lzo_compress(in_buf, in_len, out_buf, &lzo_len);
	zlib_ret = zlib_deflate(in_buf, in_len, zlib_buf, &zlib_len);

	if (lzo_ret && zlib_ret)
		/* Both compressors failed */
		return -1;

	if (!lzo_ret && !zlib_ret) {
		double percent;

		/* Both compressors succeeded */
		if (lzo_len <= zlib_len )
			goto select_lzo;

		percent = (double)zlib_len / (double)lzo_len;
		percent *= 100;
		if (percent > 100 - c->favor_percent)
			goto select_lzo;
		goto select_zlib;
	}

	if (lzo_ret)
		/* Only zlib compressor succeeded */
		goto select_zlib;

	/* Only LZO compressor succeeded */

select_lzo:
	*out_len = lzo_len;
	*type = MKFS_UBIFS_COMPR_LZO;
	return 0;

select_zlib:
	*out_len = zlib_len;
	*type = MKFS_UBIFS_COMPR_ZLIB;
	memcpy(out_buf, zlib_buf, zlib_len);
	return 0;
}
Example #4
0
/*
 * Compress, fragment, encrypt and HMAC-sign an outgoing packet.
 * Input: c->c2.buf
 * Output: c->c2.to_link
 */
void
encrypt_sign (struct context *c, bool comp_frag)
{
  struct context_buffers *b = c->c2.buffers;
  const uint8_t *orig_buf = c->c2.buf.data;

#if P2MP_SERVER
  /*
   * Drop non-TLS outgoing packet if client-connect script/plugin
   * has not yet succeeded.
   */
  if (c->c2.context_auth != CAS_SUCCEEDED)
    c->c2.buf.len = 0;
#endif

  if (comp_frag)
    {
#ifdef ENABLE_LZO
      /* Compress the packet. */
      if (lzo_defined (&c->c2.lzo_compwork))
	lzo_compress (&c->c2.buf, b->lzo_compress_buf, &c->c2.lzo_compwork, &c->c2.frame);
#endif
#ifdef ENABLE_FRAGMENT
      if (c->c2.fragment)
	fragment_outgoing (c->c2.fragment, &c->c2.buf, &c->c2.frame_fragment);
#endif
    }

#ifdef ENABLE_CRYPTO
#ifdef ENABLE_SSL
  /*
   * If TLS mode, get the key we will use to encrypt
   * the packet.
   */
  if (c->c2.tls_multi)
    {
      tls_pre_encrypt (c->c2.tls_multi, &c->c2.buf, &c->c2.crypto_options);
    }
#endif

  /*
   * Encrypt the packet and write an optional
   * HMAC signature.
   */
  openvpn_encrypt (&c->c2.buf, b->encrypt_buf, &c->c2.crypto_options, &c->c2.frame);
#endif
  /*
   * Get the address we will be sending the packet to.
   */
  link_socket_get_outgoing_addr (&c->c2.buf, get_link_socket_info (c),
				 &c->c2.to_link_addr);
#ifdef ENABLE_CRYPTO
#ifdef ENABLE_SSL
  /*
   * In TLS mode, prepend the appropriate one-byte opcode
   * to the packet which identifies it as a data channel
   * packet and gives the low-permutation version of
   * the key-id to the recipient so it knows which
   * decrypt key to use.
   */
  if (c->c2.tls_multi)
    {
      tls_post_encrypt (c->c2.tls_multi, &c->c2.buf);
    }
#endif
#endif

  /* if null encryption, copy result to read_tun_buf */
  buffer_turnover (orig_buf, &c->c2.to_link, &c->c2.buf, &b->read_tun_buf);
}
Example #5
0
lzo_bool x_compress(file_t *fip, file_t *fop, header_t *h)
{
    lzo_bool ok = 0;

    init_compress_header(h,fip,fop);
    switch (h->method)
    {
#if defined(WITH_LZO)
    case M_LZO1X_1:
    case M_LZO1X_1_15:
    case M_LZO1X_999:
        lzo_init_compress_header(h);
        break;
#endif
#if defined(WITH_NRV)
    case M_NRV1A:
    case M_NRV1B:
    case M_NRV2A:
    case M_NRV2B:
        nrv_init_compress_header(h);
        break;
#endif
#if defined(WITH_ZLIB)
    case M_ZLIB:
        zlib_init_compress_header(h);
        break;
#endif
    default:
        fatal(fip,"Internal error");
        break;
    }

    if (!x_enter(h))
        e_memory();

    if (opt_verbose > 1)
    {
        if (opt_unlink)
            fprintf(con_term,"replacing %s with %s", fip->name, fop->name);
        else
            fprintf(con_term,"compressing %s into %s", fip->name, fop->name);
        fflush(con_term);
        set_err_nl(1);
    }

    write_header(fop,h);

    fip->bytes_processed = fop->bytes_processed = 0;

    switch (h->method)
    {
#if defined(WITH_LZO)
    case M_LZO1X_1:
    case M_LZO1X_1_15:
    case M_LZO1X_999:
        ok = lzo_compress(fip,fop,h);
        break;
#endif
#if defined(WITH_NRV)
    case M_NRV1A:
    case M_NRV1B:
    case M_NRV2A:
    case M_NRV2B:
        ok = nrv_compress(fip,fop,h);
        break;
#endif
#if defined(WITH_ZLIB)
    case M_ZLIB:
        ok = zlib_compress(fip,fop,h);
        break;
#endif
    default:
        fatal(fip,"Internal error");
        ok = 0;
        break;
    }

    if (opt_cmd == CMD_COMPRESS && opt_verbose > 1)
    {
        fprintf(con_term, ok ? "\n" : " FAILED\n");
        fflush(con_term);
    }
    set_err_nl(0);

    x_leave(h);
    return ok;
}
Example #6
0
void add_file_block(BLKDTA * blkdta)
{
    INOBNO inobno;
    DBT *cachedata = NULL;
    INUSE *inuse;
	OFFHASH *offhash;

    inobno.inode = blkdta->inode;
    inobno.blocknr = blkdta->blocknr;

    FUNC;
    LDEBUG("add_file_block : inode %llu - %llu", inobno.inode,
           inobno.blocknr);
    if (blkdta->bsize + blkdta->offsetblock < BLKSIZE) {
// Flush the blockcache before overwriting.
        cachedata = try_block_cache(blkdta->inode, blkdta->blocknr, 0);
        if (cachedata)
            DBTfree(cachedata);
        add_blk_to_cache(blkdta->inode, blkdta->blocknr, 
			blkdta->bsize + blkdta->offsetblock, blkdta->blockfiller, 
			blkdta->offsetfile);
        LDEBUG
            ("add_file_block : wrote with add_blk_to_cache  : inode %llu - %llu size %i",
             inobno.inode, inobno.blocknr, blkdta->bsize);
        update_filesize(blkdta->inode, blkdta->bsize, blkdta->offsetblock,
                        blkdta->blocknr, blkdta->sparse, 0, 0);
        return;
    }
    inuse = file_get_inuse(blkdta->stiger);
    if (inuse == NULL) {
        if (NULL == blkdta->compressed) {
#ifdef LZO
            blkdta->compressed =
                lzo_compress(blkdta->blockfiller, BLKSIZE);
#else
            blkdta->compressed =
                clz_compress(blkdta->blockfiller, BLKSIZE);
#endif
        }
        LDEBUG("Compressed %i bytes to %lu bytes", BLKSIZE,
               blkdta->compressed->size);
        loghash("add_file_block call qdta for hash :", blkdta->stiger);
        inuse = s_malloc(sizeof(INUSE));
        inuse->inuse = 0;
        inuse->offset = get_offset(blkdta->compressed->size);
        LDEBUG("add to offset %llu", inuse->offset);
        inuse->size = blkdta->compressed->size;
        file_qdta(&inobno, blkdta->stiger, blkdta->compressed->data,
                  blkdta->compressed->size, inuse->offset);
        loghash("add_file_block queued with qdta", blkdta->stiger);
        update_filesize(blkdta->inode, blkdta->bsize, blkdta->offsetblock,
                        blkdta->blocknr, blkdta->sparse,
                        blkdta->compressed->size, 0);
    } else {
        update_filesize(blkdta->inode, blkdta->bsize, blkdta->offsetblock,
                        blkdta->blocknr, blkdta->sparse, 0, 1);
    }
    if (NULL != blkdta->compressed)
        comprfree(blkdta->compressed);
    inuse->inuse = inuse->inuse + 1;
    file_update_inuse(blkdta->stiger, inuse);
	offhash = s_malloc(sizeof(OFFHASH));
	offhash->offset = blkdta->offsetfile;
	memcpy(offhash->stiger, blkdta->stiger, config->hashlen);
    write_dbb_to_cache(&inobno, offhash);
	free(offhash);
    free(inuse);
    EFUNC;
    return;
}