google / emboss

Emboss is a tool for generating code that reads and writes binary data structures.
Apache License 2.0
70 stars 21 forks source link

CRC/Checksum Support #31

Open reventlov opened 2 years ago

reventlov commented 2 years ago

CRCs and checksums are common enough that there should be built-in support for verifying and setting CRCs/checksums to the correct value. One possible implementation would be $crc(range : UInt:8[]) and $checksum(range : UInt:8[]) functions which can be used in [requires].

AaronWebster commented 2 years ago

Would be useful if the type could be specified, e.g. $checksum(type : CRC32C, range : UInt:8[]). Maybe more useful if Emboss had a way of supporting a type whose value was computed using a user-defined function.

reventlov commented 2 years ago

I was thinking $crc() and $checksum() specifically so that you don't have to specify which algorithm as a parameter. User-defined functions would be nice, but a much bigger feature to design.

$crc() should have an optional parameter for the polynomial, though. Probably default to the IEEE polynomial, at least for CRC-32.

We also need some way to specify how many bits are in the CRC/checksum.

AaronWebster commented 2 years ago

pycrc might be a good fit using its c code generator. Supports arbitrary polynomials/bits/endianness/etc.

reventlov commented 2 years ago

Something like CRC_t is probably better: a C++ template takes advantage of the C++ compiler to avoid having multiple copies of the same code or extra unneeded code in the final binary, even across translation units.

We would need to import a copy and shove it into a different namespace to avoid potential version skew between the copy in Emboss and a copy that happens to be used in application code.

Also, it would be good to add some options to the C++ backend or site_defines.h to allow applications to pick their own implementation. I assume a lot of embedded projects will already have a CRC library somewhere; we should use theirs when possible instead of adding binary image size.