Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoiron committed Jul 2, 2016
0 parents commit ba77079
Show file tree
Hide file tree
Showing 12 changed files with 1,628 additions and 0 deletions.
27 changes: 27 additions & 0 deletions LICENSE
@@ -0,0 +1,27 @@
Simplified BSD License

Copyright (c) 2016, Datadog <info@datadoghq.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
79 changes: 79 additions & 0 deletions adler32.go
@@ -0,0 +1,79 @@
// Pulled from https://github.com/youtube/vitess 229422035ca0c716ad0c1397ea1351fe62b0d35a
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package czlib

// #cgo pkg-config: zlib

/*
#include "zlib.h"
*/
import "C"

import (
"hash"
"unsafe"
)

type adler32Hash struct {
adler C.uLong
}

// an empty buffer has an adler32 of '1' by default, so start with that
// (the go hash/adler32 does the same)
func newAdler32() hash.Hash32 {
a := &adler32Hash{}
a.Reset()
return a
}

// Write implements an io.Writer interface
func (a *adler32Hash) Write(p []byte) (n int, err error) {
if len(p) > 0 {
a.adler = C.adler32(a.adler, (*C.Bytef)(unsafe.Pointer(&p[0])), (C.uInt)(len(p)))
}
return len(p), nil
}

// Sum implements a hash.Hash interface
func (a *adler32Hash) Sum(b []byte) []byte {
s := a.Sum32()
b = append(b, byte(s>>24))
b = append(b, byte(s>>16))
b = append(b, byte(s>>8))
b = append(b, byte(s))
return b
}

// Reset resets the hash to default value
func (a *adler32Hash) Reset() {
a.adler = C.adler32(0, (*C.Bytef)(unsafe.Pointer(nil)), 0)
}

// Size returns the (fixed) size of the hash
func (a *adler32Hash) Size() int {
return 4
}

// BlockSize returns the (fixed) block size
func (a *adler32Hash) BlockSize() int {
return 1
}

// Sum32 implements a hash.Hash32 interface
func (a *adler32Hash) Sum32() uint32 {
return uint32(a.adler)
}

// 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)))
}
76 changes: 76 additions & 0 deletions crc32.go
@@ -0,0 +1,76 @@
// Pulled from https://github.com/youtube/vitess 229422035ca0c716ad0c1397ea1351fe62b0d35a
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package czlib

/*
#include "zlib.h"
*/
import "C"

import (
"hash"
"unsafe"
)

type crc32Hash struct {
crc C.uLong
}

// an empty buffer has an crc32 of '1' by default, so start with that
// (the go hash/crc32 does the same)
func newCrc32() hash.Hash32 {
c := &crc32Hash{}
c.Reset()
return c
}

// Write implements an io.Writer interface
func (a *crc32Hash) Write(p []byte) (n int, err error) {
if len(p) > 0 {
a.crc = C.crc32(a.crc, (*C.Bytef)(unsafe.Pointer(&p[0])), (C.uInt)(len(p)))
}
return len(p), nil
}

// Sum implements a hash.Hash interface
func (a *crc32Hash) Sum(b []byte) []byte {
s := a.Sum32()
b = append(b, byte(s>>24))
b = append(b, byte(s>>16))
b = append(b, byte(s>>8))
b = append(b, byte(s))
return b
}

// Reset resets the hash to default value
func (a *crc32Hash) Reset() {
a.crc = C.crc32(0, (*C.Bytef)(unsafe.Pointer(nil)), 0)
}

// Size returns the (fixed) size of the hash
func (a *crc32Hash) Size() int {
return 4
}

// BlockSize returns the (fixed) block size of the hash
func (a *crc32Hash) BlockSize() int {
return 1
}

// Sum32 implements a hash.Hash32 interface
func (a *crc32Hash) Sum32() uint32 {
return uint32(a.crc)
}

// 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)))
}
26 changes: 26 additions & 0 deletions czlib.go
@@ -0,0 +1,26 @@
// Copyright 2016, Datadog Inc. All rights reserved.

package czlib

import (
"compress/flate"
"compress/zlib"
)

// Constants copied from the flate package, so that code that imports czlib
// does not also have to import "compress/flate".
const (
NoCompression = flate.NoCompression
BestSpeed = flate.BestSpeed
BestCompression = flate.BestCompression
DefaultCompression = flate.DefaultCompression
)

var (
// ErrChecksum is returned when reading ZLIB data that has an invalid checksum.
ErrChecksum = zlib.ErrChecksum
// ErrDictionary is returned when reading ZLIB data that has an invalid dictionary.
ErrDictionary = zlib.ErrDictionary
// ErrHeader is returned when reading ZLIB data that has an invalid header.
ErrHeader = zlib.ErrHeader
)

0 comments on commit ba77079

Please sign in to comment.