Use multiple sum algorithm from zlib for Adler and Fletcher checksums.

This commit is contained in:
2021-09-22 23:49:32 +01:00
parent 186f57d7cb
commit bf1b026a7e
6 changed files with 409 additions and 38 deletions

165
adler32.c
View File

@@ -1,21 +1,29 @@
/*
* This file is part of the Aaru Data Preservation Suite.
* Copyright (c) 2019-2021 Natalia Portillo.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/* adler32.c -- compute the Adler-32 checksum of a data stream
Copyright (C) 1995-2011 Mark Adler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jean-loup Gailly Mark Adler
jloup@gzip.org madler@alumni.caltech.edu
*/
#include <stdint.h>
#include <stdlib.h>
@@ -38,15 +46,138 @@ AARU_EXPORT adler32_ctx* AARU_CALL adler32_init()
AARU_EXPORT int AARU_CALL adler32_update(adler32_ctx* ctx, const uint8_t* data, uint32_t len)
{
uint32_t i;
if(!ctx || !data) return -1;
for(i = 0; i < len; i++)
uint32_t sum1 = ctx->sum1;
uint32_t sum2 = ctx->sum2;
unsigned n;
/* in case user likes doing a byte at a time, keep it fast */
if(len == 1)
{
ctx->sum1 = (ctx->sum1 + data[i]) % ADLER_MODULE;
ctx->sum2 = (ctx->sum2 + ctx->sum1) % ADLER_MODULE;
sum1 += data[0];
if(sum1 >= ADLER_MODULE) sum1 -= ADLER_MODULE;
sum2 += sum1;
if(sum2 >= ADLER_MODULE) sum2 -= ADLER_MODULE;
ctx->sum1 = sum1 & 0xFFFF;
ctx->sum2 = sum2 & 0xFFFF;
return 0;
}
/* in case short lengths are provided, keep it somewhat fast */
if(len < 16)
{
while(len--)
{
sum1 += *data++;
sum2 += sum1;
}
if(sum1 >= ADLER_MODULE) sum1 -= ADLER_MODULE;
sum2 %= ADLER_MODULE; /* only added so many ADLER_MODULE's */
ctx->sum1 = sum1 & 0xFFFF;
ctx->sum2 = sum2 & 0xFFFF;
return 0;
}
/* do length NMAX blocks -- requires just one modulo operation */
while(len >= NMAX)
{
len -= NMAX;
n = NMAX / 16; /* NMAX is divisible by 16 */
do {
sum1 += (data)[0];
sum2 += sum1;
sum1 += (data)[0 + 1];
sum2 += sum1;
sum1 += (data)[0 + 2];
sum2 += sum1;
sum1 += (data)[0 + 2 + 1];
sum2 += sum1;
sum1 += (data)[0 + 4];
sum2 += sum1;
sum1 += (data)[0 + 4 + 1];
sum2 += sum1;
sum1 += (data)[0 + 4 + 2];
sum2 += sum1;
sum1 += (data)[0 + 4 + 2 + 1];
sum2 += sum1;
sum1 += (data)[8];
sum2 += sum1;
sum1 += (data)[8 + 1];
sum2 += sum1;
sum1 += (data)[8 + 2];
sum2 += sum1;
sum1 += (data)[8 + 2 + 1];
sum2 += sum1;
sum1 += (data)[8 + 4];
sum2 += sum1;
sum1 += (data)[8 + 4 + 1];
sum2 += sum1;
sum1 += (data)[8 + 4 + 2];
sum2 += sum1;
sum1 += (data)[8 + 4 + 2 + 1];
sum2 += sum1;
/* 16 sums unrolled */
data += 16;
} while(--n);
sum1 %= ADLER_MODULE;
sum2 %= ADLER_MODULE;
}
/* do remaining bytes (less than NMAX, still just one modulo) */
if(len)
{ /* avoid modulos if none remaining */
while(len >= 16)
{
len -= 16;
sum1 += (data)[0];
sum2 += sum1;
sum1 += (data)[0 + 1];
sum2 += sum1;
sum1 += (data)[0 + 2];
sum2 += sum1;
sum1 += (data)[0 + 2 + 1];
sum2 += sum1;
sum1 += (data)[0 + 4];
sum2 += sum1;
sum1 += (data)[0 + 4 + 1];
sum2 += sum1;
sum1 += (data)[0 + 4 + 2];
sum2 += sum1;
sum1 += (data)[0 + 4 + 2 + 1];
sum2 += sum1;
sum1 += (data)[8];
sum2 += sum1;
sum1 += (data)[8 + 1];
sum2 += sum1;
sum1 += (data)[8 + 2];
sum2 += sum1;
sum1 += (data)[8 + 2 + 1];
sum2 += sum1;
sum1 += (data)[8 + 4];
sum2 += sum1;
sum1 += (data)[8 + 4 + 1];
sum2 += sum1;
sum1 += (data)[8 + 4 + 2];
sum2 += sum1;
sum1 += (data)[8 + 4 + 2 + 1];
sum2 += sum1;
data += 16;
}
while(len--)
{
sum1 += *data++;
sum2 += sum1;
}
sum1 %= ADLER_MODULE;
sum2 %= ADLER_MODULE;
}
ctx->sum1 = sum1 & 0xFFFF;
ctx->sum2 = sum2 & 0xFFFF;
return 0;
}