├── .gitignore
├── .travis.yml
├── fuzz.go
├── lz4_test.go
├── LICENSE
├── README.md
├── lz4-example
└── main.go
├── fuzzer
└── main.go
├── reader.go
└── writer.go
/.gitignore:
--------------------------------------------------------------------------------
1 | /lz4-example/lz4-example
2 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: go
2 |
3 | go:
4 | - 1.1
5 | - 1.2
6 | - 1.3
7 | - 1.4
8 | - 1.5
9 | - tip
10 |
--------------------------------------------------------------------------------
/fuzz.go:
--------------------------------------------------------------------------------
1 | // +build gofuzz
2 |
3 | package lz4
4 |
5 | import "encoding/binary"
6 |
7 | func Fuzz(data []byte) int {
8 |
9 | if len(data) < 4 {
10 | return 0
11 | }
12 |
13 | ln := binary.LittleEndian.Uint32(data)
14 | if ln > (1 << 21) {
15 | return 0
16 | }
17 |
18 | if _, err := Decode(nil, data); err != nil {
19 | return 0
20 | }
21 |
22 | return 1
23 | }
24 |
--------------------------------------------------------------------------------
/lz4_test.go:
--------------------------------------------------------------------------------
1 | package lz4
2 |
3 | import (
4 | "bytes"
5 | "io/ioutil"
6 | "testing"
7 | )
8 |
9 | var testfile, _ = ioutil.ReadFile("testdata/pg1661.txt")
10 |
11 | func roundtrip(t *testing.T, input []byte) {
12 |
13 | dst, err := Encode(nil, input)
14 | if err != nil {
15 | t.Errorf("got error during compression: %s", err)
16 | }
17 |
18 | output, err := Decode(nil, dst)
19 |
20 | if err != nil {
21 | t.Errorf("got error during decompress: %s", err)
22 | }
23 |
24 | if !bytes.Equal(output, input) {
25 | t.Errorf("roundtrip failed")
26 | }
27 | }
28 |
29 | func TestEmpty(t *testing.T) {
30 | roundtrip(t, nil)
31 | }
32 |
33 | func TestLengths(t *testing.T) {
34 |
35 | for i := 0; i < 1024; i++ {
36 | roundtrip(t, testfile[:i])
37 | }
38 |
39 | for i := 1024; i < 4096; i += 23 {
40 | roundtrip(t, testfile[:i])
41 | }
42 | }
43 |
44 | func TestWords(t *testing.T) {
45 | roundtrip(t, testfile)
46 | }
47 |
48 | func BenchmarkLZ4Encode(b *testing.B) {
49 | for i := 0; i < b.N; i++ {
50 | Encode(nil, testfile)
51 | }
52 | }
53 |
54 | func BenchmarkLZ4Decode(b *testing.B) {
55 |
56 | var compressed, _ = Encode(nil, testfile)
57 |
58 | b.ResetTimer()
59 |
60 | for i := 0; i < b.N; i++ {
61 | Decode(nil, compressed)
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2011-2012 Branimir Karadzic. All rights reserved.
2 | Copyright 2013 Damian Gryski. All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without modification,
5 | are permitted provided that the following conditions are met:
6 |
7 | 1. Redistributions of source code must retain the above copyright notice, this
8 | list of conditions and the following disclaimer.
9 |
10 | 2. Redistributions in binary form must reproduce the above copyright notice,
11 | this list of conditions and the following disclaimer in the documentation
12 | and/or other materials provided with the distribution.
13 |
14 | THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR
15 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
17 | SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 | THE POSSIBILITY OF SUCH DAMAGE.
24 |
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | go-lz4
2 | ======
3 |
4 | go-lz4 is port of LZ4 lossless compression algorithm to Go. The original C code
5 | is located at:
6 |
7 | https://github.com/Cyan4973/lz4
8 |
9 | Status
10 | ------
11 | [](http://travis-ci.org/bkaradzic/go-lz4)
12 | [](https://godoc.org/github.com/bkaradzic/go-lz4)
13 |
14 | Usage
15 | -----
16 |
17 | go get github.com/bkaradzic/go-lz4
18 |
19 | import "github.com/bkaradzic/go-lz4"
20 |
21 | The package name is `lz4`
22 |
23 | Notes
24 | -----
25 |
26 | * go-lz4 saves a uint32 with the original uncompressed length at the beginning
27 | of the encoded buffer. They may get in the way of interoperability with
28 | other implementations.
29 |
30 | Alternative
31 | -----------
32 |
33 | https://github.com/pierrec/lz4
34 |
35 | Contributors
36 | ------------
37 |
38 | Damian Gryski ([@dgryski](https://github.com/dgryski))
39 | Dustin Sallings ([@dustin](https://github.com/dustin))
40 |
41 | Contact
42 | -------
43 |
44 | [@bkaradzic](https://twitter.com/bkaradzic)
45 | http://www.stuckingeometry.com
46 |
47 | Project page
48 | https://github.com/bkaradzic/go-lz4
49 |
50 | License
51 | -------
52 |
53 | Copyright 2011-2012 Branimir Karadzic. All rights reserved.
54 | Copyright 2013 Damian Gryski. All rights reserved.
55 |
56 | Redistribution and use in source and binary forms, with or without modification,
57 | are permitted provided that the following conditions are met:
58 |
59 | 1. Redistributions of source code must retain the above copyright notice, this
60 | list of conditions and the following disclaimer.
61 |
62 | 2. Redistributions in binary form must reproduce the above copyright notice,
63 | this list of conditions and the following disclaimer in the documentation
64 | and/or other materials provided with the distribution.
65 |
66 | THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR
67 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
68 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
69 | SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
70 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
71 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
72 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
73 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
74 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
75 | THE POSSIBILITY OF SUCH DAMAGE.
76 |
77 |
--------------------------------------------------------------------------------
/lz4-example/main.go:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2011 Branimir Karadzic. All rights reserved.
3 | *
4 | * Redistribution and use in source and binary forms, with or without modification,
5 | * are permitted provided that the following conditions are met:
6 | *
7 | * 1. Redistributions of source code must retain the above copyright notice, this
8 | * list of conditions and the following disclaimer.
9 | *
10 | * 2. Redistributions in binary form must reproduce the above copyright notice,
11 | * this list of conditions and the following disclaimer in the documentation
12 | * and/or other materials provided with the distribution.
13 | *
14 | * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS OR
15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
17 | * SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 | * THE POSSIBILITY OF SUCH DAMAGE.
24 | */
25 |
26 | package main
27 |
28 | import (
29 | "flag"
30 | "fmt"
31 | "io/ioutil"
32 | "log"
33 | "os"
34 | "runtime/pprof"
35 |
36 | lz4 "github.com/bkaradzic/go-lz4"
37 | )
38 |
39 | var (
40 | decompress = flag.Bool("d", false, "decompress")
41 | )
42 |
43 | func main() {
44 |
45 | var optCPUProfile = flag.String("cpuprofile", "", "profile")
46 | flag.Parse()
47 |
48 | if *optCPUProfile != "" {
49 | f, err := os.Create(*optCPUProfile)
50 | if err != nil {
51 | log.Fatal(err)
52 | }
53 | pprof.StartCPUProfile(f)
54 | defer pprof.StopCPUProfile()
55 | }
56 |
57 | args := flag.Args()
58 |
59 | var data []byte
60 |
61 | if len(args) < 2 {
62 | fmt.Print("Usage: lz4 [-d]