📄 vcrc.c
字号:
#include "Vcrc.h"
static ulong Crc32Table[256];
int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,unsigned char *outbuf,int outlen)
{
iconv_t cd;
int rc;
char **pin=&inbuf;
char **pout=&outbuf;
cd=iconv_open(to_charset,from_charset);
if(cd==0) return OPEN_CHARSET_FAIL;
memset(outbuf,0,outlen);
if(iconv(cd,pin,&inlen,pout,&outlen)==-1)
return CONVERT_CHARSET_FAIL;
iconv_close(cd);
return CONVERT_CHARSET_SUCCESS;
}
void ValToStr(const int value,char Buffer[])
{
int count ;
int i,j;
char temp[20];
char zero;
int curvalue;
curvalue = value;
zero = '0';
count = 0;
while ( curvalue > 0 )
{
i = curvalue % 10 ;
curvalue = ( curvalue - i )/10;
temp[count++] = zero + i;
}
if(count<12){
for(j=0;j<12-count;j++)
Buffer[j]='0';
}
for ( i = 0; i < count ; i ++,j++)
{
Buffer[j] = temp[count - i -1];
}
return;
}
void GetCRC32Table()
{
ulong Crc;
int i,j;
for(i=0;i<256;i++)
{
Crc=(ulong)i;
for(j=8;j>0;j--)
{
if((Crc&1)==1)
Crc=(Crc>>1)^0xEDB88320;
else
Crc>>=1;
}
Crc32Table[i]=Crc;
}
}
ulong _GetCRC32Str( char* sInputString)
{
int i;
int inlen;
int outlen;
int rc;
memset(Crc32Table,0,256);
GetCRC32Table();
inlen=strlen(sInputString);
printf("the firest line\n");
unsigned char out_buffer[inlen*2];
unsigned char buffer[inlen*2];
rc=code_convert("gb2312","utf-8",sInputString,inlen,out_buffer,inlen*2);
outlen=strlen(out_buffer);
printf("the second line\n");
ulong value = 0xffffffff;
for (i = 0; i <outlen; i++)
{
value = (value >> 8) ^ Crc32Table[(value & 0xFF)^out_buffer[i]];
}
printf("%x\n",value^ 0xffffffff);
return value ^ 0xffffffff;
}
ulong _GetCRC32Buf(unsigned char buffer[],ulong len)
{
ulong i;
memset(Crc32Table,0,256);
GetCRC32Table();
ulong value=0xffffffff;
for(i=0;i<len;i++)
{
value=(value>>8)^Crc32Table[(value&0xFF)^buffer[i]];
}
return value^0xffffffff;
}
char* GetCRC32_Buf(unsigned char Buf[],ulong lenth,char *s)
{
ulong uval;
uval=_GetCRC32Buf(Buf,lenth);
sprintf(s,"%08X",uval);
printf("%s\n",s);
return s;
}
char *GetCRC32_Str(char* sInputStr,char *s)
{
ulong uval;
s=malloc(20);
uval=_GetCRC32Str(sInputStr);
sprintf(s,"%08X",uval);
printf("%s\n",s);
return s;
}
char *FileCrc32(char *filename,char *crc32)
{
FILE *fp;
char ch;
ulong count,len;
ulong TotalLenth,StepLenth,JumpLenth,BufLenth,StepNum,StepPtr,StrModNum;
crc32=(char*)malloc(10);
fp=fopen(filename,"r");
fseek(fp,0,SEEK_END);
if(fp==NULL)
perror("file open!");
len= ftell(fp);
fseek(fp,0,SEEK_SET);
count=0;
printf("%d\n",len);
if(len<1024*1024*2)
{
unsigned char buf[len+1];
ch=fgetc(fp);
while(count<len)
{
buf[count]=ch;
ch=fgetc(fp);
count++;
}
crc32=GetCRC32_Buf(buf,len,crc32);
}
else
{
StepNum=len/(1024*32);
StrModNum=len%(1024*32);
TotalLenth=StepNum*128+StrModNum+12;
unsigned char buf[TotalLenth+1];
printf("TotalLenth=%d\n",TotalLenth);
ValToStr(len,buf);
BufLenth=12;
StepPtr=0;
while(count<len)
{
StepLenth=0;
JumpLenth=0;
while(StepLenth<128)
{
ch=fgetc(fp);
buf[BufLenth]=ch;
BufLenth++;
StepLenth++;
count++;
}
while(JumpLenth<(1024*32-128))
{
ch=fgetc(fp);
JumpLenth++;
count++;
}
StepPtr++;
if((StepPtr==StepNum)&&(StrModNum>0))
{
while(count<len)
{
ch=fgetc(fp);
buf[BufLenth]=ch;
BufLenth++;
count++;
}
}
}
crc32=GetCRC32_Buf(buf,BufLenth,crc32);
}
fclose(fp);
printf("crc32=%s\n",crc32);
return crc32;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -