- 论坛徽章:
- 0
|
发个在AS/400上可以跑的算CRC32的C程序
- /*
- CRC32F.c -- Calculate CRC32 for a file
- M.L.Y 2005.5
-
- MODIFIED (YYYY.MM.DD)
- M.L.Y 2005.05. - Creation
- */
-
- #ifdef __OS400__ /* AS/400 */
- #include "CRC32FH"
- #else
- # include "CRC32F.h"
- #endif
-
- #include <stdlib.h>
-
- USGL CRC32Table[256] = {0, 0};
-
- #define CRC32_POLYNOMIAL 0xEDB88320UL
-
- /* ------------------------------------------------------------------------- */
- void BuildCRC32Table(USGL aPoly)
- /*
- 构造 32 位 CRC 表
- */
- {
- int i;
- int j;
- USGL crc;
-
- for(i = 0; i <= 255 ; i++)
- {
- crc = i;
- for(j = 8 ; j > 0; j--)
- {
- if(crc & 1)
- crc = (crc >> 1) ^ aPoly;
- else
- crc >>= 1;
- }
- CRC32Table[i] = crc;
- }
- }
-
- /* ------------------------------------------------------------------------- */
- USGL CalculateBufferCRC32(void *buffer, USGI count, USGL crc)
- {
- USGC *p;
- USGL temp1;
- USGL temp2;
-
- if(CRC32Table[1] == 0)
- BuildCRC32Table(CRC32_POLYNOMIAL);
- p = (USGC*) buffer;
- while(count-- != 0)
- {
- temp1 = (crc >> 8) & 0x00FFFFFFUL;
- temp2 = CRC32Table[((int) crc ^ *p++) & 0xff];
- crc = temp1 ^ temp2;
- }
- return crc;
- }
-
- /* ------------------------------------------------------------------------- */
- USGL CalculateFileCRC32(FILE *fp)
- {
- USGL crc;
- int count;
- USGC buffer[512];
-
- crc = 0xFFFFFFFFUL;
- for( ; ;)
- {
- count = fread(buffer, 1, 512, fp);
- if(count == 0)
- break;
- crc = CalculateBufferCRC32(buffer, count, crc);
- }
- return(crc ^= 0xFFFFFFFFUL);
- }
-
- /* ------------------------------------------------------------------------- */
- USGL CalculateStringCRC32(void *buffer, USGI len)
- /*
- 计算字符串的 32 位 CRC 值
- */
- {
- USGL crc;
-
- if(CRC32Table[1] == 0)
- BuildCRC32Table(CRC32_POLYNOMIAL);
- crc = 0xFFFFFFFFUL;
- crc = CalculateBufferCRC32(buffer, len, crc);
- return (crc ^= 0xFFFFFFFFUL);
- }
-
- /* ------------------------------------------------------------------------- */
- int DispFileCRC32(char *filename)
- {
- FILE *fp;
- USGL crc;
-
- fp = fopen(filename, "rb");
- if(fp == NULL )
- {
- printf("Error: could not open file %s!\n", filename);
- return -1;
- }
- crc = CalculateFileCRC32(fp);
- fclose(fp);
- printf("CRC32: %08lX\n", crc);
- return 0;
- }
-
- /* ------------------------------------------------------------------------- */
- long filesize(FILE *stream)
- {
- long curpos, length;
-
- curpos = ftell(stream);
- fseek(stream, 0L, SEEK_END);
- length = ftell(stream);
- fseek(stream, curpos, SEEK_SET);
- return length;
- }
-
- /* ------------------------------------------------------------------------- */
- int FileCRC32(char *filename)
- {
- FILE *fp;
- USGL crc;
- char *str;
- USGI flen;
-
- fp = fopen(filename, "rb");
- if(fp == NULL )
- {
- printf("Error: could not open file %s!\n", filename);
- return -1;
- }
- flen = filesize(fp);
- /* allocate memory for string */
- if((str = (char *) malloc((USGI)flen)) == NULL)
- {
- printf("Not enough memory to allocate buffer\n");
- fclose(fp);
- exit(1); /* terminate program if out of memory */
- }
- fread(str, 1, flen, fp);
- fclose(fp);
- crc = CalculateStringCRC32(str, flen);
- printf("string CRC32: %08lX\n", crc);
- /* free memory */
- free(str);
- return 0;
- }
-
- /* ------------------------------------------------------------------------- */
- int main(int argc, char *argv[])
- {
- printf("CRC32F -- 计算文件的 CRC32 值\n");
- if(argc != 2)
- {
- printf("用法:CRC32F 文件名\n");
- return 1;
- }
- printf("\nCalculating ...\n");
- DispFileCRC32(argv[1]);
- FileCRC32(argv[1]);
- return 0;
- }
-
- /* End of file */
复制代码
[ 本帖最后由 ux400 于 2008-8-26 17:57 编辑 ] |
|