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)
32 changes: 32 additions & 0 deletions doc/datatypes/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,35 @@ 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, currently only `crc32c`
* 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.

`alg` is an explicit list rather than whatever hashes the implementation's platform happens to offer, so that a protocol using `hash` means the same thing in every implementation:

| alg | digest |
| --- | --- |
| `crc32c` | a 4 byte unsigned integer (CRC-32C, the Castagnoli polynomial, reflected, all-ones init and final xor) |

Because the digest's width is fixed by `alg`, the size of a `hash` field is known without hashing anything. `type` must therefore be a type of constant size, at least as wide as the digest; a variable-length `type` such as `varint` is an error.

A CRC is unsigned. 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.

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": {
"enum": ["crc32c"]
},
"type": {
"$ref": "dataType"
},
"body": {
"$ref": "dataType"
}
},
"required": ["alg", "type", "body"],
"additionalProperties": false
}
],
"additionalItems": false
}
}