Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/datatypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
* * [bitfield](./datatypes/utils.md)
* * [mapper](./datatypes/utils.md)
* * [pstring](./datatypes/utils.md)
* * [hash](./datatypes/utils.md)
24 changes: 24 additions & 0 deletions doc/datatypes/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,27 @@ 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: `crc32`, `crc32c`, or any digest the platform's crypto library provides (`md5`, `sha1`, `sha256`, ...)
* type : the type the hash is written and read as
* body : 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 the bytes and writes the hash as `type`; reading reads `type` and yields the hash. The value is not recoverable from the buffer, so the read side sees only the hash.

CRC hashes are unsigned integers. When `type` is signed (for example `i32`) the hash is written in two's complement, so it reads back the way a language with only signed integers would produce it. Other algorithms produce raw digest bytes, so `type` must then be a `buffer` of the digest's length.

Example: a CRC32C of an item component, written as a signed int like Minecraft's `HashedSlot` carries it.
```json
[
"hash",
{
"alg": "crc32c",
"type": "i32",
"body": "ItemComponent"
}
]
```

Example of value: `{ "id": 5, "level": 3 }` (whatever `body` accepts) / reads as `-486237565`
3 changes: 2 additions & 1 deletion schemas/datatype.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
{ "$ref": "buffer" },
{ "$ref": "bitfield" },
{ "$ref": "bitflags" },
{ "$ref": "mapper" }
{ "$ref": "mapper" },
{ "$ref": "hash" }
]
}
26 changes: 26 additions & 0 deletions schemas/utils.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,31 @@
}
],
"additionalItems": false
},
"hash": {
"title": "hash",
"type": "array",
"items": [
{
"enum": ["hash"]
},
{
"type": "object",
"properties": {
"alg": {
"type": "string"
},
"type": {
"$ref": "dataType"
},
"body": {
"$ref": "dataType"
}
},
"required": ["alg", "type", "body"],
"additionalProperties": false
}
],
"additionalItems": false
}
}