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 #define _SUM_PRIVATE_ \
29 struct Method_s* method; \
30 uintmax_t total_count; \
31 uintmax_t total_size; \
39 #define SCALE(n,m) (((n)+(m)-1)/(m))
41 typedef struct Method_s
44 const char* description
;
46 Sum_t
* (*open
)(const struct Method_s
*, const char*);
48 int (*block
)(Sum_t
*, const void*, size_t);
49 int (*data
)(Sum_t
*, Sumdata_t
*);
50 int (*print
)(Sum_t
*, Sfio_t
*, int, size_t);
58 const char* description
;
63 * 16 and 32 bit common code
66 #define _INTEGRAL_PRIVATE_ \
70 typedef struct Integral_s
78 long_open(const Method_t
* method
, const char* name
)
82 if (p
= newof(0, Integral_t
, 1, 0))
84 p
->method
= (Method_t
*)method
;
93 ((Integral_t
*)p
)->sum
= 0;
100 register Integral_t
* x
= (Integral_t
*)p
;
102 x
->total_sum
^= (x
->sum
&= 0xffffffff);
109 register Integral_t
* x
= (Integral_t
*)p
;
111 x
->total_sum
^= (x
->sum
&= 0xffff);
116 long_print(Sum_t
* p
, Sfio_t
* sp
, register int flags
, size_t scale
)
118 register Integral_t
* x
= (Integral_t
*)p
;
120 register uintmax_t z
;
123 c
= (flags
& SUM_TOTAL
) ? x
->total_sum
: x
->sum
;
124 sfprintf(sp
, "%.*I*u", (flags
& SUM_LEGACY
) ? 5 : 1, sizeof(c
), c
);
125 if (flags
& SUM_SIZE
)
127 z
= (flags
& SUM_TOTAL
) ? x
->total_size
: x
->size
;
128 if ((flags
& SUM_SCALE
) && ((n
= scale
) || (n
= x
->method
->scale
)))
130 sfprintf(sp
, " %*I*u", (flags
& SUM_LEGACY
) ? 6 : 0, sizeof(z
), z
);
132 if (flags
& SUM_TOTAL
)
133 sfprintf(sp
, " %*I*u", (flags
& SUM_LEGACY
) ? 6 : 0, sizeof(x
->total_count
), x
->total_count
);
138 long_data(Sum_t
* p
, Sumdata_t
* data
)
140 register Integral_t
* x
= (Integral_t
*)p
;
142 data
->size
= sizeof(data
->num
);
148 #include "FEATURE/sum"
151 #include "sum-ast4.c"
154 #include "sum-prng.c"
156 #if _LIB_md && _lib_MD5Init && _hdr_md5 && _lib_SHA2Init && _hdr_sha2
163 #include "sum-sha1.c"
164 #include "sum-sha2.c"
169 * now the library interface
172 #undef METHOD /* solaris <sys/localedef.h>! */
173 #define METHOD(x) x##_match,x##_description,x##_options,x##_open,x##_init,x##_block,x##_data,x##_print,x##_done,x##_scale
175 static const Method_t methods
[] =
182 #ifdef md4_description
185 #ifdef md5_description
188 #ifdef sha1_description
191 #ifdef sha256_description
194 #ifdef sha384_description
197 #ifdef sha512_description
202 static const Map_t maps
[] =
205 "posix|cksum|std|standard",
206 "The posix 1003.2-1992 32 bit crc checksum. This is the"
207 " default \bcksum\b(1) method.",
208 "crc-0x04c11db7-rotate-done-size"
212 "The \bzip\b(1) crc.",
213 "crc-0xedb88320-init-done"
218 "crc-0xedb88320-size=0xcc55cc55"
222 "The Fowler-Noll-Vo 32 bit PRNG hash with non-zero"
223 " initializer (FNV-1).",
224 "prng-0x01000193-init=0x811c9dc5"
228 "The \bast\b \bstrsum\b(3) PRNG hash.",
229 "prng-0x63c63cd9-add=0x9c39c33d"
234 * simple alternation prefix match
238 match(register const char* s
, register const char* p
)
240 register const char* b
= s
;
246 if (*p
== '|' || *p
== 0)
248 } while (*s
++ == *p
++);
268 * open sum method name
272 sumopen(register const char* name
)
276 if (!name
|| !name
[0] || name
[0] == '-' && !name
[1])
278 for (n
= 0; n
< elementsof(maps
); n
++)
279 if (match(name
, maps
[n
].match
))
284 for (n
= 0; n
< elementsof(methods
); n
++)
285 if (match(name
, methods
[n
].match
))
286 return (*methods
[n
].open
)(&methods
[n
], name
);
291 * initialize for a new run of blocks
298 return (*p
->method
->init
)(p
);
302 * compute the running sum on buf
306 sumblock(Sum_t
* p
, const void* buf
, size_t siz
)
309 return (*p
->method
->block
)(p
, buf
, siz
);
313 * done with this run of blocks
320 p
->total_size
+= p
->size
;
321 return (*p
->method
->done
)(p
);
325 * print the sum [size] on sp
329 sumprint(Sum_t
* p
, Sfio_t
* sp
, int flags
, size_t scale
)
331 return (*p
->method
->print
)(p
, sp
, flags
, scale
);
335 * return the current sum (internal) data
339 sumdata(Sum_t
* p
, Sumdata_t
* d
)
341 return (*p
->method
->data
)(p
, d
);
345 * close an open sum handle
356 * print the checksum method optget(3) usage on sp and return the length
365 for (i
= n
= 0; i
< elementsof(methods
); i
++)
367 n
+= sfprintf(sp
, "[+%s?%s]", methods
[i
].match
, methods
[i
].description
);
368 if (methods
[i
].options
)
369 n
+= sfprintf(sp
, "{\n%s\n}", methods
[i
].options
);
371 for (i
= 0; i
< elementsof(maps
); i
++)
372 n
+= sfprintf(sp
, "[+%s?%s Shorthand for \b%s\b.]", maps
[i
].match
, maps
[i
].description
, maps
[i
].map
);