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