Spin out a std/adler32 package

This commit is contained in:
Nigel Tao committed 2018-04-27 17:20:10 +10:00
1 parent 977f1fe64d
commit 035b138cb1
13 files changed
+3189 -319

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
# Adler32
Adler32 is a checksum algorithm that hashes byte sequences to 32 bit values.
TODO: a worked example.
+50
View File
@@ -0,0 +1,50 @@
// Copyright 2017 The Wuffs Authors.
//
// 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
//
// https://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.
packageid "adl3"
// TODO: drop the '?' but still generate wuffs_adler32__hasher__initialize?
pub struct hasher?(
state base.u32 = 1,
)
// TODO: add a ! as this function is impure.
pub func hasher.update(x slice base.u8)(checksum base.u32) {
// The Adler-32 checksum's magic 65521 and 5552 numbers are discussed in
// RFC 1950.
var s1 base.u32 = this.state.low_bits(n:16)
var s2 base.u32 = this.state.high_bits(n:16)
while in.x.length() > 0 {
var remaining slice base.u8
if in.x.length() > 5552 {
remaining = in.x[5552:]
in.x = in.x[:5552]
}
iterate[8](p ptr base.u8:in.x) {
s1 ~mod+= deref p as base.u32
s2 ~mod+= s1
}
s1 %= 65521
s2 %= 65521
in.x = remaining
}
this.state = ((s2 & 0xFFFF) << 16) | (s1 & 0xFFFF)
return this.state
}