《算法》算法大杂烩 专栏收录该内容
1 篇文章 0 订阅

【C语言实现】

/**
******************************************************************************
* @file                main.c
* @author              BruceOu
* @version             V1.0
* @date                2021-09-06
* @blog                https://blog.bruceou.cn/
* @Official Accounts   嵌入式实验楼
* @brief
******************************************************************************
*/
/**Include*********************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <string.h>

//
const uint32_t MOD_ADLER = 65521;

/**
* @param  pData, unLen
* @retval unCheckSum
*/
uint32_t adler32(uint8_t *pData, size_t unLen)
{
uint32_t s1 = 1, s2 = 0;
size_t ulIndex;
uint32_t unCheckSum = 0 ;

// Process each byte of the data in order
for (ulIndex = 0; ulIndex < unLen; ++ulIndex)
{
s1 = (s1 + pData[ulIndex]) % MOD_ADLER;
s2 = (s2 + s1) % MOD_ADLER;
}

unCheckSum = (s2 << 16) | s1;

return unCheckSum;
}

/**
* @brief  main
* @param  None
* @retval int
*/
int main()
{
char chData[] = "Wikipedia";
uint32_t unResult = 0;

unResult = adler32(chData, strlen(chData));

printf("result : %x", unResult);

return 0;
}


/**
* @param  unCheckSum, pBuf, nLen
* @retval unCheckSum
*/
static uint32_t adler32(uint32_t unCheckSum, const void* pBuf, size_t nLen)
{

const uint8_t* ptr = (const uint8_t*)pBuf;
uint32_t s1 = unCheckSum & 0xffff;
uint32_t s2 = (unCheckSum >> 16) & 0xffff;

while (nLen > 0)
{
uint32_t k = nLen < 5552 ? nLen : 5552;
nLen -= k;

while (k >= 8)
{
s1 += *ptr++;
s2 += s1;
s1 += *ptr++;
s2 += s1;
s1 += *ptr++;
s2 += s1;
s1 += *ptr++;
s2 += s1;
s1 += *ptr++;
s2 += s1;
s1 += *ptr++;
s2 += s1;
s1 += *ptr++;
s2 += s1;
s1 += *ptr++;
s2 += s1;
k -= 8;
}

while (k-- > 0)
{
s1 += *ptr++;
s2 += s1;
}
s1 = s1 % MOD_ADLER;
s2 = s2 % MOD_ADLER;
}
return (s2 << 16) + s1;
}


### 欢迎订阅我的微信公众号

• 0
点赞
• 0
评论
• 0
收藏
• 打赏
• 扫一扫，分享海报

11-23 4067
07-10 1287

06-15 3496
06-24 3244
07-06 1695
06-29 5421
04-12 3757
12-27 276
09-26 744
11-23 1万+
06-29 1692
08-31 6692
08-20 84
©️2022 CSDN 皮肤主题：代码科技 设计师：Amelia_0503

Bruceoxl

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。