X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=metacube2.cpp;fp=metacube2.cpp;h=f7e4ce20c9468c572b97390a8a320008474fda39;hp=0000000000000000000000000000000000000000;hb=979a284b4039b0ea74525b700b9f1089b8c4248d;hpb=70c0baf4bcec3a77f0626d5a7bfde87fc7339698 diff --git a/metacube2.cpp b/metacube2.cpp new file mode 100644 index 0000000..f7e4ce2 --- /dev/null +++ b/metacube2.cpp @@ -0,0 +1,50 @@ +/* + * Implementation of Metacube2 utility functions. + * + * Note: This file is meant to compile as both C and C++, for easier inclusion + * in other projects. + */ + +#include + +#include "metacube2.h" + +/* + * https://www.ece.cmu.edu/~koopman/pubs/KoopmanCRCWebinar9May2012.pdf + * recommends this for messages as short as ours (see table at page 34). + */ +#define METACUBE2_CRC_POLYNOMIAL 0x8FDB + +/* Semi-random starting value to make sure all-zero won't pass. */ +#define METACUBE2_CRC_START 0x1234 + +/* This code is based on code generated by pycrc. */ +uint16_t metacube2_compute_crc(const struct metacube2_block_header *hdr) +{ + static const int data_len = sizeof(hdr->size) + sizeof(hdr->flags); + const uint8_t *data = (uint8_t *)&hdr->size; + uint16_t crc = METACUBE2_CRC_START; + int i, j; + + for (i = 0; i < data_len; ++i) { + uint8_t c = data[i]; + for (j = 0; j < 8; j++) { + int bit = crc & 0x8000; + crc = (crc << 1) | ((c >> (7 - j)) & 0x01); + if (bit) { + crc ^= METACUBE2_CRC_POLYNOMIAL; + } + } + } + + /* Finalize. */ + for (i = 0; i < 16; i++) { + int bit = crc & 0x8000; + crc = crc << 1; + if (bit) { + crc ^= METACUBE2_CRC_POLYNOMIAL; + } + } + + return crc; +}