compiler: build separation runtime with clite
This commit is contained in:
30
runtime/internal/clite/zlib/README.md
Normal file
30
runtime/internal/clite/zlib/README.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# LLGo wrapper of madler/zlib
|
||||
|
||||
## How to install
|
||||
|
||||
### on macOS (Homebrew)
|
||||
|
||||
```sh
|
||||
brew install zlib
|
||||
```
|
||||
|
||||
### on Linux (Debian/Ubuntu)
|
||||
|
||||
```sh
|
||||
TODO
|
||||
```
|
||||
|
||||
## Demos
|
||||
|
||||
The `_demo` directory contains our demos (it start with `_` to prevent the `go` command from compiling it):
|
||||
|
||||
- [normal](_demo/normal/compress.go): a basic zlib demo
|
||||
|
||||
### How to run demos
|
||||
|
||||
To run the demos in directory `_demo`:
|
||||
|
||||
```sh
|
||||
cd <demo-directory> # eg. cd _demo/normal
|
||||
llgo run .
|
||||
```
|
||||
11
runtime/internal/clite/zlib/_demo/crc32demo/crc.go
Normal file
11
runtime/internal/clite/zlib/_demo/crc32demo/crc.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/goplus/llgo/runtime/internal/clite/zlib"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("%08x\n", zlib.Crc32ZString(0, "Hello world"))
|
||||
}
|
||||
35
runtime/internal/clite/zlib/_demo/efficiency/compress.go
Normal file
35
runtime/internal/clite/zlib/_demo/efficiency/compress.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
c "github.com/goplus/llgo/runtime/internal/clite"
|
||||
"github.com/goplus/llgo/runtime/internal/clite/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 := 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.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.OK {
|
||||
c.Printf(c.Str("\nDecompression failed at level %d: %d\n"), level, unRes)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
46
runtime/internal/clite/zlib/_demo/normal/compress.go
Normal file
46
runtime/internal/clite/zlib/_demo/normal/compress.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
|
||||
c "github.com/goplus/llgo/runtime/internal/clite"
|
||||
"github.com/goplus/llgo/runtime/internal/clite/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 := 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.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.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!
|
||||
*/
|
||||
260
runtime/internal/clite/zlib/zlib.go
Normal file
260
runtime/internal/clite/zlib/zlib.go
Normal file
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
* 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 (
|
||||
"unsafe"
|
||||
|
||||
c "github.com/goplus/llgo/runtime/internal/clite"
|
||||
)
|
||||
|
||||
const (
|
||||
LLGoPackage = "link: $(pkg-config --libs zlib); -lz"
|
||||
)
|
||||
|
||||
/* errno */
|
||||
const (
|
||||
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
|
||||
)
|
||||
|
||||
/* compression levels */
|
||||
const (
|
||||
NO_COMPRESSION = 0
|
||||
BEST_SPEED = 1
|
||||
BEST_COMPRESSION = 9
|
||||
DEFAULT_COMPRESSION = -1
|
||||
)
|
||||
|
||||
const (
|
||||
NO_FLUSH = 0
|
||||
PARTIAL_FLUSH = 1
|
||||
SYNC_FLUSH = 2
|
||||
FULL_FLUSH = 3
|
||||
FINISH = 4
|
||||
BLOCK = 5
|
||||
TREES = 6
|
||||
)
|
||||
|
||||
const (
|
||||
FILTERED = 1
|
||||
HUFFMAN_ONLY = 2
|
||||
RLE = 3
|
||||
FIXED = 4
|
||||
DEFAULT_STRATEGY = 0
|
||||
)
|
||||
|
||||
const (
|
||||
BINARY = 0
|
||||
TEXT = 1
|
||||
ASCII = TEXT
|
||||
UNKNOWN = 2
|
||||
)
|
||||
|
||||
const (
|
||||
DEFLATED = 8
|
||||
)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
|
||||
|
||||
compressBound() returns an upper bound on the compressed size after
|
||||
compress() or compress2() on sourceLen bytes. It would be used before a
|
||||
compress() or compress2() call to allocate the destination buffer.
|
||||
*/
|
||||
//go:linkname CompressBound C.compressBound
|
||||
func CompressBound(sourceLen c.Ulong) c.Ulong
|
||||
|
||||
/*
|
||||
ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
|
||||
const Bytef *source, uLong sourceLen));
|
||||
|
||||
Compresses the source buffer into the destination buffer. sourceLen is
|
||||
the byte length of the source buffer. Upon entry, destLen is the total size
|
||||
of the destination buffer, which must be at least the value returned by
|
||||
compressBound(sourceLen). Upon exit, destLen is the actual size of the
|
||||
compressed data. compress() is equivalent to compress2() with a level
|
||||
parameter of Z_DEFAULT_COMPRESSION.
|
||||
|
||||
compress returns Z_OK if success, Z_MEM_ERROR if there was not
|
||||
enough memory, Z_BUF_ERROR if there was not enough room in the output
|
||||
buffer.
|
||||
*/
|
||||
//go:linkname Compress C.compress
|
||||
func Compress(dest *byte, destLen *c.Ulong, source *byte, sourceLen c.Ulong) c.Int
|
||||
|
||||
/*
|
||||
ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen,
|
||||
const Bytef *source, uLong sourceLen, int level));
|
||||
|
||||
Compresses the source buffer into the destination buffer. The level
|
||||
parameter has the same meaning as in deflateInit. sourceLen is the byte
|
||||
length of the source buffer. Upon entry, destLen is the total size of the
|
||||
destination buffer, which must be at least the value returned by
|
||||
compressBound(sourceLen). Upon exit, destLen is the actual size of the
|
||||
compressed data.
|
||||
|
||||
compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
|
||||
memory, Z_BUF_ERROR if there was not enough room in the output buffer,
|
||||
Z_STREAM_ERROR if the level parameter is invalid.
|
||||
*/
|
||||
//go:linkname Compress2 C.compress2
|
||||
func Compress2(dest *byte, destLen *c.Ulong, source *byte, sourceLen c.Ulong, level c.Int) c.Int
|
||||
|
||||
/*
|
||||
ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
|
||||
const Bytef *source, uLong sourceLen));
|
||||
|
||||
Decompresses the source buffer into the destination buffer. sourceLen is
|
||||
the byte length of the source buffer. Upon entry, destLen is the total size
|
||||
of the destination buffer, which must be large enough to hold the entire
|
||||
uncompressed data. (The size of the uncompressed data must have been saved
|
||||
previously by the compressor and transmitted to the decompressor by some
|
||||
mechanism outside the scope of this compression library.) Upon exit, destLen
|
||||
is the actual size of the uncompressed data.
|
||||
|
||||
uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
|
||||
enough memory, Z_BUF_ERROR if there was not enough room in the output
|
||||
buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. In
|
||||
the case where there is not enough room, uncompress() will fill the output
|
||||
buffer with the uncompressed data up to that point.
|
||||
*/
|
||||
//go:linkname Uncompress C.uncompress
|
||||
func Uncompress(dest *byte, destLen *c.Ulong, source *byte, sourceLen c.Ulong) c.Int
|
||||
|
||||
/*
|
||||
ZEXTERN int ZEXPORT uncompress2 OF((Bytef *dest, uLongf *destLen,
|
||||
const Bytef *source, uLong *sourceLen));
|
||||
|
||||
Same as uncompress, except that sourceLen is a pointer, where the
|
||||
length of the source is *sourceLen. On return, *sourceLen is the number of
|
||||
source bytes consumed.
|
||||
*/
|
||||
//go:linkname Uncompress2 C.uncompress2
|
||||
func Uncompress2(dest *byte, destLen *c.Ulong, source *byte, sourceLen *c.Ulong) c.Int
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len));
|
||||
|
||||
Update a running CRC-32 with the bytes buf[0..len-1] and return the
|
||||
updated CRC-32. If buf is Z_NULL, this function returns the required
|
||||
initial value for the crc. Pre- and post-conditioning (one's complement) is
|
||||
performed within this function so it shouldn't be done by the application.
|
||||
|
||||
Usage example:
|
||||
|
||||
uLong crc = crc32(0L, Z_NULL, 0);
|
||||
|
||||
while (read_buffer(buffer, length) != EOF) {
|
||||
crc = crc32(crc, buffer, length);
|
||||
}
|
||||
if (crc != original_crc) error();
|
||||
*/
|
||||
//go:linkname Crc32 C.crc32
|
||||
func Crc32(crc c.Ulong, buf *byte, len c.Uint) c.Ulong
|
||||
|
||||
/*
|
||||
ZEXTERN uLong ZEXPORT crc32_z OF((uLong adler, const Bytef *buf, z_size_t len));
|
||||
|
||||
Same as crc32(), but with a size_t length.
|
||||
*/
|
||||
//go:linkname Crc32Z C.crc32_z
|
||||
func Crc32Z(crc c.Ulong, buf *byte, len uintptr) c.Ulong
|
||||
|
||||
func Crc32ZBytes(crc c.Ulong, buf []byte) c.Ulong {
|
||||
return Crc32Z(crc, unsafe.SliceData(buf), uintptr(len(buf)))
|
||||
}
|
||||
|
||||
func Crc32ZString(crc c.Ulong, buf string) c.Ulong {
|
||||
return Crc32Z(crc, unsafe.StringData(buf), uintptr(len(buf)))
|
||||
}
|
||||
|
||||
/*
|
||||
ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
|
||||
|
||||
Combine two CRC-32 check values into one. For two sequences of bytes,
|
||||
seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
|
||||
calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32
|
||||
check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
|
||||
len2.
|
||||
*/
|
||||
//go:linkname Crc32Combine C.crc32_combine
|
||||
func Crc32Combine(crc1 c.Ulong, crc2 c.Ulong, len2 int64) c.Ulong
|
||||
|
||||
/*
|
||||
ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
|
||||
|
||||
Update a running Adler-32 checksum with the bytes buf[0..len-1] and
|
||||
return the updated checksum. If buf is Z_NULL, this function returns the
|
||||
required initial value for the checksum.
|
||||
|
||||
An Adler-32 checksum is almost as reliable as a CRC-32 but can be computed
|
||||
much faster.
|
||||
|
||||
Usage example:
|
||||
|
||||
uLong adler = adler32(0L, Z_NULL, 0);
|
||||
|
||||
while (read_buffer(buffer, length) != EOF) {
|
||||
adler = adler32(adler, buffer, length);
|
||||
}
|
||||
if (adler != original_adler) error();
|
||||
*/
|
||||
//go:linkname Adler32 C.adler32
|
||||
func Adler32(adler c.Ulong, buf *byte, len c.Uint) c.Ulong
|
||||
|
||||
/*
|
||||
ZEXTERN uLong ZEXPORT adler32_z OF((uLong adler, const Bytef *buf, z_size_t len));
|
||||
|
||||
Same as adler32(), but with a size_t length.
|
||||
*/
|
||||
//go:linkname Adler32Z C.adler32_z
|
||||
func Adler32Z(adler c.Ulong, buf *byte, len uintptr) c.Ulong
|
||||
|
||||
func Adler32ZBytes(adler c.Ulong, buf []byte) c.Ulong {
|
||||
return Adler32Z(adler, unsafe.SliceData(buf), uintptr(len(buf)))
|
||||
}
|
||||
|
||||
func Adler32ZString(adler c.Ulong, buf string) c.Ulong {
|
||||
return Adler32Z(adler, unsafe.StringData(buf), uintptr(len(buf)))
|
||||
}
|
||||
|
||||
/*
|
||||
ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, z_off_t len2));
|
||||
|
||||
Combine two Adler-32 checksums into one. For two sequences of bytes, seq1
|
||||
and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
|
||||
each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of
|
||||
seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note
|
||||
that the z_off_t type (like off_t) is a signed integer. If len2 is
|
||||
negative, the result has no meaning or utility.
|
||||
*/
|
||||
//go:linkname Adler32Combine C.adler32_combine
|
||||
func Adler32Combine(adler1 c.Ulong, adler2 c.Ulong, len2 int64) c.Ulong
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
Reference in New Issue
Block a user