package sarama

import (
	"bytes"
	"github.com/golang/snappy/snappy"
	"encoding/binary"
)

var snappyMagic = []byte{130, 83, 78, 65, 80, 80, 89, 0}

// SnappyEncode encodes binary data
func snappyEncode(src []byte) ([]byte, error) {
	return snappy.Encode(nil, src)
}

// SnappyDecode decodes snappy data
func snappyDecode(src []byte) ([]byte, error) {
	if bytes.Equal(src[:8], snappyMagic) {
		pos := uint32(16)
		max := uint32(len(src))
		dst := make([]byte, 0)
		for pos < max {
			size := binary.BigEndian.Uint32(src[pos : pos+4])
			pos = pos + 4
			chunk, err := snappy.Decode(nil, src[pos:pos+size])
			if err != nil {
				return nil, err
			}
			pos = pos + size
			dst = append(dst, chunk...)
		}
		return dst, nil
	}
	return snappy.Decode(nil, src)
}

package sarama

import (
	"bytes"
	"code.google.com/p/snappy-go/snappy"
	"encoding/binary"
)

var snappyMagic = []byte{130, 83, 78, 65, 80, 80, 89, 0}

// SnappyEncode encodes binary data
func snappyEncode(src []byte) ([]byte, error) {
	return snappy.Encode(nil, src)
}

// SnappyDecode decodes snappy data
func snappyDecode(src []byte) ([]byte, error) {
	if bytes.Equal(src[:8], snappyMagic) {
		pos := uint32(16)
		max := uint32(len(src))
		dst := make([]byte, 0)
		for pos < max {
			size := binary.BigEndian.Uint32(src[pos : pos+4])
			pos = pos + 4
			chunk, err := snappy.Decode(nil, src[pos:pos+size])
			if err != nil {
				return nil, err
			}
			pos = pos + size
			dst = append(dst, chunk...)
		}
		return dst, nil
	}
	return snappy.Decode(nil, src)
}

--- 
+++ 
@@ -2,7 +2,7 @@
 
 import (
 	"bytes"
-	"code.google.com/p/snappy-go/snappy"
+	"github.com/golang/snappy/snappy"
 	"encoding/binary"
 )
 
Change dependency URL for Snappy

[Snappy has been migrated from Google Code to GitHub](https://github.com/golang/snappy/commit/eaa750b9bf4dcb7cb20454be850613b66cda3273). If you try to `go get` with the code.google.com URL in place, `go get` chokes because of the 302 redirect.

Upstream, Shopify did the same thing [here](https://github.com/Shopify/sarama/commit/6d47b6cfdee82ba22aee428ea46d9934c097f49b)