17c2fbfb3SApril Chin /***********************************************************************
27c2fbfb3SApril Chin *                                                                      *
37c2fbfb3SApril Chin *               This software is part of the ast package               *
4*b30d1939SAndy Fiddaman *          Copyright (c) 1996-2011 AT&T Intellectual Property          *
57c2fbfb3SApril Chin *                      and is licensed under the                       *
6*b30d1939SAndy Fiddaman *                 Eclipse Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
87c2fbfb3SApril Chin *                                                                      *
97c2fbfb3SApril Chin *                A copy of the License is available at                 *
10*b30d1939SAndy Fiddaman *          http://www.eclipse.org/org/documents/epl-v10.html           *
11*b30d1939SAndy Fiddaman *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
127c2fbfb3SApril Chin *                                                                      *
137c2fbfb3SApril Chin *              Information and Software Systems Research               *
147c2fbfb3SApril Chin *                            AT&T Research                             *
157c2fbfb3SApril Chin *                           Florham Park NJ                            *
167c2fbfb3SApril Chin *                                                                      *
177c2fbfb3SApril Chin *                 Glenn Fowler <gsf@research.att.com>                  *
187c2fbfb3SApril Chin *                                                                      *
197c2fbfb3SApril Chin ***********************************************************************/
207c2fbfb3SApril Chin #pragma prototyped
217c2fbfb3SApril Chin /*
227c2fbfb3SApril Chin  * Glenn Fowler
237c2fbfb3SApril Chin  * AT&T Research
247c2fbfb3SApril Chin  *
257c2fbfb3SApril Chin  * man this is sum library
267c2fbfb3SApril Chin  */
277c2fbfb3SApril Chin 
287c2fbfb3SApril Chin #define _SUM_PRIVATE_	\
297c2fbfb3SApril Chin 			struct Method_s*	method;	\
307c2fbfb3SApril Chin 			uintmax_t		total_count;	\
317c2fbfb3SApril Chin 			uintmax_t		total_size;	\
327c2fbfb3SApril Chin 			uintmax_t		size;
337c2fbfb3SApril Chin 
347c2fbfb3SApril Chin #include <sum.h>
357c2fbfb3SApril Chin #include <ctype.h>
367c2fbfb3SApril Chin #include <swap.h>
377c2fbfb3SApril Chin #include <hashpart.h>
387c2fbfb3SApril Chin 
397c2fbfb3SApril Chin #define SCALE(n,m)	(((n)+(m)-1)/(m))
407c2fbfb3SApril Chin 
417c2fbfb3SApril Chin typedef struct Method_s
427c2fbfb3SApril Chin {
437c2fbfb3SApril Chin 	const char*	match;
447c2fbfb3SApril Chin 	const char*	description;
457c2fbfb3SApril Chin 	const char*	options;
467c2fbfb3SApril Chin 	Sum_t*		(*open)(const struct Method_s*, const char*);
477c2fbfb3SApril Chin 	int		(*init)(Sum_t*);
487c2fbfb3SApril Chin 	int		(*block)(Sum_t*, const void*, size_t);
497c2fbfb3SApril Chin 	int		(*data)(Sum_t*, Sumdata_t*);
507c2fbfb3SApril Chin 	int		(*print)(Sum_t*, Sfio_t*, int, size_t);
517c2fbfb3SApril Chin 	int		(*done)(Sum_t*);
527c2fbfb3SApril Chin 	int		scale;
537c2fbfb3SApril Chin } Method_t;
547c2fbfb3SApril Chin 
557c2fbfb3SApril Chin typedef struct Map_s
567c2fbfb3SApril Chin {
577c2fbfb3SApril Chin 	const char*	match;
587c2fbfb3SApril Chin 	const char*	description;
597c2fbfb3SApril Chin 	const char*	map;
607c2fbfb3SApril Chin } Map_t;
617c2fbfb3SApril Chin 
627c2fbfb3SApril Chin /*
637c2fbfb3SApril Chin  * 16 and 32 bit common code
647c2fbfb3SApril Chin  */
657c2fbfb3SApril Chin 
667c2fbfb3SApril Chin #define _INTEGRAL_PRIVATE_ \
677c2fbfb3SApril Chin 	uint32_t	sum; \
687c2fbfb3SApril Chin 	uint32_t	total_sum;
697c2fbfb3SApril Chin 
707c2fbfb3SApril Chin typedef struct Integral_s
717c2fbfb3SApril Chin {
727c2fbfb3SApril Chin 	_SUM_PUBLIC_
737c2fbfb3SApril Chin 	_SUM_PRIVATE_
747c2fbfb3SApril Chin 	_INTEGRAL_PRIVATE_
757c2fbfb3SApril Chin } Integral_t;
767c2fbfb3SApril Chin 
777c2fbfb3SApril Chin static Sum_t*
long_open(const Method_t * method,const char * name)787c2fbfb3SApril Chin long_open(const Method_t* method, const char* name)
797c2fbfb3SApril Chin {
807c2fbfb3SApril Chin 	Integral_t*	p;
817c2fbfb3SApril Chin 
827c2fbfb3SApril Chin 	if (p = newof(0, Integral_t, 1, 0))
837c2fbfb3SApril Chin 	{
847c2fbfb3SApril Chin 		p->method = (Method_t*)method;
857c2fbfb3SApril Chin 		p->name = name;
867c2fbfb3SApril Chin 	}
877c2fbfb3SApril Chin 	return (Sum_t*)p;
887c2fbfb3SApril Chin }
897c2fbfb3SApril Chin 
907c2fbfb3SApril Chin static int
long_init(Sum_t * p)917c2fbfb3SApril Chin long_init(Sum_t* p)
927c2fbfb3SApril Chin {
937c2fbfb3SApril Chin 	((Integral_t*)p)->sum = 0;
947c2fbfb3SApril Chin 	return 0;
957c2fbfb3SApril Chin }
967c2fbfb3SApril Chin 
977c2fbfb3SApril Chin static int
long_done(Sum_t * p)987c2fbfb3SApril Chin long_done(Sum_t* p)
997c2fbfb3SApril Chin {
1007c2fbfb3SApril Chin 	register Integral_t*	x = (Integral_t*)p;
1017c2fbfb3SApril Chin 
1027c2fbfb3SApril Chin 	x->total_sum ^= (x->sum &= 0xffffffff);
1037c2fbfb3SApril Chin 	return 0;
1047c2fbfb3SApril Chin }
1057c2fbfb3SApril Chin 
1067c2fbfb3SApril Chin static int
short_done(Sum_t * p)1077c2fbfb3SApril Chin short_done(Sum_t* p)
1087c2fbfb3SApril Chin {
1097c2fbfb3SApril Chin 	register Integral_t*	x = (Integral_t*)p;
1107c2fbfb3SApril Chin 
1117c2fbfb3SApril Chin 	x->total_sum ^= (x->sum &= 0xffff);
1127c2fbfb3SApril Chin 	return 0;
1137c2fbfb3SApril Chin }
1147c2fbfb3SApril Chin 
1157c2fbfb3SApril Chin static int
long_print(Sum_t * p,Sfio_t * sp,register int flags,size_t scale)1167c2fbfb3SApril Chin long_print(Sum_t* p, Sfio_t* sp, register int flags, size_t scale)
1177c2fbfb3SApril Chin {
1187c2fbfb3SApril Chin 	register Integral_t*	x = (Integral_t*)p;
1197c2fbfb3SApril Chin 	register uint32_t	c;
1207c2fbfb3SApril Chin 	register uintmax_t	z;
1217c2fbfb3SApril Chin 	register size_t		n;
1227c2fbfb3SApril Chin 
1237c2fbfb3SApril Chin 	c = (flags & SUM_TOTAL) ? x->total_sum : x->sum;
1247c2fbfb3SApril Chin 	sfprintf(sp, "%.*I*u", (flags & SUM_LEGACY) ? 5 : 1, sizeof(c), c);
1257c2fbfb3SApril Chin 	if (flags & SUM_SIZE)
1267c2fbfb3SApril Chin 	{
1277c2fbfb3SApril Chin 		z = (flags & SUM_TOTAL) ? x->total_size : x->size;
1287c2fbfb3SApril Chin 		if ((flags & SUM_SCALE) && ((n = scale) || (n = x->method->scale)))
1297c2fbfb3SApril Chin 			z = SCALE(z, n);
1307c2fbfb3SApril Chin 		sfprintf(sp, " %*I*u", (flags & SUM_LEGACY) ? 6 : 0, sizeof(z), z);
1317c2fbfb3SApril Chin 	}
1327c2fbfb3SApril Chin 	if (flags & SUM_TOTAL)
1337c2fbfb3SApril Chin 		sfprintf(sp, " %*I*u", (flags & SUM_LEGACY) ? 6 : 0, sizeof(x->total_count), x->total_count);
1347c2fbfb3SApril Chin 	return 0;
1357c2fbfb3SApril Chin }
1367c2fbfb3SApril Chin 
1377c2fbfb3SApril Chin static int
long_data(Sum_t * p,Sumdata_t * data)1387c2fbfb3SApril Chin long_data(Sum_t* p, Sumdata_t* data)
1397c2fbfb3SApril Chin {
1407c2fbfb3SApril Chin 	register Integral_t*	x = (Integral_t*)p;
1417c2fbfb3SApril Chin 
1427c2fbfb3SApril Chin 	data->size = sizeof(data->num);
1437c2fbfb3SApril Chin 	data->num = x->sum;
1447c2fbfb3SApril Chin 	data->buf = 0;
1457c2fbfb3SApril Chin 	return 0;
1467c2fbfb3SApril Chin }
1477c2fbfb3SApril Chin 
1487c2fbfb3SApril Chin #include "FEATURE/sum"
1497c2fbfb3SApril Chin 
1507c2fbfb3SApril Chin #include "sum-att.c"
1517c2fbfb3SApril Chin #include "sum-ast4.c"
1527c2fbfb3SApril Chin #include "sum-bsd.c"
1537c2fbfb3SApril Chin #include "sum-crc.c"
1547c2fbfb3SApril Chin #include "sum-prng.c"
1557c2fbfb3SApril Chin 
1567c2fbfb3SApril Chin #if _LIB_md && _lib_MD5Init && _hdr_md5 && _lib_SHA2Init && _hdr_sha2
1577c2fbfb3SApril Chin 
1587c2fbfb3SApril Chin #include "sum-lmd.c"
1597c2fbfb3SApril Chin 
1607c2fbfb3SApril Chin #else
1617c2fbfb3SApril Chin 
1627c2fbfb3SApril Chin #include "sum-md5.c"
1637c2fbfb3SApril Chin #include "sum-sha1.c"
1647c2fbfb3SApril Chin #include "sum-sha2.c"
1657c2fbfb3SApril Chin 
1667c2fbfb3SApril Chin #endif
1677c2fbfb3SApril Chin 
1687c2fbfb3SApril Chin /*
1697c2fbfb3SApril Chin  * now the library interface
1707c2fbfb3SApril Chin  */
1717c2fbfb3SApril Chin 
1727c2fbfb3SApril Chin #undef	METHOD		/* solaris <sys/localedef.h>! */
1737c2fbfb3SApril Chin #define METHOD(x)	x##_match,x##_description,x##_options,x##_open,x##_init,x##_block,x##_data,x##_print,x##_done,x##_scale
1747c2fbfb3SApril Chin 
1757c2fbfb3SApril Chin static const Method_t	methods[] =
1767c2fbfb3SApril Chin {
1777c2fbfb3SApril Chin 	METHOD(att),
1787c2fbfb3SApril Chin 	METHOD(ast4),
1797c2fbfb3SApril Chin 	METHOD(bsd),
1807c2fbfb3SApril Chin 	METHOD(crc),
1817c2fbfb3SApril Chin 	METHOD(prng),
1827c2fbfb3SApril Chin #ifdef md4_description
1837c2fbfb3SApril Chin 	METHOD(md4),
1847c2fbfb3SApril Chin #endif
1857c2fbfb3SApril Chin #ifdef md5_description
1867c2fbfb3SApril Chin 	METHOD(md5),
1877c2fbfb3SApril Chin #endif
1887c2fbfb3SApril Chin #ifdef sha1_description
1897c2fbfb3SApril Chin 	METHOD(sha1),
1907c2fbfb3SApril Chin #endif
1917c2fbfb3SApril Chin #ifdef sha256_description
1927c2fbfb3SApril Chin 	METHOD(sha256),
1937c2fbfb3SApril Chin #endif
1947c2fbfb3SApril Chin #ifdef sha384_description
1957c2fbfb3SApril Chin 	METHOD(sha384),
1967c2fbfb3SApril Chin #endif
1977c2fbfb3SApril Chin #ifdef sha512_description
1987c2fbfb3SApril Chin 	METHOD(sha512),
1997c2fbfb3SApril Chin #endif
2007c2fbfb3SApril Chin };
2017c2fbfb3SApril Chin 
2027c2fbfb3SApril Chin static const Map_t	maps[] =
2037c2fbfb3SApril Chin {
2047c2fbfb3SApril Chin 	{
2057c2fbfb3SApril Chin 		"posix|cksum|std|standard",
2067c2fbfb3SApril Chin 		"The posix 1003.2-1992 32 bit crc checksum. This is the"
2077c2fbfb3SApril Chin 		" default \bcksum\b(1)  method.",
2087c2fbfb3SApril Chin 		"crc-0x04c11db7-rotate-done-size"
2097c2fbfb3SApril Chin 	},
2107c2fbfb3SApril Chin 	{
2117c2fbfb3SApril Chin 		"zip",
2127c2fbfb3SApril Chin 		"The \bzip\b(1) crc.",
2137c2fbfb3SApril Chin 		"crc-0xedb88320-init-done"
2147c2fbfb3SApril Chin 	},
2157c2fbfb3SApril Chin 	{
2167c2fbfb3SApril Chin 		"fddi",
2177c2fbfb3SApril Chin 		"The FDDI crc.",
2187c2fbfb3SApril Chin 		"crc-0xedb88320-size=0xcc55cc55"
2197c2fbfb3SApril Chin 	},
2207c2fbfb3SApril Chin 	{
2217c2fbfb3SApril Chin 		"fnv|fnv1",
2227c2fbfb3SApril Chin 		"The Fowler-Noll-Vo 32 bit PRNG hash with non-zero"
2237c2fbfb3SApril Chin 		" initializer (FNV-1).",
2247c2fbfb3SApril Chin 		"prng-0x01000193-init=0x811c9dc5"
2257c2fbfb3SApril Chin 	},
2267c2fbfb3SApril Chin 	{
2277c2fbfb3SApril Chin 		"ast|strsum",
2287c2fbfb3SApril Chin 		"The \bast\b \bstrsum\b(3) PRNG hash.",
2297c2fbfb3SApril Chin 		"prng-0x63c63cd9-add=0x9c39c33d"
2307c2fbfb3SApril Chin 	},
2317c2fbfb3SApril Chin };
2327c2fbfb3SApril Chin 
23334f9b3eeSRoland Mainz /*
23434f9b3eeSRoland Mainz  * simple alternation prefix match
23534f9b3eeSRoland Mainz  */
23634f9b3eeSRoland Mainz 
23734f9b3eeSRoland Mainz static int
match(register const char * s,register const char * p)23834f9b3eeSRoland Mainz match(register const char* s, register const char* p)
23934f9b3eeSRoland Mainz {
24034f9b3eeSRoland Mainz 	register const char*	b = s;
24134f9b3eeSRoland Mainz 
24234f9b3eeSRoland Mainz 	for (;;)
24334f9b3eeSRoland Mainz 	{
24434f9b3eeSRoland Mainz 		do
24534f9b3eeSRoland Mainz 		{
24634f9b3eeSRoland Mainz 			if (*p == '|' || *p == 0)
24734f9b3eeSRoland Mainz 				return 1;
24834f9b3eeSRoland Mainz 		} while (*s++ == *p++);
24934f9b3eeSRoland Mainz 		for (;;)
25034f9b3eeSRoland Mainz 		{
25134f9b3eeSRoland Mainz 			switch (*p++)
25234f9b3eeSRoland Mainz 			{
25334f9b3eeSRoland Mainz 			case 0:
25434f9b3eeSRoland Mainz 				return 0;
25534f9b3eeSRoland Mainz 			case '|':
25634f9b3eeSRoland Mainz 				break;
25734f9b3eeSRoland Mainz 			default:
25834f9b3eeSRoland Mainz 				continue;
25934f9b3eeSRoland Mainz 			}
26034f9b3eeSRoland Mainz 			break;
26134f9b3eeSRoland Mainz 		}
26234f9b3eeSRoland Mainz 		s = b;
26334f9b3eeSRoland Mainz 	}
26434f9b3eeSRoland Mainz 	return 0;
26534f9b3eeSRoland Mainz }
26634f9b3eeSRoland Mainz 
2677c2fbfb3SApril Chin /*
2687c2fbfb3SApril Chin  * open sum method name
2697c2fbfb3SApril Chin  */
2707c2fbfb3SApril Chin 
2717c2fbfb3SApril Chin Sum_t*
sumopen(register const char * name)2727c2fbfb3SApril Chin sumopen(register const char* name)
2737c2fbfb3SApril Chin {
2747c2fbfb3SApril Chin 	register int	n;
2757c2fbfb3SApril Chin 
2767c2fbfb3SApril Chin 	if (!name || !name[0] || name[0] == '-' && !name[1])
2777c2fbfb3SApril Chin 		name = "default";
2787c2fbfb3SApril Chin 	for (n = 0; n < elementsof(maps); n++)
27934f9b3eeSRoland Mainz 		if (match(name, maps[n].match))
2807c2fbfb3SApril Chin 		{
2817c2fbfb3SApril Chin 			name = maps[n].map;
2827c2fbfb3SApril Chin 			break;
2837c2fbfb3SApril Chin 		}
2847c2fbfb3SApril Chin 	for (n = 0; n < elementsof(methods); n++)
28534f9b3eeSRoland Mainz 		if (match(name, methods[n].match))
2867c2fbfb3SApril Chin 			return (*methods[n].open)(&methods[n], name);
2877c2fbfb3SApril Chin 	return 0;
2887c2fbfb3SApril Chin }
2897c2fbfb3SApril Chin 
2907c2fbfb3SApril Chin /*
2917c2fbfb3SApril Chin  * initialize for a new run of blocks
2927c2fbfb3SApril Chin  */
2937c2fbfb3SApril Chin 
2947c2fbfb3SApril Chin int
suminit(Sum_t * p)2957c2fbfb3SApril Chin suminit(Sum_t* p)
2967c2fbfb3SApril Chin {
2977c2fbfb3SApril Chin 	p->size = 0;
2987c2fbfb3SApril Chin 	return (*p->method->init)(p);
2997c2fbfb3SApril Chin }
3007c2fbfb3SApril Chin 
3017c2fbfb3SApril Chin /*
3027c2fbfb3SApril Chin  * compute the running sum on buf
3037c2fbfb3SApril Chin  */
3047c2fbfb3SApril Chin 
3057c2fbfb3SApril Chin int
sumblock(Sum_t * p,const void * buf,size_t siz)3067c2fbfb3SApril Chin sumblock(Sum_t* p, const void* buf, size_t siz)
3077c2fbfb3SApril Chin {
3087c2fbfb3SApril Chin 	p->size += siz;
3097c2fbfb3SApril Chin 	return (*p->method->block)(p, buf, siz);
3107c2fbfb3SApril Chin }
3117c2fbfb3SApril Chin 
3127c2fbfb3SApril Chin /*
3137c2fbfb3SApril Chin  * done with this run of blocks
3147c2fbfb3SApril Chin  */
3157c2fbfb3SApril Chin 
3167c2fbfb3SApril Chin int
sumdone(Sum_t * p)3177c2fbfb3SApril Chin sumdone(Sum_t* p)
3187c2fbfb3SApril Chin {
3197c2fbfb3SApril Chin 	p->total_count++;
3207c2fbfb3SApril Chin 	p->total_size += p->size;
3217c2fbfb3SApril Chin 	return (*p->method->done)(p);
3227c2fbfb3SApril Chin }
3237c2fbfb3SApril Chin 
3247c2fbfb3SApril Chin /*
3257c2fbfb3SApril Chin  * print the sum [size] on sp
3267c2fbfb3SApril Chin  */
3277c2fbfb3SApril Chin 
3287c2fbfb3SApril Chin int
sumprint(Sum_t * p,Sfio_t * sp,int flags,size_t scale)3297c2fbfb3SApril Chin sumprint(Sum_t* p, Sfio_t* sp, int flags, size_t scale)
3307c2fbfb3SApril Chin {
3317c2fbfb3SApril Chin 	return (*p->method->print)(p, sp, flags, scale);
3327c2fbfb3SApril Chin }
3337c2fbfb3SApril Chin 
3347c2fbfb3SApril Chin /*
3357c2fbfb3SApril Chin  * return the current sum (internal) data
3367c2fbfb3SApril Chin  */
3377c2fbfb3SApril Chin 
3387c2fbfb3SApril Chin int
sumdata(Sum_t * p,Sumdata_t * d)3397c2fbfb3SApril Chin sumdata(Sum_t* p, Sumdata_t* d)
3407c2fbfb3SApril Chin {
3417c2fbfb3SApril Chin 	return (*p->method->data)(p, d);
3427c2fbfb3SApril Chin }
3437c2fbfb3SApril Chin 
3447c2fbfb3SApril Chin /*
3457c2fbfb3SApril Chin  * close an open sum handle
3467c2fbfb3SApril Chin  */
3477c2fbfb3SApril Chin 
3487c2fbfb3SApril Chin int
sumclose(Sum_t * p)3497c2fbfb3SApril Chin sumclose(Sum_t* p)
3507c2fbfb3SApril Chin {
3517c2fbfb3SApril Chin 	free(p);
3527c2fbfb3SApril Chin 	return 0;
3537c2fbfb3SApril Chin }
3547c2fbfb3SApril Chin 
3557c2fbfb3SApril Chin /*
3567c2fbfb3SApril Chin  * print the checksum method optget(3) usage on sp and return the length
3577c2fbfb3SApril Chin  */
3587c2fbfb3SApril Chin 
3597c2fbfb3SApril Chin int
sumusage(Sfio_t * sp)3607c2fbfb3SApril Chin sumusage(Sfio_t* sp)
3617c2fbfb3SApril Chin {
3627c2fbfb3SApril Chin 	register int	i;
3637c2fbfb3SApril Chin 	register int	n;
3647c2fbfb3SApril Chin 
3657c2fbfb3SApril Chin 	for (i = n = 0; i < elementsof(methods); i++)
3667c2fbfb3SApril Chin 	{
3677c2fbfb3SApril Chin 		n += sfprintf(sp, "[+%s?%s]", methods[i].match, methods[i].description);
3687c2fbfb3SApril Chin 		if (methods[i].options)
3697c2fbfb3SApril Chin 			n += sfprintf(sp, "{\n%s\n}", methods[i].options);
3707c2fbfb3SApril Chin 	}
3717c2fbfb3SApril Chin 	for (i = 0; i < elementsof(maps); i++)
3727c2fbfb3SApril Chin 		n += sfprintf(sp, "[+%s?%s Shorthand for \b%s\b.]", maps[i].match, maps[i].description, maps[i].map);
3737c2fbfb3SApril Chin 	return n;
3747c2fbfb3SApril Chin }