Skip to content

Commit 8762e32

Browse files
committed
try to fix serialization
there's padding issues, idk how portable this is... The CI's should be interesting.
1 parent 896351a commit 8762e32

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

ext/hash/php_hash_blake3.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,26 @@
2424

2525
// typedef struct blake3_hasher PHP_BLAKE3_CTX;
2626
#define PHP_BLAKE3_CTX blake3_hasher
27-
// help: is V correct?
28-
//#define PHP_BLAKE3_SPEC "b8b8qb64bbbbb1760"
29-
#define PHP_BLAKE3_SPEC "L8L8Qa64CCCCL8Ca1760"
27+
// When testing, the compiler insert 5 alignment-padding bytes after buf_len, and 7 trailing alignment bytes at the end of the struct.
28+
// Unsure if it is portable to rely on this exact padding behavior..
29+
// If the CIs pass, I'll assume it is portable.
30+
#define PHP_BLAKE3_SPEC /* uint32_t key[8]; */"l8" \
31+
/* uint32_t cv[8]; */ "l8" \
32+
/* uint64_t chunk_counter; */ "q" \
33+
/* uint8_t buf[BLAKE3_BLOCK_LEN]; */ "b64" \
34+
/* uint8_t buf_len; */ "b" \
35+
/* skip 5 bytes of alignment padding in chunk */ "B5" \
36+
/* uint8_t blocks_compressed */ "b" \
37+
/* uint8_t flags; */ "b" \
38+
/* uint8_t cv_stack_len; */ "b" \
39+
/* uint8_t cv_stack[(BLAKE3_MAX_DEPTH + 1) * BLAKE3_OUT_LEN]; */ "b1760" \
40+
/* skip 7 trailing alignment bytes */ "B7" \
41+
"."
42+
43+
44+
45+
46+
3047

3148
PHP_HASH_API void PHP_BLAKE3Init(PHP_BLAKE3_CTX *context, HashTable *args);
3249
PHP_HASH_API void PHP_BLAKE3Update(PHP_BLAKE3_CTX *context, const unsigned char *input, size_t len);

ext/hash/tests/hash_serialize_001.phpt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ function not_serializable(string $algo)
4444
return in_array($algo, [
4545
"xxh3",
4646
"xxh128",
47-
"blake3", // todo: blake3 can be seralized but it's not implemented yet
4847
], true);
4948
}
5049

@@ -180,6 +179,9 @@ string(8) "eee74423"
180179
string(5) "xxh64"
181180
string(16) "9d6ab4708056a619"
182181
string(16) "9d6ab4708056a619"
182+
string(6) "blake3"
183+
string(64) "e232493e3d416e24ffc588b24a1772ccc6f80290f1920cf15f21313bc3543a51"
184+
string(64) "e232493e3d416e24ffc588b24a1772ccc6f80290f1920cf15f21313bc3543a51"
183185
string(10) "haval128,3"
184186
string(32) "86362472c8895e68e223ef8b3711d8d9"
185187
string(32) "86362472c8895e68e223ef8b3711d8d9"
@@ -354,6 +356,9 @@ string(8) "3b7a100b"
354356
string(5) "xxh64"
355357
string(16) "5a90002ef76d172f"
356358
string(16) "5a90002ef76d172f"
359+
string(6) "blake3"
360+
string(64) "dbdea45e5a6c3bad18a4f96d9d4b9105e4cceaa4fc06568f69829435c47587fb"
361+
string(64) "dbdea45e5a6c3bad18a4f96d9d4b9105e4cceaa4fc06568f69829435c47587fb"
357362
string(10) "haval128,3"
358363
string(32) "ebeeeb05c18af1e53d2d127b561d5e0d"
359364
string(32) "ebeeeb05c18af1e53d2d127b561d5e0d"

0 commit comments

Comments
 (0)