c/zlib: Z_OK => OK

This commit is contained in:
xushiwei
2024-06-25 01:02:03 +08:00
parent e68737dcfb
commit cae1b3ebd4
3 changed files with 78 additions and 53 deletions

View File

@@ -9,14 +9,14 @@ import (
func main() { 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.") 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++ { for level := 0; level <= 9; level++ {
cmpSize := zlib.CompressBound(txtLen) cmpSize := zlib.CompressBound(txtLen)
cmpData := make([]byte, int(cmpSize)) cmpData := make([]byte, int(cmpSize))
res := zlib.Compress2(unsafe.SliceData(cmpData), &cmpSize, unsafe.SliceData(txt), txtLen, c.Int(level)) 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) c.Printf(c.Str("\nCompression failed at level %d: %d\n"), level, res)
continue continue
} }
@@ -27,7 +27,7 @@ func main() {
ucmpData := make([]byte, int(ucmpSize)) ucmpData := make([]byte, int(ucmpSize))
unRes := zlib.Uncompress(unsafe.SliceData(ucmpData), &ucmpSize, unsafe.SliceData(cmpData), cmpSize) 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) c.Printf(c.Str("\nDecompression failed at level %d: %d\n"), level, unRes)
continue continue
} }

View File

@@ -9,13 +9,13 @@ import (
func main() { 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.") 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) cmpSize := zlib.CompressBound(txtLen)
cmpData := make([]byte, int(cmpSize)) cmpData := make([]byte, int(cmpSize))
res := zlib.Compress(unsafe.SliceData(cmpData), &cmpSize, unsafe.SliceData(txt), txtLen) 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) c.Printf(c.Str("\nCompression failed: %d\n"), res)
return return
} }
@@ -28,7 +28,7 @@ func main() {
unRes := zlib.Uncompress(unsafe.SliceData(ucmpData), &ucmpSize, unsafe.SliceData(cmpData), cmpSize) unRes := zlib.Uncompress(unsafe.SliceData(ucmpData), &ucmpSize, unsafe.SliceData(cmpData), cmpSize)
c.Printf(c.Str("Decompression result = %d, Decompressed size %d\n"), unRes, ucmpSize) 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) c.Printf(c.Str("\nDecompression failed: %d\n"), unRes)
return return
} }

View File

@@ -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 package zlib
import ( import (
@@ -10,58 +26,67 @@ const (
LLGoPackage = "link: $(pkg-config --libs zlib); -lz" LLGoPackage = "link: $(pkg-config --libs zlib); -lz"
) )
/* errno */
const ( const (
Z_NO_FLUSH = 0 OK = 0
Z_PARTIAL_FLUSH = 1 STREAM_END = 1
Z_SYNC_FLUSH = 2 NEED_DICT = 2
Z_FULL_FLUSH = 3 ERRNO = -1
Z_FINISH = 4 STREAM_ERROR = -2
Z_BLOCK = 5 DATA_ERROR = -3
Z_TREES = 6 MEM_ERROR = -4
BUF_ERROR = -5
Z_OK = 0 VERSION_ERROR = -6
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 /* compression levels */
func Compress(dest *uint8, destLen *uint32, source *uint8, sourceLen uint32) c.Int const (
NO_COMPRESSION = 0
BEST_SPEED = 1
BEST_COMPRESSION = 9
DEFAULT_COMPRESSION = -1
)
//go:linkname Compress2 C.compress2 const (
func Compress2(dest *uint8, destLen *uint32, source *uint8, sourceLen uint32, level c.Int) c.Int NO_FLUSH = 0
PARTIAL_FLUSH = 1
SYNC_FLUSH = 2
FULL_FLUSH = 3
FINISH = 4
BLOCK = 5
TREES = 6
)
//go:linkname Uncompress C.uncompress const (
func Uncompress(dest *uint8, destLen *uint32, source *uint8, sourceLen uint32) c.Int FILTERED = 1
HUFFMAN_ONLY = 2
RLE = 3
FIXED = 4
DEFAULT_STRATEGY = 0
)
//go:linkname Uncompress2 C.uncompress2 const (
func Uncompress2(dest *uint8, destLen *uint32, source *uint8, sourceLen *uint32) c.Int BINARY = 0
TEXT = 1
ASCII = TEXT
UNKNOWN = 2
)
const (
DEFLATED = 8
)
//go:linkname CompressBound C.compressBound //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