4 * hbytes raw2h BINARY => hex
5 * hbytes h2raw HEX => binary
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)
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
21 * hbytes ushort2h LONG => LONG must be <2^16, returns as hex
22 * hbytes h2ushort HEX => |HEX| must be 2 bytes, returns as ulong
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)
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
31 * hbytes pad pa|ua VAR ALG METH [METHARGS] => worked? (always 1 for p)
32 * hbytes pad pn|un VAR BS METH [METHARGS] => worked? (always 1 for p)
33 * hbytes pad pa|pn VAR ALG|BS pkcs5 => 1
34 * hbytes pad ua|un VAR ALG|BS pkcs5 => worked?
35 * hbytes pad pa|pn VAR ALG|BS rfc2406 NXTHDR => 1
36 * hbytes pad ua|un VAR ALG|BS rfc2406 NXTHDRVAR => worked?
38 * hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
39 * hbytes blockcipher mac MSG ALG KEY MODE IV => final block
40 * hbytes blockcipher prop PROPERTY ALG => property value
42 * hbytes hash ALG MESSAGE => hash
43 * hbytes hmac ALG MESSAGE KEY [MACLENGTH] => mac
44 * hbytes hash-prop PROPERTY ALG => property value
46 * ulong ul2int ULONG => INT can fail if >INT_MAX
47 * ulong int2ul INT => ULONG can fail if <0
48 * ulong mask A B => A & B
49 * ulong add A B => A + B (mod 2^32)
50 * ulong subtract A B => A - B (mod 2^32)
51 * ulong compare A B => 0 -1 (A<B) +1 (A>B)
52 * ulong shift l|r ULONG BITS fails if BITS >32
54 * ulong ul2bitfields VALUE [SIZE TYPE [TYPE-ARG...] ...] => 0/1
55 * ulong bitfields2ul BASE [SIZE TYPE [TYPE-ARG...] ...] => ULONG
56 * goes from left (MSbit) to right (LSbit) where
57 * SIZE is size in bits
58 * TYPE [TYPE-ARGS...] is as below
62 * uint VARNAME/VALUE (VARNAME if ul2bitfields;
63 * ulong VARNAME/VALUE VALUE if bitfields2ul)
67 * HBYTES BLOCKCIPHER CRYPTFAIL CRYPT block cipher mode failed somehow (!)
68 * HBYTES BLOCKCIPHER CRYPTFAIL MAC HMAC failed somehow (!)
69 * HBYTES BLOCKCIPHER LENGTH block cipher input has unsuitable length
70 * HBYTES BLOCKCIPHER PARAMS key or iv not suitable
71 * HBYTES HMAC PARAMS key, input or output size not suitable
72 * HBYTES LENGTH OVERRUN block too long
73 * HBYTES LENGTH RANGE input length or offset is -ve or silly
74 * HBYTES LENGTH UNDERRUN block too short (or offset too big)
75 * HBYTES LENGTH MISMATCH when blocks must be exactly same length
76 * HBYTES SYNTAX supposed hex block had wrong syntax
77 * HBYTES VALUE OVERFLOW value to be conv'd to hex too big/long
78 * SOCKADDR AFUNIX LENGTH path for AF_UNIX socket too long
79 * SOCKADDR SYNTAX IPV4 bad IPv4 socket address &/or port
80 * SOCKADDR SYNTAX OTHER bad socket addr, couldn't tell what kind
81 * ULONG BITCOUNT NEGATIVE -ve bitcount specified where not allowed
82 * ULONG BITCOUNT OVERRUN attempt to use more than 32 bits
83 * ULONG BITCOUNT UNDERRUN bitfields add up to less than 32
84 * ULONG VALUE NEGATIVE attempt convert -ve integers to ulong
85 * ULONG VALUE OVERFLOW converted value does not fit in result
98 #include <sys/socket.h>
101 #include <arpa/inet.h>
105 typedef unsigned char Byte;
109 int Hbytes_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
111 /* Internal representation details: */
112 #define HBYTES_ISEMPTY(hb) (!(hb)->begin_complex && !(hb)->end_0)
113 #define HBYTES_ISSENTINEL(hb) (!(hb)->begin_complex && (hb)->end_0)
114 #define HBYTES_ISSIMPLE(hb) ((hb)->begin_complex && (hb)->end_0)
115 #define HBYTES_ISCOMPLEX(hb) ((hb)->begin_complex && !(hb)->end_0)
118 void *begin_complex, *end_0;
119 } HBytes_Value; /* overlays internalRep */
122 Byte *dstart; /* always allocated dynamically */
123 int prespace, len, avail;
125 * | SPARE | USED | SPARE |
126 * |<-prespace->|<-len->| |
127 * | |<----avail---->|
130 } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */
132 /* Public interfaces: */
134 extern Tcl_ObjType hbytes_type;
136 int hbytes_len(const HBytes_Value *v);
137 Byte *hbytes_data(const HBytes_Value *v); /* caller may then modify data! */
138 int hbytes_issentinel(const HBytes_Value *v);
140 Byte *hbytes_prepend(HBytes_Value *upd, int el);
141 Byte *hbytes_append(HBytes_Value *upd, int el);
142 /* return value is where to put the data */
144 const Byte *hbytes_unprepend(HBytes_Value *upd, int rl);
145 const Byte *hbytes_unappend(HBytes_Value *upd, int rl);
146 /* return value points to the removed data, which remains valid
147 * until next op on the HBytes_Value. If original value is
148 * shorter than rl or negative, returns 0 and does nothing. */
150 void hbytes_empty(HBytes_Value *returns);
151 void hbytes_sentinel(HBytes_Value *returns);
152 void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
153 Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
154 void hbytes_free(const HBytes_Value *frees);
155 /* _empty, _sentinel and _array do not free or read the old value;
156 * _free it first if needed. _free leaves it garbage, so you
157 * have to call _empty to reuse it. _arrayspace doesn't fill
158 * the array; you get a pointer and must fill it with data
161 /* The value made by hbytes_sentinel should not be passed to
162 * anything except HBYTES_IS..., and hbytes_free. */
164 /* from sockaddr.c */
170 extern Tcl_ObjType sockaddr_type;
172 void sockaddr_clear(SockAddr_Value*);
173 void sockaddr_create(SockAddr_Value*, const struct sockaddr *addr, int len);
174 int sockaddr_len(const SockAddr_Value*);
175 const struct sockaddr *sockaddr_addr(const SockAddr_Value*);
176 void sockaddr_free(const SockAddr_Value*);
181 const char *const prefix;
186 extern Tcl_ObjType tabledataid_nearlytype;
187 int tabledataid_parse(Tcl_Interp *ip, Tcl_Obj *o, IdDataTable *tab);
191 extern IdDataTable dgram_socks;
192 int newfdposixerr(Tcl_Interp *ip, int fd, const char *m);
196 extern IdDataTable tuntap_socks;
200 int staticerr(Tcl_Interp *ip, const char *m, const char *ec);
201 int posixerr(Tcl_Interp *ip, int errnoval, const char *m);
202 void objfreeir(Tcl_Obj *o);
203 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l);
205 void obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
206 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
207 int l, const char *prefix);
209 void obj_updatestr_vstringls(Tcl_Obj *o, ...);
210 /* const char*, int, const char*, int, ..., (const char*)0 */
211 void obj_updatestr_string_len(Tcl_Obj *o, const char *str, int l);
212 void obj_updatestr_string(Tcl_Obj *o, const char *str);
222 void fini_hbv(Tcl_Interp *ip, int rc, HBytes_Var *agg);
225 /* only do_... functions declared in tables.h */
229 Tcl_ObjType ulong_type;
233 extern Tcl_ObjType enum_nearlytype;
234 extern Tcl_ObjType enum1_nearlytype;
236 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
237 const void *firstentry, size_t entrysize,
239 #define enum_lookup_cached(ip,o,table,what) \
240 (enum_lookup_cached_func((ip),(o), \
241 &(table)[0],sizeof((table)[0]), \
243 /* table should be a pointer to an array of structs of size
244 * entrysize, the first member of which should be a const char*.
245 * The table should finish with a null const char *.
246 * On error, 0 is returned and the ip->result will have been
247 * set to the error message.
250 int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
251 const char *opts, const char *what);
256 void memxor(Byte *dest, const Byte *src, int l);
260 int pad, use_algname;
263 extern Tcl_ObjType blockcipherkey_type;
265 /* from algtables.c */
270 } BlockCipherPropInfo, HashAlgPropInfo;
274 int hashsize, blocksize, statesize;
275 void (*init)(void *state);
276 void (*update)(void *state, const void *data, int len);
277 void (*final)(void *state, void *digest);
278 void (*oneshot)(void *digest, const void *data, int len);
281 extern const HashAlgInfo hashalginfos[];
284 void (*make_schedule)(void *schedule, const void *key, int keylen);
285 void (*crypt)(const void *schedule, const void *in, void *out);
286 /* in and out may be the same, but if they aren't they may not overlap */
287 /* in and out for crypt will have been through block_byteswap */
288 } BlockCipherPerDirectionInfo;
292 int blocksize, schedule_size, key_min, key_max;
293 BlockCipherPerDirectionInfo encrypt, decrypt;
294 } BlockCipherAlgInfo;
296 extern const BlockCipherAlgInfo blockcipheralginfos[];
302 int iv_blocks, buf_blocks, mac_blocks;
304 /* Each function is allowed to use up to buf_blocks * blocksize
305 * bytes of space in buf. data is blocks * blocksize bytes
306 * long. data should be modified in place by encrypt and decrypt;
307 * modes may not change the size of data. iv is always provided and
308 * is always of length iv_blocks * blocksize; encrypt and
309 * decrypt may modify the iv value (in which case the Tcl caller
310 * will get the modified IV) but this is not recommended. mac
311 * should leave the mac, which must be mac_blocks * blocksize
312 * bytes, in buf. (Therefore mac_blocks must be at least
315 const char *(*encrypt)(Byte *data, int nblocks,
316 const Byte *iv, Byte *buf,
317 const BlockCipherAlgInfo *alg, int encr,
319 const char *(*decrypt)(Byte *data, int nblocks,
320 const Byte *iv, Byte *buf,
321 const BlockCipherAlgInfo *alg, int encr,
323 const char *(*mac)(const Byte *data, int nblocks,
324 const Byte *iv, Byte *buf,
325 const BlockCipherAlgInfo *alg,
327 } BlockCipherModeInfo;
329 extern const BlockCipherModeInfo blockciphermodeinfos[];
333 int setnonblock(int fd, int isnonblock);
337 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
338 #define OBJ_SOCKADDR(o) ((SockAddr_Value*)&(o)->internalRep.twoPtrValue)
340 #define TALLOC(s) ((void*)Tcl_Alloc((s)))
341 #define TFREE(f) (Tcl_Free((void*)(f)))