1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1996-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
19 ***********************************************************************/
25 * man this is sum library
28 static const char id
[] = "\n@(#)$Id: sumlib (AT&T Research) 2009-09-28 $\0\n";
30 #define _SUM_PRIVATE_ \
31 struct Method_s* method; \
32 uintmax_t total_count; \
33 uintmax_t total_size; \
41 #define SCALE(n,m) (((n)+(m)-1)/(m))
43 typedef struct Method_s
46 const char* description
;
48 Sum_t
* (*open
)(const struct Method_s
*, const char*);
50 int (*block
)(Sum_t
*, const void*, size_t);
51 int (*data
)(Sum_t
*, Sumdata_t
*);
52 int (*print
)(Sum_t
*, Sfio_t
*, int, size_t);
60 const char* description
;
65 * 16 and 32 bit common code
68 #define _INTEGRAL_PRIVATE_ \
72 typedef struct Integral_s
80 long_open(const Method_t
* method
, const char* name
)
84 if (p
= newof(0, Integral_t
, 1, 0))
86 p
->method
= (Method_t
*)method
;
95 ((Integral_t
*)p
)->sum
= 0;
102 register Integral_t
* x
= (Integral_t
*)p
;
104 x
->total_sum
^= (x
->sum
&= 0xffffffff);
111 register Integral_t
* x
= (Integral_t
*)p
;
113 x
->total_sum
^= (x
->sum
&= 0xffff);
118 long_print(Sum_t
* p
, Sfio_t
* sp
, register int flags
, size_t scale
)
120 register Integral_t
* x
= (Integral_t
*)p
;
122 register uintmax_t z
;
125 c
= (flags
& SUM_TOTAL
) ? x
->total_sum
: x
->sum
;
126 sfprintf(sp
, "%.*I*u", (flags
& SUM_LEGACY
) ? 5 : 1, sizeof(c
), c
);
127 if (flags
& SUM_SIZE
)
129 z
= (flags
& SUM_TOTAL
) ? x
->total_size
: x
->size
;
130 if ((flags
& SUM_SCALE
) && ((n
= scale
) || (n
= x
->method
->scale
)))
132 sfprintf(sp
, " %*I*u", (flags
& SUM_LEGACY
) ? 6 : 0, sizeof(z
), z
);
134 if (flags
& SUM_TOTAL
)
135 sfprintf(sp
, " %*I*u", (flags
& SUM_LEGACY
) ? 6 : 0, sizeof(x
->total_count
), x
->total_count
);
140 long_data(Sum_t
* p
, Sumdata_t
* data
)
142 register Integral_t
* x
= (Integral_t
*)p
;
144 data
->size
= sizeof(data
->num
);
150 #include "FEATURE/sum"
153 #include "sum-ast4.c"
156 #include "sum-prng.c"
158 #if _LIB_md && _lib_MD5Init && _hdr_md5 && _lib_SHA2Init && _hdr_sha2
165 #include "sum-sha1.c"
166 #include "sum-sha2.c"
171 * now the library interface
174 #undef METHOD /* solaris <sys/localedef.h>! */
175 #define METHOD(x) x##_match,x##_description,x##_options,x##_open,x##_init,x##_block,x##_data,x##_print,x##_done,x##_scale
177 static const Method_t methods
[] =
184 #ifdef md4_description
187 #ifdef md5_description
190 #ifdef sha1_description
193 #ifdef sha256_description
196 #ifdef sha384_description
199 #ifdef sha512_description
204 static const Map_t maps
[] =
207 "posix|cksum|std|standard",
208 "The posix 1003.2-1992 32 bit crc checksum. This is the"
209 " default \bcksum\b(1) method.",
210 "crc-0x04c11db7-rotate-done-size"
214 "The \bzip\b(1) crc.",
215 "crc-0xedb88320-init-done"
220 "crc-0xedb88320-size=0xcc55cc55"
224 "The Fowler-Noll-Vo 32 bit PRNG hash with non-zero"
225 " initializer (FNV-1).",
226 "prng-0x01000193-init=0x811c9dc5"
230 "The \bast\b \bstrsum\b(3) PRNG hash.",
231 "prng-0x63c63cd9-add=0x9c39c33d"
236 * simple alternation prefix match
240 match(register const char* s
, register const char* p
)
242 register const char* b
= s
;
248 if (*p
== '|' || *p
== 0)
250 } while (*s
++ == *p
++);
270 * open sum method name
274 sumopen(register const char* name
)
278 if (!name
|| !name
[0] || name
[0] == '-' && !name
[1])
280 for (n
= 0; n
< elementsof(maps
); n
++)
281 if (match(name
, maps
[n
].match
))
286 for (n
= 0; n
< elementsof(methods
); n
++)
287 if (match(name
, methods
[n
].match
))
288 return (*methods
[n
].open
)(&methods
[n
], name
);
293 * initialize for a new run of blocks
300 return (*p
->method
->init
)(p
);
304 * compute the running sum on buf
308 sumblock(Sum_t
* p
, const void* buf
, size_t siz
)
311 return (*p
->method
->block
)(p
, buf
, siz
);
315 * done with this run of blocks
322 p
->total_size
+= p
->size
;
323 return (*p
->method
->done
)(p
);
327 * print the sum [size] on sp
331 sumprint(Sum_t
* p
, Sfio_t
* sp
, int flags
, size_t scale
)
333 return (*p
->method
->print
)(p
, sp
, flags
, scale
);
337 * return the current sum (internal) data
341 sumdata(Sum_t
* p
, Sumdata_t
* d
)
343 return (*p
->method
->data
)(p
, d
);
347 * close an open sum handle
358 * print the checksum method optget(3) usage on sp and return the length
367 for (i
= n
= 0; i
< elementsof(methods
); i
++)
369 n
+= sfprintf(sp
, "[+%s?%s]", methods
[i
].match
, methods
[i
].description
);
370 if (methods
[i
].options
)
371 n
+= sfprintf(sp
, "{\n%s\n}", methods
[i
].options
);
373 for (i
= 0; i
< elementsof(maps
); i
++)
374 n
+= sfprintf(sp
, "[+%s?%s Shorthand for \b%s\b.]", maps
[i
].match
, maps
[i
].description
, maps
[i
].map
);