1 #pragma prototyped
2
3 /*
4 * checksum filter
5 */
6
7 #include <codex.h>
8 #include <sum.h>
9
10 typedef struct State_s
11 {
12 Codex_t codex;
13
14 Sum_t* sum;
15 } State_t;
16
17 static int
sum_options(Codexmeth_t * meth,Sfio_t * sp)18 sum_options(Codexmeth_t* meth, Sfio_t* sp)
19 {
20 return sumusage(sp) >= 0 ? 0 : -1;
21 }
22
23 static int
sum_open(Codex_t * p,char * const args[],Codexnum_t flags)24 sum_open(Codex_t* p, char* const args[], Codexnum_t flags)
25 {
26 register State_t* state;
27 register Sum_t* sum;
28 register const char* s;
29
30 s = args[0];
31 if (args[2])
32 while (*s && *s++ != '-');
33 if (!(sum = sumopen(s)))
34 {
35 if (p->disc->errorf)
36 (*p->disc->errorf)(NiL, p->disc, 2, "%s: unknown method", args[0]);
37 return -1;
38 }
39 if (!(state = newof(0, State_t, 1, 0)))
40 {
41 if (p->disc->errorf)
42 (*p->disc->errorf)(NiL, p->disc, 2, "out of space");
43 return -1;
44 }
45 state->sum = sum;
46 p->data = state;
47 return 0;
48 }
49
50 static int
sum_init(Codex_t * p)51 sum_init(Codex_t* p)
52 {
53 return suminit(((State_t*)p->data)->sum);
54 }
55
56 static int
sum_close(Codex_t * p)57 sum_close(Codex_t* p)
58 {
59 return sumclose(((State_t*)p->data)->sum);
60 }
61
62 static ssize_t
sum_read(Sfio_t * sp,void * buf,size_t n,Sfdisc_t * disc)63 sum_read(Sfio_t* sp, void* buf, size_t n, Sfdisc_t* disc)
64 {
65 ssize_t r;
66
67 if ((r = sfrd(sp, buf, n, disc)) > 0)
68 sumblock(((State_t*)CODEX(disc)->data)->sum, buf, r);
69 return r;
70 }
71
72 static ssize_t
sum_write(Sfio_t * sp,const void * buf,size_t n,Sfdisc_t * disc)73 sum_write(Sfio_t* sp, const void* buf, size_t n, Sfdisc_t* disc)
74 {
75 ssize_t r;
76
77 if ((r = sfwr(sp, buf, n, disc)) > 0)
78 sumblock(((State_t*)CODEX(disc)->data)->sum, buf, r);
79 return r;
80 }
81
82 static int
sum_data(Codex_t * p,Codexdata_t * data)83 sum_data(Codex_t* p, Codexdata_t* data)
84 {
85 sumdone(((State_t*)p->data)->sum);
86 return sumdata(((State_t*)p->data)->sum, (Sumdata_t*)data);
87 }
88
89 Codexmeth_t codex_sum =
90 {
91 "sum",
92 "checksum filter. The checksum value returned by \bcodexdata\b(3) is"
93 " written to the standard error.",
94 "[+(version)?codex-sum (AT&T Research) 2003-12-16]"
95 "[+(author)?Glenn Fowler <gsf@research.att.com>]",
96 CODEX_DECODE|CODEX_ENCODE|CODEX_SUM,
97 sum_options,
98 0,
99 sum_open,
100 0,
101 sum_init,
102 0,
103 sum_read,
104 sum_write,
105 sum_init,
106 0,
107 sum_data,
108 0,
109 0,
110 CODEXNEXT(sum)
111 };
112
113 CODEXLIB(sum)
114