首页 > 编程语言 >Alder32校验算法

Alder32校验算法

时间:2024-10-10 14:45:07浏览次数:6  
标签:int v10 edi 校验 unsigned 算法 BASE Alder32 ecx

c源码

/* adler32.c -- compute the Adler-32 checksum of a data stream
* Copyright (C) 1995-2004 Mark Adler
* For conditions of distribution and use, see copyright notice in zlib.h
*/
/* @(#) $Id$ */

#define ZLIB_INTERNAL
#include "zlib.h"

#define BASE 65521UL /* largest prime smaller than 65536 */
#define NMAX 5552
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */

#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
#define DO16(buf) DO8(buf,0); DO8(buf,8);

/* use NO_DIVIDE if your processor does not do division in hardware */
#ifdef NO_DIVIDE
# define MOD(a) \
do { \
if (a >= (BASE << 16)) a -= (BASE << 16); \
if (a >= (BASE << 15)) a -= (BASE << 15); \
if (a >= (BASE << 14)) a -= (BASE << 14); \
if (a >= (BASE << 13)) a -= (BASE << 13); \
if (a >= (BASE << 12)) a -= (BASE << 12); \
if (a >= (BASE << 11)) a -= (BASE << 11); \
if (a >= (BASE << 10)) a -= (BASE << 10); \
if (a >= (BASE << 9)) a -= (BASE << 9); \
if (a >= (BASE << 8)) a -= (BASE << 8); \
if (a >= (BASE << 7)) a -= (BASE << 7); \
if (a >= (BASE << 6)) a -= (BASE << 6); \
if (a >= (BASE << 5)) a -= (BASE << 5); \
if (a >= (BASE << 4)) a -= (BASE << 4); \
if (a >= (BASE << 3)) a -= (BASE << 3); \
if (a >= (BASE << 2)) a -= (BASE << 2); \
if (a >= (BASE << 1)) a -= (BASE << 1); \
if (a >= BASE) a -= BASE; \
} while (0)
# define MOD4(a) \
do { \
if (a >= (BASE << 4)) a -= (BASE << 4); \
if (a >= (BASE << 3)) a -= (BASE << 3); \
if (a >= (BASE << 2)) a -= (BASE << 2); \
if (a >= (BASE << 1)) a -= (BASE << 1); \
if (a >= BASE) a -= BASE; \
} while (0)
#else
# define MOD(a) a %= BASE
# define MOD4(a) a %= BASE
#endif

/* ========================================================================= */
uLong ZEXPORT adler32(adler, buf, len)
uLong adler;
const Bytef *buf;
uInt len;
{
unsigned long sum2;
unsigned n;

/* split Adler-32 into component sums */
sum2 = (adler >> 16) & 0xffff;
adler &= 0xffff;

/* in case user likes doing a byte at a time, keep it fast */
if (len == 1) {
adler += buf[0];
if (adler >= BASE)
adler -= BASE;
sum2 += adler;
if (sum2 >= BASE)
sum2 -= BASE;
return adler | (sum2 << 16);
}

/* initial Adler-32 value (deferred check for len == 1 speed) */
if (buf == Z_NULL)
return 1L;

/* in case short lengths are provided, keep it somewhat fast */
if (len < 16) {
while (len--) {
adler += *buf++;
sum2 += adler;
}
if (adler >= BASE)
adler -= BASE;
MOD4(sum2); /* only added so many BASE's */
return adler | (sum2 << 16);
}

/* do length NMAX blocks -- requires just one modulo operation */
while (len >= NMAX) {
len -= NMAX;
n = NMAX / 16; /* NMAX is divisible by 16 */
do {
DO16(buf); /* 16 sums unrolled */
buf += 16;
} while (--n);
MOD(adler);
MOD(sum2);
}

/* do remaining bytes (less than NMAX, still just one modulo) */
if (len) { /* avoid modulos if none remaining */
while (len >= 16) {
len -= 16;
DO16(buf);
buf += 16;
}
while (len--) {
adler += *buf++;
sum2 += adler;
}
MOD(adler);
MOD(sum2);
}

/* return recombined sums */
return adler | (sum2 << 16);
}

/* ========================================================================= */
uLong ZEXPORT adler32_combine(adler1, adler2, len2)
uLong adler1;
uLong adler2;
z_off_t len2;
{
unsigned long sum1;
unsigned long sum2;
unsigned rem;

/* the derivation of this formula is left as an exercise for the reader */
rem = (unsigned)(len2 % BASE);
sum1 = adler1 & 0xffff;
sum2 = rem * sum1;
MOD(sum2);
sum1 += (adler2 & 0xffff) + BASE - 1;
sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
if (sum1 > BASE) sum1 -= BASE;
if (sum1 > BASE) sum1 -= BASE;
if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
if (sum2 > BASE) sum2 -= BASE;
return sum1 | (sum2 << 16);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////

反编译源码

//----- (10088260) --------------------------------------------------------
unsigned int __cdecl sub_10088260(unsigned int a1, unsigned __int8 *a2, unsigned int a3)
{
unsigned int v3; // ebx
unsigned int v4; // ecx
unsigned int v5; // edi
unsigned int v6; // eax
int v7; // edx
int v8; // ecx
unsigned __int8 *v10; // esi
int v11; // eax
unsigned int v12; // esi
int v13; // edx
unsigned int v14; // ecx
unsigned int v15; // edi
unsigned int v16; // ecx
unsigned int v17; // edi
unsigned int v18; // ecx
unsigned int v19; // edi
unsigned int v20; // ecx
unsigned int v21; // edi
unsigned int v22; // ecx
unsigned int v23; // edi
unsigned int v24; // ecx
unsigned int v25; // edi
unsigned int v26; // ecx
int v27; // edi
unsigned int v28; // ecx
int v29; // edi
unsigned int v30; // ecx
int v31; // edi
int v32; // ecx
int v33; // edi
int v34; // ecx
int v35; // edi
int v36; // ecx
int v37; // edi
int v38; // ecx
int v39; // edi
int v40; // ecx
int v41; // edi
int v42; // ecx
int v43; // eax
int v44; // edi
unsigned int v45; // edx
unsigned int v46; // ecx
unsigned int v47; // edi
unsigned int v48; // ecx
unsigned int v49; // edi
unsigned int v50; // ecx
unsigned int v51; // edi
unsigned int v52; // ecx
unsigned int v53; // edi
unsigned int v54; // ecx
unsigned int v55; // edi
unsigned int v56; // ecx
unsigned int v57; // edi
unsigned int v58; // ecx
int v59; // edi
unsigned int v60; // ecx
int v61; // edi
unsigned int v62; // ecx
int v63; // edi
int v64; // ecx
int v65; // edi
int v66; // ecx
int v67; // edi
int v68; // ecx
int v69; // edi
int v70; // ecx
int v71; // edi
int v72; // ecx
int v73; // edi
int v74; // ecx
int v75; // eax
int v76; // edi
int v77; // eax
unsigned int v78; // [esp+10h] [ebp+8h]

v3 = a3;
v4 = (unsigned __int16)a1;
v5 = HIWORD(a1);
if ( a3 == 1 )
{
v6 = (unsigned __int16)a1 + *a2;
v7 = v6 - 65521;
if ( v6 < 0xFFF1 )
v7 = (unsigned __int16)a1 + *a2;
v8 = v7 + v5 - 65521;
if ( v7 + v5 < 0xFFF1 )
v8 = v7 + v5;
return v7 | (v8 << 16);
}
else
{
v10 = a2;
if ( a2 )
{
if ( a3 >= 0x10 )
{
if ( a3 >= 0x15B0 )
{
v78 = a3 / 0x15B0;
do
{
v3 -= 5552;
v13 = 347;
do
{
v14 = *v10 + v4;
v15 = v14 + v5;
v16 = v10[1] + v14;
v17 = v16 + v15;
v18 = v10[2] + v16;
v19 = v18 + v17;
v20 = v10[3] + v18;
v21 = v20 + v19;
v22 = v10[4] + v20;
v23 = v22 + v21;
v24 = v10[5] + v22;
v25 = v24 + v23;
v26 = v10[6] + v24;
v27 = v26 + v25;
v28 = v10[7] + v26;
v29 = v28 + v27;
v30 = v10[8] + v28;
v31 = v30 + v29;
v32 = v10[9] + v30;
v33 = v32 + v31;
v34 = v10[10] + v32;
v35 = v34 + v33;
v36 = v10[11] + v34;
v37 = v36 + v35;
v38 = v10[12] + v36;
v39 = v38 + v37;
v40 = v10[13] + v38;
v41 = v40 + v39;
v42 = v10[14] + v40;
v43 = v10[15];
v44 = v42 + v41;
v10 += 16;
v4 = v43 + v42;
v5 = v4 + v44;
--v13;
}
while ( v13 );
v4 %= 0xFFF1u;
v5 %= 0xFFF1u;
--v78;
}
while ( v78 );
}
if ( v3 )
{
if ( v3 >= 0x10 )
{
v45 = v3 >> 4;
do
{
v3 -= 16;
v46 = *v10 + v4;
v47 = v46 + v5;
v48 = v10[1] + v46;
v49 = v48 + v47;
v50 = v10[2] + v48;
v51 = v50 + v49;
v52 = v10[3] + v50;
v53 = v52 + v51;
v54 = v10[4] + v52;
v55 = v54 + v53;
v56 = v10[5] + v54;
v57 = v56 + v55;
v58 = v10[6] + v56;
v59 = v58 + v57;
v60 = v10[7] + v58;
v61 = v60 + v59;
v62 = v10[8] + v60;
v63 = v62 + v61;
v64 = v10[9] + v62;
v65 = v64 + v63;
v66 = v10[10] + v64;
v67 = v66 + v65;
v68 = v10[11] + v66;
v69 = v68 + v67;
v70 = v10[12] + v68;
v71 = v70 + v69;
v72 = v10[13] + v70;
v73 = v72 + v71;
v74 = v10[14] + v72;
v75 = v10[15];
v76 = v74 + v73;
v10 += 16;
v4 = v75 + v74;
v5 = v4 + v76;
--v45;
}
while ( v45 );
}
for ( ; v3; --v3 )
{
v77 = *v10++;
v4 += v77;
v5 += v4;
}
v4 %= 0xFFF1u;
v5 %= 0xFFF1u;
}
return v4 | (v5 << 16);
}
else
{
if ( a3 )
{
do
{
v11 = *v10++;
v4 += v11;
v5 += v4;
--v3;
}
while ( v3 );
}
v12 = v4 - 65521;
if ( v4 < 0xFFF1 )
v12 = v4;
return v12 | ((v5 + 15 * (v5 / 0xFFF1)) << 16);
}
}
else
{
return 1;
}
}
}
// 10088260: too many cbuild loops

标签:int,v10,edi,校验,unsigned,算法,BASE,Alder32,ecx
From: https://www.cnblogs.com/dianziguan/p/18456342

相关文章

  • 基于MSER和HOG特征提取的SVM交通标志检测和识别算法matlab仿真
    1.算法运行效果图预览(完整程序运行后无水印)   2.算法运行软件版本matlab2017b 3.部分核心程序(完整版代码包含中文注释和操作步骤视频)function[Ic,Xmin3,Xmax3,Ymin3,Ymax3]=func_merge(I,Trafficxy,Smj,SCALE);%提取交通标志的中心点,判断是否为同一......
  • STL——2.算法
    一、遍历1.for_eachvoidMyPrint(intval){cout<<val<<endl;}vector<int>v1={1,2,3,4};for_each(v1.begin(),v1.end(),MyPrint);2.transformv2.resize(v1.size());//先开辟空间,否则报错transform(v1,begin(),v1.end(),v2.begin(),MyPri......
  • K近邻算法
    一、K近邻算法基础介绍K近邻算法也是常说的KNN算法,是一种常见的分类和回归算法,当然我们常将其用于分类。是一种监督算法,该算法的内容其实和名字很像,根据邻居来进行判断。有点近朱者赤近墨者黑的意味。比如我们常说:某个人的工资一般是与其玩的最好的5个朋友(或者说是N个)工资......
  • kmp算法模板
    voidkmp(){n=strlen(s+1);//s是目标串m=strlen(p+1);//p是模板串//nxt预处理开始intj=0;nxt[1]=0;for(inti=2;i<=m;i++){while(j>0&&p[j+1]!=p[i])/*判断当前子串的前后缀相等的长度是否能增......
  • 从理论到实践:AI智能分析网关V4烟火检测算法的应用场景探索
    在信息化和智能化的今天,AI智能分析网关V4作为一款集成了先进技术的硬件设备,在烟火检测领域展现出了强大的应用价值。本文将详细阐述AI智能分析网关V4烟火检测算法的原理及其在各种场景中的应用。一、AI智能分析网关V4烟火检测算法原理深度学习基础TSINGSEE青犀AI智能分析网关V4......
  • 数据结构与算法2
    目录栈和队列1.栈(stack)1.1栈的定义和特点1.2顺序栈的表示1.3顺序栈的实现1.3.1顺序栈的初始化1.3.2顺序栈判断栈是否为空1.3.3求顺序栈长度1.3.4清空顺序栈1.3.5销毁顺序栈1.3.6顺序栈的入栈1.3.7顺序栈的出栈1.4栈链的表示1.5栈链的实现1.5.1栈链的初始化1.5.2判断......
  • 算法笔记(十五)——BFS 解决拓扑排序
    文章目录拓扑排序课程表课程表II火星词典拓扑排序有向无环图(DAG图)有向无环图指的是一个无回路的有向图AOV网:顶点活动图在有向无环图中,用顶点表示一个活动,用边来表示活动的先后顺序的图结构拓扑排序找到一个先后顺序,结果可能不唯一如何拓扑排序?找到一......
  • 二分图最大匹配-匈牙利算法
    二分图最大匹配设G为二分图,若在G的子图M中,任意两条边都没有公共节点,那么称M为二分图G的一组匹配。在二分图中,包含边数最多的一组匹配称为二分图的最大匹配。交替路:从一个未匹配点出发,依次经过非匹配边、匹配边、非匹配边…形成的路径叫交替路。增广路:从一个未匹配点......
  • 算法修炼之路之前缀和
    目录一:一维前缀和二:二维前缀和 三:LeetCodeOJ练习  1.第一题2.第二题 3.第三题 4.第四题5.第五题6.第六题一:一维前缀和这里通过例题来引出牛客_DP34【模板】前缀和画图分析:具体代码:#include<iostream>#include<vector>usingnamespacestd;int......
  • 算法校赛准备
    独木桥题目背景战争已经进入到紧要时间。你是运输小队长,正在率领运输部队向前线运送物资。运输任务像做题一样的无聊。你希望找些刺激,于是命令你的士兵们到前方的一座独木桥上欣赏风景,而你留在桥下欣赏士兵们。士兵们十分愤怒,因为这座独木桥十分狭窄,只能容纳$1$个人通过。假如......