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
20 * hbytes zeroes COUNT => COUNT zero bytes
21 * hbytes random COUNT => COUNT random bytes
22 * hbytes xor VAR VALUE $VAR (+)= VALUE
24 * hbytes ushort2h LONG => LONG must be <2^16, returns as hex
25 * hbytes h2ushort HEX => |HEX| must be 2 bytes, returns as ulong
28 * => -2 A is lexically earlier than B and not a prefix of B (A<B)
29 * -1 A is prefix of B but not equal (A<B)
31 * +1 A is B plus a nonempty suffix (ie, A has B as a prefix)
32 * +2 A is lexically later than B and does not have B as a prefix
34 * hbytes pad pa|ua VAR ALG METH [METHARGS] => worked? (always 1 for p)
35 * hbytes pad pn|un VAR BS METH [METHARGS] => worked? (always 1 for p)
36 * hbytes pad pa|pn VAR ALG|BS pkcs5 => 1
37 * hbytes pad ua|un VAR ALG|BS pkcs5 => worked?
38 * hbytes pad pa|pn VAR ALG|BS rfc2406 NXTHDR => 1
39 * hbytes pad ua|un VAR ALG|BS rfc2406 NXTHDRVAR => worked?
41 * hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
42 * hbytes blockcipher mac MSG ALG KEY MODE IV => final block
43 * hbytes blockcipher prop PROPERTY ALG => property value
45 * hbytes hash ALG MESSAGE => hash
46 * hbytes hmac ALG MESSAGE KEY [MACLENGTH] => mac
47 * hbytes hash-prop PROPERTY ALG => property value
49 * ulong ul2int ULONG => INT can fail if >INT_MAX
50 * ulong int2ul INT => ULONG can fail if <0
51 * ulong mask A B => A & B
52 * ulong add A B => A + B (mod 2^32)
53 * ulong subtract A B => A - B (mod 2^32)
54 * ulong compare A B => 0 -1 (A<B) +1 (A>B)
55 * ulong shift l|r ULONG BITS fails if BITS >32
57 * ulong ul2bitfields VALUE [SIZE TYPE [TYPE-ARG...] ...] => 0/1
58 * ulong bitfields2ul BASE [SIZE TYPE [TYPE-ARG...] ...] => ULONG
59 * goes from left (MSbit) to right (LSbit) where
60 * SIZE is size in bits
61 * TYPE [TYPE-ARGS...] is as below
65 * uint VARNAME/VALUE (VARNAME if ul2bitfields;
66 * ulong VARNAME/VALUE VALUE if bitfields2ul)
68 * Address ranges (addrmap.c):
70 * An address range is a slightly efficient partial mapping from
71 * addresses to arbitrary data values. An address is a number of
72 * octets expressed as an hbytes. All the addresses covered by the
73 * same addrmap should have the same length.
75 * hbytes addr-map lookup MAP-VAR ADDRESS [DEFAULT] => DATA
76 * Error on missing default or if any prefix longer than ADDRESS.
78 * hbytes addr-map amend-range MAP-VAR START END DATA
79 * hbytes addr-map amend-mask MAP-VAR PREFIX PREFIX-LENGTH DATA
80 * Sets all of the addresses in PREFIX/PREFIX-LENGTH to the
84 * An address map MAP is
86 * [list START END DATA-VALUE] \
87 * [list START' END' DATA-VALUE'] \
90 * The list is sorted by ascending START and entries do not overlap.
91 * START and END are both inclusive. BIT-LENGTH is in usual Tcl
92 * integer notation and must be a multiple of 8.
96 * HBYTES BLOCKCIPHER CRYPTFAIL CRYPT block cipher mode failed somehow (!)
97 * HBYTES BLOCKCIPHER CRYPTFAIL MAC HMAC failed somehow (!)
98 * HBYTES BLOCKCIPHER LENGTH block cipher input has unsuitable length
99 * HBYTES BLOCKCIPHER PARAMS key or iv not suitable
100 * HBYTES HMAC PARAMS key, input or output size not suitable
101 * HBYTES LENGTH OVERRUN block too long
102 * HBYTES LENGTH RANGE input length or offset is -ve or silly
103 * HBYTES LENGTH UNDERRUN block too short (or offset too big)
104 * HBYTES LENGTH MISMATCH when blocks must be exactly same length
105 * HBYTES SYNTAX supposed hex block had wrong syntax
106 * HBYTES VALUE OVERFLOW value to be conv'd to hex too big/long
107 * HBYTES ADDRMAP NOMATCH no addr/mask matches address for lookup
108 * HBYTES ADDRMAP UNDERRUN addr for lookup or amend is too short
109 * HBYTES ADDRMAP OVERRUN addr for lookup or amend is too long
110 * HBYTES ADDRMAP EXCLBITS amend-mask 1-bits outside prefix len
111 * HBYTES ADDRMAP BADRANGE amend-range start > end
112 * HBYTES ADDRMAP VALUE addr-map string value is erroneous
113 * SOCKADDR AFUNIX LENGTH path for AF_UNIX socket too long
114 * SOCKADDR SYNTAX IPV4 bad IPv4 socket address &/or port
115 * SOCKADDR SYNTAX OTHER bad socket addr, couldn't tell what kind
116 * ULONG BITCOUNT NEGATIVE -ve bitcount specified where not allowed
117 * ULONG BITCOUNT OVERRUN attempt to use more than 32 bits
118 * ULONG BITCOUNT UNDERRUN bitfields add up to less than 32
119 * ULONG VALUE NEGATIVE attempt convert -ve integers to ulong
120 * ULONG VALUE OVERFLOW converted value does not fit in result
121 * TUNTAP IFNAME LENGTH tun/tap interface name too long
122 * TUNTAP MTU OVERRUN tun/tap mtu limited to 2^16 bytes
124 * Refs: HMAC: RFC2104 */
134 #include <sys/socket.h>
137 #include <arpa/inet.h>
140 #include "chiark-tcl.h"
144 int Hbytes_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
146 /* Internal representation details: */
147 #define HBYTES_ISEMPTY(hb) (!(hb)->begin_complex && !(hb)->end_0)
148 #define HBYTES_ISSENTINEL(hb) (!(hb)->begin_complex && (hb)->end_0)
149 #define HBYTES_ISSIMPLE(hb) ((hb)->begin_complex && (hb)->end_0)
150 #define HBYTES_ISCOMPLEX(hb) ((hb)->begin_complex && !(hb)->end_0)
153 void *begin_complex, *end_0;
154 } HBytes_Value; /* overlays internalRep */
157 Byte *dstart; /* always allocated dynamically */
158 int prespace, len, avail;
160 * | SPARE | USED | SPARE |
161 * |<-prespace->|<-len->| |
162 * | |<----avail---->|
165 } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */
167 void memxor(Byte *dest, const Byte *src, int l);
169 /* Public interfaces: */
171 extern Tcl_ObjType cht_hbytes_type;
173 int cht_hb_len(const HBytes_Value *v);
174 Byte *cht_hb_data(const HBytes_Value *v); /* caller may then modify data! */
175 int cht_hb_issentinel(const HBytes_Value *v);
177 Byte *cht_hb_prepend(HBytes_Value *upd, int el);
178 Byte *cht_hb_append(HBytes_Value *upd, int el);
179 /* return value is where to put the data */
181 const Byte *cht_hb_unprepend(HBytes_Value *upd, int rl);
182 const Byte *cht_hb_unappend(HBytes_Value *upd, int rl);
183 /* return value points to the removed data, which remains valid
184 * until next op on the HBytes_Value. If original value is
185 * shorter than rl or negative, returns 0 and does nothing. */
187 void cht_hb_empty(HBytes_Value *returns);
188 void cht_hb_sentinel(HBytes_Value *returns);
189 void cht_hb_array(HBytes_Value *returns, const Byte *array, int l);
190 Byte *cht_hb_arrayspace(HBytes_Value *returns, int l);
191 void cht_hb_free(const HBytes_Value *frees);
192 /* _empty, _sentinel and _array do not free or read the old value;
193 * _free it first if needed. _free leaves it garbage, so you
194 * have to call _empty to reuse it. _arrayspace doesn't fill
195 * the array; you get a pointer and must fill it with data
198 /* The value made by cht_hb_sentinel should not be passed to
199 * anything except HBYTES_IS..., and cht_hb_free. */
203 void obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
204 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
205 int l, const char *prefix);
216 typedef struct AddrMap_Value AddrMap_Value;
223 extern Tcl_ObjType cht_addrmap_type;
226 /* only do_... functions declared in tables.h */
230 Tcl_ObjType cht_ulong_type;
234 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
235 #define OBJ_SOCKADDR(o) ((SockAddr_Value*)&(o)->internalRep.twoPtrValue)