Пример #1
0
int _tmain(int argc, _TCHAR* argv[])
{
	FILE* f = NULL;
	errno_t err = fopen_s(&f, "readme.txt", "rb");
	fseek(f, 0, SEEK_END);
	int fileSize = ftell(f);
	fseek(f, 0, SEEK_SET);

	char* source = new char[fileSize];

	int bufferLen = fileSize + 300;
	char* compressed = new char[bufferLen];
	fread(source, fileSize, 1, f);

	fclose(f);

	size_t length = bufferLen;
	snappy_status status = snappy_compress(source, fileSize, compressed, &length);

	delete source;

	char* uncompressed = new char[bufferLen];
	size_t uncompressedLength = bufferLen;
	status = snappy_uncompress(compressed, length, uncompressed, &uncompressedLength);

	assert(uncompressedLength == fileSize);

	delete compressed;
	delete uncompressed;

	return 0;
}
Пример #2
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;
}
Пример #3
0
static int
wt_snappy_decompress(WT_COMPRESSOR *compressor, WT_SESSION *session,
                     uint8_t *src, size_t src_len,
                     uint8_t *dst, size_t dst_len,
                     size_t *result_lenp)
{
    snappy_status snret;
    size_t snaplen;

    __UNUSED(compressor);

    /* retrieve the saved length */
    snaplen = *(size_t *)src;
    if (snaplen + sizeof(size_t) > src_len) {
        wiredtiger_err_printf(
            session,
            "wt_snappy_decompress: stored size exceeds buffer size");
        return (WT_ERROR);
    }

    /* dst_len is an input and an output arg. */
    snret = snappy_uncompress(
                (char *)src + sizeof(size_t), snaplen, (char *)dst, &dst_len);

    if (snret == SNAPPY_OK) {
        *result_lenp = dst_len;
        return (0);
    }

    return (wt_snappy_error(session, "snappy_decompress", snret));
}
Пример #4
0
Datum
snappy_decompress_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);
	int32			dst_sz = PG_GETARG_INT32(3);
	int32			*dst_used = PG_GETARG_POINTER(4);
	size_t			uncompressed_length;
	snappy_status	retval;

	Insist(src_sz > 0 && dst_sz > 0);

	retval = snappy_uncompressed_length((char *) src, (size_t) src_sz,
										&uncompressed_length);
	if (retval != SNAPPY_OK)
		elog_snappy_error(retval, "snappy_uncompressed_length",
						  src_sz, dst_sz, *dst_used);

	Insist(dst_sz >= uncompressed_length);

	retval = snappy_uncompress((char *) src, src_sz, (char *) dst,
							   &uncompressed_length);
	*dst_used = uncompressed_length;

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

	PG_RETURN_VOID();
}
Пример #5
0
/*
 * wt_snappy_decompress --
 *	WiredTiger snappy decompression.
 */
static int
wt_snappy_decompress(WT_COMPRESSOR *compressor, WT_SESSION *session,
    uint8_t *src, size_t src_len,
    uint8_t *dst, size_t dst_len,
    size_t *result_lenp)
{
	WT_EXTENSION_API *wt_api;
	snappy_status snret;
	size_t snaplen;

	wt_api = ((SNAPPY_COMPRESSOR *)compressor)->wt_api;

	/* retrieve the saved length */
	snaplen = *(size_t *)src;
	if (snaplen + sizeof(size_t) > src_len) {
		(void)wt_api->err_printf(wt_api,
		    session,
		    "wt_snappy_decompress: stored size exceeds buffer size");
		return (WT_ERROR);
	}

	/* dst_len is an input and an output arg. */
	snret = snappy_uncompress(
	    (char *)src + sizeof(size_t), snaplen, (char *)dst, &dst_len);

	if (snret == SNAPPY_OK) {
		*result_lenp = dst_len;
		return (0);
	}

	return (
	    wt_snappy_error(compressor, session, "snappy_decompress", snret));
}
Пример #6
0
int pread_compressed(Db *db, off_t pos, char **ret_ptr)
{
    char *compressed_buf;
    char *new_buf;
    int len = pread_bin_internal(db, pos, &compressed_buf, 0);
    if (len < 0) {
        return len;
    }
    size_t uncompressed_len;
    if (snappy_uncompressed_length(compressed_buf, len, &uncompressed_len) != SNAPPY_OK) {
        //should be compressed but snappy doesn't see it as valid.
        free(compressed_buf);
        return COUCHSTORE_ERROR_CORRUPT;
    }

    new_buf = (char *) malloc(uncompressed_len);
    if (!new_buf) {
        free(compressed_buf);
        return COUCHSTORE_ERROR_ALLOC_FAIL;
    }
    snappy_status ss = (snappy_uncompress(compressed_buf, len, new_buf, &uncompressed_len));
    free(compressed_buf);
    if (ss != SNAPPY_OK) {
        return COUCHSTORE_ERROR_CORRUPT;
    }

    *ret_ptr = new_buf;
    return (int) uncompressed_len;
}
Пример #7
0
static VALUE
snappy_inflate(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);

    result = snappy_uncompressed_length(RSTRING_PTR(src), RSTRING_LEN(src), &output_length);
    if (result != SNAPPY_OK) {
        return snappy_raise(result);
    }

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

    result = snappy_uncompress(RSTRING_PTR(src), RSTRING_LEN(src), RSTRING_PTR(dst), &output_length);
    if (result != SNAPPY_OK) {
        return snappy_raise(result);
    }

    StringValue(dst);
    rb_str_resize(dst, output_length);

    return dst;
}
Пример #8
0
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;
}
int SnappyImplNative::decompress(u8* in, int inOffset, u8 *out, int outOffset, int len) {
	int res = snappy_uncompress((const char*) (in + inOffset), (size_t) len, (char*) (out + outOffset));

	if (res != 0) { // not ok
		return -res;
	} else {
		return 0;
	}
}
Пример #10
0
static int foldprint(Db *db, DocInfo *docinfo, void *ctx)
{
    int *count = (int *) ctx;
    (*count)++;
    Doc *doc = NULL;
    uint64_t cas;
    uint32_t expiry, flags;
    if (mode == DumpBySequence) {
        printf("Doc seq: %"PRIu64"\n", docinfo->db_seq);
        printf("     id: ");
        printsb(&docinfo->id);
    } else {
        printf(" Doc ID: ");
        printsb(&docinfo->id);
        if (docinfo->db_seq > 0) {
            printf("    seq: %"PRIu64"\n", docinfo->db_seq);
        }
    }
    if (docinfo->bp == 0 && docinfo->deleted == 0) {
        printf("         ** This b-tree node is corrupt; raw node value follows:*\n");
        printf("    raw: ");
        printsbhex(&docinfo->rev_meta, 1);
        return 0;
    }
    printf("     rev: %"PRIu64"\n", docinfo->rev_seq);
    printf("     content_meta: %d\n", docinfo->content_meta);
    if (docinfo->rev_meta.size == sizeof(CouchbaseRevMeta)) {
        const CouchbaseRevMeta* meta = (const CouchbaseRevMeta*)docinfo->rev_meta.buf;
        cas = decode_raw64(meta->cas);
        expiry = decode_raw32(meta->expiry);
        flags = decode_raw32(meta->flags);
        printf("     cas: %"PRIu64", expiry: %"PRIu32", flags: %"PRIu32"\n", cas, expiry, flags);
    }
    if (docinfo->deleted) {
        printf("     doc deleted\n");
    }

    couchstore_error_t docerr = couchstore_open_doc_with_docinfo(db, docinfo, &doc, 0);
    if(docerr != COUCHSTORE_SUCCESS) {
        printf("     could not read document body: %s\n", couchstore_strerror(docerr));
    } else if (doc && (docinfo->content_meta & COUCH_DOC_IS_COMPRESSED)) {
        size_t rlen;
        snappy_uncompressed_length(doc->data.buf, doc->data.size, &rlen);
        char *decbuf = (char *) malloc(rlen);
        size_t uncompr_len;
        snappy_uncompress(doc->data.buf, doc->data.size, decbuf, &uncompr_len);
        printf("     data: (snappy) %.*s\n", (int) uncompr_len, decbuf);
        free(decbuf);
    } else if(doc) {
        printf("     data: ");
        printsb(&doc->data);
    }

    couchstore_free_document(doc);
    return 0;
}
Пример #11
0
bool
mongoc_uncompress (int32_t compressor_id,
                   const uint8_t *compressed,
                   size_t compressed_len,
                   uint8_t *uncompressed,
                   size_t *uncompressed_len)
{
   TRACE ("Uncompressing with '%s' (%d)",
          mongoc_compressor_id_to_name (compressor_id),
          compressor_id);
   switch (compressor_id) {
   case MONGOC_COMPRESSOR_SNAPPY_ID: {
#ifdef MONGOC_ENABLE_COMPRESSION_SNAPPY
      snappy_status status;
      status = snappy_uncompress ((const char *) compressed,
                                  compressed_len,
                                  (char *) uncompressed,
                                  uncompressed_len);

      return status == SNAPPY_OK;
#else
      MONGOC_WARNING ("Received snappy compressed opcode, but snappy "
                      "compression is not compiled in");
      return false;
#endif
      break;
   }

   case MONGOC_COMPRESSOR_ZLIB_ID: {
#ifdef MONGOC_ENABLE_COMPRESSION_ZLIB
      int ok;

      ok = uncompress (uncompressed,
                       (unsigned long *) uncompressed_len,
                       compressed,
                       compressed_len);

      return ok == Z_OK;
#else
      MONGOC_WARNING ("Received zlib compressed opcode, but zlib "
                      "compression is not compiled in");
      return false;
#endif
      break;
   }
   case MONGOC_COMPRESSOR_NOOP_ID:
      memcpy (uncompressed, compressed, compressed_len);
      *uncompressed_len = compressed_len;
      return true;

   default:
      MONGOC_WARNING ("Unknown compressor ID %d", compressor_id);
   }

   return false;
}
Пример #12
0
int bp__uncompress(const char* compressed,
                   size_t compressed_length,
                   char* uncompressed,
                   size_t* uncompressed_length) {
  int ret = snappy_uncompress(compressed,
                              compressed_length,
                              uncompressed,
                              uncompressed_length);

  return ret == SNAPPY_OK ? BP_OK : BP_EDECOMP;
}
Пример #13
0
primitiveUncompressWithOffset(void)
{
	const char*compressed;
	size_t compressedLength;
	sqInt compressedObj;
	size_t inputOffset;
	sqInt num;
	char* ptr;
	snappy_status status;
	char* uncompressed;
	size_t uncompressedLength;

	if (!((interpreterProxy->methodArgumentCount()) == 2)) {
		interpreterProxy->primitiveFail(); return;
	}
	compressedObj = interpreterProxy->stackValue(1);
	/* begin charPointerFor: */
	if (!(interpreterProxy->isBytes(compressedObj))) {
		compressed = ((char*) null);
		goto l1;
	}
	ptr = ((char*) (interpreterProxy->firstIndexableField(compressedObj)));
	compressed = ((char*) ptr);
l1:	/* end charPointerFor: */;
	if (!(compressed)) {
		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: */;
	compressedLength = interpreterProxy->byteSizeOf(compressedObj);
	status = snappy_uncompressed_length(compressed + inputOffset, compressedLength, &uncompressedLength);
	if (!(status == 0)) {
		interpreterProxy->primitiveFailFor(status); return;
	}
	uncompressed = malloc(uncompressedLength);
	status = snappy_uncompress(compressed + inputOffset, compressedLength, uncompressed, &uncompressedLength);
	if (!(status == 0)) {
		/* begin returnErrorInfoFor: */
		interpreterProxy->pop((interpreterProxy->methodArgumentCount()) + 1);
		interpreterProxy->pushInteger(-status);
		return;
	}
	interpreterProxy->pop((interpreterProxy->methodArgumentCount()) + 1);
	interpreterProxy->push(oopFromCBytessized(uncompressed, uncompressedLength));
	free(uncompressed);
}
Пример #14
0
static int snp_decompress(struct crypto_tfm *tfm, const u8 *src,
			      unsigned int slen, u8 *dst, unsigned int *dlen)
{
	size_t ulen;

	if (!snappy_uncompressed_length(src, slen, &ulen))
		return -EIO;
	if (*dlen < ulen)
		return -EINVAL;
	*dlen = ulen;
	return snappy_uncompress(src, slen, dst) ? 0 : -EIO;
}
Пример #15
0
static SquashStatus
squash_snappy_decompress_buffer (SquashCodec* codec,
                                 size_t* decompressed_length,
                                 uint8_t decompressed[SQUASH_ARRAY_PARAM(*decompressed_length)],
                                 size_t compressed_length,
                                 const uint8_t compressed[SQUASH_ARRAY_PARAM(compressed_length)],
                                 SquashOptions* options) {
  snappy_status e;

  e = snappy_uncompress ((char*) compressed, compressed_length,
                         (char*) decompressed, decompressed_length);

  return squash_snappy_status (e);
}
Пример #16
0
int main(int argc, char* argv[])
{
	char arch[50];
	size_t arch_sz = sizeof(arch);
	snappy_status status = snappy_compress("Hello World!", 12, arch, &arch_sz);
	if (status != SNAPPY_OK)
		printf("compress fail");

	char src[50];
	size_t src_sz = sizeof(src);
	status = snappy_uncompress(arch, arch_sz, src, &src_sz);
	if (status != SNAPPY_OK)
		return -1;

	if (strncmp(src, "Hello, world!", 12) == 0)
		return -1;

	return 0;
}
Пример #17
0
static char * decompress_block(size_t input_size, const char *input, size_t *output_size)
{
    int input_remaining = (int)input_size;

    *output_size = uint32_be(input);
    input += 4;
    input_remaining -= 4;

    int output_remaining = (int)*output_size;
    char *output = calloc((size_t)output_remaining, sizeof(char));
    char *cursor = output;

    while (input_remaining > 0 && output_remaining >= 0) {
        size_t compressed_size = uint32_be(input);

        input += 4;
        input_remaining -= 4;

        if (compressed_size == 0) continue;

        size_t uncompressed_size = (size_t)output_remaining;
        if (snappy_uncompress(
                input, compressed_size,
                cursor, &uncompressed_size) != SNAPPY_OK) {
            goto fail;
        }

        input += compressed_size;
        input_remaining -= compressed_size;

        cursor += uncompressed_size;
        output_remaining -= uncompressed_size;
    }

    if (input_remaining == 0 && output_remaining == 0) {
        return output;
    }

fail:
    free(output);
    *output_size = 0;
    return NULL;
}
Пример #18
0
static ZEND_FUNCTION(snappy_uncompress)
{
    zval *data;
    char *output = NULL;
    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_uncompress : expects parameter to be string.");
        RETURN_FALSE;
    }

    if (snappy_uncompressed_length(Z_STRVAL_P(data), Z_STRLEN_P(data),
                                   &output_len) != SNAPPY_OK) {
        zend_error(E_WARNING, "snappy_uncompress : output length error");
        RETURN_FALSE;
    }

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

    if (snappy_uncompress(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 {
        zend_error(E_WARNING, "snappy_uncompress : data error");
        RETVAL_FALSE;
    }

    efree(output);
}
Пример #19
0
primitiveUncompress(void)
{
	const char*compressed;
	size_t compressedLength;
	sqInt compressedObj;
	char* ptr;
	snappy_status status;
	char* uncompressed;
	size_t uncompressedLength;

	if (!((interpreterProxy->methodArgumentCount()) == 1)) {
		interpreterProxy->primitiveFail(); return;
	}
	compressedObj = interpreterProxy->stackValue(0);
	/* begin charPointerFor: */
	if (!(interpreterProxy->isBytes(compressedObj))) {
		compressed = ((char*) null);
		goto l1;
	}
	ptr = ((char*) (interpreterProxy->firstIndexableField(compressedObj)));
	compressed = ((char*) ptr);
l1:	/* end charPointerFor: */;
	if (!(compressed)) {
		interpreterProxy->primitiveFail(); return;
	}
	compressedLength = interpreterProxy->byteSizeOf(compressedObj);
	status = snappy_uncompressed_length(compressed, compressedLength, &uncompressedLength);
	if (!(status == 0)) {
		interpreterProxy->primitiveFailFor(status); return;
	}
	uncompressed = malloc(uncompressedLength);
	status = snappy_uncompress(compressed, compressedLength, uncompressed, &uncompressedLength);
	if (!(status == 0)) {
		/* begin returnErrorInfoFor: */
		interpreterProxy->pop((interpreterProxy->methodArgumentCount()) + 1);
		interpreterProxy->pushInteger(-status);
		return;
	}
	interpreterProxy->pop((interpreterProxy->methodArgumentCount()) + 1);
	interpreterProxy->push(oopFromCBytessized(uncompressed, uncompressedLength));
	free(uncompressed);
}
Пример #20
0
CChunkFileReader::Error CChunkFileReader::LoadFile(const std::string &filename, const char *gitVersion, u8 *&_buffer, size_t &sz, std::string *failureReason) {
	if (!File::Exists(filename)) {
		*failureReason = "LoadStateDoesntExist";
		ERROR_LOG(COMMON, "ChunkReader: File doesn't exist");
		return ERROR_BAD_FILE;
	}

	File::IOFile pFile(filename, "rb");
	SChunkHeader header;
	Error err = LoadFileHeader(pFile, header, nullptr);
	if (err != ERROR_NONE) {
		return err;
	}

	// read the state
	sz = header.ExpectedSize;
	u8 *buffer = new u8[sz];
	if (!pFile.ReadBytes(buffer, sz))
	{
		ERROR_LOG(COMMON, "ChunkReader: Error reading file");
		delete [] buffer;
		return ERROR_BAD_FILE;
	}

	_buffer = buffer;
	if (header.Compress) {
		u8 *uncomp_buffer = new u8[header.UncompressedSize];
		size_t uncomp_size = header.UncompressedSize;
		snappy_uncompress((const char *)buffer, sz, (char *)uncomp_buffer, &uncomp_size);
		if ((u32)uncomp_size != header.UncompressedSize) {
			ERROR_LOG(COMMON, "Size mismatch: file: %u  calc: %u", header.UncompressedSize, (u32)uncomp_size);
			delete [] uncomp_buffer;
			return ERROR_BAD_FILE;
		}
		_buffer = uncomp_buffer;
		sz = uncomp_size;
		delete [] buffer;
	}

	return ERROR_NONE;
}
Пример #21
0
static int decode_snappy(avro_codec_t c, void * data, int64_t len)
{
        uint32_t crc;
        size_t outlen;

        if (snappy_uncompressed_length(data, len-4, &outlen) != SNAPPY_OK) {
		avro_set_error("Uncompressed length error in snappy");
		return 1;
        }

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

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

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

        crc = __bswap_32(crc32(0, c->block_data, outlen));
        if (memcmp(&crc, (char*)data+len-4, 4))
        {
                avro_set_error("CRC32 check failure uncompressing block with Snappy");
		return 1;
	}

        c->used_size = outlen;

	return 0;
}
Пример #22
0
int ness_decompress(const char *src,
                    uint32_t src_size,
                    char *dst,
                    uint32_t dst_size)
{
	int ret = NESS_OK;

	/* compressed data is NULL */
	if (src_size == 1)
		return NESS_ERR;

	switch (src[0] & 0xF) {
	case NESS_NO_COMPRESS:
		memcpy(dst, src + 1, src_size - 1);
		break;

	case NESS_SNAPPY_METHOD: {
			int status;
			struct snappy_env env;
			snappy_init_env(&env);

			status = snappy_uncompress(src + 1, src_size - 1, dst);
			snappy_free_env(&env);
			if (status != 0) {
				__ERROR("snappy uncompress error %d", status);
				ret = 0;
				goto ERR;
			}
			(void)dst_size;
		}
		break;
	default:
		ret = 0;
		__ERROR("%s", "no decompress method support!");
		break;
	}

ERR:
	return ret;
}
Пример #23
0
static sparkey_returncode seekblock(sparkey_logiter *iter, sparkey_logreader *log, uint64_t position) {
  iter->block_offset = 0;
  if (iter->block_position == position) {
    return SPARKEY_SUCCESS;
  }
  if (log->header.compression_type == SPARKEY_COMPRESSION_NONE) {
    iter->compression_buf = &log->data[position];
    iter->block_position = position;
    iter->next_block_position = log->header.data_end;
    iter->block_len = log->data_len - position;
    return SPARKEY_SUCCESS;
  }
  if (log->header.compression_type == SPARKEY_COMPRESSION_SNAPPY) {
    uint64_t pos = position;
    // TODO: assert that size_t >= uint64_t
    size_t compressed_size = read_vlq(log->data, &pos);
    uint64_t next_pos = pos + compressed_size;
    const char *input = (char *) &log->data[pos];

    size_t uncompressed_size = log->header.compression_block_size;
    snappy_status status = snappy_uncompress(input, compressed_size, (char *) iter->compression_buf, &uncompressed_size);
    switch (status) {
    case SNAPPY_OK: break;
    case SNAPPY_INVALID_INPUT:
      return SPARKEY_INTERNAL_ERROR;
    case SNAPPY_BUFFER_TOO_SMALL:
      return SPARKEY_INTERNAL_ERROR;
    default:
      return SPARKEY_INTERNAL_ERROR;
    }
    iter->block_position = position;
    iter->next_block_position = next_pos;
    iter->block_len = uncompressed_size;
    return SPARKEY_SUCCESS;
  }

  return SPARKEY_INTERNAL_ERROR;
}
Пример #24
0
int pread_bin(int fd, off_t pos, char **ret_ptr)
{
    char *new_buf;
    int len = pread_bin_int(fd, pos, ret_ptr);
    if(len < 0)
    {
        return len;
    }
    size_t new_len;
    if((*ret_ptr)[0] == 1) //Snappy
    {
        if(snappy_uncompressed_length((*ret_ptr) + 1, len - 1, &new_len)
                != SNAPPY_OK)
        {
            //marked as compressed but snappy doesn't see it as valid.
            free(*ret_ptr);
            return -1;
        }

        new_buf = (char*) malloc(new_len);
        snappy_status ss = (snappy_uncompress((*ret_ptr) + 1, len - 1, new_buf, &new_len));
        if(ss == SNAPPY_OK)
        {
            free(*ret_ptr);
            *ret_ptr = new_buf;
            return new_len;
        }
        else
        {
            free(*ret_ptr);
            return -1;
        }
    }
    else
    {
        return len;
    }
}
Пример #25
0
Файл: hap.c Проект: jossgray/hap
static void hap_decode_chunk(HapChunkDecodeInfo chunks[], unsigned int index)
{
    if (chunks)
    {
        if (chunks[index].compressor == kHapCompressorSnappy)
        {
            snappy_status snappy_result = snappy_uncompress(chunks[index].compressed_chunk_data,
                                                            chunks[index].compressed_chunk_size,
                                                            chunks[index].uncompressed_chunk_data,
                                                            &chunks[index].uncompressed_chunk_size);

            switch (snappy_result)
            {
                case SNAPPY_INVALID_INPUT:
                    chunks[index].result = HapResult_Bad_Frame;
                    break;
                case SNAPPY_OK:
                    chunks[index].result = HapResult_No_Error;
                    break;
                default:
                    chunks[index].result = HapResult_Internal_Error;
                    break;
            }
        }
        else if (chunks[index].compressor == kHapCompressorNone)
        {
            memcpy(chunks[index].uncompressed_chunk_data,
                   chunks[index].compressed_chunk_data,
                   chunks[index].compressed_chunk_size);
            chunks[index].result = HapResult_No_Error;
        }
        else
        {
            chunks[index].result = HapResult_Bad_Frame;
        }
    }
}
Пример #26
0
static stream_state_t process_block(FILE *fp, stream_state_t state, block_data_t *bd, char *work, size_t work_len)
{
  unsigned int crc32c;
  size_t outlen;

  switch (state) {
  case INITIAL_STATE:
  case END_OF_STREAM_STATE:
    /* the next block must be a header block. */

    if (bd->type != HEADER_TYPE_CODE) {
      print_error("Invaid file format\n");
      return ERROR_STATE;
    }
    if (bd->data_len != 6) {
      print_error("invalid data length %d for header block\n", bd->data_len);
      return ERROR_STATE;
    }
    if (memcmp(bd->data, "snappy", 6) != 0) {
      print_error("invalid file header\n");
      return ERROR_STATE;
    }
    return PROCESSING_STATE;

  case PROCESSING_STATE:

    switch (bd->type) {
    case COMPRESSED_TYPE_CODE:
      if (bd->data_len <= 4) {
        print_error("too short data length for compressed data block\n");
        return ERROR_STATE;
      }
      crc32c  = ((unsigned char)bd->data[0] << 0);
      crc32c |= ((unsigned char)bd->data[1] << 8);
      crc32c |= ((unsigned char)bd->data[2] << 16);
      crc32c |= ((unsigned char)bd->data[3] << 24);

      /* uncompress and write */
      outlen = work_len;
      if (snappy_uncompress(bd->data + 4, bd->data_len - 4, work, &outlen)) {
        print_error("Invalid data: RawUncompress failed\n");
        return ERROR_STATE;
      }
      if (crc32c != masked_crc32c(work, outlen)) {
        print_error("Invalid data: CRC32c error\n");
        return ERROR_STATE;
      }
      if (fwrite_unlocked(work, outlen, 1, fp) != 1) {
        print_error("Failed to write: %s\n", strerror(errno));
        return ERROR_STATE;
      }
      break;
    case UNCOMPRESSED_TYPE_CODE:
      if (bd->data_len <= 4) {
        print_error("too short data length for uncompressed data block\n");
        return ERROR_STATE;
      }
      crc32c  = ((unsigned char)bd->data[0] << 0);
      crc32c |= ((unsigned char)bd->data[1] << 8);
      crc32c |= ((unsigned char)bd->data[2] << 16);
      crc32c |= ((unsigned char)bd->data[3] << 24);

      if (crc32c != masked_crc32c(bd->data + 4, bd->data_len - 4)) {
        print_error("Invalid data: CRC32c error\n");
        return ERROR_STATE;
      }
      if (fwrite_unlocked(bd->data + 4, bd->data_len - 4, 1, fp) != 1) {
        print_error("Failed to write: %s\n", strerror(errno));
        return ERROR_STATE;
      }
      break;
    case END_OF_STREAM_TYPE_CODE:
      if (bd->data_len != 0) {
        print_error("invalid data length for end-of-stream block\n");
        return ERROR_STATE;
      }
      return END_OF_STREAM_STATE;
    case HEADER_TYPE_CODE:
      print_error("Invalid data: unexpected header\n");
      return ERROR_STATE;
    default:
      if (bd->type < 0x80) {
        print_error("Invalid data: unknown block type %d\n", bd->type);
        return ERROR_STATE;
      }
    }
    return PROCESSING_STATE;

  case ERROR_STATE:
    ;
  }
  /* never reach here. This is added to suppress a warning */
  return ERROR_STATE;
}
Пример #27
0
CChunkFileReader::Error CChunkFileReader::LoadFile(const std::string& _rFilename, int _Revision, const char *_VersionString, u8 *&_buffer, size_t &sz, std::string *_failureReason) {
    if (!File::Exists(_rFilename)) {
        *_failureReason = "LoadStateDoesntExist";
        ERROR_LOG(COMMON, "ChunkReader: File doesn't exist");
        return ERROR_BAD_FILE;
    }

    // Check file size
    const u64 fileSize = File::GetSize(_rFilename);
    static const u64 headerSize = sizeof(SChunkHeader);
    if (fileSize < headerSize)
    {
        ERROR_LOG(COMMON, "ChunkReader: File too small");
        return ERROR_BAD_FILE;
    }

    File::IOFile pFile(_rFilename, "rb");
    if (!pFile)
    {
        ERROR_LOG(COMMON, "ChunkReader: Can't open file for reading");
        return ERROR_BAD_FILE;
    }

    // read the header
    SChunkHeader header;
    if (!pFile.ReadArray(&header, 1))
    {
        ERROR_LOG(COMMON, "ChunkReader: Bad header size");
        return ERROR_BAD_FILE;
    }

    // Check revision
    if (header.Revision != _Revision)
    {
        ERROR_LOG(COMMON, "ChunkReader: Wrong file revision, got %d expected %d", header.Revision, _Revision);
        return ERROR_BAD_FILE;
    }

    // get size
    sz = (int)(fileSize - headerSize);
    if (header.ExpectedSize != sz)
    {
        ERROR_LOG(COMMON, "ChunkReader: Bad file size, got %u expected %u", (u32)sz, header.ExpectedSize);
        return ERROR_BAD_FILE;
    }

    // read the state
    u8 *buffer = new u8[sz];
    if (!pFile.ReadBytes(buffer, sz))
    {
        ERROR_LOG(COMMON, "ChunkReader: Error reading file");
        delete [] buffer;
        return ERROR_BAD_FILE;
    }

    _buffer = buffer;
    if (header.Compress) {
        u8 *uncomp_buffer = new u8[header.UncompressedSize];
        size_t uncomp_size = header.UncompressedSize;
        snappy_uncompress((const char *)buffer, sz, (char *)uncomp_buffer, &uncomp_size);
        if ((u32)uncomp_size != header.UncompressedSize) {
            ERROR_LOG(COMMON, "Size mismatch: file: %u  calc: %u", header.UncompressedSize, (u32)uncomp_size);
            return ERROR_BAD_FILE;
        }
        _buffer = uncomp_buffer;
        sz = uncomp_size;
        delete [] buffer;
    }

    return ERROR_NONE;
}
Пример #28
0
/* This routine gets executed after a call to HG_Trigger and
 * the RPC has completed */
static hg_return_t
snappy_compress_rpc_cb(const struct hg_cb_info *callback_info)
{
    struct snappy_compress_rpc_args *snappy_compress_rpc_args =
            (struct snappy_compress_rpc_args *) callback_info->arg;
    hg_handle_t handle = callback_info->info.forward.handle;

    int *input;
    size_t source_length;

    void *compressed;
    size_t compressed_length;

    int *uncompressed;
    size_t uncompressed_length;

    snappy_compress_out_t snappy_compress_output;
    snappy_status ret;

    /* Get output */
    printf("Received output from target\n");
    HG_Get_output(handle, &snappy_compress_output);

    /* Get Snappy output parameters */
    ret = snappy_compress_output.ret;
    compressed_length = snappy_compress_output.compressed_length;
    compressed = snappy_compress_rpc_args->compressed;
    input = snappy_compress_rpc_args->input;
    source_length = snappy_compress_rpc_args->input_length;

    /* Check ret */
    if (ret != SNAPPY_OK) {
        fprintf(stderr, "Error: snappy_compressed failed with ret %d\n", ret);
    }

    /* The output data is now in the bulk buffer */
    printf("Compressed buffer length is: %zu\n", compressed_length);
    print_buf(5, (int *)compressed);
    if (snappy_validate_compressed_buffer(compressed, compressed_length) == SNAPPY_OK) {
        printf("Compressed buffer validated: compressed successfully\n");
    }

    uncompressed_length = source_length * sizeof(int);
    uncompressed = (int *) malloc(uncompressed_length);

    /* Uncompress data and check uncompressed_length */
    printf("Uncompressing buffer...\n");
    snappy_uncompress(compressed, compressed_length,
            (char *) uncompressed, &uncompressed_length);
    printf("Uncompressed buffer length is: %zu\n", uncompressed_length);
    print_buf(20, uncompressed);

    /* Free output and handles */
    HG_Free_output(handle, &snappy_compress_output);
    HG_Bulk_free(snappy_compress_rpc_args->input_bulk_handle);
    HG_Bulk_free(snappy_compress_rpc_args->compressed_bulk_handle);

    /* Free data */
    free(uncompressed);
    free(compressed);
    free(input);
    free(snappy_compress_rpc_args);

    /* We're done */
    snappy_compress_done_g = HG_TRUE;

    return HG_SUCCESS;
}
Пример #29
0
size_t cfs_decompress(void* dst, size_t dst_size, void const* src, size_t src_size)
{
    return snappy_uncompress(src, src_size, dst, &dst_size) == SNAPPY_OK ? dst_size : 0;
}
Пример #30
0
int memDecompressSnappy( char *out, int outlen, char *in, int inlen ) {
    size_t osz = outlen;
    snappy_status ret = snappy_uncompress( in, inlen, out, &osz );
    if(ret == SNAPPY_OK ) return osz; else assertmsg(false,"snappy_uncompress failed");
    return 0;
}