-
Notifications
You must be signed in to change notification settings - Fork 26
Add the hash datatype #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
u9g
wants to merge
4
commits into
master
Choose a base branch
from
hash-type
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule ProtoDef
updated
4 files
| +1 −0 | doc/datatypes.md | |
| +22 −0 | doc/datatypes/utils.md | |
| +2 −1 | schemas/datatype.json | |
| +26 −0 | schemas/utils.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| const { getFieldInfo } = require('../utils') | ||
|
|
||
| // CRC-32C (Castagnoli): reflected table-driven, all-ones init and final xor. | ||
| // The compiler copies this function into the code it generates, so it has to | ||
| // stand on its own: no imports, no module scope, its table cached on itself. | ||
| function crc32c (buffer) { | ||
| let table = crc32c.table | ||
| if (!table) { | ||
| table = crc32c.table = new Int32Array(256) | ||
| for (let n = 0; n < 256; n++) { | ||
| let c = n | ||
| for (let k = 0; k < 8; k++) c = c & 1 ? 0x82F63B78 ^ (c >>> 1) : c >>> 1 | ||
| table[n] = c | ||
| } | ||
| } | ||
| let c = -1 | ||
| for (let i = 0; i < buffer.length; i++) c = table[(c ^ buffer[i]) & 0xff] ^ (c >>> 8) | ||
| return (c ^ -1) >>> 0 | ||
| } | ||
|
|
||
| // `alg` is an explicit list in the spec, so a protocol means the same thing in | ||
| // every implementation; adding an algorithm here is a spec change. The width of | ||
| // the digest is what lets a hash be sized without hashing. | ||
| const algorithms = { | ||
| crc32c: { bytes: 4, digest: crc32c } | ||
| } | ||
|
|
||
| function digest (alg, buffer) { | ||
| const algorithm = algorithms[alg] | ||
| if (!algorithm) throw new Error('Unknown hash algorithm: ' + alg) | ||
| return algorithm.digest(buffer) | ||
| } | ||
|
|
||
| function readHash (buffer, offset, { type }, rootNode) { | ||
| return this.read(buffer, offset, type, rootNode) | ||
| } | ||
|
|
||
| // A CRC is unsigned; a signed `type` takes its two's complement. | ||
| function writeHash (value, buffer, offset, { alg, type, body }, rootNode) { | ||
| if (typeof body !== 'string') throw new Error('hash body must be a named type, ' + JSON.stringify(body) + ' is not') | ||
| const bodyBuffer = Buffer.alloc(this.sizeOf(value, body, rootNode)) | ||
| this.write(value, bodyBuffer, 0, body, rootNode) | ||
| const hash = digest(alg, bodyBuffer) | ||
| try { | ||
| return this.write(hash, buffer, offset, type, rootNode) | ||
| } catch (e) { | ||
| if (!(e instanceof RangeError)) throw e | ||
| return this.write(hash | 0, buffer, offset, type, rootNode) | ||
| } | ||
| } | ||
|
|
||
| // The digest has a fixed width, so the size of a hash never depends on the | ||
| // value: `type` is required to be of constant size and is looked up as one. | ||
| function sizeOfHash (value, { alg, type }, rootNode) { | ||
| const functions = this.types[getFieldInfo(type).type] | ||
| const size = functions ? functions[2] : undefined | ||
| if (typeof size !== 'number') throw new Error('hash type must be of constant size, ' + JSON.stringify(type) + ' is not') | ||
| if (size < algorithms[alg].bytes) throw new Error('hash type is too small for a ' + alg + ' digest') | ||
| return size | ||
| } | ||
|
|
||
| module.exports = { | ||
| digest, | ||
| algorithms, | ||
| hash: [readHash, writeHash, sizeOfHash, require('../../ProtoDef/schemas/utils.json').hash] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Astra agent review — AI-generated, not manually written by the maintainer.
Checking only the byte width allows
f32/lf32, which cannot preserve a 32-bit CRC. WithBody: ['buffer', { count: 9 }]and['hash', { alg: 'crc32c', type: 'f32', body: 'Body' }], serializingBuffer.from('123456789')succeeds in both the interpreter and compiler, but reading the digest returns0xe3069300instead of0xe3069283. Please validate that the output representation can carry the complete digest, consistently in both paths, and add a regression that rejects lossy output types. Defining the supported integer representations and alias handling alongside the spec would also avoid accepting other fixed-size types whose writers cannot consume the numeric digest.