mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
base: add hasher_u64
This commit is contained in:
1 parent
74a6ea2fcd
commit
88efa8360f
4 files changed
+281
-5
No files matched your search
+2
-1
@@ -9,7 +9,8 @@ The LICENSE has changed from a single license (Apache 2) to a dual license
|
||||
- Added `compact_retaining` and `history_retain_length`.
|
||||
- Added `example/toy-aux-image`.
|
||||
- Added `get_quirk(key: u32) u64`.
|
||||
- Added `hasher_u32` `update!` and `checksum_u32` methods
|
||||
- Added `hasher_u32` `update!` and `checksum_u32` methods.
|
||||
- Added `hasher_u64`.
|
||||
- Added `if.likely` and `if.unlikely`.
|
||||
- Added `slice_var as nptr array[etc] etc` conversion.
|
||||
- Added `std/jpeg`.
|
||||
|
||||
+14
-4
@@ -4,10 +4,10 @@ Hashers take a stream of bytes (of arbitrary length) and produce a fixed length
|
||||
value - the hash. For example, the CRC-32/IEEE hashing algorithm produces a 32
|
||||
bit value (a `base.u32`). The MD5 hashing algorithm produces a 128 bit value.
|
||||
|
||||
Wuffs' hasher implementations have only one method. For 32 bit hashes, the
|
||||
method signature is `update_u32!(x: slice base.u8) base.u32`. It incrementally
|
||||
updates the hasher object's state with the addition data `x`, and returns the
|
||||
hash value so far, for all of the data up to and including `x`.
|
||||
Wuffs' hasher implementations have one core method. For 32 bit hashes, the
|
||||
method signature is `update_u32!(x: roslice base.u8) base.u32`. It
|
||||
incrementally updates the hasher object's state with the addition data `x`, and
|
||||
returns the hash value so far, for all of the data up to and including `x`.
|
||||
|
||||
This method is stateful. Calling `update_u32` twice with the same slice of
|
||||
bytes can produce two different hash values. Conversely, calling `update_u32`
|
||||
@@ -15,6 +15,13 @@ twice with two different slices should be equivalent to calling it once on
|
||||
their concatenation. [Re-initialize](/doc/note/initialization.md) the object to
|
||||
reset the state.
|
||||
|
||||
The `update_u32!` method is equivalent to calling `update!` and then
|
||||
`checksum_u32` (which is a pure method). For the rare algorithms where
|
||||
computing the checksum from internal state is relatively expensive, and when
|
||||
streaming many relatively-small updates, it might be more efficient to call
|
||||
`update!` N times and `checksum_u32` only once, instead of calling
|
||||
`update_u32!` N times.
|
||||
|
||||
Wuffs' hasher implementations are not cryptographic. They make no attempt to
|
||||
resist timing attacks.
|
||||
|
||||
@@ -29,6 +36,9 @@ In Wuffs syntax, the `base.hasher_u32` methods are:
|
||||
- `update!(x: slice u8)`
|
||||
- `update_u32!(x: slice u8) u32`
|
||||
|
||||
The `base.hasher_u64` methods are similar, except for the obvious difference of
|
||||
calculating a 64-bit (not 32-bit) checksum.
|
||||
|
||||
|
||||
## Implementations
|
||||
|
||||
|
||||
@@ -935,6 +935,7 @@ var funcsOther = [...]string{
|
||||
|
||||
var Interfaces = []string{
|
||||
"hasher_u32",
|
||||
"hasher_u64",
|
||||
"image_decoder",
|
||||
"io_transformer",
|
||||
"token_decoder",
|
||||
@@ -942,6 +943,7 @@ var Interfaces = []string{
|
||||
|
||||
var InterfacesMap = map[string]bool{
|
||||
"hasher_u32": true,
|
||||
"hasher_u64": true,
|
||||
"image_decoder": true,
|
||||
"io_transformer": true,
|
||||
"token_decoder": true,
|
||||
@@ -956,6 +958,14 @@ var InterfaceFuncs = []string{
|
||||
"hasher_u32.update!(x: roslice u8)",
|
||||
"hasher_u32.update_u32!(x: roslice u8) u32",
|
||||
|
||||
// ---- hasher_u64
|
||||
|
||||
"hasher_u64.checksum_u64() u64",
|
||||
"hasher_u64.get_quirk(key: u32) u64",
|
||||
"hasher_u64.set_quirk!(key: u32, value: u64) status",
|
||||
"hasher_u64.update!(x: roslice u8)",
|
||||
"hasher_u64.update_u64!(x: roslice u8) u64",
|
||||
|
||||
// ---- image_decoder
|
||||
|
||||
"image_decoder.decode_frame?(" +
|
||||
|
||||
@@ -6098,6 +6098,113 @@ struct wuffs_base__hasher_u32__struct {
|
||||
|
||||
// --------
|
||||
|
||||
extern const char wuffs_base__hasher_u64__vtable_name[];
|
||||
|
||||
typedef struct wuffs_base__hasher_u64__func_ptrs__struct {
|
||||
uint64_t (*checksum_u64)(
|
||||
const void* self);
|
||||
uint64_t (*get_quirk)(
|
||||
const void* self,
|
||||
uint32_t a_key);
|
||||
wuffs_base__status (*set_quirk)(
|
||||
void* self,
|
||||
uint32_t a_key,
|
||||
uint64_t a_value);
|
||||
wuffs_base__empty_struct (*update)(
|
||||
void* self,
|
||||
wuffs_base__slice_u8 a_x);
|
||||
uint64_t (*update_u64)(
|
||||
void* self,
|
||||
wuffs_base__slice_u8 a_x);
|
||||
} wuffs_base__hasher_u64__func_ptrs;
|
||||
|
||||
typedef struct wuffs_base__hasher_u64__struct wuffs_base__hasher_u64;
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_base__hasher_u64__checksum_u64(
|
||||
const wuffs_base__hasher_u64* self);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_base__hasher_u64__get_quirk(
|
||||
const wuffs_base__hasher_u64* self,
|
||||
uint32_t a_key);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
|
||||
wuffs_base__hasher_u64__set_quirk(
|
||||
wuffs_base__hasher_u64* self,
|
||||
uint32_t a_key,
|
||||
uint64_t a_value);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct
|
||||
wuffs_base__hasher_u64__update(
|
||||
wuffs_base__hasher_u64* self,
|
||||
wuffs_base__slice_u8 a_x);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_base__hasher_u64__update_u64(
|
||||
wuffs_base__hasher_u64* self,
|
||||
wuffs_base__slice_u8 a_x);
|
||||
|
||||
#if defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION)
|
||||
|
||||
struct wuffs_base__hasher_u64__struct {
|
||||
struct {
|
||||
uint32_t magic;
|
||||
uint32_t active_coroutine;
|
||||
wuffs_base__vtable first_vtable;
|
||||
} private_impl;
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if defined(WUFFS_BASE__HAVE_UNIQUE_PTR)
|
||||
using unique_ptr = std::unique_ptr<wuffs_base__hasher_u64, wuffs_unique_ptr_deleter>;
|
||||
#endif
|
||||
|
||||
inline uint64_t
|
||||
checksum_u64() const {
|
||||
return wuffs_base__hasher_u64__checksum_u64(this);
|
||||
}
|
||||
|
||||
inline uint64_t
|
||||
get_quirk(
|
||||
uint32_t a_key) const {
|
||||
return wuffs_base__hasher_u64__get_quirk(
|
||||
this, a_key);
|
||||
}
|
||||
|
||||
inline wuffs_base__status
|
||||
set_quirk(
|
||||
uint32_t a_key,
|
||||
uint64_t a_value) {
|
||||
return wuffs_base__hasher_u64__set_quirk(
|
||||
this, a_key, a_value);
|
||||
}
|
||||
|
||||
inline wuffs_base__empty_struct
|
||||
update(
|
||||
wuffs_base__slice_u8 a_x) {
|
||||
return wuffs_base__hasher_u64__update(
|
||||
this, a_x);
|
||||
}
|
||||
|
||||
inline uint64_t
|
||||
update_u64(
|
||||
wuffs_base__slice_u8 a_x) {
|
||||
return wuffs_base__hasher_u64__update_u64(
|
||||
this, a_x);
|
||||
}
|
||||
|
||||
#endif // __cplusplus
|
||||
}; // struct wuffs_base__hasher_u64__struct
|
||||
|
||||
#endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION)
|
||||
|
||||
// --------
|
||||
|
||||
extern const char wuffs_base__image_decoder__vtable_name[];
|
||||
|
||||
typedef struct wuffs_base__image_decoder__func_ptrs__struct {
|
||||
@@ -14088,6 +14195,7 @@ const char wuffs_base__error__unsupported_pixel_swizzler_option[] = "#base: unsu
|
||||
const char wuffs_base__error__too_much_data[] = "#base: too much data";
|
||||
|
||||
const char wuffs_base__hasher_u32__vtable_name[] = "{vtable}wuffs_base__hasher_u32";
|
||||
const char wuffs_base__hasher_u64__vtable_name[] = "{vtable}wuffs_base__hasher_u64";
|
||||
const char wuffs_base__image_decoder__vtable_name[] = "{vtable}wuffs_base__image_decoder";
|
||||
const char wuffs_base__io_transformer__vtable_name[] = "{vtable}wuffs_base__io_transformer";
|
||||
const char wuffs_base__token_decoder__vtable_name[] = "{vtable}wuffs_base__token_decoder";
|
||||
@@ -14248,6 +14356,153 @@ wuffs_base__hasher_u32__update_u32(
|
||||
|
||||
// --------
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_base__hasher_u64__checksum_u64(
|
||||
const wuffs_base__hasher_u64* self) {
|
||||
if (!self) {
|
||||
return 0;
|
||||
}
|
||||
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
|
||||
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const wuffs_base__vtable* v = &self->private_impl.first_vtable;
|
||||
int i;
|
||||
for (i = 0; i < 63; i++) {
|
||||
if (v->vtable_name == wuffs_base__hasher_u64__vtable_name) {
|
||||
const wuffs_base__hasher_u64__func_ptrs* func_ptrs =
|
||||
(const wuffs_base__hasher_u64__func_ptrs*)(v->function_pointers);
|
||||
return (*func_ptrs->checksum_u64)(self);
|
||||
} else if (v->vtable_name == NULL) {
|
||||
break;
|
||||
}
|
||||
v++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_base__hasher_u64__get_quirk(
|
||||
const wuffs_base__hasher_u64* self,
|
||||
uint32_t a_key) {
|
||||
if (!self) {
|
||||
return 0;
|
||||
}
|
||||
if ((self->private_impl.magic != WUFFS_BASE__MAGIC) &&
|
||||
(self->private_impl.magic != WUFFS_BASE__DISABLED)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const wuffs_base__vtable* v = &self->private_impl.first_vtable;
|
||||
int i;
|
||||
for (i = 0; i < 63; i++) {
|
||||
if (v->vtable_name == wuffs_base__hasher_u64__vtable_name) {
|
||||
const wuffs_base__hasher_u64__func_ptrs* func_ptrs =
|
||||
(const wuffs_base__hasher_u64__func_ptrs*)(v->function_pointers);
|
||||
return (*func_ptrs->get_quirk)(self, a_key);
|
||||
} else if (v->vtable_name == NULL) {
|
||||
break;
|
||||
}
|
||||
v++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
|
||||
wuffs_base__hasher_u64__set_quirk(
|
||||
wuffs_base__hasher_u64* self,
|
||||
uint32_t a_key,
|
||||
uint64_t a_value) {
|
||||
if (!self) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
|
||||
}
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return wuffs_base__make_status(
|
||||
(self->private_impl.magic == WUFFS_BASE__DISABLED)
|
||||
? wuffs_base__error__disabled_by_previous_error
|
||||
: wuffs_base__error__initialize_not_called);
|
||||
}
|
||||
|
||||
const wuffs_base__vtable* v = &self->private_impl.first_vtable;
|
||||
int i;
|
||||
for (i = 0; i < 63; i++) {
|
||||
if (v->vtable_name == wuffs_base__hasher_u64__vtable_name) {
|
||||
const wuffs_base__hasher_u64__func_ptrs* func_ptrs =
|
||||
(const wuffs_base__hasher_u64__func_ptrs*)(v->function_pointers);
|
||||
return (*func_ptrs->set_quirk)(self, a_key, a_value);
|
||||
} else if (v->vtable_name == NULL) {
|
||||
break;
|
||||
}
|
||||
v++;
|
||||
}
|
||||
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_vtable);
|
||||
}
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct
|
||||
wuffs_base__hasher_u64__update(
|
||||
wuffs_base__hasher_u64* self,
|
||||
wuffs_base__slice_u8 a_x) {
|
||||
if (!self) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
const wuffs_base__vtable* v = &self->private_impl.first_vtable;
|
||||
int i;
|
||||
for (i = 0; i < 63; i++) {
|
||||
if (v->vtable_name == wuffs_base__hasher_u64__vtable_name) {
|
||||
const wuffs_base__hasher_u64__func_ptrs* func_ptrs =
|
||||
(const wuffs_base__hasher_u64__func_ptrs*)(v->function_pointers);
|
||||
return (*func_ptrs->update)(self, a_x);
|
||||
} else if (v->vtable_name == NULL) {
|
||||
break;
|
||||
}
|
||||
v++;
|
||||
}
|
||||
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC uint64_t
|
||||
wuffs_base__hasher_u64__update_u64(
|
||||
wuffs_base__hasher_u64* self,
|
||||
wuffs_base__slice_u8 a_x) {
|
||||
if (!self) {
|
||||
return 0;
|
||||
}
|
||||
if (self->private_impl.magic != WUFFS_BASE__MAGIC) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const wuffs_base__vtable* v = &self->private_impl.first_vtable;
|
||||
int i;
|
||||
for (i = 0; i < 63; i++) {
|
||||
if (v->vtable_name == wuffs_base__hasher_u64__vtable_name) {
|
||||
const wuffs_base__hasher_u64__func_ptrs* func_ptrs =
|
||||
(const wuffs_base__hasher_u64__func_ptrs*)(v->function_pointers);
|
||||
return (*func_ptrs->update_u64)(self, a_x);
|
||||
} else if (v->vtable_name == NULL) {
|
||||
break;
|
||||
}
|
||||
v++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --------
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
WUFFS_BASE__MAYBE_STATIC wuffs_base__status
|
||||
wuffs_base__image_decoder__decode_frame(
|
||||
|
||||
Reference in new issue
Block a user