diff --git a/c/zlib/_demo/efficiency/compress.go b/c/zlib/_demo/efficiency/compress.go new file mode 100644 index 00000000..ec820122 --- /dev/null +++ b/c/zlib/_demo/efficiency/compress.go @@ -0,0 +1,35 @@ +package main + +import ( + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/zlib" +) + +func main() { + txt := []byte("zlib is a software library used for data compression. It was created by Jean-loup Gailly and Mark Adler and first released in 1995. zlib is designed to be a free, legally unencumbered—that is, not covered by any patents—alternative to the proprietary DEFLATE compression algorithm, which is often used in software applications for data compression.The library provides functions to compress and decompress data using the DEFLATE algorithm, which is a combination of the LZ77 algorithm and Huffman coding. zlib is notable for its versatility; it can be used in a wide range of applications, from web servers and web clients compressing HTTP data, to the compression of data for storage or transmission in various file formats, such as PNG, ZIP, and GZIP.") + txtLen := uint32(len(txt) + 1) + + for level := 0; level <= 9; level++ { + cmpSize := zlib.CompressBound(txtLen) + cmpData := make([]byte, int(cmpSize)) + + res := zlib.Compress2(unsafe.SliceData(cmpData), &cmpSize, unsafe.SliceData(txt), txtLen, c.Int(level)) + if res != zlib.Z_OK { + c.Printf(c.Str("\nCompression failed at level %d: %d\n"), level, res) + continue + } + + c.Printf(c.Str("Compression level %d: Text length = %d, Compressed size = %d\n"), level, txtLen, cmpSize) + + ucmpSize := txtLen + ucmpData := make([]byte, int(ucmpSize)) + + unRes := zlib.Uncompress(unsafe.SliceData(ucmpData), &ucmpSize, unsafe.SliceData(cmpData), cmpSize) + if unRes != zlib.Z_OK { + c.Printf(c.Str("\nDecompression failed at level %d: %d\n"), level, unRes) + continue + } + } +} diff --git a/c/zlib/_demo/normal/compress.go b/c/zlib/_demo/normal/compress.go new file mode 100644 index 00000000..6a71e41c --- /dev/null +++ b/c/zlib/_demo/normal/compress.go @@ -0,0 +1,46 @@ +package main + +import ( + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/zlib" +) + +func main() { + txt := []byte("zlib is a software library used for data compression. It was created by Jean-loup Gailly and Mark Adler and first released in 1995. zlib is designed to be a free, legally unencumbered—that is, not covered by any patents—alternative to the proprietary DEFLATE compression algorithm, which is often used in software applications for data compression.The library provides functions to compress and decompress data using the DEFLATE algorithm, which is a combination of the LZ77 algorithm and Huffman coding. zlib is notable for its versatility; it can be used in a wide range of applications, from web servers and web clients compressing HTTP data, to the compression of data for storage or transmission in various file formats, such as PNG, ZIP, and GZIP.") + txtLen := uint32(len(txt) + 1) + + cmpSize := zlib.CompressBound(txtLen) + cmpData := make([]byte, int(cmpSize)) + + res := zlib.Compress(unsafe.SliceData(cmpData), &cmpSize, unsafe.SliceData(txt), txtLen) + if res != zlib.Z_OK { + c.Printf(c.Str("\nCompression failed: %d\n"), res) + return + } + + c.Printf(c.Str("Text length = %d, Compressed size = %d\n"), txtLen, cmpSize) + + ucmpSize := txtLen + ucmpData := make([]byte, int(ucmpSize)) + + unRes := zlib.Uncompress(unsafe.SliceData(ucmpData), &ucmpSize, unsafe.SliceData(cmpData), cmpSize) + c.Printf(c.Str("Decompression result = %d, Decompressed size %d\n"), unRes, ucmpSize) + + if unRes != zlib.Z_OK { + c.Printf(c.Str("\nDecompression failed: %d\n"), unRes) + return + } + + c.Printf(c.Str("Decompressed data: \n")) + for i := 0; i < int(ucmpSize); i++ { + c.Printf(c.Str("%c"), ucmpData[i]) + } +} + +/* Expected output: +origin textLen = 73 compressed_size = 36 +uncompress result = 0 uncompress result size 73 +after uncompressed data: Hello, zlib compression!Hello, zlib compression!Hello, zlib compression! +*/ diff --git a/c/zlib/llgo_autogen.lla b/c/zlib/llgo_autogen.lla new file mode 100644 index 00000000..f21d0302 Binary files /dev/null and b/c/zlib/llgo_autogen.lla differ diff --git a/c/zlib/zlib.go b/c/zlib/zlib.go new file mode 100644 index 00000000..333dc4a5 --- /dev/null +++ b/c/zlib/zlib.go @@ -0,0 +1,67 @@ +package zlib + +import ( + _ "unsafe" + + "github.com/goplus/llgo/c" +) + +const ( + LLGoPackage = "link: $(pkg-config --libs zlib); -lz" +) + +const ( + Z_NO_FLUSH = 0 + Z_PARTIAL_FLUSH = 1 + Z_SYNC_FLUSH = 2 + Z_FULL_FLUSH = 3 + Z_FINISH = 4 + Z_BLOCK = 5 + Z_TREES = 6 + + Z_OK = 0 + Z_STREAM_END = 1 + Z_NEED_DICT = 2 + Z_ERRNO = -1 + Z_STREAM_ERROR = -2 + Z_DATA_ERROR = -3 + Z_MEM_ERROR = -4 + Z_BUF_ERROR = -5 + Z_VERSION_ERROR = -6 + + Z_NO_COMPRESSION = 0 + Z_BEST_SPEED = 1 + Z_BEST_COMPRESSION = 9 + Z_DEFAULT_COMPRESSION = -1 + /* compression levels */ + + Z_FILTERED = 1 + Z_HUFFMAN_ONLY = 2 + Z_RLE = 3 + Z_FIXED = 4 + Z_DEFAULT_STRATEGY = 0 + + Z_BINARY = 0 + Z_TEXT = 1 + Z_ASCII = Z_TEXT + Z_UNKNOWN = 2 + + Z_DEFLATED = 8 + + Z_NULL = 0 +) + +//go:linkname Compress C.compress +func Compress(dest *uint8, destLen *uint32, source *uint8, sourceLen uint32) c.Int + +//go:linkname Compress2 C.compress2 +func Compress2(dest *uint8, destLen *uint32, source *uint8, sourceLen uint32, level c.Int) c.Int + +//go:linkname Uncompress C.uncompress +func Uncompress(dest *uint8, destLen *uint32, source *uint8, sourceLen uint32) c.Int + +//go:linkname Uncompress2 C.uncompress2 +func Uncompress2(dest *uint8, destLen *uint32, source *uint8, sourceLen *uint32) c.Int + +//go:linkname CompressBound C.compressBound +func CompressBound(sourceLen uint32) uint32