library: hash/{crc32, adler32}
This commit is contained in:
@@ -298,6 +298,8 @@ Here are the Go packages that can be imported correctly:
|
||||
* [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)
|
||||
|
||||
@@ -2,10 +2,27 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash/adler32"
|
||||
"hash/crc32"
|
||||
"hash/crc64"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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()
|
||||
}
|
||||
|
||||
11
c/zlib/_demo/crc32demo/crc.go
Normal file
11
c/zlib/_demo/crc32demo/crc.go
Normal file
@@ -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"))
|
||||
}
|
||||
@@ -17,7 +17,7 @@
|
||||
package zlib
|
||||
|
||||
import (
|
||||
_ "unsafe"
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
)
|
||||
@@ -186,6 +186,14 @@ 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));
|
||||
|
||||
@@ -228,6 +236,14 @@ 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));
|
||||
|
||||
|
||||
@@ -759,6 +759,7 @@ type none struct{}
|
||||
var hasAltPkg = map[string]none{
|
||||
"crypto/md5": {},
|
||||
"fmt": {},
|
||||
"hash/crc32": {},
|
||||
"internal/abi": {},
|
||||
"internal/bytealg": {},
|
||||
"internal/itoa": {},
|
||||
|
||||
112
internal/lib/hash/crc32/crc32.go
Normal file
112
internal/lib/hash/crc32/crc32.go
Normal file
@@ -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))
|
||||
}
|
||||
Reference in New Issue
Block a user