encode_cgo.go 3.2 KB

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