파일 이나 데 이 터 를 CRC 검사 합 니 다.
6869 단어 커 뮤 니 케 이 션
쓸데없는 말 은 그만 하고 코드 를 바로 달 아 라!
crc32.h
#ifndef CRC_32_H #define CRC_32_H #ifdef __cplusplus extern "C" { #endif void init_crc_table(void); unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size); int calc_img_crc(const char *in_file, unsigned int *img_crc); #ifdef __cplusplus } #endif #endif crc32.c
#include /***************************************************** ** Name : crc32.c ** Author : ** Version : 1.0 ** Date : ** Description : CRC32 Checking ******************************************************/ #include #include #include #include #include #include #include #define BUFSIZE 1024*4 static unsigned int crc_table[256]; const static char * program_name = "crc32"; /* ** crc , 32 crc */ void init_crc_table(void) { unsigned int c; unsigned int i, j; for (i = 0; i < 256; i++) { c = (unsigned int)i; for (j = 0; j < 8; j++) { if (c & 1) c = 0xedb88320L ^ (c >> 1); else c = c >> 1; } crc_table[i] = c; } } /* buffer crc */ unsigned int crc32(unsigned int crc,unsigned char *buffer, unsigned int size) { unsigned int i; for (i = 0; i < size; i++) { crc = crc_table[(crc ^ buffer[i]) & 0xff] ^ (crc >> 8); } return crc ; } /* ** CRC :crc32 , buffer , ** , ** crc , ** crc buffer , ** , crc crc . */ int calc_img_crc(const char *in_file, unsigned int *img_crc) { int fd; int nread; int ret; unsigned char buf[BUFSIZE]; /* , crc , ** */ unsigned int crc = 0xffffffff; fd = open(in_file, O_RDONLY); if (fd < 0) { printf("%d:open %s.
", __LINE__, strerror(errno)); return -1; } while ((nread = read(fd, buf, BUFSIZE)) > 0) { crc = crc32(crc, buf, nread); } *img_crc = crc; close(fd); if (nread < 0) { printf("%d:read %s.
", __LINE__, strerror(errno)); return -1; } return 0; } /* int main(int argc, char **argv) { int ret; unsigned int img_crc; const char *in_file = argv[1]; if (argc < 2) { exit(1); } init_crc_table(); ret = calc_img_crc(in_file, &img_crc); if (ret < 0) { exit(1); } printf("The crc of %s is:%u
", in_file, img_crc); return 0; } */ 파일 검사:
init_crc_table(); unsigned int bin_crc; calc_img_crc(argv[1],&bin_crc); buf 의 데이터 검사:
init_crc_table(); unsigned int binCrcNew = 0xFFFFFFFF; binCrcNew = crc32(binCrcNew, (unsigned char*)fwBuff, binLen); 가장 간단 한 Makefile 시험 예:
all: encryptBIN encryptBIN:encryptBIN.cpp crc32.c gcc -c crc32.c -o crc32.o g++ -c encryptBIN.cpp -o encryptBIN.o gcc crc32.o encryptBIN.o -lstdc++ -o encryptBIN clean: rm -rf *.o encryptBIN 이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
heartbeat nginx 고 사용 가능 한 클 러 스 터・ 고가 용 원리: 두 대의 기계 A 와 B 는 정상적으로 A 가 서 비 스 를 제공 하고 B 가 방치 되 어 있다. · 하트 비트 원리: 심장 박동 검사 와 자원 연결 두 부분.클 러 스 터 의 호스트 는 서로 메...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.