Debian Codegraph @

encode_cgo.go

raw
1 // Copyright 2014 The zappy Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Copyright 2011 The Snappy-Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the SNAPPY-GO-LICENSE file.
8
9 // +build cgo,!purego
10
11 package zappy
12
13 /*
14
15 #include <stdint.h>
16 #include <string.h>
17
18 #define MAXOFFSET 1<<20
19
20 int putUvarint(uint8_t* buf, unsigned int x) {
21 int i = 1;
22 for (; x >= 0x80; i++) {
23 *buf++ = x|0x80;
24 x >>= 7;
25 }
26 *buf = x;
27 return i;
28 }
29
30 int putVarint(uint8_t* buf, int x) {
31 unsigned int ux = x << 1;
32 if (x < 0)
33 ux = ~ux;
34 return putUvarint(buf, ux);
35 }
36
37 int emitLiteral(uint8_t* dst, uint8_t* lit, int len_lit) {
38 int n = putVarint(dst, len_lit-1);
39 memcpy(dst+n, lit, len_lit);
40 return n+len_lit;
41 }
42
43 int emitCopy(uint8_t* dst, int off, int len) {
44 int n = putVarint(dst, -len);
45 return n+putUvarint(dst+n, (unsigned int)off);
46 }
47
48 int encode(int d, uint8_t* dst, uint8_t* src, int len_src) {
49 int table[1<<12];
50 int s = 0;
51 int t = 0;
52 int lit = 0;
53 int lim = 0;
54 memset(table, 0, sizeof(table));
55 for (lim = len_src-3; s < lim; ) {
56 // Update the hash table.
57 uint32_t b0 = src[s];
58 uint32_t b1 = src[s+1];
59 uint32_t b2 = src[s+2];
60 uint32_t b3 = src[s+3];
61 uint32_t h = b0 | (b1<<8) | (b2<<16) | (b3<<24);
62 uint32_t i;
63 more:
64 i = (h*0x1e35a7bd)>>20;
65 t = table[i];
66 table[i] = s;
67 // If t is invalid or src[s:s+4] differs from src[t:t+4], accumulate a literal byte.
68 if ((t == 0) || (s-t >= MAXOFFSET) || (b0 != src[t]) || (b1 != src[t+1]) || (b2 != src[t+2]) || (b3 != src[t+3])) {
69 s++;
70 if (s >= lim)
71 break;
72
73 b0 = b1;
74 b1 = b2;
75 b2 = b3;
76 b3 = src[s+3];
77 h = (h>>8) | ((b3)<<24);
78 goto more;
79 }
80
81 // Otherwise, we have a match. First, emit any pending literal bytes.
82 if (lit != s) {
83 d += emitLiteral(dst+d, src+lit, s-lit);
84 }
85 // Extend the match to be as long as possible.
86 int s0 = s;
87 s += 4;
88 t += 4;
89 while ((s < len_src) && (src[s] == src[t])) {
90 s++;
91 t++;
92 }
93 d += emitCopy(dst+d, s-t, s-s0);
94 lit = s;
95 }
96 // Emit any final pending literal bytes and return.
97 if (lit != len_src) {
98 d += emitLiteral(dst+d, src+lit, len_src-lit);
99 }
100 return d;
101 }
102
103 */
104 import "C"
105
106 import (
107 "encoding/binary"
108 "fmt"
109 "math"
110 )
111
112 func puregoEncode() bool { return false }
113
114 // Encode returns the encoded form of src. The returned slice may be a sub-
115 // slice of buf if buf was large enough to hold the entire encoded block.
116 // Otherwise, a newly allocated slice will be returned.
117 // It is valid to pass a nil buf.
118 func Encode(buf, src []byte) ([]byte, error) {
119 if n := MaxEncodedLen(len(src)); len(buf) < n {
120 buf = make([]byte, n)
121 }
122
123 if len(src) > math.MaxInt32 {
124 return nil, fmt.Errorf("zappy.Encode: too long data: %d bytes", len(src))
125 }
126
127 // The block starts with the varint-encoded length of the decompressed bytes.
128 d := binary.PutUvarint(buf, uint64(len(src)))
129
130 // Return early if src is short.
131 if len(src) <= 4 {
132 if len(src) != 0 {
133 d += emitLiteral(buf[d:], src)
134 }
135 return buf[:d], nil
136 }
137
138 d = int(C.encode(C.int(d), (*C.uint8_t)(&buf[0]), (*C.uint8_t)(&src[0]), C.int(len(src))))
139 return buf[:d], nil
140 }