mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 14:48:47 +00:00
Allow seeding CRC32, CRC64 & Adler32 with previous partial hash.
Foo := []u8{'F', 'o','o', '3', 'F', 'o', 'o', '4'};
crc := hash.crc32(Foo[0:4]);
crc = hash.crc32(Foo[4:], crc);
crc_all := hash.crc32(Foo);
fmt.printf("%8x %8x\n", crc, crc_all);
d6285ff7 d6285ff7
a32 := hash.adler32(Foo[0:4]);
a32 = hash.adler32(Foo[4:], a32);
a32_all := hash.adler32(Foo);
fmt.printf("%8x %8x\n", a32, a32_all);
0c5102b0 0c5102b0
This commit is contained in:
+4
-4
@@ -1,14 +1,14 @@
|
|||||||
package hash
|
package hash
|
||||||
|
|
||||||
crc32 :: proc(data: []byte) -> u32 #no_bounds_check {
|
crc32 :: proc(data: []byte, seed := u32(0)) -> u32 #no_bounds_check {
|
||||||
result := ~u32(0);
|
result := ~u32(seed);
|
||||||
for b in data {
|
for b in data {
|
||||||
result = result>>8 ~ _crc32_table[(result ~ u32(b)) & 0xff];
|
result = result>>8 ~ _crc32_table[(result ~ u32(b)) & 0xff];
|
||||||
}
|
}
|
||||||
return ~result;
|
return ~result;
|
||||||
}
|
}
|
||||||
crc64 :: proc(data: []byte) -> u64 #no_bounds_check {
|
crc64 :: proc(data: []byte, seed := u32(0)) -> u64 #no_bounds_check {
|
||||||
result := ~u64(0);
|
result := ~u64(seed);
|
||||||
for b in data {
|
for b in data {
|
||||||
result = result>>8 ~ _crc64_table[(result ~ u64(b)) & 0xff];
|
result = result>>8 ~ _crc64_table[(result ~ u64(b)) & 0xff];
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -2,9 +2,9 @@ package hash
|
|||||||
|
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
|
|
||||||
adler32 :: proc(data: []byte) -> u32 {
|
adler32 :: proc(data: []byte, seed := u32(1)) -> u32 {
|
||||||
ADLER_CONST :: 65521;
|
ADLER_CONST :: 65521;
|
||||||
a, b: u32 = 1, 0;
|
a, b: u32 = seed & 0xFFFF, seed >> 16;
|
||||||
for x in data {
|
for x in data {
|
||||||
a = (a + u32(x)) % ADLER_CONST;
|
a = (a + u32(x)) % ADLER_CONST;
|
||||||
b = (b + a) % ADLER_CONST;
|
b = (b + a) % ADLER_CONST;
|
||||||
|
|||||||
Reference in New Issue
Block a user