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