chiark / gitweb /
951ae12ee3e5d28889ceacab9bb84dcf15ef89a3
[chiark-tcl.git] / base / chiark-tcl.h
1 /*
2  */
3 /*
4  *  hbytes raw2h BINARY                          => hex
5  *  hbytes h2raw HEX                             => binary
6  *
7  *  hbytes length VALUE                          => count
8  *  hbytes prepend VAR [VALUE ...]         = set VAR [concat VALUE ... $VAR]
9  *  hbytes append VAR [VALUE ...]          = set VAR [concat $VAR VALUE ...]
10  *  hbytes concat VAR [VALUE ...]          = set VAR [concat VALUE ...]
11  *  hbytes unprepend VAR PREFIXLENGTH            => prefix (removed from VAR)
12  *  hbytes unappend VAR SUFFIXLENGTH             => suffix (removed from VAR)
13  *  hbytes chopto VAR NEWVARLENGTH               => suffix (removed from VAR)
14  *                                                  (too short? error)
15  *
16  *  hbytes pkcs5 pa|ua VAR ALG                   => worked?  (always 1 for p)
17  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
18  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
19  *
20  *  hbytes hash ALG MESSAGE                      => hash
21  *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
22  *
23  * Refs: HMAC: RFC2104
24  */
25
26 #ifndef HBYTES_H
27 #define HBYTES_H
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <arpa/inet.h>
37
38 #include <tcl.h>
39
40 typedef unsigned char Byte;
41
42 /* from hbytes.c */
43
44 int Hbytes_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
45
46 /* Internal representation details: */
47 #define HBYTES_ISEMPTY(hb)    (!(hb)->begin_complex && !(hb)->end_0)
48 #define HBYTES_ISSENTINEL(hb) (!(hb)->begin_complex && (hb)->end_0)
49 #define HBYTES_ISSIMPLE(hb)   ((hb)->begin_complex && (hb)->end_0)
50 #define HBYTES_ISCOMPLEX(hb)  ((hb)->begin_complex && !(hb)->end_0)
51
52 typedef struct {
53   void *begin_complex, *end_0;
54 } HBytes_Value; /* overlays internalRep */
55
56 typedef struct {
57   Byte *dstart; /* always allocated dynamically */
58   int prespace, len, avail;
59   /*        
60    * | SPARE      | USED  | SPARE |
61    * |<-prespace->|<-len->|       |
62    * |            |<----avail---->|
63    *              ^start
64    */
65 } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */
66
67 /* Public interfaces: */
68
69 extern Tcl_ObjType hbytes_type;
70
71 int hbytes_len(const HBytes_Value *v);
72 Byte *hbytes_data(const HBytes_Value *v); /* caller may then modify data! */
73 int hbytes_issentinel(const HBytes_Value *v);
74
75 Byte *hbytes_prepend(HBytes_Value *upd, int el);
76 Byte *hbytes_append(HBytes_Value *upd, int el);
77   /* return value is where to put the data */
78
79 const Byte *hbytes_unprepend(HBytes_Value *upd, int rl);
80 const Byte *hbytes_unappend(HBytes_Value *upd, int rl);
81   /* return value points to the removed data, which remains valid
82    * until next op on the HBytes_Value.  If original value is
83    * shorter than rl or negative, returns 0 and does nothing. */
84
85 void hbytes_empty(HBytes_Value *returns);
86 void hbytes_sentinel(HBytes_Value *returns);
87 void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
88 Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
89 void hbytes_free(const HBytes_Value *frees);
90   /* _empty, _sentinel and _array do not free or read the old value;
91    * _free it first if needed.  _free leaves it garbage, so you
92    * have to call _empty to reuse it.  _arrayspace doesn't fill
93    * the array; you get a pointer and must fill it with data
94    * yourself. */
95
96 /* The value made by hbytes_sentinel should not be passed to
97  * anything except HBYTES_IS..., and hbytes_free. */
98
99 /* from sockaddr.c */
100
101 typedef struct {
102   Byte *begin, *end;
103 } SockAddr_Value;
104
105 extern Tcl_ObjType sockaddr_type;
106
107 void sockaddr_clear(SockAddr_Value*);
108 void sockaddr_create(SockAddr_Value*, const struct sockaddr *addr, int len);
109 int sockaddr_len(const SockAddr_Value*);
110 const struct sockaddr *sockaddr_addr(const SockAddr_Value*);
111 void sockaddr_free(const SockAddr_Value*);
112
113 /* from dgram.c */
114
115 extern Tcl_ObjType sockid_type;
116
117 /* from hook.c */
118
119 int staticerr(Tcl_Interp *ip, const char *m);
120 int posixerr(Tcl_Interp *ip, int errnoval, const char *m);
121 void objfreeir(Tcl_Obj *o);
122 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
123
124 void obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
125 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
126                                 int l, const char *prefix);
127
128 /* from parse.c */
129
130 typedef struct {
131   HBytes_Value *hb;
132   Tcl_Obj *obj, *var;
133 } HBytes_Var;
134
135 void fini_hbv(Tcl_Interp *ip, int rc, HBytes_Var *agg);
136
137 /* from chop.c */
138
139 /* from enum.c */
140
141 extern Tcl_ObjType enum_nearlytype;
142 extern Tcl_ObjType enum1_nearlytype;
143
144 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
145                                     const void *firstentry, size_t entrysize,
146                                     const char *what);
147 #define enum_lookup_cached(ip,o,table,what)                     \
148     (enum_lookup_cached_func((ip),(o),                          \
149                              &(table)[0],sizeof((table)[0]),    \
150                              (what)))
151   /* table should be a pointer to an array of structs of size
152    * entrysize, the first member of which should be a const char*.
153    * The table should finish with a null const char *.
154    * On error, 0 is returned and the ip->result will have been
155    * set to the error message.
156    */
157
158 int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
159                              const char *opts, const char *what);
160   /* -1 => error */
161
162 /* from crypto.c */
163
164 void memxor(Byte *dest, const Byte *src, int l);
165
166 typedef struct {
167   const char *name;
168   int pad, use_algname;
169 } PadMethod;
170
171 Tcl_ObjType blockcipherkey_type;
172
173 /* from algtables.c */
174
175 typedef struct {
176   const char *name;
177   int hashsize, blocksize, statesize;
178   void (*init)(void *state);
179   void (*update)(void *state, const Byte *data, int len);
180   void (*final)(void *state, Byte *digest);
181   void (*oneshot)(Byte *digest, const Byte *data, int len);
182 } HashAlgInfo;
183
184 extern const HashAlgInfo hashalginfos[];
185
186 typedef struct {
187   void (*make_schedule)(void *schedule, const Byte *key, int keylen);
188   void (*crypt)(const void *schedule, const void *in, void *out);
189      /* in and out may be the same, but if they aren't they may not overlap */
190      /* in and out for crypt will have been through block_byteswap */
191 } BlockCipherDirectionInfo;
192
193 typedef struct {
194   const char *name;
195   int blocksize, schedule_size, key_min, key_max;
196   void (*byteswap)(Byte *block);
197   BlockCipherDirectionInfo encrypt, decrypt;
198 } BlockCipherAlgInfo;
199
200 extern const BlockCipherAlgInfo blockcipheralginfos[];
201
202 /* from bcmode.c */
203
204 typedef struct {
205   const char *name;
206   int iv_blocks, buf_blocks;
207   const char *(*encrypt)(Byte *data, int blocks,
208                          const Byte *iv, Byte *buf,
209                          const BlockCipherAlgInfo *alg, int encr,
210                          int blocksize, const void *sch);
211   const char *(*decrypt)(Byte *data, int blocks,
212                          const Byte *iv, Byte *buf,
213                          const BlockCipherAlgInfo *alg, int encr,
214                          int blocksize, const void *sch);
215     /* in each case, *iv is provided, but may be modified */
216 } BlockCipherModeInfo;
217
218 extern const BlockCipherModeInfo blockciphermodeinfos[];
219
220 /* from misc.c */
221
222 int setnonblock(int fd, int isnonblock);
223
224 /* useful macros */
225
226 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
227 #define OBJ_SOCKADDR(o) ((SockAddr_Value*)&(o)->internalRep.twoPtrValue)
228
229 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
230 #define TFREE(f) (Tcl_Free((void*)(f)))
231
232 #endif /*HBYTES_H*/