/*1* Compute the Adler-32 checksum of a data stream.2* This is a modified version based on adler32.c from the zlib library.3*4* Copyright (C) 1995 Mark Adler5*6* This software is provided 'as-is', without any express or implied7* warranty. In no event will the authors be held liable for any damages8* arising from the use of this software.9*10* Permission is granted to anyone to use this software for any purpose,11* including commercial applications, and to alter it and redistribute it12* freely, subject to the following restrictions:13*14* 1. The origin of this software must not be misrepresented; you must not15* claim that you wrote the original software. If you use this software16* in a product, an acknowledgment in the product documentation would be17* appreciated but is not required.18* 2. Altered source versions must be plainly marked as such, and must not be19* misrepresented as being the original software.20* 3. This notice may not be removed or altered from any source distribution.21*/2223/**24* @file25* Computes the Adler-32 checksum of a data stream26*27* This is a modified version based on adler32.c from the zlib library.28* @author Mark Adler29* @ingroup lavu_adler3230*/3132#include "config.h"33#include "adler32.h"34#include "common.h"35#include "intreadwrite.h"3637#define BASE 65521L /* largest prime smaller than 65536 */3839#define DO1(buf) { s1 += *buf++; s2 += s1; }40#define DO4(buf) DO1(buf); DO1(buf); DO1(buf); DO1(buf);41#define DO16(buf) DO4(buf); DO4(buf); DO4(buf); DO4(buf);4243unsigned long av_adler32_update(unsigned long adler, const uint8_t * buf,44unsigned int len)45{46unsigned long s1 = adler & 0xffff;47unsigned long s2 = adler >> 16;4849while (len > 0) {50#if HAVE_FAST_64BIT && HAVE_FAST_UNALIGNED && !CONFIG_SMALL51unsigned len2 = FFMIN((len-1) & ~7, 23*8);52if (len2) {53uint64_t a1= 0;54uint64_t a2= 0;55uint64_t b1= 0;56uint64_t b2= 0;57len -= len2;58s2 += s1*len2;59while (len2 >= 8) {60uint64_t v = AV_RN64(buf);61a2 += a1;62b2 += b1;63a1 += v &0x00FF00FF00FF00FF;64b1 += (v>>8)&0x00FF00FF00FF00FF;65len2 -= 8;66buf+=8;67}6869//We combine the 8 interleaved adler32 checksums without overflows70//Decreasing the number of iterations would allow below code to be71//simplified but would likely be slower due to the fewer iterations72//of the inner loop73s1 += ((a1+b1)*0x1000100010001)>>48;74s2 += ((((a2&0xFFFF0000FFFF)+(b2&0xFFFF0000FFFF)+((a2>>16)&0xFFFF0000FFFF)+((b2>>16)&0xFFFF0000FFFF))*0x800000008)>>32)75#if HAVE_BIGENDIAN76+ 2*((b1*0x1000200030004)>>48)77+ ((a1*0x1000100010001)>>48)78+ 2*((a1*0x0000100020003)>>48);79#else80+ 2*((a1*0x4000300020001)>>48)81+ ((b1*0x1000100010001)>>48)82+ 2*((b1*0x3000200010000)>>48);83#endif84}85#else86while (len > 4 && s2 < (1U << 31)) {87DO4(buf);88len -= 4;89}90#endif91DO1(buf); len--;92s1 %= BASE;93s2 %= BASE;94}95return (s2 << 16) | s1;96}9798#ifdef TEST99// LCOV_EXCL_START100#include <string.h>101#include "log.h"102#include "timer.h"103#define LEN 7001104105static volatile int checksum;106107int main(int argc, char **argv)108{109int i;110uint8_t data[LEN];111112av_log_set_level(AV_LOG_DEBUG);113114for (i = 0; i < LEN; i++)115data[i] = ((i * i) >> 3) + 123 * i;116117if (argc > 1 && !strcmp(argv[1], "-t")) {118for (i = 0; i < 1000; i++) {119START_TIMER;120checksum = av_adler32_update(1, data, LEN);121STOP_TIMER("adler");122}123} else {124checksum = av_adler32_update(1, data, LEN);125}126127av_log(NULL, AV_LOG_DEBUG, "%X (expected 50E6E508)\n", checksum);128return checksum == 0x50e6e508 ? 0 : 1;129}130// LCOV_EXCL_STOP131#endif132133134