45 lines
864 B
C
45 lines
864 B
C
|
// crc32.c
|
||
|
// WeChat Embedded
|
||
|
//
|
||
|
// Created by harlliu on 14-03-03.
|
||
|
// Copyright 2014 Tencent. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include "wechar_crc.h"
|
||
|
|
||
|
#define DO1(buf) crc = crc_table(((int)crc ^ (*buf++)) & 0xff) ^ (crc >> 8);
|
||
|
#define DO2(buf) DO1(buf); DO1(buf);
|
||
|
#define DO4(buf) DO2(buf); DO2(buf);
|
||
|
#define DO8(buf) DO4(buf); DO4(buf);
|
||
|
|
||
|
|
||
|
static uint32_t crc_table(uint32_t index)
|
||
|
{
|
||
|
uint32_t c = index;
|
||
|
uint32_t poly = 0xedb88320L;
|
||
|
int k;
|
||
|
|
||
|
for (k = 0; k < 8; k++)
|
||
|
c = c & 1 ? poly ^ (c >> 1) : c >> 1;
|
||
|
|
||
|
return c;
|
||
|
}
|
||
|
|
||
|
uint32_t crc32(uint32_t crc, const uint8_t *buf, int len)
|
||
|
{
|
||
|
if (buf == NULL) return 0L;
|
||
|
|
||
|
crc = crc ^ 0xffffffffL;
|
||
|
while (len >= 8)
|
||
|
{
|
||
|
DO8(buf);
|
||
|
len -= 8;
|
||
|
}
|
||
|
if (len) do {
|
||
|
DO1(buf);
|
||
|
} while (--len);
|
||
|
return crc ^ 0xffffffffL;
|
||
|
}
|
||
|
|