diff --git a/README.md b/README.md index b52b82d6..86793dcf 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,10 @@ Here are the Go packages that can be imported correctly: * [encoding/base32](https://pkg.go.dev/encoding/base32) * [encoding/base64](https://pkg.go.dev/encoding/base64) * [encoding/csv](https://pkg.go.dev/encoding/csv) +* [hash](https://pkg.go.dev/hash) +* [hash/adler32](https://pkg.go.dev/hash/adler32) +* [hash/crc32](https://pkg.go.dev/hash/crc32) (partially) +* [hash/crc64](https://pkg.go.dev/hash/crc64) * [crypto/md5](https://pkg.go.dev/crypto/md5) * [regexp](https://pkg.go.dev/regexp) * [regexp/syntax](https://pkg.go.dev/regexp/syntax) diff --git a/_cmptest/crcdemo/crc.go b/_cmptest/crcdemo/crc.go new file mode 100644 index 00000000..b2d6eaae --- /dev/null +++ b/_cmptest/crcdemo/crc.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "hash/adler32" + "hash/crc32" + "hash/crc64" +) + +func crc64Demo() { + crc := crc64.MakeTable(crc64.ECMA) + fmt.Printf("%016x\n", crc64.Checksum([]byte("Hello world"), crc)) +} + +func crc32Demo() { + crc32q := crc32.MakeTable(crc32.IEEE) + fmt.Printf("%08x\n", crc32.Checksum([]byte("Hello world"), crc32q)) +} + +func adler32Demo() { + fmt.Printf("%08x\n", adler32.Checksum([]byte("Hello world"))) +} + +func main() { + adler32Demo() + crc32Demo() + crc64Demo() +} diff --git a/c/zlib/_demo/crc32demo/crc.go b/c/zlib/_demo/crc32demo/crc.go new file mode 100644 index 00000000..0d0c6843 --- /dev/null +++ b/c/zlib/_demo/crc32demo/crc.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + + "github.com/goplus/llgo/c/zlib" +) + +func main() { + fmt.Printf("%08x\n", zlib.Crc32ZString(0, "Hello world")) +} diff --git a/c/zlib/zlib.go b/c/zlib/zlib.go index 1cd6111d..0aa1aaad 100644 --- a/c/zlib/zlib.go +++ b/c/zlib/zlib.go @@ -17,7 +17,7 @@ package zlib import ( - _ "unsafe" + "unsafe" "github.com/goplus/llgo/c" ) @@ -76,17 +76,185 @@ 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 + +// ----------------------------------------------------------------------------- diff --git a/internal/build/build.go b/internal/build/build.go index 0d9d546d..8b1485d5 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -759,6 +759,7 @@ type none struct{} var hasAltPkg = map[string]none{ "crypto/md5": {}, "fmt": {}, + "hash/crc32": {}, "internal/abi": {}, "internal/bytealg": {}, "internal/itoa": {}, diff --git a/internal/lib/hash/crc32/crc32.go b/internal/lib/hash/crc32/crc32.go new file mode 100644 index 00000000..9acfcc2c --- /dev/null +++ b/internal/lib/hash/crc32/crc32.go @@ -0,0 +1,112 @@ +/* + * 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 crc32 + +// llgo:skipall +import ( + "hash" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/zlib" +) + +// The size of a CRC-32 checksum in bytes. +const Size = 4 + +// Predefined polynomials. +const ( + // IEEE is by far and away the most common CRC-32 polynomial. + // Used by ethernet (IEEE 802.3), v.42, fddi, gzip, zip, png, ... + IEEE = 0xedb88320 + + // Castagnoli's polynomial, used in iSCSI. + // Has better error detection characteristics than IEEE. + // https://dx.doi.org/10.1109/26.231911 + Castagnoli = 0x82f63b78 + + // Koopman's polynomial. + // Also has better error detection characteristics than IEEE. + // https://dx.doi.org/10.1109/DSN.2002.1028931 + Koopman = 0xeb31d82e +) + +// Table is a 256-word table representing the polynomial for efficient processing. +type Table [256]uint32 + +// IEEETable is the table for the IEEE polynomial. +var IEEETable *Table = new(Table) + +// MakeTable returns a Table constructed from the specified polynomial. +// The contents of this Table must not be modified. +func MakeTable(poly uint32) *Table { + if poly == IEEE { + return IEEETable + } + panic("todo: hash/crc32.MakeTable") +} + +type digest uint32 + +func (d *digest) Size() int { return Size } + +func (d *digest) BlockSize() int { return 1 } + +func (d *digest) Reset() { *d = 0 } + +func (d *digest) Write(p []byte) (n int, err error) { + *d = digest(zlib.Crc32ZBytes(c.Ulong(*d), p)) + return len(p), nil +} + +func (d *digest) Sum32() uint32 { return uint32(*d) } + +func (d *digest) Sum(in []byte) []byte { + s := d.Sum32() + return append(in, byte(s>>24), byte(s>>16), byte(s>>8), byte(s)) +} + +func New(tab *Table) hash.Hash32 { + if tab == IEEETable { + return new(digest) + } + panic("todo: hash/crc32.New") +} + +// NewIEEE creates a new hash.Hash32 computing the CRC-32 checksum using +// the IEEE polynomial. Its Sum method will lay the value out in +// big-endian byte order. The returned Hash32 also implements +// encoding.BinaryMarshaler and encoding.BinaryUnmarshaler to marshal +// and unmarshal the internal state of the hash. +func NewIEEE() hash.Hash32 { return New(IEEETable) } + +// Update returns the result of adding the bytes in p to the crc. +func Update(crc uint32, tab *Table, p []byte) uint32 { + if tab == IEEETable { + return uint32(zlib.Crc32ZBytes(c.Ulong(crc), p)) + } + panic("todo: hash/crc32.Update") +} + +// Checksum returns the CRC-32 checksum of data +// using the polynomial represented by the Table. +func Checksum(data []byte, tab *Table) uint32 { return Update(0, tab, data) } + +// ChecksumIEEE returns the CRC-32 checksum of data +// using the IEEE polynomial. +func ChecksumIEEE(data []byte) uint32 { + return uint32(zlib.Crc32ZBytes(0, data)) +} diff --git a/ssa/expr.go b/ssa/expr.go index 263ebb7a..d1b158dd 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -562,7 +562,7 @@ func (b Builder) BinOp(op token.Token, x, y Expr) Expr { return Expr{llvm.CreateICmp(b.impl, pred, x.impl, y.impl), tret} } case vkArray: - typ := x.raw.Type.(*types.Array) + typ := x.raw.Type.Underlying().(*types.Array) elem := b.Prog.Elem(x.Type) ret := prog.BoolVal(true) for i, n := 0, int(typ.Len()); i < n; i++ {