chiark / gitweb /
changelog: start 1.2.1~
[chiark-tcl.git] / hbytes / hbytes.h
1 /*
2  *  hbytes raw2h BINARY                          => hex
3  *  hbytes h2raw HEX                             => binary
4  *
5  *  hbytes length VALUE                          => count
6  *  hbytes prepend VAR [VALUE ...]         = set VAR [concat VALUE ... $VAR]
7  *  hbytes append VAR [VALUE ...]          = set VAR [concat $VAR VALUE ...]
8  *  hbytes concat VAR [VALUE ...]          = set VAR [concat VALUE ...]
9  *  hbytes unprepend VAR PREFIXLENGTH            => prefix (removed from VAR)
10  *  hbytes unappend VAR SUFFIXLENGTH             => suffix (removed from VAR)
11  *  hbytes chopto VAR NEWVARLENGTH               => suffix (removed from VAR)
12  *                                                  (too short? error)
13  *
14  *  hbytes range VALUE START SIZE                => substring (or error)
15  *  hbytes overwrite VAR START VALUE
16  *  hbytes trimleft VAR                         removes any leading 0 octets
17  *  hbytes repeat VALUE COUNT                    => COUNT copies of VALUE
18  *  hbytes zeroes COUNT                          => COUNT zero bytes
19  *  hbytes random COUNT                          => COUNT random bytes
20  *  hbytes xor VAR VALUE                         $VAR (+)= VALUE
21  *
22  *  hbytes ushort2h LONG           => LONG must be <2^16, returns as hex
23  *  hbytes h2ushort HEX            => |HEX| must be 2 bytes, returns as ulong
24  *
25  *  hbytes compare A B
26  *      =>  -2   A is lexically earlier than B and not a prefix of B  (A<B)
27  *          -1   A is prefix of B but not equal                       (A<B)
28  *           0   A == B
29  *          +1   A is B plus a nonempty suffix (ie, A has B as a prefix)
30  *          +2   A is lexically later than B and does not have B as a prefix
31  *
32  *  hbytes pad pa|ua VAR ALG METH [METHARGS]       => worked? (always 1 for p)
33  *  hbytes pad pn|un VAR BS METH [METHARGS]        => worked? (always 1 for p)
34  *  hbytes pad pa|pn VAR ALG|BS pkcs5              => 1
35  *  hbytes pad ua|un VAR ALG|BS pkcs5              => worked?
36  *  hbytes pad pa|pn VAR ALG|BS rfc2406 NXTHDR     => 1
37  *  hbytes pad ua|un VAR ALG|BS rfc2406 NXTHDRVAR  => worked?
38  *
39  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
40  *  hbytes blockcipher mac MSG ALG KEY MODE IV   => final block
41  *  hbytes blockcipher prop PROPERTY ALG         => property value
42  *
43  *  hbytes hash ALG MESSAGE                      => hash
44  *  hbytes hmac ALG MESSAGE KEY [MACLENGTH]      => mac
45  *  hbytes hash-prop PROPERTY ALG                => property value
46  *
47  *  ulong ul2int ULONG    => INT           can fail if >INT_MAX
48  *  ulong int2ul INT      => ULONG         can fail if <0
49  *  ulong mask A B                         => A & B
50  *  ulong add A B                          => A + B  (mod 2^32)
51  *  ulong subtract A B                     => A - B  (mod 2^32)
52  *  ulong compare A B                      => 0  -1 (A<B)  +1 (A>B)
53  *  ulong shift l|r ULONG BITS             fails if BITS >32
54  *
55  *  ulong ul2bitfields VALUE [SIZE TYPE [TYPE-ARG...] ...]  => 0/1
56  *  ulong bitfields2ul BASE  [SIZE TYPE [TYPE-ARG...] ...]  => ULONG
57  *      goes from left (MSbit) to right (LSbit) where
58  *            SIZE is size in bits
59  *            TYPE [TYPE-ARGS...] is as below
60  *               zero
61  *               ignore
62  *               fixed ULONG-VALUE
63  *               uint VARNAME/VALUE         (VARNAME if ul2bitfields;
64  *               ulong VARNAME/VALUE         VALUE if bitfields2ul)
65  *
66  * Address ranges (addrmap.c):
67  *
68  *  An address range is a slightly efficient partial mapping from
69  *  addresses to arbitrary data values.  An address is a number of
70  *  octets expressed as an hbytes.  All the addresses covered by the
71  *  same addrmap should have the same length.
72  *
73  *  hbytes addr-map lookup MAP-VAR ADDRESS [DEFAULT]   => DATA
74  *     Error on missing default or if any prefix longer than ADDRESS.
75  *
76  *  hbytes addr-map amend-range MAP-VAR START END DATA
77  *  hbytes addr-map amend-mask MAP-VAR PREFIX PREFIX-LENGTH DATA
78  *     Sets all of the addresses in PREFIX/PREFIX-LENGTH to the
79  *     relevant value.
80  *
81  *  Representation:
82  *     An address map MAP is
83  *           [list BIT-LENGTH                           \
84  *                 [list START END DATA-VALUE]          \
85  *                 [list START' END' DATA-VALUE']       \
86  *                 ...
87  *            ]
88  *     The list is sorted by ascending START and entries do not overlap.
89  *     START and END are both inclusive.  BIT-LENGTH is in usual Tcl
90  *     integer notation and must be a multiple of 8.
91  *
92  * Error codes
93  *
94  * HBYTES BLOCKCIPHER CRYPTFAIL CRYPT  block cipher mode failed somehow (!)
95  * HBYTES BLOCKCIPHER CRYPTFAIL MAC    HMAC failed somehow (!)
96  * HBYTES BLOCKCIPHER LENGTH           block cipher input has unsuitable length
97  * HBYTES BLOCKCIPHER PARAMS           key or iv not suitable
98  * HBYTES HMAC PARAMS                  key, input or output size not suitable
99  * HBYTES LENGTH OVERRUN               block too long
100  * HBYTES LENGTH RANGE                 input length or offset is -ve or silly
101  * HBYTES LENGTH UNDERRUN              block too short (or offset too big)
102  * HBYTES LENGTH MISMATCH              when blocks must be exactly same length
103  * HBYTES SYNTAX                       supposed hex block had wrong syntax
104  * HBYTES VALUE OVERFLOW               value to be conv'd to hex too big/long
105  * HBYTES ADDRMAP NOMATCH              no addr/mask matches address for lookup
106  * HBYTES ADDRMAP UNDERRUN             addr for lookup or amend is too short
107  * HBYTES ADDRMAP OVERRUN              addr for lookup or amend is too long
108  * HBYTES ADDRMAP EXCLBITS             amend-mask 1-bits outside prefix len
109  * HBYTES ADDRMAP BADRANGE             amend-range start > end
110  * HBYTES ADDRMAP VALUE                addr-map string value is erroneous
111  * SOCKADDR AFUNIX LENGTH              path for AF_UNIX socket too long
112  * SOCKADDR SYNTAX IPV4                bad IPv4 socket address &/or port
113  * SOCKADDR SYNTAX OTHER               bad socket addr, couldn't tell what kind
114  * ULONG BITCOUNT NEGATIVE             -ve bitcount specified where not allowed
115  * ULONG BITCOUNT OVERRUN              attempt to use more than 32 bits
116  * ULONG BITCOUNT UNDERRUN             bitfields add up to less than 32
117  * ULONG VALUE NEGATIVE                attempt convert -ve integers to ulong
118  * ULONG VALUE OVERFLOW                converted value does not fit in result
119  * TUNTAP IFNAME LENGTH                tun/tap interface name too long
120  * TUNTAP MTU OVERRUN                  tun/tap mtu limited to 2^16 bytes
121  *
122  * Refs: HMAC: RFC2104
123  */
124 /* ---8<--- end of documentation comment --8<-- */
125
126 /*
127  * hbytes - hex-stringrep efficient byteblocks for Tcl
128  * Copyright 2006-2012 Ian Jackson
129  *
130  * This program is free software; you can redistribute it and/or
131  * modify it under the terms of the GNU General Public License as
132  * published by the Free Software Foundation; either version 2 of the
133  * License, or (at your option) any later version.
134  *
135  * This program is distributed in the hope that it will be useful, but
136  * WITHOUT ANY WARRANTY; without even the implied warranty of
137  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
138  * General Public License for more details.
139  *
140  * You should have received a copy of the GNU General Public License
141  * along with this library; if not, see <http://www.gnu.org/licenses/>.
142  */
143
144
145 #ifndef HBYTES_H
146 #define HBYTES_H
147
148 #include <assert.h>
149 #include <stdlib.h>
150 #include <errno.h>
151 #include <unistd.h>
152 #include <fcntl.h>
153 #include <sys/socket.h>
154 #include <sys/uio.h>
155 #include <sys/un.h>
156 #include <arpa/inet.h>
157 #include <string.h>
158
159 #include "chiark-tcl.h"
160
161 /* from hbytes.c */
162
163 int Hbytes_Init(Tcl_Interp *ip); /* called by Tcl's "load" */
164
165 /* Internal representation details: */
166 #define HBYTES_ISEMPTY(hb)    (!(hb)->begin_complex && !(hb)->end_0)
167 #define HBYTES_ISSENTINEL(hb) (!(hb)->begin_complex && (hb)->end_0)
168 #define HBYTES_ISSIMPLE(hb)   ((hb)->begin_complex && (hb)->end_0)
169 #define HBYTES_ISCOMPLEX(hb)  ((hb)->begin_complex && !(hb)->end_0)
170
171 typedef struct {
172   void *begin_complex, *end_0;
173 } HBytes_Value; /* overlays internalRep */
174
175 typedef struct {
176   Byte *dstart; /* always allocated dynamically */
177   int prespace, len, avail;
178   /*        
179    * | SPARE      | USED  | SPARE |
180    * |<-prespace->|<-len->|       |
181    * |            |<----avail---->|
182    *              ^start
183    */
184 } HBytes_ComplexValue; /* pointed to from internalRep.otherValuePtr */
185
186 void memxor(Byte *dest, const Byte *src, int l);
187 extern int Chiark_tcl_hbytes_Init(Tcl_Interp *ip);
188   /* called by load(3tcl) and also by extensions which depend on this one */
189
190 /* Public interfaces: */
191
192 extern Tcl_ObjType cht_hbytes_type;
193
194 int cht_hb_len(const HBytes_Value *v);
195 Byte *cht_hb_data(const HBytes_Value *v); /* caller may then modify data! */
196 int cht_hb_issentinel(const HBytes_Value *v);
197
198 Byte *cht_hb_prepend(HBytes_Value *upd, int el);
199 Byte *cht_hb_append(HBytes_Value *upd, int el);
200   /* return value is where to put the data */
201
202 const Byte *cht_hb_unprepend(HBytes_Value *upd, int rl);
203 const Byte *cht_hb_unappend(HBytes_Value *upd, int rl);
204   /* return value points to the removed data, which remains valid
205    * until next op on the HBytes_Value.  If original value is
206    * shorter than rl or negative, returns 0 and does nothing. */
207
208 void cht_hb_empty(HBytes_Value *returns);
209 void cht_hb_sentinel(HBytes_Value *returns);
210 void cht_hb_array(HBytes_Value *returns, const Byte *array, int l);
211 Byte *cht_hb_arrayspace(HBytes_Value *returns, int l);
212 void cht_hb_free(const HBytes_Value *frees);
213   /* _empty, _sentinel and _array do not free or read the old value;
214    * _free it first if needed.  _free leaves it garbage, so you
215    * have to call _empty to reuse it.  _arrayspace doesn't fill
216    * the array; you get a pointer and must fill it with data
217    * yourself. */
218
219 /* The value made by cht_hb_sentinel should not be passed to
220  * anything except HBYTES_IS..., and cht_hb_free. */
221
222 /* from hook.c */
223
224 void cht_obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l);
225 void cht_obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
226                                 int l, const char *prefix);
227
228 /* from parse.c */
229
230 typedef struct {
231   HBytes_Value *hb;
232   Something_Var sth;
233 } HBytes_Var;
234
235 /* from addrmap.c */
236
237 typedef struct AddrMap_Value AddrMap_Value;
238
239 typedef struct {
240   AddrMap_Value *am;
241   Something_Var sth;
242 } AddrMap_Var;
243
244 extern Tcl_ObjType cht_addrmap_type;
245
246 /* from chop.c */
247   /* only do_... functions declared in tables.h */
248
249 /* from ulong.c */
250
251 Tcl_ObjType cht_ulong_type;
252
253 /* useful macros */
254
255 #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue)
256 #define OBJ_SOCKADDR(o) ((SockAddr_Value*)&(o)->internalRep.twoPtrValue)
257
258 #endif /*HBYTES_H*/