コード例 #1
0
ファイル: api.c プロジェクト: besquared/snappy
static VALUE
snappy_deflate(int argc, VALUE *argv, VALUE self)
{
    VALUE src, dst;
    size_t  output_length;
    snappy_status result;

    rb_scan_args(argc, argv, "11", &src, &dst);
    StringValue(src);

    output_length = snappy_max_compressed_length(RSTRING_LEN(src));

    if (NIL_P(dst)) {
        dst = rb_str_new(NULL, output_length);
    } else {
    	StringValue(dst);
    	rb_str_resize(dst, output_length);
    }

    result = snappy_compress(RSTRING_PTR(src), RSTRING_LEN(src), RSTRING_PTR(dst), &output_length);
    if (result == SNAPPY_OK) {
        rb_str_resize(dst, output_length);
        return dst;
    } else {
        return snappy_raise(result);
    }
}
コード例 #2
0
ファイル: codec.c プロジェクト: andrioni/mypipe
static int encode_snappy(avro_codec_t c, void * data, int64_t len)
{
        uint32_t crc;
        size_t outlen = snappy_max_compressed_length(len);

	if (!c->block_data) {
		c->block_data = avro_malloc(outlen+4);
		c->block_size = outlen+4;
	} else if (c->block_size < (int64_t) (outlen+4)) {
            c->block_data = avro_realloc(c->block_data, c->block_size, (outlen+4));
		c->block_size = outlen+4;
	}

	if (!c->block_data) {
		avro_set_error("Cannot allocate memory for snappy");
		return 1;
	}

        if (snappy_compress(data, len, c->block_data, &outlen) != SNAPPY_OK)
        {
                avro_set_error("Error compressing block with Snappy");
		return 1;
	}

        crc = __bswap_32(crc32(0, data, len));
        memcpy(c->block_data+outlen, &crc, 4);
        c->used_size = outlen+4;

	return 0;
}
コード例 #3
0
size_t
mongoc_compressor_max_compressed_length (int32_t compressor_id, size_t len)
{
   TRACE ("Getting compression length for '%s' (%d)",
          mongoc_compressor_id_to_name (compressor_id),
          compressor_id);
   switch (compressor_id) {
#ifdef MONGOC_ENABLE_COMPRESSION_SNAPPY
   case MONGOC_COMPRESSOR_SNAPPY_ID:
      return snappy_max_compressed_length (len);
      break;
#endif

#ifdef MONGOC_ENABLE_COMPRESSION_ZLIB
   case MONGOC_COMPRESSOR_ZLIB_ID:
      return compressBound (len);
      break;
#endif

   case MONGOC_COMPRESSOR_NOOP_ID:
      return len;
      break;
   default:
      return 0;
   }
}
コード例 #4
0
ファイル: SnappyPlugin.c プロジェクト: kleopatra999/sqnappy
primitiveMaxCompressedLength(void)
{
	size_t destLength;
	sqInt num;
	size_t sourceLength;

	if (!((interpreterProxy->methodArgumentCount()) == 1)) {
		interpreterProxy->primitiveFail(); return;
	}
	/* begin stackPositiveIntegerValue: */
	num = interpreterProxy->stackValue(0);
	if ((interpreterProxy->isIntegerValue(num))
	 && (num < 0)) {
		sourceLength = ((sqInt) null);
		goto l1;
	}
	sourceLength = ((sqInt) (interpreterProxy->positive32BitValueOf(num)));
l1:	/* end stackPositiveIntegerValue: */;
	destLength = snappy_max_compressed_length(sourceLength);
	if (destLength < 1) {
		interpreterProxy->primitiveFail(); return;
	}
	/* begin returnIntegerFor: */
	interpreterProxy->pop((interpreterProxy->methodArgumentCount()) + 1);
	if (destLength <= 1073741823) {
		interpreterProxy->pushInteger(destLength);
	}
	else {
		interpreterProxy->push(interpreterProxy->positive32BitIntegerFor(destLength));
	}
}
コード例 #5
0
ファイル: Logger.cpp プロジェクト: BADBDY23/BHumanCodeRelease
void Logger::writeThread()
{
  OutBinaryFile file(logFilename);
  ASSERT(file.exists());
  file << logFileCompressed; // Write magic byte that indicates a compressed log file

  const size_t compressedSize = snappy_max_compressed_length(parameters.blockSize + 2 * sizeof(unsigned));
  std::vector<char> compressedBuffer(compressedSize + sizeof(unsigned)); // Also reserve 4 bytes for header

  while(writerThread.isRunning()) // Check if we are expecting more data
    if(framesToWrite.wait(100)) // Wait 100 ms for new data then check again if we should quit
    {
      writerIdle = false;
      MessageQueue& queue = *buffer[readIndex];
      if(queue.getNumberOfMessages() > 0)
      {
        size_t size = compressedSize;
        VERIFY(snappy_compress(queue.getStreamedData(), queue.getStreamedSize(),
                               compressedBuffer.data() + sizeof(unsigned), &size) == SNAPPY_OK);
        (unsigned&) compressedBuffer[0] = (unsigned) size;
        file.write(compressedBuffer.data(), size + sizeof(unsigned));
        queue.clear();
      }
      readIndex = (readIndex + 1) % parameters.maxBufferSize;
    }
    else if(!writerIdle)
    {
      writerIdle = true;
      writerIdleStart = SystemCall::getCurrentSystemTime();
    }
}
コード例 #6
0
void ColorTable::serialize(In* in, Out* out)
{
  STREAM_REGISTER_BEGIN;
  size_t ctUncompressedSize = sizeof(colorTable);
  size_t ctCompressedSize = 0;
  std::vector<char> ctCompressed;

  if(out)
  {
    ctCompressedSize = snappy_max_compressed_length(ctUncompressedSize);
    ctCompressed.resize(ctCompressedSize);
    VERIFY(snappy_compress(reinterpret_cast<char*>(&colorTable[0][0][0]), ctUncompressedSize, ctCompressed.data(), &ctCompressedSize) == SNAPPY_OK);
    out->write(&ctCompressedSize, sizeof(int));
    out->write(ctCompressed.data(), ctCompressedSize);
  }
  else
  {
    in->read(&ctCompressedSize, sizeof(int));
    ctCompressed.resize(ctCompressedSize);
    in->read(ctCompressed.data(), ctCompressedSize);
    VERIFY(snappy_uncompress(ctCompressed.data(), ctCompressedSize, reinterpret_cast<char*>(&colorTable[0][0][0]), &ctUncompressedSize) == SNAPPY_OK);
    ASSERT(ctUncompressedSize == sizeof(colorTable));
  }
  STREAM_REGISTER_FINISH;
}
コード例 #7
0
ファイル: verify.c プロジェクト: MrBadger42/snappy-c
int main(int ac, char **av)
{
	int failed = 0, verbose = 0;
	struct snappy_env env;
	snappy_init_env(&env);

	if (av[1] && !strcmp(av[1], "-v")) {
		verbose++;
		av++;
	}

	while (*++av) { 
		size_t size;
		char *map = mapfile(*av, O_RDONLY, &size);
		if (!map) {
			if (size > 0) {
				perror(*av);
				failed = 1;
			}
			continue;
		}

		size_t outlen;
		int err;       
		char *out = xmalloc(snappy_max_compressed_length(size));
		char *buf2 = xmalloc(size);

		err = snappy_compress(&env, map, size, out, &outlen);		
		if (err) {
			failed = 1;
			printf("compression of %s failed: %d\n", *av, err);
			goto next;
		}
		err = snappy_uncompress(out, outlen, buf2);

		if (err) {
			failed = 1;
			printf("uncompression of %s failed: %d\n", *av, err);
			goto next;
		}
		if (memcmp(buf2, map, size)) {
			int o = compare(buf2, map, size);			
			if (o >= 0) {
				failed = 1;
				printf("final comparision of %s failed at %d of %lu\n", 
				       *av, o, (unsigned long)size);
			}
		} else {
			if (verbose)
				printf("%s OK!\n", *av);
		}

	next:
		unmap_file(map, size);
		free(out);
		free(buf2);
	}
	return failed;
}
コード例 #8
0
ファイル: hoge.cpp プロジェクト: kengonakajima/snippets
int memCompressSnappy( char *out, int outlen, char *in, int inlen ) {
    size_t maxsz = snappy_max_compressed_length(inlen);
    assertmsg( outlen >= maxsz, "snappy requires buffer size:%d given:%d", maxsz, outlen );
    size_t osz = outlen;
    snappy_status ret = snappy_compress( in, inlen, out, &osz);
    if(ret == SNAPPY_OK ) return (int)osz; else assertmsg(false,"snappy_compress failed. outlen:%d inlen:%d ret:%d", outlen, inlen,ret );
    return 0;
}
コード例 #9
0
ファイル: SnappyPlugin.c プロジェクト: kleopatra999/sqnappy
primitiveCompressWithOffset(void)
{
	char* compressed;
	size_t compressedLength;
	const char*input;
	sqInt inputLength;
	sqInt inputObj;
	size_t inputOffset;
	sqInt num;
	char* ptr;
	sqInt status;

	if (!((interpreterProxy->methodArgumentCount()) == 2)) {
		interpreterProxy->primitiveFail(); return;
	}
	inputObj = interpreterProxy->stackValue(1);
	/* begin charPointerFor: */
	if (!(interpreterProxy->isBytes(inputObj))) {
		input = ((char*) null);
		goto l1;
	}
	ptr = ((char*) (interpreterProxy->firstIndexableField(inputObj)));
	input = ((char*) ptr);
l1:	/* end charPointerFor: */;
	if (!(input)) {
		interpreterProxy->primitiveFail(); return;
	}
	/* begin stackPositiveIntegerValue: */
	num = interpreterProxy->stackValue(0);
	if ((interpreterProxy->isIntegerValue(num))
	 && (num < 0)) {
		inputOffset = ((sqInt) null);
		goto l2;
	}
	inputOffset = ((sqInt) (interpreterProxy->positive32BitValueOf(num)));
l2:	/* end stackPositiveIntegerValue: */;
	inputLength = interpreterProxy->byteSizeOf(interpreterProxy->stackValue(1));
	;
	compressedLength = snappy_max_compressed_length(inputLength);
	if (compressedLength < 1) {
		interpreterProxy->primitiveFail(); return;
	}
	compressed = malloc(compressedLength);
	if (!(compressed)) {
		interpreterProxy->primitiveFail(); return;
	}
	status = snappy_compress(input + inputOffset, inputLength, compressed, &compressedLength);
	if (!(status == 0)) {
		/* begin returnErrorInfoFor: */
		interpreterProxy->pop((interpreterProxy->methodArgumentCount()) + 1);
		interpreterProxy->pushInteger(-status);
		return;
	}
	interpreterProxy->pop((interpreterProxy->methodArgumentCount()) + 1);
	interpreterProxy->push(oopFromCBytessized(compressed, compressedLength));
	free(compressed);
}
コード例 #10
0
CChunkFileReader::Error CChunkFileReader::SaveFile(const std::string& _rFilename, int _Revision, const char *_VersionString, u8 *buffer, size_t sz) {
    INFO_LOG(COMMON, "ChunkReader: Writing %s" , _rFilename.c_str());

    File::IOFile pFile(_rFilename, "wb");
    if (!pFile)
    {
        ERROR_LOG(COMMON, "ChunkReader: Error opening file for write");
        return ERROR_BAD_FILE;
    }

    bool compress = true;

    // Create header
    SChunkHeader header;
    header.Compress = compress ? 1 : 0;
    header.Revision = _Revision;
    header.ExpectedSize = (u32)sz;
    header.UncompressedSize = (u32)sz;
    strncpy(header.GitVersion, _VersionString, 32);
    header.GitVersion[31] = '\0';

    // Write to file
    if (compress) {
        size_t comp_len = snappy_max_compressed_length(sz);
        u8 *compressed_buffer = new u8[comp_len];
        snappy_compress((const char *)buffer, sz, (char *)compressed_buffer, &comp_len);
        delete [] buffer;
        header.ExpectedSize = (u32)comp_len;
        if (!pFile.WriteArray(&header, 1))
        {
            ERROR_LOG(COMMON, "ChunkReader: Failed writing header");
            return ERROR_BAD_FILE;
        }
        if (!pFile.WriteBytes(&compressed_buffer[0], comp_len)) {
            ERROR_LOG(COMMON, "ChunkReader: Failed writing compressed data");
            return ERROR_BAD_FILE;
        }	else {
            INFO_LOG(COMMON, "Savestate: Compressed %i bytes into %i", (int)sz, (int)comp_len);
        }
        delete [] compressed_buffer;
    } else {
        if (!pFile.WriteArray(&header, 1))
        {
            ERROR_LOG(COMMON, "ChunkReader: Failed writing header");
            return ERROR_BAD_FILE;
        }
        if (!pFile.WriteBytes(&buffer[0], sz))
        {
            ERROR_LOG(COMMON, "ChunkReader: Failed writing data");
            return ERROR_BAD_FILE;
        }
        delete [] buffer;
    }

    INFO_LOG(COMMON, "ChunkReader: Done writing %s",  _rFilename.c_str());
    return ERROR_NONE;
}
コード例 #11
0
ファイル: hap.c プロジェクト: jossgray/hap
unsigned long HapMaxEncodedLength(unsigned long inputBytes)
{
    /*
     Actually our max encoded length is inputBytes + 8U but snappy may produce longer output
     and the only way we can find out is by trying with a suitably-sized buffer
     */
    unsigned long compressedLength = snappy_max_compressed_length(inputBytes);
    if (compressedLength < inputBytes) compressedLength = inputBytes;
    return compressedLength + 8U;
}
コード例 #12
0
ファイル: compress.c プロジェクト: FromPointer/nessDB
uint32_t ness_compress_bound(ness_compress_method_t m, uint32_t size)
{
	switch (m) {
	case NESS_NO_COMPRESS:
		return size + 1;
	case NESS_SNAPPY_METHOD:
		return snappy_max_compressed_length(size) + 1;
	default:
		break;
	}

	return 0;
}
コード例 #13
0
ファイル: example_snappy.c プロジェクト: ifadams/mercury
static hg_return_t
snappy_pull_cb(const struct hg_bulk_cb_info *hg_bulk_cb_info)
{
    struct snappy_transfer_args *snappy_transfer_args =
            (struct snappy_transfer_args *) hg_bulk_cb_info->arg;
    hg_return_t ret = HG_SUCCESS;
    void *input;
    size_t input_length;
    size_t source_length = HG_Bulk_get_size(snappy_transfer_args->local_input_bulk_handle);

    /* Get pointer to input buffer from local handle */
    HG_Bulk_access(hg_bulk_cb_info->local_handle, 0, source_length,
            HG_BULK_READ_ONLY, 1, &input, &input_length, NULL);
    printf("Transferred input buffer of length: %zu\n", input_length);
    print_buf(20, (int *) input);

    /* Allocate compressed buffer for compressing input data */
    snappy_transfer_args->compressed_length = snappy_max_compressed_length(input_length);
    snappy_transfer_args->compressed = malloc(snappy_transfer_args->compressed_length);

    /* Compress data */
    printf("Compressing buffer...\n");
    snappy_transfer_args->ret = snappy_compress(input, input_length,
            snappy_transfer_args->compressed, &snappy_transfer_args->compressed_length);
    printf("Return value of snappy_compress is: %d\n", snappy_transfer_args->ret);
    printf("Compressed buffer length is: %zu\n", snappy_transfer_args->compressed_length);
    print_buf(5, (int *) snappy_transfer_args->compressed);

    /* Free bulk handles */
    HG_Bulk_free(snappy_transfer_args->local_input_bulk_handle);

    if (snappy_validate_compressed_buffer(snappy_transfer_args->compressed,
            snappy_transfer_args->compressed_length) == SNAPPY_OK) {
        printf("Compressed buffer validated: compressed successfully\n");
    }

    /* Now set up bulk transfer for "push to origin" callback */
    HG_Bulk_create(HG_Get_info(snappy_transfer_args->handle)->hg_bulk_class, 1,
            &snappy_transfer_args->compressed, &snappy_transfer_args->compressed_length,
            HG_BULK_WRITE_ONLY, &snappy_transfer_args->local_compressed_bulk_handle);

    HG_Bulk_transfer(HG_Get_info(snappy_transfer_args->handle)->bulk_context,
            snappy_push_cb, snappy_transfer_args,
            HG_BULK_PUSH, HG_Get_info(snappy_transfer_args->handle)->addr,
            snappy_transfer_args->snappy_compress_input.compressed_bulk_handle, 0, /* origin */
            snappy_transfer_args->local_compressed_bulk_handle, 0, /* local */
            snappy_transfer_args->compressed_length, HG_OP_ID_IGNORE);

    return ret;
}
コード例 #14
0
static int snp_compress(struct crypto_tfm *tfm, const u8 *src,
			    unsigned int slen, u8 *dst, unsigned int *dlen)
{
	struct snappy_ctx *ctx = crypto_tfm_ctx(tfm);
	size_t olen;
	int err;

	/* XXXX very pessimistic. check in snappy? */
	if (*dlen < snappy_max_compressed_length(*dlen))
		return -EINVAL;
	err = snappy_compress(&ctx->env, src, slen, dst, &olen);
	*dlen = olen;
	return err;
}
コード例 #15
0
/*
 * wt_snappy_pre_size --
 *  WiredTiger snappy destination buffer sizing.
 */
static int wt_snappy_pre_size(WT_COMPRESSOR *compressor, WT_SESSION *session, uint8_t *src, size_t src_len, size_t *result_lenp)
{
    (void) compressor; /* Unused parameters */
    (void) session;
    (void) src;

    /*
     * Snappy requires the dest buffer be somewhat larger than the source.
     * Fortunately, this is fast to compute, and will give us a dest buffer
     * in wt_snappy_compress that we can compress to directly.  We add space
     * in the dest buffer to store the accurate compressed size.
     */
    *result_lenp = snappy_max_compressed_length(src_len) + sizeof(size_t);
    return (0);
}
コード例 #16
0
ファイル: mkdiskdump.c プロジェクト: jeffmahoney/libkdumpfile
static size_t
do_snappy(struct page_data *pg)
{
	struct page_data_kdump *pgkdump = pg->priv;
	size_t clen;

	clen = snappy_max_compressed_length(pg->len);
	if (clen > pgkdump->cbufsz &&
	    !(clen = enlarge_cbuf(pgkdump, clen)))
		return clen;

	if (snappy_compress((const char*)pg->buf, pg->len,
			    pgkdump->cbuf, &clen) != SNAPPY_OK) {
		fprintf(stderr, "snappy compression failed\n");
		clen = 0;
	}
	return clen;
}
コード例 #17
0
ファイル: snzip.c プロジェクト: nresare/snzip
int work_buffer_init(work_buffer_t *wb, size_t block_size)
{
    wb->uclen = block_size;
    wb->uc = malloc(wb->uclen);
    if (wb->uc == NULL) {
        fprintf(stderr, "Out of memory\n");
        exit(1);
    }
    wb->clen = snappy_max_compressed_length(wb->uclen);
    wb->c = malloc(wb->clen);
    if (wb->c == NULL) {
        fprintf(stderr, "Out of memory\n");
        exit(1);
    }
    trace("max length of compressed data = %lu\n", wb->clen);
    trace("max length of uncompressed data = %lu\n", wb->uclen);
    return 0;
}
コード例 #18
0
ssize_t internal_buf_write_compressed(struct trace_internal_buf *buf, const struct iovec *iov, int iovcnt)
{
    const size_t max_size = snappy_max_compressed_length(total_iovec_len(iov, iovcnt));
    const size_t max_recs = internal_buf_bytes_to_recs(max_size);
    const unsigned start_idx = internal_buf_alloc_space(buf, max_recs);
    if (INTERNAL_BUF_INVALID_INDEX == start_idx) {
        return -1;
    }

    const ssize_t compressed_len = compress_iov_to_buffer(buf->records + start_idx, iov, iovcnt);
    if (compressed_len < 0) {
        compactify_buffer(buf, start_idx, 0);
        return -1;
    }

    compactify_buffer(buf, start_idx, internal_buf_bytes_to_recs(compressed_len));
    fill_compression_remainder_with_pattern(buf, compressed_len);

    return compressed_len;
}
コード例 #19
0
ファイル: hap.c プロジェクト: Vidvox/hap
static size_t hap_max_encoded_length(size_t input_bytes, unsigned int texture_format, unsigned int compressor, unsigned int chunk_count)
{
    size_t decode_instructions_length, max_compressed_length;

    chunk_count = hap_limited_chunk_count_for_frame(input_bytes, texture_format, chunk_count);

    decode_instructions_length = hap_decode_instructions_length(chunk_count);

    if (compressor == HapCompressorSnappy)
    {
        size_t chunk_size = input_bytes / chunk_count;
        max_compressed_length = snappy_max_compressed_length(chunk_size) * chunk_count;
    }
    else
    {
        max_compressed_length = input_bytes;
    }

    // top section header + decode instructions section header + decode instructions + compressed data
    return max_compressed_length + 8U + decode_instructions_length + 4U;
}
コード例 #20
0
Datum
snappy_compress_internal(PG_FUNCTION_ARGS)
{
	const char		*src = PG_GETARG_POINTER(0);
	size_t			src_sz = PG_GETARG_INT32(1);
	char			*dst = PG_GETARG_POINTER(2);
	size_t			dst_sz = PG_GETARG_INT32(3);
	size_t			*dst_used = PG_GETARG_POINTER(4);
	size_t			compressed_length;
	snappy_status	retval;

	compressed_length = snappy_max_compressed_length(src_sz);
	Insist(dst_sz >= compressed_length);

	retval = snappy_compress(src, src_sz, dst, &compressed_length);
	*dst_used = compressed_length;

	if (retval != SNAPPY_OK)
		elog_snappy_error(retval, "snappy_compress", src_sz, dst_sz, *dst_used);

	PG_RETURN_VOID();
}
コード例 #21
0
ファイル: snappy.c プロジェクト: Dzuii/php-ext-snappy
static ZEND_FUNCTION(snappy_compress)
{
    zval *data;
    char *output;
    size_t output_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                              "z", &data) == FAILURE) {
        RETURN_FALSE;
    }

    if (Z_TYPE_P(data) != IS_STRING) {
        zend_error(E_WARNING,
                   "snappy_compress : expects parameter to be string.");
        RETURN_FALSE;
    }

    output_len = snappy_max_compressed_length(Z_STRLEN_P(data));
    output = (char *)emalloc(output_len);
    if (!output) {
        zend_error(E_WARNING, "snappy_compress : memory error");
        RETURN_FALSE;
    }


    if (snappy_compress(Z_STRVAL_P(data), Z_STRLEN_P(data),
                        output, &output_len) == SNAPPY_OK) {
#if ZEND_MODULE_API_NO >= 20141001
        RETVAL_STRINGL(output, output_len);
#else
        RETVAL_STRINGL(output, output_len, 1);
#endif
    } else {
        RETVAL_FALSE;
    }

    efree(output);
}
コード例 #22
0
ファイル: comment-43-format.c プロジェクト: Karry/snzip
static int comment_43_uncompress(FILE *infp, FILE *outfp, int skip_magic)
{
  block_data_t *block_data = malloc(sizeof(block_data_t));
  size_t work_len = snappy_max_compressed_length(UINT16_MAX); /* length of worst case */
  char *work = malloc(work_len);
  int err = 1;
  stream_state_t state = skip_magic ? PROCESSING_STATE : INITIAL_STATE;

  if (block_data == NULL || work == NULL) {
    print_error("out of memory\n");
    goto cleanup;
  }

  while (state != ERROR_STATE) {
    switch (read_block(infp, block_data)) {
    case EOF:
      if (state == END_OF_STREAM_STATE) {
        err = 0; /* success */
        goto cleanup;
      }
      /* FALLTHROUGH */
    case TOO_SHORT_DATA_BLOCK:
      if (feof_unlocked(infp)) {
        print_error("Unexpected end of file\n");
      } else {
        print_error("Failed to read a file: %s\n", strerror(errno));
      }
      goto cleanup;
    }
    state = process_block(outfp, state, block_data, work, work_len);
  }
 cleanup:
  free(block_data);
  free(work);
  return err;
}
コード例 #23
0
ファイル: squash-snappy.c プロジェクト: Dr-Emann/squash
static size_t
squash_snappy_get_max_compressed_size (SquashCodec* codec, size_t uncompressed_length) {
  return snappy_max_compressed_length (uncompressed_length);
}
コード例 #24
0
ファイル: example_snappy_client.c プロジェクト: carns/mercury
static int
snappy_compress_rpc(hg_class_t *hg_class, hg_context_t *hg_context,
        na_addr_t na_target_addr)
{
    int *input;
    size_t source_length = NR_ITEMS * sizeof(int);
    hg_bulk_t input_bulk_handle;

    void *compressed;
    size_t max_compressed_length;
    hg_bulk_t compressed_bulk_handle;

    snappy_compress_in_t snappy_compress_input;
    struct snappy_compress_rpc_args *snappy_compress_rpc_args;
    hg_handle_t handle;
    int i;

    /**
     * We are going to take a buffer and send it to the server for compression.
     * Mercury works better when you know how much (or an uppper bound on) data
     * to expect.
     */
    max_compressed_length = snappy_max_compressed_length(source_length);
    printf("Input buffer length is: %zu\n", source_length);
    printf("Max compressed length is: %zu\n", max_compressed_length);

    /* Generate input buffer */
    input = (int *) malloc(source_length);
    for (i = 0; i < NR_ITEMS; i++) {
        input[i] = rand() % 10;
    }
    print_buf(20, input);

    /* Allocate compressed buffer */
    compressed = malloc(max_compressed_length);
    memset(compressed, '\0', max_compressed_length);

    /* Create HG handle bound to target */
    HG_Create(hg_context, na_target_addr, snappy_compress_id_g, &handle);

    /**
     * Associate 'handle' with a region of memory. Mercury's bulk transfer is
     * going to get/put data from this region.
     */
    HG_Bulk_create(hg_class, 1, (void **) &input,
            &source_length, HG_BULK_READ_ONLY, &input_bulk_handle);
    HG_Bulk_create(hg_class, 1, &compressed,
            &max_compressed_length, HG_BULK_READWRITE, &compressed_bulk_handle);

    /* Create struct to keep arguments as the call will be executed
     * asynchronously */
    snappy_compress_rpc_args = (struct snappy_compress_rpc_args *) malloc(
            sizeof(struct snappy_compress_rpc_args));
    snappy_compress_rpc_args->input = input;
    snappy_compress_rpc_args->input_length = source_length;
    snappy_compress_rpc_args->input_bulk_handle = input_bulk_handle;
    snappy_compress_rpc_args->compressed = compressed;
    snappy_compress_rpc_args->compressed_bulk_handle = compressed_bulk_handle;

    /* Set input arguments that will be passed to HG_Forward */
    snappy_compress_input.input_bulk_handle = input_bulk_handle;
    snappy_compress_input.compressed_bulk_handle = compressed_bulk_handle;

    /* Forward the call */
    printf("Sending input to target\n");
    HG_Forward(handle, snappy_compress_rpc_cb, snappy_compress_rpc_args,
            &snappy_compress_input);

    /* Handle will be destroyed when call completes (reference count) */
    HG_Destroy(handle);

    return 0;
}
コード例 #25
0
ファイル: hapenc.c プロジェクト: Bilibili/FFmpeg
static av_cold int hap_init(AVCodecContext *avctx)
{
    HapContext *ctx = avctx->priv_data;
    int ratio;
    int corrected_chunk_count;
    int ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);

    if (ret < 0) {
        av_log(avctx, AV_LOG_ERROR, "Invalid video size %dx%d.\n",
               avctx->width, avctx->height);
        return ret;
    }

    if (avctx->width % 4 || avctx->height % 4) {
        av_log(avctx, AV_LOG_ERROR, "Video size %dx%d is not multiple of 4.\n",
               avctx->width, avctx->height);
        return AVERROR_INVALIDDATA;
    }

    ff_texturedspenc_init(&ctx->dxtc);

    switch (ctx->opt_tex_fmt) {
    case HAP_FMT_RGBDXT1:
        ratio = 8;
        avctx->codec_tag = MKTAG('H', 'a', 'p', '1');
        avctx->bits_per_coded_sample = 24;
        ctx->tex_fun = ctx->dxtc.dxt1_block;
        break;
    case HAP_FMT_RGBADXT5:
        ratio = 4;
        avctx->codec_tag = MKTAG('H', 'a', 'p', '5');
        avctx->bits_per_coded_sample = 32;
        ctx->tex_fun = ctx->dxtc.dxt5_block;
        break;
    case HAP_FMT_YCOCGDXT5:
        ratio = 4;
        avctx->codec_tag = MKTAG('H', 'a', 'p', 'Y');
        avctx->bits_per_coded_sample = 24;
        ctx->tex_fun = ctx->dxtc.dxt5ys_block;
        break;
    default:
        av_log(avctx, AV_LOG_ERROR, "Invalid format %02X\n", ctx->opt_tex_fmt);
        return AVERROR_INVALIDDATA;
    }

    /* Texture compression ratio is constant, so can we computer
     * beforehand the final size of the uncompressed buffer. */
    ctx->tex_size   = FFALIGN(avctx->width,  TEXTURE_BLOCK_W) *
                      FFALIGN(avctx->height, TEXTURE_BLOCK_H) * 4 / ratio;

    switch (ctx->opt_compressor) {
    case HAP_COMP_NONE:
        /* No benefit chunking uncompressed data */
        corrected_chunk_count = 1;

        ctx->max_snappy = ctx->tex_size;
        ctx->tex_buf = NULL;
        break;
    case HAP_COMP_SNAPPY:
        /* Round the chunk count to divide evenly on DXT block edges */
        corrected_chunk_count = av_clip(ctx->opt_chunk_count, 1, HAP_MAX_CHUNKS);
        while ((ctx->tex_size / (64 / ratio)) % corrected_chunk_count != 0) {
            corrected_chunk_count--;
        }

        ctx->max_snappy = snappy_max_compressed_length(ctx->tex_size / corrected_chunk_count);
        ctx->tex_buf = av_malloc(ctx->tex_size);
        if (!ctx->tex_buf) {
            return AVERROR(ENOMEM);
        }
        break;
    default:
        av_log(avctx, AV_LOG_ERROR, "Invalid compresor %02X\n", ctx->opt_compressor);
        return AVERROR_INVALIDDATA;
    }
    if (corrected_chunk_count != ctx->opt_chunk_count) {
        av_log(avctx, AV_LOG_INFO, "%d chunks requested but %d used.\n",
                                    ctx->opt_chunk_count, corrected_chunk_count);
    }
    ret = ff_hap_set_chunk_count(ctx, corrected_chunk_count, 1);
    if (ret != 0)
        return ret;

    return 0;
}
コード例 #26
0
ファイル: compile.c プロジェクト: josephgodwinkimani/wrench
int main(int argc, char* argv[])
{
	char* output;
	char* input;
	FILE* fwork;
	size_t output_length;
	size_t input_length;
	size_t execlen = 0;
	int result = 0;
	char* cflags = NULL;
	char* exec_cmd = NULL;
	char rc_cmd[128] = "\0";
	bool no_compression = false;
	bool exec_temp = false;
	int i = 0;
	
	if(_access("obj", 00) != 0) {
		if(_mkdir("obj") != 0) {
			fatal("Could not create intermediate folder.");
		}
	}
	
	for(i = 1; i < argc; i++) {
		if((_stricmp(argv[i], "--no-compression") == 0) || (_stricmp(argv[i], "-n") == 0)){
			no_compression = true;
		} else if((_stricmp(argv[i], "--no-local") == 0) || (_stricmp(argv[i], "-l") == 0)) {
			exec_temp = true;
		} else if((_stricmp(argv[i], "--help") == 0)||(_stricmp(argv[i], "-h") == 0)) {
			out("Usage: %s [--no-compression|-n] [--no-local|-l] [-h|--help]\n", argv[0]);
			exit(0);
		}
	}
	
	if(_access(STUB_OUT, 00) == 0) _unlink(STUB_OUT);
	
	if((fwork = fopen(STUB_IN, "rb")) == NULL) {
		fatal("Failed to open input file.");
	}
	
	if((input_length = get_file_length(fwork)) == -1) {
		fatal("Could not get filesize of " STUB_IN ".");
	}
	
	input = (char*)malloc((input_length+1)*sizeof(char));
	memset((void*)input, 0, (input_length+1)*sizeof(char));
	if(input == NULL) {
		fatal("Could not allocate buffer for uncompressed data.");
	}
	
	if(!fread((void*)input, sizeof(char), input_length, fwork)) {
		free(input);
		fatal("Failed to read input file.");
	}
	
	fclose(fwork);
	output_length = snappy_max_compressed_length(input_length);
	output = (char*)malloc(output_length);
	if(output == NULL) {
		free(input);
		fatal("Could not allocate buffer for compressed data.");
	}
	
	if (snappy_compress(input, input_length, output, &output_length) == SNAPPY_OK) {
		free(input);
		outl("Compressed data.");
		if((fwork = fopen(STUB_OUT, "wb")) == NULL) {
			fatal("Failed to open output file.");
		}
		if(!fwrite((const void*)output, sizeof(char), output_length / sizeof(char), fwork)) {
			fatal("Failed to write to output file.");
		}
		fclose(fwork);
	} else {
		free(input);
		free(output);
		fatal("Failed to compress stub file.");
	}
	free(output);

	if(_access(STUB_OBJ, 00) == 0) _unlink(STUB_OBJ);
	if(_access(EDIT_OBJ, 00) == 0) _unlink(EDIT_OBJ);
	if(_access(STUB_RES, 00) == 0) _unlink(STUB_RES);
	if(_access(EDIT_RES, 00) == 0) _unlink(EDIT_RES);
	
	// Compile stub.rc
	outl("\nCompiling resource file.");
	strcpy(rc_cmd, RC_CMD);
	if(!exec_temp) strcat(rc_cmd, RC_EXEC_LOCAL);
	if(no_compression) strcat(rc_cmd, RC_NO_COMPRESS);
	strcat(rc_cmd, STUB_RC);
	
	if((result = system(rc_cmd)) != EXIT_SUCCESS) {
		fatal("Failed to compile " STUB_RC "\n\nrc.exe returned code %d", result);
	}
	
	
	// cl.exe -Foobj\stub.obj src\stub.c obj\stub.res -link -LTCG -OPT:REF -OPT:ICF\0
	execlen = strlen(CL_CMD CL_END);
	if((cflags = getenv("CFLAGS")) != NULL) {
		execlen += lstrlenA(cflags) + 1;
	}
	
	if(!exec_temp) execlen += 15;
	if(no_compression) execlen += 19;
	
	exec_cmd = (char*)malloc(sizeof(char) * execlen);
	if(exec_cmd == NULL) {
		fatal("Could not allocate buffer for command-line.");
	}
	
	memset((void*)exec_cmd, 0, sizeof(char) * execlen);
	strcpy(exec_cmd, CL_CMD);
	if(cflags != NULL) {
		strcat(exec_cmd, cflags);
		strcat(exec_cmd, " ");
	}
	if(!exec_temp) {
		out("Enabling local-exec.\n");
		strcat(exec_cmd, CL_EXEC_LOCAL);
	}
	if(no_compression) {
		out("Enabling resource compression.\n");
		strcat(exec_cmd, CL_NO_COMPRESS);
	}
	
	strcat(exec_cmd, CL_END);

	outl("Compiling source files.\n");
	if((result = system(exec_cmd)) != EXIT_SUCCESS) {
		free(exec_cmd);
		fatal("Failed to compile stub.c\n\ncl.exe returned code %d", result);
	}
	free(exec_cmd);
	
	// Compile bat2exe.rc
	strcpy(rc_cmd, "");
	outl("\nCompiling resource file.");
	strcpy(rc_cmd, RC_EDIT_CMD);
	if(!exec_temp) strcat(rc_cmd, RC_EXEC_LOCAL);
	if(no_compression) strcat(rc_cmd, RC_NO_COMPRESS);
	strcat(rc_cmd, EDIT_RC);
	
	if((result = system(rc_cmd)) != EXIT_SUCCESS) {
		fatal("Failed to compile " EDIT_RC "\n\nrc.exe returned code %d", result);
	}
	
	// cl.exe %CFLAGS% -Foobj\bat2exe.obj src\bat2exe.c obj\bat2exe.res -link -LTCG -OPT:REF -OPT:ICF\0
	execlen = strlen(CL_EDIT_CMD CL_EDIT_END);
	if((cflags = getenv("CFLAGS")) != NULL) {
		execlen += lstrlenA(cflags) + 1;
	}
	
	if(!exec_temp) execlen += 15;
	if(no_compression) execlen += 19;
	
	exec_cmd = (char*)malloc(sizeof(char) * execlen);
	if(exec_cmd == NULL) {
		fatal("Could not allocate buffer for command-line.");
	}
	memset((void*)exec_cmd, 0, sizeof(char) * execlen);
	strcpy(exec_cmd, CL_EDIT_CMD);
	if(cflags != NULL) {
		strcat(exec_cmd, cflags);
		strcat(exec_cmd, " ");
	}
	if(!exec_temp) {
		out("Enabling local-exec.\n");
		strcat(exec_cmd, CL_EXEC_LOCAL);
	}
	if(no_compression) {
		out("Enabling resource compression.\n");
		strcat(exec_cmd, CL_NO_COMPRESS);
	}
	strcat(exec_cmd, CL_EDIT_END);
	if((result = system(exec_cmd)) != EXIT_SUCCESS) {
		free(exec_cmd);
		fatal("Failed to compile batcompile.c\n\ncl.exe returned code %d", result);
	}
	free(exec_cmd);
	return 0;
}
コード例 #27
0
ファイル: comment-43-format.c プロジェクト: Karry/snzip
static int comment_43_compress(FILE *infp, FILE *outfp, size_t block_size)
{
  const size_t max_raw_data_len = 32 * 1024; /* maximum data length */
  const size_t max_compressed_data_len = snappy_max_compressed_length(max_raw_data_len); /* maximum compressed length */
  size_t raw_data_len;
  size_t compressed_data_len;
  char *raw_data = malloc(max_raw_data_len);
  char *compressed_data = malloc(max_compressed_data_len);
  int err = 1;

  if (raw_data == NULL || compressed_data == NULL) {
    print_error("out of memory\n");
    goto cleanup;
  }

  putc_unlocked(HEADER_TYPE_CODE, outfp);
  putc_unlocked(MAGIC_LEN, outfp);
  putc_unlocked(MAGIC_LEN >> 8, outfp);
  fwrite_unlocked(MAGIC, MAGIC_LEN, 1, outfp);

  /* write file body */
  while ((raw_data_len = fread_unlocked(raw_data, 1, max_raw_data_len, infp)) > 0) {
    unsigned int crc32c = masked_crc32c(raw_data, raw_data_len);
    char type_code;
    size_t write_len;
    const char *write_data;

    /* compress the block. */
    compressed_data_len = max_compressed_data_len;
    snappy_compress(raw_data, raw_data_len, compressed_data, &compressed_data_len);

    if (compressed_data_len >= (raw_data_len - (raw_data_len / 8))) {
      /* write uncompressed data */
      type_code = UNCOMPRESSED_TYPE_CODE;
      write_len = raw_data_len;
      write_data = raw_data;
    } else {
      /* write compressed data */
      type_code = COMPRESSED_TYPE_CODE;
      write_len = compressed_data_len;
      write_data = compressed_data;
    }

    /* block type */
    putc_unlocked(type_code, outfp);
    /* data length */
    putc_unlocked(((write_len + 4) >> 0), outfp);
    putc_unlocked(((write_len + 4) >> 8), outfp);
    /* data */
    putc_unlocked((crc32c >>  0), outfp);
    putc_unlocked((crc32c >>  8), outfp);
    putc_unlocked((crc32c >> 16), outfp);
    putc_unlocked((crc32c >> 24), outfp);
    if (fwrite_unlocked(write_data, write_len, 1, outfp) != 1) {
      print_error("Failed to write a file: %s\n", strerror(errno));
      goto cleanup;
    }
  }
  if (!feof_unlocked(infp)) {
    /* fread_unlocked() failed. */
    print_error("Failed to read a file: %s\n", strerror(errno));
    goto cleanup;
  }
  putc_unlocked(END_OF_STREAM_TYPE_CODE, outfp);
  putc_unlocked(0, outfp);
  putc_unlocked(0, outfp);
  if (ferror_unlocked(outfp)) {
    print_error("Failed to write a file: %s\n", strerror(errno));
    goto cleanup;
  }
  err = 0;
 cleanup:
  free(raw_data);
  free(compressed_data);
  return err;
}
コード例 #28
0
ファイル: hap.c プロジェクト: jossgray/hap
unsigned int HapEncode(const void *inputBuffer, unsigned long inputBufferBytes, unsigned int textureFormat,
                       unsigned int compressor, void *outputBuffer, unsigned long outputBufferBytes,
                       unsigned long *outputBufferBytesUsed)
{
    size_t maxCompressedLength;
    size_t maxOutputBufferLength;
    size_t headerLength;
    void *compressedStart;
    size_t storedLength;
    unsigned int storedCompressor;
    unsigned int storedFormat;

    /*
     Check arguments
     */
    if (inputBuffer == NULL
        || inputBufferBytes == 0
        || (textureFormat != HapTextureFormat_RGB_DXT1
            && textureFormat != HapTextureFormat_RGBA_DXT5
            && textureFormat != HapTextureFormat_YCoCg_DXT5
            )
        || (compressor != HapCompressorNone
            && compressor != HapCompressorSnappy
            )
        )
    {
        return HapResult_Bad_Arguments;
    }
    
    maxCompressedLength = compressor == HapCompressorSnappy ? snappy_max_compressed_length(inputBufferBytes) : inputBufferBytes;
    if (maxCompressedLength < inputBufferBytes)
    {
        // Sanity check in case a future Snappy promises to always compress
        maxCompressedLength = inputBufferBytes;
    }
    
    /*
     To store frames of length greater than can be expressed in three bytes, we use an eight byte header (the last four bytes are the
     frame size). We don't know the compressed size until we have performed compression, but we know the worst-case size
     (the uncompressed size), so choose header-length based on that.
     
     A simpler encoder could always use the eight-byte header variation.
     */
    if (inputBufferBytes > kHapUInt24Max)
    {
        headerLength = 8U;
    }
    else
    {
        headerLength = 4U;
    }
    
    maxOutputBufferLength = maxCompressedLength + headerLength;
    if (outputBufferBytes < maxOutputBufferLength
        || outputBuffer == NULL)
    {
        return HapResult_Buffer_Too_Small;
    }
    compressedStart = ((uint8_t *)outputBuffer) + headerLength;
    
    if (compressor == HapCompressorSnappy)
    {
        snappy_status result;
        storedLength = outputBufferBytes;
        result = snappy_compress((const char *)inputBuffer, inputBufferBytes, (char *)compressedStart, &storedLength);
        if (result != SNAPPY_OK)
        {
            return HapResult_Internal_Error;
        }
        storedCompressor = kHapCompressorSnappy;
    }
    else
    {
        // HapCompressorNone
        // Setting storedLength to 0 causes the frame to be used uncompressed
        storedLength = 0;
    }
    
    /*
     If our "compressed" frame is no smaller than our input frame then store the input uncompressed.
     */
    if (storedLength == 0 || storedLength >= inputBufferBytes)
    {
        memcpy(compressedStart, inputBuffer, inputBufferBytes);
        storedLength = inputBufferBytes;
        storedCompressor = kHapCompressorNone;
    }
    
    storedFormat = hap_texture_format_identifier_for_format_constant(textureFormat);
    
    hap_write_section_header(outputBuffer, headerLength, storedLength, hap_4_bit_packed_byte(storedCompressor, storedFormat));
    
    if (outputBufferBytesUsed != NULL)
    {
        *outputBufferBytesUsed = storedLength + headerLength;
    }
    
    return HapResult_No_Error;
}
コード例 #29
0
ファイル: framing-format.c プロジェクト: hivefans/snzip
static int framing_format_compress(FILE *infp, FILE *outfp, size_t block_size)
{
  const size_t max_uncompressed_data_len = MAX_UNCOMPRESSED_DATA_LEN;
  const size_t max_compressed_data_len = snappy_max_compressed_length(max_uncompressed_data_len);
  size_t uncompressed_data_len;
  size_t compressed_data_len;
  char *uncompressed_data = malloc(max_uncompressed_data_len);
  char *compressed_data = malloc(max_compressed_data_len);
  int err = 1;

  if (uncompressed_data == NULL || compressed_data == NULL) {
    print_error("out of memory\n");
    goto cleanup;
  }

  /* write the steam header */
  fwrite_unlocked(stream_header, sizeof(stream_header), 1, outfp);

  /* write file body */
  while ((uncompressed_data_len = fread_unlocked(uncompressed_data, 1, max_uncompressed_data_len, infp)) > 0) {
    unsigned int crc32c = masked_crc32c(uncompressed_data, uncompressed_data_len);
    char type_code;
    size_t write_len;
    const char *write_data;

    /* compress the block. */
    compressed_data_len = max_compressed_data_len;
    snappy_compress(uncompressed_data, uncompressed_data_len, compressed_data, &compressed_data_len);

    if (compressed_data_len >= (uncompressed_data_len - (uncompressed_data_len / 8))) {
      /* uncompressed data */
      type_code = UNCOMPRESSED_DATA_IDENTIFIER;
      write_len = uncompressed_data_len;
      write_data = uncompressed_data;
    } else {
      /* compressed data */
      type_code = COMPRESSED_DATA_IDENTIFIER;
      write_len = compressed_data_len;
      write_data = compressed_data;
    }

    /* write block type */
    putc_unlocked(type_code, outfp);
    /* write data length */
    putc_unlocked(((write_len + 4) >> 0), outfp);
    putc_unlocked(((write_len + 4) >> 8), outfp);
    /* write checksum */
    putc_unlocked((crc32c >>  0), outfp);
    putc_unlocked((crc32c >>  8), outfp);
    putc_unlocked((crc32c >> 16), outfp);
    putc_unlocked((crc32c >> 24), outfp);
    /* write data */
    if (fwrite_unlocked(write_data, write_len, 1, outfp) != 1) {
      print_error("Failed to write a file: %s\n", strerror(errno));
      goto cleanup;
    }
  }
  /* check stream errors */
  if (ferror_unlocked(infp)) {
    print_error("Failed to read a file: %s\n", strerror(errno));
    goto cleanup;
  }
  if (ferror_unlocked(outfp)) {
    print_error("Failed to write a file: %s\n", strerror(errno));
    goto cleanup;
  }
  err = 0;
 cleanup:
  free(uncompressed_data);
  free(compressed_data);
  return err;
}
コード例 #30
0
ファイル: compressor.c プロジェクト: JerrySievert/bplus
size_t bp__max_compressed_size(size_t size) {
  return snappy_max_compressed_length(size);
}