Coder Social home page Coder Social logo

4kills / go-zlib Goto Github PK

View Code? Open in Web Editor NEW
25.0 2.0 10.0 5.93 MB

Ultra fast native zlib implementation for golang using cgo and the original zlib library written in C by Jean-loup Gailly and Mark Adler.

License: zlib License

Go 97.98% C 2.02%
go native zlib zlib-library zlib-1-2-11 cgo zlib-port wrapper-library compression decompression

go-zlib's People

Contributors

4kills avatar elee1766 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

go-zlib's Issues

How can we compile on arm64 arch

When I tried to build my go project on arm64 Ubuntu 18.04, it failed to build and the error message is:

# github.com/4kills/go-zlib/native
/usr/bin/ld: ../../../go/pkg/mod/github.com/4kills/[email protected]/native/libs/linuxlibz.a(deflate.o): Relocations in generic ELF (EM: 62)
/usr/bin/ld: ../../../go/pkg/mod/github.com/4kills/[email protected]/native/libs/linuxlibz.a(deflate.o): Relocations in generic ELF (EM: 62)
/usr/bin/ld: ../../../go/pkg/mod/github.com/4kills/[email protected]/native/libs/linuxlibz.a(deflate.o): Relocations in generic ELF (EM: 62)
/usr/bin/ld: ../../../go/pkg/mod/github.com/4kills/[email protected]/native/libs/linuxlibz.a: error adding symbols: file in wrong format
collect2: error: ld returned 1 exit status

Crash when decompressing JSON string

I have the following compressor / decompressor combo:

zlibCompressorFactory := func(compressionLevel int) func(rawBytes []byte) ([]byte, error) {
	return func(rawBytes []byte) ([]byte, error) {
		compressedBytesBufferWriter := &bytes.Buffer{}

		zlibCompressor, err := zlib.NewWriterLevel(compressedBytesBufferWriter, compressionLevel)
		if err != nil {
			return nil, err
		}

		_, err = zlibCompressor.Write(rawBytes)
		zlibCompressor.Close() //dont use defer   it wont work

		if err != nil {
			return nil, err
		}

		return compressedBytesBufferWriter.Bytes(), err
	}
}
zlibDecompressorFactory := func() func(compressedBytes []byte) ([]byte, error) {
	return func(compressedBytes []byte) ([]byte, error) {
		compressedInputBuffer := bytes.NewReader(compressedBytes)

		zlibDecompressor, err := zlib.NewReader(compressedInputBuffer)
		if err != nil {
			return nil, err
		}

		decompressedBytes, err := ioutil.ReadAll(zlibDecompressor)
		zlibDecompressor.Close()

		if err != nil {
			return nil, err
		}

		return decompressedBytes, nil
	}
}

But when trying to decompress I get the following error - this doesn't happen with "common/zlib" it only happens with "go-zlib" (tested with both golang 1.17 and 1.18):

Running tool: /usr/local/go/bin/go test -benchmem -run=^$ -bench ^Benchmark___DecompressionAndDeserializationPerformance___Json$ github.com/klauspost/compress/arena/bb-decompression-deserialization-performance

goos: linux
panic: runtime error: slice bounds out of range [:1034] with capacity 896

goroutine 21 [running]:
io.ReadAll({0xbae4e0, 0xc0002208d0})
/usr/local/go/src/io/io.go:646 +0x197
io/ioutil.ReadAll(...)
/usr/local/go/src/io/ioutil/ioutil.go:27
github.com/klauspost/compress/arena.InitCompressionTestCases.func2.1({0xc000382000, 0x2d3, 0x351})
/mnt/c/VS/laerdal/magellan-serialization-and-compression-benchmarks-testbed/arena/initialize-alternative-datasources-from-main-datasource.go:210 +0x9f
github.com/klauspost/compress/arena/bb-decompression-deserialization-performance.Benchmark___DecompressionAndDeserializationPerformance___Json.func1(0xc0001f6240)
/mnt/c/VS/laerdal/magellan-serialization-and-compression-benchmarks-testbed/arena/bb-decompression-deserialization-performance/xaa_json_test.go:40 +0x3c3
testing.(*B).runN(0xc0001f6240, 0x1)
/usr/local/go/src/testing/benchmark.go:193 +0x102
testing.(*B).run1.func1()
/usr/local/go/src/testing/benchmark.go:233 +0x59
created by testing.(*B).run1
/usr/local/go/src/testing/benchmark.go:226 +0x9c
exit status 2
FAIL github.com/klauspost/compress/arena/bb-decompression-deserialization-performance 0.018s
FAIL

What gives?

Read() doesn't adhere to io.Reader specifications

Hi,

I noticed that Read() does not adhere to the io.Reader specifications;.

The specification says Read reads up to len(p) bytes into p (source)
Meanwhile, the library's Read() does the following (source)

		min := len(p)
		if len(p) < r.outBuffer.Len() {
			min = r.outBuffer.Len()
		}
		copy(p, r.outBuffer.Bytes()[:min])

Consequently, the read could be larger than len(p).
I'm wondering whether this deviation is deliberate?

Thanks!

Different bytewise compressed data as default library though decompression is compatiable

Running the below code on AMD64 platform, MBP 2019

func main() {

	var a = "i love you"

	writter := zlib2.NewWriter(nil)
	result := OldCompress([]byte(a)) 
	result2, _ := writter.WriteBuffer([]byte(a), nil)
	fmt.Println(result)
	fmt.Println(result2)

	reader, _ := zlib2.NewReader(nil)
	readResult := DeCompress(result2)
	_, readReuslt2, _ := reader.ReadBuffer(result2, nil)
	fmt.Println(string(readResult))
	fmt.Println(string(readReuslt2))

}

func OldCompress(src []byte) []byte {
	var in bytes.Buffer
	w := zlib.NewWriter(&in)
	_, error := w.Write(src)
	if error != nil {
		return nil
	}
	error = w.Close()
	if error != nil {
		return nil
	}
	return in.Bytes()
}

func DeCompress(compressSrc []byte) []byte {
	b := bytes.NewReader(compressSrc)
	var out bytes.Buffer
	r, error := zlib.NewReader(b)
	if error != nil {
		return nil
	}
	_, error = io.Copy(&out, r)
	if error != nil {
		return nil
	}
	return out.Bytes()
}
output:
[120 156 202 84 200 201 47 75 85 168 204 47 5 4 0 0 255 255 19 168 3 189]
[120 156 203 84 200 201 47 75 85 168 204 47 5 0 19 168 3 189]
i love you
i love you

The compressed bytes are a bit different so I would love to know why

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.