chiark / gitweb /
"quick" rw table entry extra value
[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  *  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
23  *
24  *  hbytes ushort2h LONG           => LONG must be <2^16, returns as hex
25  *  hbytes h2ushort HEX            => |HEX| must be 2 bytes, returns as ulong
26  *
27  *  hbytes compare A B
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)
30  *           0   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
33  *
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?
40  *
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
44  *
45  *  hbytes hash ALG MESSAGE                      => hash
46  *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
47  *  hbytes hash-prop PROPERTY ALG                => property value
48  *
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
56  *
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
62  *               zero
63  *               ignore
64  *               fixed ULONG-VALUE
65  *               uint VARNAME/VALUE         (VARNAME if ul2bitfields;
66  *               ulong VARNAME/VALUE         VALUE if bitfields2ul)
67  *
68  * Address ranges (addrmap.c):
69  *
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.
74  *
75  *  hbytes addr-map lookup MAP-VAR ADDRESS [DEFAULT]   => DATA
76  *     Error on missing default or if any prefix longer than ADDRESS.
77  *
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
81  *     relevant value.
82  *
83  *  Representation:
84  *     An address map MAP is
85  *           [list BIT-LENGTH                           \
86  *                 [list START END DATA-VALUE]          \
87  *                 [list START' END' DATA-VALUE']       \
88  *                 ...
89  *            ]
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.
93  *
94  * Error codes
95  *
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
123  *
124  * Refs: HMAC: RFC2104 */
125
126 #ifndef HBYTES_H
127 #define HBYTES_H
128
129 #include <assert.h>
130 #include <stdlib.h>
131 #include <errno.h>
132 #include <unistd.h>
133 #include <fcntl.h>
134 #include <sys/socket.h>
135 #include <sys/uio.h>
136 #include <sys/un.h>
137 #include <arpa/inet.h>
138 #include <string.h>
139
140 #include "chiark-tcl.h"
141
142 /* from hbytes.c */
143
144 int Hbytes_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
145
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)
151
152 typedef struct {
153   void *begin_complex, *end_0;
154 } HBytes_Value; /* overlays internalRep */
155
156 typedef struct {
157   Byte *dstart; /* always allocated dynamically */
158   int prespace, len, avail;
159   /*        
160    * | SPARE      | USED  | SPARE |
161    * |<-prespace->|<-len->|       |
162    * |            |<----avail---->|
163    *              ^start
164    */
165 } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */
166
167 void memxor(Byte *dest, const Byte *src, int l);
168 extern int Chiark_tcl_hbytes_Init(Tcl_Interp *ip);
169   /* called by load(3tcl) and also by extensions which depend on this one */
170
171 /* Public interfaces: */
172
173 extern Tcl_ObjType cht_hbytes_type;
174
175 int cht_hb_len(const HBytes_Value *v);
176 Byte *cht_hb_data(const HBytes_Value *v); /* caller may then modify data! */
177 int cht_hb_issentinel(const HBytes_Value *v);
178
179 Byte *cht_hb_prepend(HBytes_Value *upd, int el);
180 Byte *cht_hb_append(HBytes_Value *upd, int el);
181   /* return value is where to put the data */
182
183 const Byte *cht_hb_unprepend(HBytes_Value *upd, int rl);
184 const Byte *cht_hb_unappend(HBytes_Value *upd, int rl);
185   /* return value points to the removed data, which remains valid
186    * until next op on the HBytes_Value.  If original value is
187    * shorter than rl or negative, returns 0 and does nothing. */
188
189 void cht_hb_empty(HBytes_Value *returns);
190 void cht_hb_sentinel(HBytes_Value *returns);
191 void cht_hb_array(HBytes_Value *returns, const Byte *array, int l);
192 Byte *cht_hb_arrayspace(HBytes_Value *returns, int l);
193 void cht_hb_free(const HBytes_Value *frees);
194   /* _empty, _sentinel and _array do not free or read the old value;
195    * _free it first if needed.  _free leaves it garbage, so you
196    * have to call _empty to reuse it.  _arrayspace doesn't fill
197    * the array; you get a pointer and must fill it with data
198    * yourself. */
199
200 /* The value made by cht_hb_sentinel should not be passed to
201  * anything except HBYTES_IS..., and cht_hb_free. */
202
203 /* from hook.c */
204
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);
208
209 /* from parse.c */
210
211 typedef struct {
212   HBytes_Value *hb;
213   Something_Var sth;
214 } HBytes_Var;
215
216 /* from addrmap.c */
217
218 typedef struct AddrMap_Value AddrMap_Value;
219
220 typedef struct {
221   AddrMap_Value *am;
222   Something_Var sth;
223 } AddrMap_Var;
224
225 extern Tcl_ObjType cht_addrmap_type;
226
227 /* from chop.c */
228   /* only do_... functions declared in tables.h */
229
230 /* from ulong.c */
231
232 Tcl_ObjType cht_ulong_type;
233
234 /* useful macros */
235
236 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
237 #define OBJ_SOCKADDR(o) ((SockAddr_Value*)&(o)->internalRep.twoPtrValue)
238
239 #endif /*HBYTES_H*/