chiark / gitweb /
23cad209c11840758e99052d62198efd632bca04
[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 range VALUE START SIZE                => substring (or error)
17  *  hbytes overwrite VAR START VALUE
18  *  hbytes repeat VALUE COUNT                    => COUNT copies of VALUE
19  *
20  *  hbytes h2ulong HEX                           => ulong  (HEX len must be 4)
21  *  hbytes ulong2h UL                            => hex
22  *
23  *  ulong ul2bitfields VALUE [SIZE TYPE [TYPE-ARG...] ...]  => 0/1
24  *  ulong bitfields2ul BASE  [SIZE TYPE [TYPE-ARG...] ...]  => ULONG
25  *      goes from left (MSbit) to right (LSbit) where
26  *            SIZE is size in bits
27  *            TYPE [TYPE-ARGS...] is as below
28  *               zero
29  *               ignore
30  *               fixed ULONG-VALUE
31  *               uint VARNAME/VALUE         (VARNAME if ul2bitfields;
32  *               ulong VARNAME/VALUE         VALUE if bitfields2ul)
33  *
34  *  ulong ul2int ULONG    => INT            can fail if >INT_MAX
35  *  ulong int2ul INT      => ULONG          can fail if <0
36  *
37  *  hbytes shift l|r ULONG BITS             fails if BITS >32
38  *  hbytes mask A B                         => A & B
39  *
40  *  hbytes compare A B
41  *      =>  -2   A is lexically earlier than B and not a prefix of B  (A<B)
42  *          -1   A is prefix of B but not equal                       (A<B)
43  *           0   A == B
44  *          +1   A is B plus a nonempty suffix (ie, A has B as a prefix)
45  *          +2   A is lexically later than B and does not have B as a prefix
46  *
47  *  hbytes pkcs5 pa|ua VAR ALG                   => worked?  (always 1 for p)
48  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
49  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
50  *
51  *  hbytes hash ALG MESSAGE                      => hash
52  *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
53  *
54  * Refs: HMAC: RFC2104
55  */
56
57 #ifndef HBYTES_H
58 #define HBYTES_H
59
60 #include <assert.h>
61 #include <stdlib.h>
62 #include <errno.h>
63 #include <unistd.h>
64 #include <fcntl.h>
65 #include <sys/socket.h>
66 #include <sys/uio.h>
67 #include <sys/un.h>
68 #include <arpa/inet.h>
69
70 #include <tcl.h>
71
72 typedef unsigned char Byte;
73
74 /* from hbytes.c */
75
76 int Hbytes_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
77
78 /* Internal representation details: */
79 #define HBYTES_ISEMPTY(hb)    (!(hb)->begin_complex && !(hb)->end_0)
80 #define HBYTES_ISSENTINEL(hb) (!(hb)->begin_complex && (hb)->end_0)
81 #define HBYTES_ISSIMPLE(hb)   ((hb)->begin_complex && (hb)->end_0)
82 #define HBYTES_ISCOMPLEX(hb)  ((hb)->begin_complex && !(hb)->end_0)
83
84 typedef struct {
85   void *begin_complex, *end_0;
86 } HBytes_Value; /* overlays internalRep */
87
88 typedef struct {
89   Byte *dstart; /* always allocated dynamically */
90   int prespace, len, avail;
91   /*        
92    * | SPARE      | USED  | SPARE |
93    * |<-prespace->|<-len->|       |
94    * |            |<----avail---->|
95    *              ^start
96    */
97 } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */
98
99 /* Public interfaces: */
100
101 extern Tcl_ObjType hbytes_type;
102
103 int hbytes_len(const HBytes_Value *v);
104 Byte *hbytes_data(const HBytes_Value *v); /* caller may then modify data! */
105 int hbytes_issentinel(const HBytes_Value *v);
106
107 Byte *hbytes_prepend(HBytes_Value *upd, int el);
108 Byte *hbytes_append(HBytes_Value *upd, int el);
109   /* return value is where to put the data */
110
111 const Byte *hbytes_unprepend(HBytes_Value *upd, int rl);
112 const Byte *hbytes_unappend(HBytes_Value *upd, int rl);
113   /* return value points to the removed data, which remains valid
114    * until next op on the HBytes_Value.  If original value is
115    * shorter than rl or negative, returns 0 and does nothing. */
116
117 void hbytes_empty(HBytes_Value *returns);
118 void hbytes_sentinel(HBytes_Value *returns);
119 void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
120 Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
121 void hbytes_free(const HBytes_Value *frees);
122   /* _empty, _sentinel and _array do not free or read the old value;
123    * _free it first if needed.  _free leaves it garbage, so you
124    * have to call _empty to reuse it.  _arrayspace doesn't fill
125    * the array; you get a pointer and must fill it with data
126    * yourself. */
127
128 /* The value made by hbytes_sentinel should not be passed to
129  * anything except HBYTES_IS..., and hbytes_free. */
130
131 /* from sockaddr.c */
132
133 typedef struct {
134   Byte *begin, *end;
135 } SockAddr_Value;
136
137 extern Tcl_ObjType sockaddr_type;
138
139 void sockaddr_clear(SockAddr_Value*);
140 void sockaddr_create(SockAddr_Value*, const struct sockaddr *addr, int len);
141 int sockaddr_len(const SockAddr_Value*);
142 const struct sockaddr *sockaddr_addr(const SockAddr_Value*);
143 void sockaddr_free(const SockAddr_Value*);
144
145 /* from dgram.c */
146
147 extern Tcl_ObjType dgramsockid_type;
148 typedef struct DgramSocket *DgramSockID;
149
150 /* from hook.c */
151
152 int staticerr(Tcl_Interp *ip, const char *m);
153 int posixerr(Tcl_Interp *ip, int errnoval, const char *m);
154 void objfreeir(Tcl_Obj *o);
155 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
156
157 void obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
158 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
159                                 int l, const char *prefix);
160
161 void obj_updatestr_vstringls(Tcl_Obj *o, ...);
162   /* const char*, int, const char*, int, ..., (const char*)0 */
163 void obj_updatestr_string_len(Tcl_Obj *o, const char *str, int l);
164 void obj_updatestr_string(Tcl_Obj *o, const char *str);
165
166 /* from parse.c */
167
168 typedef struct {
169   HBytes_Value *hb;
170   Tcl_Obj *obj, *var;
171 } HBytes_Var;
172
173 void fini_hbv(Tcl_Interp *ip, int rc, HBytes_Var *agg);
174
175 /* from chop.c */
176   /* only do_... functions declared in tables.h */
177
178 /* from ulong.c */
179
180 Tcl_ObjType ulong_type;
181
182 /* from enum.c */
183
184 extern Tcl_ObjType enum_nearlytype;
185 extern Tcl_ObjType enum1_nearlytype;
186
187 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
188                                     const void *firstentry, size_t entrysize,
189                                     const char *what);
190 #define enum_lookup_cached(ip,o,table,what)                     \
191     (enum_lookup_cached_func((ip),(o),                          \
192                              &(table)[0],sizeof((table)[0]),    \
193                              (what)))
194   /* table should be a pointer to an array of structs of size
195    * entrysize, the first member of which should be a const char*.
196    * The table should finish with a null const char *.
197    * On error, 0 is returned and the ip->result will have been
198    * set to the error message.
199    */
200
201 int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
202                              const char *opts, const char *what);
203   /* -1 => error */
204
205 /* from crypto.c */
206
207 void memxor(Byte *dest, const Byte *src, int l);
208
209 typedef struct {
210   const char *name;
211   int pad, use_algname;
212 } PadMethod;
213
214 extern Tcl_ObjType blockcipherkey_type;
215
216 /* from algtables.c */
217
218 typedef struct {
219   const char *name;
220   int hashsize, blocksize, statesize;
221   void (*init)(void *state);
222   void (*update)(void *state, const Byte *data, int len);
223   void (*final)(void *state, Byte *digest);
224   void (*oneshot)(Byte *digest, const Byte *data, int len);
225 } HashAlgInfo;
226
227 extern const HashAlgInfo hashalginfos[];
228
229 typedef struct {
230   void (*make_schedule)(void *schedule, const Byte *key, int keylen);
231   void (*crypt)(const void *schedule, const void *in, void *out);
232      /* in and out may be the same, but if they aren't they may not overlap */
233      /* in and out for crypt will have been through block_byteswap */
234 } BlockCipherDirectionInfo;
235
236 typedef struct {
237   const char *name;
238   int blocksize, schedule_size, key_min, key_max;
239   void (*byteswap)(Byte *block);
240   BlockCipherDirectionInfo encrypt, decrypt;
241 } BlockCipherAlgInfo;
242
243 extern const BlockCipherAlgInfo blockcipheralginfos[];
244
245 /* from bcmode.c */
246
247 typedef struct {
248   const char *name;
249   int iv_blocks, buf_blocks;
250   const char *(*encrypt)(Byte *data, int blocks,
251                          const Byte *iv, Byte *buf,
252                          const BlockCipherAlgInfo *alg, int encr,
253                          int blocksize, const void *sch);
254   const char *(*decrypt)(Byte *data, int blocks,
255                          const Byte *iv, Byte *buf,
256                          const BlockCipherAlgInfo *alg, int encr,
257                          int blocksize, const void *sch);
258     /* in each case, *iv is provided, but may be modified */
259 } BlockCipherModeInfo;
260
261 extern const BlockCipherModeInfo blockciphermodeinfos[];
262
263 /* from misc.c */
264
265 int setnonblock(int fd, int isnonblock);
266
267 /* useful macros */
268
269 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
270 #define OBJ_SOCKADDR(o) ((SockAddr_Value*)&(o)->internalRep.twoPtrValue)
271
272 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
273 #define TFREE(f) (Tcl_Free((void*)(f)))
274
275 #endif /*HBYTES_H*/