Here is the source for a win32 .exe which appends the CRC32 (the inverted JAMCRC) to a file:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
/*
*
* CRC-32/JAMCRC. This is a "rolling" algorithm. Invert the *final* result for ISO-HDLC.
* Returns 0x098494f3 from "123456789" (9 bytes). Inverting this (at the end) gives 0xcbf43926.
* Polynomial is 0x04c11db7 and it holds it backwards (0xEDB88320).
* This version accepts one byte at a time, and maintains the CRC in crcvalue. This makes it suitable
* for calculating a CRC across a number of data blocks.
* Speed is approx 400kbytes/sec.
* crcvalue must be initialised to 0xffffffff by caller, and holds the accumulated CRC as you go along
*
*/
void crc32(uint8_t input_byte, uint32_t *crcvalue)
{
for (uint32_t j = 0; j < 8; j++)
{
uint32_t mask = (input_byte^*crcvalue) & 1;
*crcvalue >>= 1;
if (mask)
*crcvalue = *crcvalue ^ 0xEDB88320;
input_byte >>= 1;
}
}
int main(int argc, char* argv[])
{
uint32_t crcvalue = 0xffffffff;
FILE* file = NULL;
if (argc > 1)
{
if (argc > 2)
{
int c = 0;
if (!_strnicmp(argv[2], "0x", 2))
c = 2;
crcvalue = strtol(&argv[2][c], NULL, 16);
}
file = fopen(argv[1], "rb");
if (file)
{
while (!feof(file))
{
uint8_t input_byte;
if (fread(&input_byte, sizeof(input_byte), 1, file))
{
crc32(input_byte, &crcvalue);
}
//else
//{
// printf("Before last zero byte = %08X\r\n", crcvalue);
// crc32(0, &crcvalue);
// printf("After last zero byte = %08X\r\n", crcvalue);
//}
}
fclose(file);
crcvalue = ~crcvalue;
file = fopen(argv[1], "ab");
fwrite(&crcvalue, sizeof(uint32_t), 1, file);
fclose(file);
}
else
{
printf("Cannot open file\r\n");
}
file = NULL;
}
else
{
printf("Missing file name\r\n");
}
return 0;
}
An optional 2nd parameter is a different CRC initialisation value (default is 0xffffffff).
Just curious: someone said that anything running out of Cube IDE should be written in Java.