forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snappy.go
137 lines (120 loc) · 3.9 KB
/
snappy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Peter Mattis (peter@cockroachlabs.com)
package codec
import (
"strconv"
"unsafe"
// Link against the snappy library. This is explicit because this Go
// library does not export any Go symbols.
_ "github.com/cockroachdb/c-snappy"
"github.com/gogo/protobuf/proto"
)
// #cgo CPPFLAGS: -I ../../../c-snappy/internal
// #cgo darwin LDFLAGS: -Wl,-undefined -Wl,dynamic_lookup
// #cgo !darwin LDFLAGS: -Wl,-unresolved-symbols=ignore-all
//
// #include <stdlib.h>
// #include <snappy-c.h>
//
// snappy_status snappy_encode(const char* input,
// size_t input_length,
// void** compressed,
// size_t* compressed_length) {
// *compressed_length = snappy_max_compressed_length(input_length);
// *compressed = malloc(*compressed_length);
// snappy_status status = snappy_compress(input, input_length, *compressed, compressed_length);
// if (status != SNAPPY_OK) {
// free(*compressed);
// }
// return status;
// }
//
// snappy_status snappy_decode(const char* compressed,
// size_t compressed_length,
// void** uncompressed,
// size_t* uncompressed_length) {
// snappy_status status = snappy_uncompressed_length(compressed, compressed_length, uncompressed_length);
// if (status != SNAPPY_OK) {
// return status;
// }
// *uncompressed = malloc(*uncompressed_length);
// status = snappy_uncompress(compressed, compressed_length, *uncompressed, uncompressed_length);
// if (status != SNAPPY_OK) {
// free(*uncompressed);
// }
// return status;
// }
import "C"
type snappyError int
var errText = map[snappyError]string{
errOK: "ok",
errInvalidInput: "invalid input",
errBufferTooSmall: "buffer too small",
}
func (e snappyError) Error() string {
s := errText[e]
if s == "" {
return "snappy errno " + strconv.Itoa(int(e))
}
return s
}
var (
errOK = snappyError(0)
errInvalidInput = snappyError(1)
errBufferTooSmall = snappyError(2)
)
// snappyEncode compresses the byte array src and sends the compressed
// data to w.
func snappyEncode(src []byte, w func([]byte) error) error {
if len(src) == 0 {
return w(nil)
}
var dLen C.size_t
var dst unsafe.Pointer
cerr := C.snappy_encode((*C.char)(unsafe.Pointer(&src[0])), C.size_t(len(src)),
&dst, &dLen)
if cerr != C.SNAPPY_OK {
return snappyError(cerr)
}
err := w(unsafeSlice(dst, dLen))
C.free(dst)
return err
}
// snappyDecode uncompresses the byte array src and unmarshals the
// uncompressed data into m.
func snappyDecode(src []byte, uncompressedSize uint32, m proto.Message) error {
if len(src) == 0 {
return nilSafeUnmarshal(nil, m)
}
var dLen C.size_t
var dst unsafe.Pointer
cerr := C.snappy_decode((*C.char)(unsafe.Pointer(&src[0])),
C.size_t(len(src)), &dst, &dLen)
if cerr != C.SNAPPY_OK {
return snappyError(cerr)
}
// We call through directly to proto.Unmarshal so that we don't have
// to allocate a slice for "dst" or have some awkward interface
// where the caller has to deallocate "dst".
err := nilSafeUnmarshal(unsafeSlice(dst, dLen), m)
C.free(dst)
return err
}
func unsafeSlice(p unsafe.Pointer, n C.size_t) []byte {
const maxLen = 0x7fffffff
return (*[maxLen]byte)(p)[:n:n]
}