示例#1
0
func zlibEncode(buf []byte, level int) ([]byte, error) {

	dLen := C.compressBound(C.uLong(len(buf)))

	dst := make([]byte, dLen)

	err := C.compress2((*C.Bytef)(unsafe.Pointer(&dst[0])), (*C.uLongf)(unsafe.Pointer(&dLen)),
		(*C.Bytef)(unsafe.Pointer(&buf[0])), C.uLong(len(buf)),
		C.int(level))

	// compression failed :(
	if err != C.Z_OK {
		return nil, errors.New("zlib error")
	}

	return dst[:dLen], nil
}
示例#2
0
func zlibDecode(uln int, buf []byte) ([]byte, error) {

	dst := make([]byte, uln)

	dLen := uln

	err := C.uncompress((*C.Bytef)(unsafe.Pointer(&dst[0])), (*C.uLongf)(unsafe.Pointer(&dLen)),
		(*C.Bytef)(unsafe.Pointer(&buf[0])), C.uLong(len(buf)))

	// compression failed :(
	if err != C.Z_OK || uln != dLen {
		return nil, errors.New("zlib error")
	}

	return dst, nil
}
示例#3
0
// helper method for partial checksums. From the zlib.h header:
//
//   Combine two Adler-32 checksums into one.  For two sequences of bytes, seq1
// and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
// each, adler1 and adler2.  adler32_combine() returns the Adler-32 checksum of
// seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.
func Adler32Combine(adler1, adler2 uint32, len2 int) uint32 {
	return uint32(C.adler32_combine(C.uLong(adler1), C.uLong(adler2), C.z_off_t(len2)))
}
示例#4
0
文件: crc32.go 项目: kingpro/vitess
// helper method for partial checksums. From the zlib.h header:
//
//   Combine two CRC-32 checksums into one.  For two sequences of bytes, seq1
// and seq2 with lengths len1 and len2, CRC-32 checksums were calculated for
// each, crc1 and crc2.  crc32_combine() returns the CRC-32 checksum of
// seq1 and seq2 concatenated, requiring only crc1, crc2, and len2.
func Crc32Combine(crc1, crc2 uint32, len2 int) uint32 {
	return uint32(C.crc32_combine(C.uLong(crc1), C.uLong(crc2), C.z_off_t(len2)))
}