Skip to content

Commit ba77079

Browse files
committed
initial import
0 parents  commit ba77079

12 files changed

+1628
-0
lines changed

LICENSE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Simplified BSD License
2+
3+
Copyright (c) 2016, Datadog <info@datadoghq.com>
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice,
10+
this list of conditions and the following disclaimer.
11+
* Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
* Neither the name of the copyright holder nor the names of its contributors
15+
may be used to endorse or promote products derived from this software
16+
without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

adler32.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Pulled from https://github.com/youtube/vitess 229422035ca0c716ad0c1397ea1351fe62b0d35a
2+
// Copyright 2012, Google Inc. All rights reserved.
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package czlib
7+
8+
// #cgo pkg-config: zlib
9+
10+
/*
11+
12+
#include "zlib.h"
13+
*/
14+
import "C"
15+
16+
import (
17+
"hash"
18+
"unsafe"
19+
)
20+
21+
type adler32Hash struct {
22+
adler C.uLong
23+
}
24+
25+
// an empty buffer has an adler32 of '1' by default, so start with that
26+
// (the go hash/adler32 does the same)
27+
func newAdler32() hash.Hash32 {
28+
a := &adler32Hash{}
29+
a.Reset()
30+
return a
31+
}
32+
33+
// Write implements an io.Writer interface
34+
func (a *adler32Hash) Write(p []byte) (n int, err error) {
35+
if len(p) > 0 {
36+
a.adler = C.adler32(a.adler, (*C.Bytef)(unsafe.Pointer(&p[0])), (C.uInt)(len(p)))
37+
}
38+
return len(p), nil
39+
}
40+
41+
// Sum implements a hash.Hash interface
42+
func (a *adler32Hash) Sum(b []byte) []byte {
43+
s := a.Sum32()
44+
b = append(b, byte(s>>24))
45+
b = append(b, byte(s>>16))
46+
b = append(b, byte(s>>8))
47+
b = append(b, byte(s))
48+
return b
49+
}
50+
51+
// Reset resets the hash to default value
52+
func (a *adler32Hash) Reset() {
53+
a.adler = C.adler32(0, (*C.Bytef)(unsafe.Pointer(nil)), 0)
54+
}
55+
56+
// Size returns the (fixed) size of the hash
57+
func (a *adler32Hash) Size() int {
58+
return 4
59+
}
60+
61+
// BlockSize returns the (fixed) block size
62+
func (a *adler32Hash) BlockSize() int {
63+
return 1
64+
}
65+
66+
// Sum32 implements a hash.Hash32 interface
67+
func (a *adler32Hash) Sum32() uint32 {
68+
return uint32(a.adler)
69+
}
70+
71+
// helper method for partial checksums. From the zlib.h header:
72+
//
73+
// Combine two Adler-32 checksums into one. For two sequences of bytes, seq1
74+
// and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
75+
// each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of
76+
// seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.
77+
func adler32Combine(adler1, adler2 uint32, len2 int) uint32 {
78+
return uint32(C.adler32_combine(C.uLong(adler1), C.uLong(adler2), C.z_off_t(len2)))
79+
}

crc32.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Pulled from https://github.com/youtube/vitess 229422035ca0c716ad0c1397ea1351fe62b0d35a
2+
// Copyright 2012, Google Inc. All rights reserved.
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package czlib
7+
8+
/*
9+
#include "zlib.h"
10+
*/
11+
import "C"
12+
13+
import (
14+
"hash"
15+
"unsafe"
16+
)
17+
18+
type crc32Hash struct {
19+
crc C.uLong
20+
}
21+
22+
// an empty buffer has an crc32 of '1' by default, so start with that
23+
// (the go hash/crc32 does the same)
24+
func newCrc32() hash.Hash32 {
25+
c := &crc32Hash{}
26+
c.Reset()
27+
return c
28+
}
29+
30+
// Write implements an io.Writer interface
31+
func (a *crc32Hash) Write(p []byte) (n int, err error) {
32+
if len(p) > 0 {
33+
a.crc = C.crc32(a.crc, (*C.Bytef)(unsafe.Pointer(&p[0])), (C.uInt)(len(p)))
34+
}
35+
return len(p), nil
36+
}
37+
38+
// Sum implements a hash.Hash interface
39+
func (a *crc32Hash) Sum(b []byte) []byte {
40+
s := a.Sum32()
41+
b = append(b, byte(s>>24))
42+
b = append(b, byte(s>>16))
43+
b = append(b, byte(s>>8))
44+
b = append(b, byte(s))
45+
return b
46+
}
47+
48+
// Reset resets the hash to default value
49+
func (a *crc32Hash) Reset() {
50+
a.crc = C.crc32(0, (*C.Bytef)(unsafe.Pointer(nil)), 0)
51+
}
52+
53+
// Size returns the (fixed) size of the hash
54+
func (a *crc32Hash) Size() int {
55+
return 4
56+
}
57+
58+
// BlockSize returns the (fixed) block size of the hash
59+
func (a *crc32Hash) BlockSize() int {
60+
return 1
61+
}
62+
63+
// Sum32 implements a hash.Hash32 interface
64+
func (a *crc32Hash) Sum32() uint32 {
65+
return uint32(a.crc)
66+
}
67+
68+
// helper method for partial checksums. From the zlib.h header:
69+
//
70+
// Combine two CRC-32 checksums into one. For two sequences of bytes, seq1
71+
// and seq2 with lengths len1 and len2, CRC-32 checksums were calculated for
72+
// each, crc1 and crc2. crc32_combine() returns the CRC-32 checksum of
73+
// seq1 and seq2 concatenated, requiring only crc1, crc2, and len2.
74+
func crc32Combine(crc1, crc2 uint32, len2 int) uint32 {
75+
return uint32(C.crc32_combine(C.uLong(crc1), C.uLong(crc2), C.z_off_t(len2)))
76+
}

czlib.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016, Datadog Inc. All rights reserved.
2+
3+
package czlib
4+
5+
import (
6+
"compress/flate"
7+
"compress/zlib"
8+
)
9+
10+
// Constants copied from the flate package, so that code that imports czlib
11+
// does not also have to import "compress/flate".
12+
const (
13+
NoCompression = flate.NoCompression
14+
BestSpeed = flate.BestSpeed
15+
BestCompression = flate.BestCompression
16+
DefaultCompression = flate.DefaultCompression
17+
)
18+
19+
var (
20+
// ErrChecksum is returned when reading ZLIB data that has an invalid checksum.
21+
ErrChecksum = zlib.ErrChecksum
22+
// ErrDictionary is returned when reading ZLIB data that has an invalid dictionary.
23+
ErrDictionary = zlib.ErrDictionary
24+
// ErrHeader is returned when reading ZLIB data that has an invalid header.
25+
ErrHeader = zlib.ErrHeader
26+
)

0 commit comments

Comments
 (0)