diff --git a/doc/datatypes.md b/doc/datatypes.md index 578ce05..73d3ddc 100644 --- a/doc/datatypes.md +++ b/doc/datatypes.md @@ -18,3 +18,4 @@ * * [bitfield](./datatypes/utils.md) * * [mapper](./datatypes/utils.md) * * [pstring](./datatypes/utils.md) +* * [hash](./datatypes/utils.md) diff --git a/doc/datatypes/utils.md b/doc/datatypes/utils.md index c64a3aa..787a8c8 100644 --- a/doc/datatypes/utils.md +++ b/doc/datatypes/utils.md @@ -124,3 +124,25 @@ Example: A string length prefixed by a varint. ] ``` Example of value: `"my string"` + +### **hash** ({ alg: String, type: Type, body: Type }) +Arguments: +* alg : the hash algorithm, one of : `crc32c` (CRC-32C, the Castagnoli polynomial, reflected, all-ones init and final xor, a 4 byte digest) +* type : the type the hash is written and read as, of constant size and at least as wide as the digest +* body : the name of the type the value is serialized as before being hashed + +Represents a hash of a value instead of the value itself : writing serializes the value as `body`, hashes those bytes and writes the digest as `type`; reading reads `type` and yields the digest. When `type` is signed the digest is written in two's complement. + +Example: A CRC32C of an item component, written as a signed int. +```json +[ + "hash", + { + "alg": "crc32c", + "type": "i32", + "body": "ItemComponent" + } +] +``` + +Example of value: `{ "id": 5, "level": 3 }`, serializing to `05 03` / reads as `-1088848499` diff --git a/schemas/datatype.json b/schemas/datatype.json index 66b37d8..94a1431 100644 --- a/schemas/datatype.json +++ b/schemas/datatype.json @@ -45,6 +45,7 @@ { "$ref": "buffer" }, { "$ref": "bitfield" }, { "$ref": "bitflags" }, - { "$ref": "mapper" } + { "$ref": "mapper" }, + { "$ref": "hash" } ] } diff --git a/schemas/utils.json b/schemas/utils.json index c9d48b5..5b70022 100644 --- a/schemas/utils.json +++ b/schemas/utils.json @@ -144,5 +144,31 @@ } ], "additionalItems": false + }, + "hash": { + "title": "hash", + "type": "array", + "items": [ + { + "enum": ["hash"] + }, + { + "type": "object", + "properties": { + "alg": { + "enum": ["crc32c"] + }, + "type": { + "$ref": "dataType" + }, + "body": { + "type": "string" + } + }, + "required": ["alg", "type", "body"], + "additionalProperties": false + } + ], + "additionalItems": false } }