diff --git a/c/zlib/_demo/efficiency/compress.go b/c/zlib/_demo/efficiency/compress.go index ec820122..6a912084 100644 --- a/c/zlib/_demo/efficiency/compress.go +++ b/c/zlib/_demo/efficiency/compress.go @@ -9,14 +9,14 @@ import ( 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) + txtLen := c.Ulong(len(txt)) 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 { + if res != zlib.OK { c.Printf(c.Str("\nCompression failed at level %d: %d\n"), level, res) continue } @@ -27,7 +27,7 @@ func main() { ucmpData := make([]byte, int(ucmpSize)) unRes := zlib.Uncompress(unsafe.SliceData(ucmpData), &ucmpSize, unsafe.SliceData(cmpData), cmpSize) - if unRes != zlib.Z_OK { + if unRes != zlib.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 index 6a71e41c..baf12fc4 100644 --- a/c/zlib/_demo/normal/compress.go +++ b/c/zlib/_demo/normal/compress.go @@ -9,13 +9,13 @@ import ( 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) + txtLen := c.Ulong(len(txt)) 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 { + if res != zlib.OK { c.Printf(c.Str("\nCompression failed: %d\n"), res) return } @@ -28,7 +28,7 @@ func main() { 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 { + if unRes != zlib.OK { c.Printf(c.Str("\nDecompression failed: %d\n"), unRes) return } diff --git a/c/zlib/zlib.go b/c/zlib/zlib.go index 333dc4a5..1cd6111d 100644 --- a/c/zlib/zlib.go +++ b/c/zlib/zlib.go @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package zlib import ( @@ -10,58 +26,67 @@ const ( LLGoPackage = "link: $(pkg-config --libs zlib); -lz" ) +/* errno */ 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 + OK = 0 + STREAM_END = 1 + NEED_DICT = 2 + ERRNO = -1 + STREAM_ERROR = -2 + DATA_ERROR = -3 + MEM_ERROR = -4 + BUF_ERROR = -5 + VERSION_ERROR = -6 ) -//go:linkname Compress C.compress -func Compress(dest *uint8, destLen *uint32, source *uint8, sourceLen uint32) c.Int +/* compression levels */ +const ( + NO_COMPRESSION = 0 + BEST_SPEED = 1 + BEST_COMPRESSION = 9 + DEFAULT_COMPRESSION = -1 +) -//go:linkname Compress2 C.compress2 -func Compress2(dest *uint8, destLen *uint32, source *uint8, sourceLen uint32, level c.Int) c.Int +const ( + NO_FLUSH = 0 + PARTIAL_FLUSH = 1 + SYNC_FLUSH = 2 + FULL_FLUSH = 3 + FINISH = 4 + BLOCK = 5 + TREES = 6 +) -//go:linkname Uncompress C.uncompress -func Uncompress(dest *uint8, destLen *uint32, source *uint8, sourceLen uint32) c.Int +const ( + FILTERED = 1 + HUFFMAN_ONLY = 2 + RLE = 3 + FIXED = 4 + DEFAULT_STRATEGY = 0 +) -//go:linkname Uncompress2 C.uncompress2 -func Uncompress2(dest *uint8, destLen *uint32, source *uint8, sourceLen *uint32) c.Int +const ( + BINARY = 0 + TEXT = 1 + ASCII = TEXT + UNKNOWN = 2 +) + +const ( + DEFLATED = 8 +) //go:linkname CompressBound C.compressBound -func CompressBound(sourceLen uint32) uint32 +func CompressBound(sourceLen c.Ulong) c.Ulong + +//go:linkname Compress C.compress +func Compress(dest *byte, destLen *c.Ulong, source *byte, sourceLen c.Ulong) c.Int + +//go:linkname Compress2 C.compress2 +func Compress2(dest *byte, destLen *c.Ulong, source *byte, sourceLen c.Ulong, level c.Int) c.Int + +//go:linkname Uncompress C.uncompress +func Uncompress(dest *byte, destLen *c.Ulong, source *byte, sourceLen c.Ulong) c.Int + +//go:linkname Uncompress2 C.uncompress2 +func Uncompress2(dest *byte, destLen *c.Ulong, source *byte, sourceLen *c.Ulong) c.Int