완전 야메로 짰음.
-- 기능
바이너리 파일의 맨 앞에 정해놓은 매직코드와 CRC16 계산 값을 넣어준다. 주로 펌웨어 업그레이드 등등에서 씀.
빌드시 포스트 빌드에 넣으면 땡.
#include "stdafx.h"
#define MAGIC_ID 0x04190703
static unsigned short slow_crc16(unsigned short sum, unsigned char p);
int main(int argc, char* argv[])
{
FILE *fin, *fout;
unsigned char buf;
unsigned short csum = 0;
unsigned int lcsum = 0;
unsigned int mcode = MAGIC_ID;
if (argc != 3) {
printf("usage : makecrc in out\n");
return 0;
}
fin = fopen(argv[1], "rb");
if (fin == NULL) {
printf("input file not found!\n");
return 0;
}
while ((buf = fread((void *)&buf, 1, 1, fin)) != 0) {
csum = slow_crc16(csum, (unsigned char)buf);
}
fclose(fin);
fin = NULL;
lcsum = 0xffff0000 | csum;
fin = fopen(argv[1], "rb");
if (fin == NULL) {
printf("input file not found!\n");
return 0;
}
fout = fopen(argv[2], "wb");
if (fout == NULL) {
printf("output file error!\n");
return 0;
}
fwrite((void *)&mcode, 4, 1, fout);
fwrite((void *)&lcsum, 4, 1, fout);
while (fread((void *)&buf, 1, 1, fin) != 0) {
fwrite((void *)&buf, 1, 1, fout);
}
fclose(fin);
fclose(fout);
printf("crc16 = %x", lcsum);
return 0;
}
unsigned short slow_crc16(unsigned short sum, unsigned char p)
{
int i;
for (i = 0; i < 8; ++i)
{
unsigned long oSum = sum;
sum <<= 1;
if (p & 0x80)
sum |= 1;
if (oSum & 0x8000)
sum ^= 0x1021;
p <<= 1;
}
return sum;
}