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