Example #1
0
func Decompress(output []byte, input []byte) (out []byte, err os.Error) {
	var size C.size_t
	if C.snappy_uncompressed_length(c_pchar(&input[0]),
		C.size_t(len(input)), &size) != C.SNAPPY_OK {
		return nil, os.NewError("Invalid Input")
	}
	if C.size_t(cap(output)) < size {
		output = make([]byte, size)
	}
	if C.snappy_uncompress(c_pchar(&input[0]),
		C.size_t(len(input)),
		c_pchar(&output[0]), &size) != C.SNAPPY_OK {
		return nil, os.NewError("Invalid Input")
	}
	return output[:size], nil
}
Example #2
0
// Decode returns the decoded form of src. The returned slice may be a sub-
// slice of dst if dst was large enough to hold the entire decoded block.
// Otherwise, a newly allocated slice will be returned.
// It is valid to pass a nil dst.
func Decode(dst, src []byte) ([]byte, error) {
	dLen, _, err := decodedLen(src)
	if err != nil {
		return nil, err
	}
	if len(dst) < dLen {
		dst = make([]byte, dLen)
	}
	if dLen == 0 {
		return dst[:0], nil
	}

	tLen := C.size_t(dLen)
	status := C.snappy_uncompress((*C.char)(unsafe.Pointer(&src[0])), C.size_t(len(src)),
		(*C.char)(unsafe.Pointer(&dst[0])), &tLen)
	if status != C.SNAPPY_OK {
		return nil, ErrCorrupt
	}
	return dst[:tLen], nil
}