混乱中下与CRC16文件验证 [英] Confusion in file Verification with CRC16 in C
问题描述
在这里,我想知道我怎么可以实现计算CRC16所有类型的文件,
在这里,我对CRC16和code逻辑的想法。
在这里,我做了一个功能,它需要的文件路径作为输入,找出这个CRC值。在这里,我在这个函数传递文件的名称和该函数计算该文件的CRC值。但我要计算所有类型的文件如的.tar,.tar.gz格式,txt文件,.bin和.SCR等
。
所以我在这里开这个的所有文件和RB
模式,采取逐个字符并找出CRC值
它的正确方法?可能是我缺少这个东西。其工作罚款 .TXT
和所有其他文件,但它会创建的.tar,.tar.gz格式
键入问题文件。因为在这里我有一个文件,该文件是 890 MB
键,它的名字叫 file.tar.gz
,并对其采取 203
微秒,我还有其他的文件,该文件是 382 MB
键,它的名字叫 file2.tar
对其采取 6097850.000000微秒
其令人难以置信的对我怎么样了其可能的?
我的问题是这些
1某些事情的问题在我的CRC code?问题2读取文件数据的风格,可能是我读文件数据错误的方式为.tar.gz格式。
下面是我的code:
INT CRC16(为const char *文件路径){
//声明变量来存储CRC结果。
无符号短结果;
//声明循环变量。
INT intOuterLoopIndex,intInnerLoopIndex,nLen;
结果= 0xFFFF的; //初始化结果变量来执行CRC校验和计算。 //从文件中读取存储信息。
//炭含量[2000000] //创建文件指针打开和读取文件。
FILE * READFILE; //使用从文件中读取字符。
焦炭readChar; //打开读取文件
READFILE = FOPEN(文件路径,RB); //检查文件能够打开或者存在。
如果(!ReadFile的){
的fputs(无法打开文件%s,标准错误);
} / *
在这里阅读文件并存储到变量。
* /
INT chCnt = 0;
而((readChar = GETC(READFILE))!= EOF){
结果^ =(短)(readChar);
为(intInnerLoopIndex = 0; intInnerLoopIndex&下; 8; intInnerLoopIndex ++){
如果((结果&安培; 0×0001)== 0×0001){
结果=结果>> 1; //执行位转移。
结果=结果^ 0xA001异或; //对执行结果的XOR运算。
}其他{
结果=结果>> 1; //执行位转移。
}
} //内容[chCnt] = readChar;
chCnt ++;
}
的printf(文件CRC数据长度:%D,chCnt); 返回(结果);
}
的char readChar;
是错误的,它需要 INT readChar
;
GETC ()返回一个int,所以它可以发出信号EOF给你。 EOF可能值-1
如果您的返回值转换为char,并读取值255字节,255将PTED为-1间$ P $,你停在一个值为255的第一个字节读取。
Here i want to know the how can i implement calculate CRC16 for Any type of file,
Here i have idea about CRC16 and its code logic.
here i made one function which take file path as a input and find out CRC value of this. here i pass file name in this function and this functions calculates CRC value of this file. but i want to calculate all types of files like .tar,.tar.gz,.txt,.bin,.scr etc
.
so here i open this all files and "rb"
mode and take one by one character and find out CRC value
its right way? may be i missing something in this. its works fine for .txt
and all other files but it will create problems in .tar, .tar.gz
type files. Because here i have one file which is 890 MB
and its name is file.tar.gz
and its take 203
Microseconds and I have other file which is 382 MB
and its name is file2.tar
its take 6097850.000000 Microseconds
its unbelievable for me hows its possible?
My question are these
1 Some thing problem in my CRC code ?
2 Problem in reading file data style, may be i am reading file data in wrong manner for .tar.gz.
Here is my code :
int CRC16(const char* filePath) {
//Declare variable to store CRC result.
unsigned short result;
//Declare loop variables.
int intOuterLoopIndex, intInnerLoopIndex, nLen;
result = 0xffff; //initialize result variable to perform CRC checksum calculation.
//Store message which read from file.
//char content[2000000];
//Create file pointer to open and read file.
FILE *readFile;
//Use to read character from file.
char readChar;
//open a file for Reading
readFile = fopen(filePath, "rb");
//Checking file is able to open or exists.
if (!readFile) {
fputs("Unable to open file %s", stderr);
}
/*
Here reading file and store into variable.
*/
int chCnt = 0;
while ((readChar = getc(readFile)) != EOF) {
result ^= (short) (readChar);
for (intInnerLoopIndex = 0; intInnerLoopIndex < 8; intInnerLoopIndex++) {
if ((result & 0x0001) == 0x0001) {
result = result >> 1; //Perform bit shifting.
result = result ^ 0xa001; //Perform XOR operation on result.
} else {
result = result >> 1; //Perform bit shifting.
}
}
//content[chCnt] = readChar;
chCnt++;
}
printf("CRC data length in file: %d", chCnt);
return (result);
}
char readChar;
is wrong, it needs to be int readChar
;
getc() returns an int, so it can signal EOF to you. EOF is likely the value -1
If you convert the return value to a char, and read a byte with the value 255, 255 will be interpreted as -1, and you stop reading at the first byte that has the value 255.
这篇关于混乱中下与CRC16文件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!