summaryrefslogtreecommitdiff
path: root/regtest/checksum.c
blob: 7f433a41c86c44bb3fb2861916797bd869cb6559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
**	Copyright (C) 2005-2011 Erik de Castro Lopo
**
**	This program is free software; you can redistribute it and/or modify
**	it under the terms of the GNU General Public License as published by
**	the Free Software Foundation; either version 2 of the License, or
**	(at your option) any later version.
**
**	This program 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 General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program; if not, write to the Free Software
**	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
**	A simple checksum for short, int and float data.
*/

#include "sfconfig.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <sndfile.h>

#include "regtest.h"

#define	BIG_PRIME		999983

#define	ARRAY_LEN(x)	((int) (sizeof (x)) / (sizeof ((x) [0])))

static int short_checksum (SNDFILE * file, int start) ;
static int int_checksum (SNDFILE * file, int start) ;
static int float_checksum (SNDFILE * file, int start) ;

int
calc_checksum (SNDFILE * file, const SF_INFO * info)
{	int start ;

	/* Seed the checksum with data from the SF_INFO struct. */
	start = info->samplerate ;
	start = start * BIG_PRIME + info->channels ;
	start = start * BIG_PRIME + info->format ;

	switch (info->format & SF_FORMAT_SUBMASK)
	{	case SF_FORMAT_FLOAT :
		case SF_FORMAT_DOUBLE :
			return float_checksum (file, start) ;

		case SF_FORMAT_PCM_24 :
		case SF_FORMAT_PCM_32 :
			return int_checksum (file, start) ;

		default :
			return short_checksum (file, start) ;
		} ;

	return 0 ;
} /* calc_checksum */

/*------------------------------------------------------------------------------
*/

static union
{	short	s [1 << 16] ;
	int		i [1 << 15] ;
	float	f [1 << 15] ;
} data ;

static int
short_checksum (SNDFILE * file, int start)
{	int k, count ;

	do
	{	count = (int) sf_read_short (file, data.s, ARRAY_LEN (data.s)) ;
		for (k = 0 ; k < count ; k++)
			start = start * BIG_PRIME + data.s [k] ;
		}
	while (count > 0) ;

	return start ;
} /* short_checksum */

static int
int_checksum (SNDFILE * file, int start)
{	int k, count ;

	do
	{	count = (int) sf_read_int (file, data.i, ARRAY_LEN (data.i)) ;
		for (k = 0 ; k < count ; k++)
			start = start * BIG_PRIME + data.i [k] ;
		}
	while (count > 0) ;

	return start ;
} /* int_checksum */

static int
float_checksum (SNDFILE * file, int start)
{	int k, count ;

	do
	{	count = (int) sf_read_float (file, data.f, ARRAY_LEN (data.f)) ;
		for (k = 0 ; k < count ; k++)
			start = start * BIG_PRIME + lrintf (0x7FFFFFFF * data.f [k]) ;
		}
	while (count > 0) ;

	return start ;
} /* float_checksum */