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