2 * hbytes - hex-stringrep efficient byteblocks for Tcl
3 * Copyright 2006-2012 Ian Jackson
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "chiark_tcl_hbytes.h"
21 /* nice simple functions */
23 int cht_do_ulong_int2ul(ClientData cd, Tcl_Interp *ip, int v,
25 if (v<0) return cht_staticerr(ip,"cannot convert"
26 " -ve integer to ulong","ULONG VALUE NEGATIVE");
31 int cht_do_ulong_add(ClientData cd, Tcl_Interp *ip,
32 uint32_t a, uint32_t b, uint32_t *result) {
37 int cht_do_ulong_multiply(ClientData cd, Tcl_Interp *ip,
38 uint32_t a, uint32_t b, uint32_t *result) {
43 int cht_do_ulong_subtract(ClientData cd, Tcl_Interp *ip,
44 uint32_t a, uint32_t b, uint32_t *result) {
49 int cht_do_ulong_compare(ClientData cd, Tcl_Interp *ip,
50 uint32_t a, uint32_t b, int *result) {
57 int cht_do_ulong_ul2int(ClientData cd, Tcl_Interp *ip,
58 uint32_t v, int *result) {
60 cht_staticerr(ip,"ulong too large"
61 " to fit in an int", "ULONG VALUE OVERFLOW");
66 int cht_do_ulong_mask(ClientData cd, Tcl_Interp *ip,
67 uint32_t a, uint32_t b, uint32_t *result) {
72 int cht_do_ulong_shift(ClientData cd, Tcl_Interp *ip, int right,
73 uint32_t v, int bits, uint32_t *result) {
74 if (bits < 0) { bits= -bits; right= !right; }
75 if (bits > 32) return cht_staticerr(ip,"shift out of range (32) bits",
76 "ULONG BITCOUNT OVERRUN");
77 *result= (bits==32 ? 0 :
78 right ? v >> bits : v << bits);
87 int (*reader_writer[2])(Tcl_Interp *ip, uint32_t *value_io,
88 int *ok_io, Tcl_Obj *arg);
91 static int bf_zero_read(Tcl_Interp *ip, uint32_t *value_io,
92 int *ok_io, Tcl_Obj *arg) {
93 if (*value_io) *ok_io= 0;
97 static int bf_zero_write(Tcl_Interp *ip, uint32_t *value_io,
98 int *ok_io, Tcl_Obj *arg) {
103 static int bf_ignore(Tcl_Interp *ip, uint32_t *value_io,
104 int *ok_io, Tcl_Obj *arg) {
108 static int bf_fixed_read(Tcl_Interp *ip, uint32_t *value_io,
109 int *ok_io, Tcl_Obj *arg) {
113 rc= cht_pat_ulong(ip, arg, &ul); if (rc) return rc;
114 if (*value_io != ul) *ok_io= 0;
118 static int bf_ulong_write(Tcl_Interp *ip, uint32_t *value_io,
119 int *ok_io, Tcl_Obj *arg) {
123 rc= cht_pat_ulong(ip, arg, &ul); if (rc) return rc;
128 static int bf_var_read(Tcl_Interp *ip, Tcl_Obj *varname, Tcl_Obj *val) {
130 rp= Tcl_ObjSetVar2(ip,varname,0,val,TCL_LEAVE_ERR_MSG);
131 if (!rp) return TCL_ERROR;
135 static int bf_ulong_read(Tcl_Interp *ip, uint32_t *value_io,
136 int *ok_io, Tcl_Obj *arg) {
137 return bf_var_read(ip,arg, cht_ret_ulong(ip,*value_io));
140 static int bf_uint_write(Tcl_Interp *ip, uint32_t *value_io,
141 int *ok_io, Tcl_Obj *arg) {
143 rc= cht_pat_int(ip, arg, &v); if (rc) return rc;
144 if (v<0) return cht_staticerr(ip,"value for bitfield is -ve",
145 "ULONG VALUE NEGATIVE");
150 static int bf_uint_read(Tcl_Interp *ip, uint32_t *value_io,
151 int *ok_io, Tcl_Obj *arg) {
152 if (*value_io > INT_MAX)
153 return cht_staticerr(ip,"value from bitfield"
154 " exceeds INT_MAX","ULONG VALUE OVERFLOW");
155 return bf_var_read(ip,arg, cht_ret_int(ip,*value_io));
158 #define BFT(t,a) { #t, a, { bf_read_##t, bf_write_##t } }
159 static const BitFieldType bitfieldtypes[]= {
160 { "zero", 0, { bf_zero_read, bf_zero_write } },
161 { "ignore", 0, { bf_ignore, bf_ignore } },
162 { "fixed", 1, { bf_fixed_read, bf_ulong_write } },
163 { "ulong", 1, { bf_ulong_read, bf_ulong_write } },
164 { "uint", 1, { bf_uint_read, bf_uint_write } },
168 static int do_bitfields(Tcl_Interp *ip, int writing, int *ok_r,
170 int objc, Tcl_Obj *const *objv) {
171 const BitFieldType *ftype;
174 uint32_t value, sz_mask, this_mask, this_field;
181 rc= Tcl_GetIntFromObj(ip,*++objv,&sz); if (rc) return rc;
183 return cht_staticerr(ip,"wrong # args: missing bitfield type",0);
186 return cht_staticerr(ip,"bitfield size is -ve",
187 "ULONG BITCOUNT NEGATIVE");
189 return cht_staticerr(ip,"total size of bitfields >32",
190 "ULONG BITCOUNT OVERRUN");
194 sz_mask= ~(~0UL << sz);
195 this_mask= (sz_mask << pos);
196 this_field= (value & this_mask) >> pos;
198 ftype= enum_lookup_cached(ip,*++objv,bitfieldtypes,"bitfield type");
199 if (!ftype) return TCL_ERROR;
201 if (ftype->want_arg) {
203 return cht_staticerr(ip,"wrong # args: missing arg for bitfield",0);
208 rc= ftype->reader_writer[writing](ip, &this_field, ok_r, arg);
211 if (!*ok_r) return TCL_OK;
213 if (this_field & ~sz_mask)
214 return cht_staticerr(ip,"bitfield value has more bits than bitfield",
215 "ULONG VALUE OVERFLOW");
218 value |= (this_field << pos);
222 cht_staticerr(ip,"bitfield sizes add up to <32","ULONG BITCOUNT UNDERRUN");
228 int cht_do_ulong_bitfields2ul(ClientData cd, Tcl_Interp *ip,
230 int objc, Tcl_Obj *const *objv,
235 rc= do_bitfields(ip,1,&ok,result,objc,objv);
240 int cht_do_ulong_ul2bitfields(ClientData cd, Tcl_Interp *ip,
242 int objc, Tcl_Obj *const *objv,
244 return do_bitfields(ip,0,result,&value,objc,objv);
249 int cht_pat_ulong(Tcl_Interp *ip, Tcl_Obj *o, uint32_t *val) {
252 rc= Tcl_ConvertToType(ip,o,&cht_ulong_type);
254 *val= *(const uint32_t*)&o->internalRep.longValue;
258 Tcl_Obj *cht_ret_ulong(Tcl_Interp *ip, uint32_t val) {
262 Tcl_InvalidateStringRep(o);
263 *(uint32_t*)&o->internalRep.longValue= val;
264 o->typePtr= &cht_ulong_type;
270 static void ulong_t_free(Tcl_Obj *o) { }
272 static void ulong_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
273 dup->internalRep= src->internalRep;
274 dup->typePtr= &cht_ulong_type;
277 static void ulong_t_ustr(Tcl_Obj *o) {
281 val= *(const uint32_t*)&o->internalRep.longValue;
282 snprintf(buf,sizeof(buf), "%08lx", (unsigned long)val);
283 cht_obj_updatestr_vstringls(o, buf, sizeof(buf)-1, (char*)0);
286 static int ulong_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
290 if (o->typePtr == &cht_hbytes_type) {
293 l= cht_hb_len(OBJ_HBYTES(o));
294 if (l > 4) return cht_staticerr(ip,"hbytes as ulong with length >4",
295 "HBYTES LENGTH OVERRUN");
297 memcpy((Byte*)&ul + 4 - l, cht_hb_data(OBJ_HBYTES(o)), l);
302 str= Tcl_GetString(o);
304 if (str[0]=='0' && str[1]=='b' && str[2]) {
305 ul= strtoul(str+2,&ep,2);
306 } else if (str[0]=='0' && str[1]=='d' && str[2]) {
307 ul= strtoul(str+2,&ep,10);
309 ul= strtoul(str,&ep,16);
311 if (*ep || errno) return cht_staticerr(ip, "bad unsigned long value", 0);
316 *(uint32_t*)&o->internalRep.longValue= ul;
317 o->typePtr= &cht_ulong_type;
321 Tcl_ObjType cht_ulong_type = {
323 ulong_t_free, ulong_t_dup, ulong_t_ustr, ulong_t_sfa