7 /* nice simple functions */
9 int do_ulong_int2ul(ClientData cd, Tcl_Interp *ip, int v,
12 staticerr(ip,"cannot convert -ve integer to ulong","ULONG VALUE NEGATIVE");
17 int do_ulong_add(ClientData cd, Tcl_Interp *ip,
18 uint32_t a, uint32_t b, uint32_t *result) {
23 int do_ulong_multiply(ClientData cd, Tcl_Interp *ip,
24 uint32_t a, uint32_t b, uint32_t *result) {
29 int do_ulong_subtract(ClientData cd, Tcl_Interp *ip,
30 uint32_t a, uint32_t b, uint32_t *result) {
35 int do_ulong_compare(ClientData cd, Tcl_Interp *ip,
36 uint32_t a, uint32_t b, int *result) {
43 int do_ulong_ul2int(ClientData cd, Tcl_Interp *ip,
44 uint32_t v, int *result) {
46 staticerr(ip,"ulong too large to fit in an int", "ULONG VALUE OVERFLOW");
51 int do_ulong_mask(ClientData cd, Tcl_Interp *ip,
52 uint32_t a, uint32_t b, uint32_t *result) {
57 int do_ulong_shift(ClientData cd, Tcl_Interp *ip, int right,
58 uint32_t v, int bits, uint32_t *result) {
59 if (bits < 0) { bits= -bits; right= !right; }
61 staticerr(ip,"shift out of range (32) bits", "ULONG BITCOUNT OVERRUN");
62 *result= (bits==32 ? 0 :
63 right ? v >> bits : v << bits);
72 int (*reader_writer[2])(Tcl_Interp *ip, uint32_t *value_io,
73 int *ok_io, Tcl_Obj *arg);
76 static int bf_zero_read(Tcl_Interp *ip, uint32_t *value_io,
77 int *ok_io, Tcl_Obj *arg) {
78 if (*value_io) *ok_io= 0;
82 static int bf_zero_write(Tcl_Interp *ip, uint32_t *value_io,
83 int *ok_io, Tcl_Obj *arg) {
88 static int bf_ignore(Tcl_Interp *ip, uint32_t *value_io,
89 int *ok_io, Tcl_Obj *arg) {
93 static int bf_fixed_read(Tcl_Interp *ip, uint32_t *value_io,
94 int *ok_io, Tcl_Obj *arg) {
98 rc= pat_ulong(ip, arg, &ul); if (rc) return rc;
99 if (*value_io != ul) *ok_io= 0;
103 static int bf_ulong_write(Tcl_Interp *ip, uint32_t *value_io,
104 int *ok_io, Tcl_Obj *arg) {
108 rc= pat_ulong(ip, arg, &ul); if (rc) return rc;
113 static int bf_var_read(Tcl_Interp *ip, Tcl_Obj *varname, Tcl_Obj *val) {
115 rp= Tcl_ObjSetVar2(ip,varname,0,val,TCL_LEAVE_ERR_MSG);
116 if (!rp) return TCL_ERROR;
120 static int bf_ulong_read(Tcl_Interp *ip, uint32_t *value_io,
121 int *ok_io, Tcl_Obj *arg) {
122 return bf_var_read(ip,arg, ret_ulong(ip,*value_io));
125 static int bf_uint_write(Tcl_Interp *ip, uint32_t *value_io,
126 int *ok_io, Tcl_Obj *arg) {
128 rc= pat_int(ip, arg, &v); if (rc) return rc;
130 staticerr(ip,"value for bitfield is -ve", "ULONG VALUE NEGATIVE");
135 static int bf_uint_read(Tcl_Interp *ip, uint32_t *value_io,
136 int *ok_io, Tcl_Obj *arg) {
137 if (*value_io > INT_MAX) return
138 staticerr(ip,"value from bitfield exceeds INT_MAX","ULONG VALUE OVERFLOW");
139 return bf_var_read(ip,arg, ret_int(ip,*value_io));
142 #define BFT(t,a) { #t, a, { bf_read_##t, bf_write_##t } }
143 static const BitFieldType bitfieldtypes[]= {
144 { "zero", 0, { bf_zero_read, bf_zero_write } },
145 { "ignore", 0, { bf_ignore, bf_ignore } },
146 { "fixed", 1, { bf_fixed_read, bf_ulong_write } },
147 { "ulong", 1, { bf_ulong_read, bf_ulong_write } },
148 { "uint", 1, { bf_uint_read, bf_uint_write } },
152 static int do_bitfields(Tcl_Interp *ip, int writing, int *ok_r,
154 int objc, Tcl_Obj *const *objv) {
155 const BitFieldType *ftype;
158 uint32_t value, sz_mask, this_mask, this_field;
165 rc= Tcl_GetIntFromObj(ip,*++objv,&sz); if (rc) return rc;
166 if (!--objc) return staticerr(ip,"wrong # args: missing bitfield type",0);
169 return staticerr(ip,"bitfield size is -ve", "ULONG BITCOUNT NEGATIVE");
171 staticerr(ip,"total size of bitfields >32", "ULONG BITCOUNT OVERRUN");
175 sz_mask= ~(~0UL << sz);
176 this_mask= (sz_mask << pos);
177 this_field= (value & this_mask) >> pos;
179 ftype= enum_lookup_cached(ip,*++objv,bitfieldtypes,"bitfield type");
180 if (!ftype) return TCL_ERROR;
182 if (ftype->want_arg) {
184 return staticerr(ip,"wrong # args: missing arg for bitfield",0);
189 rc= ftype->reader_writer[writing](ip, &this_field, ok_r, arg);
192 if (!*ok_r) return TCL_OK;
194 if (this_field & ~sz_mask)
195 return staticerr(ip,"bitfield value has more bits than bitfield",
196 "ULONG VALUE OVERFLOW");
199 value |= (this_field << pos);
203 staticerr(ip,"bitfield sizes add up to <32","ULONG BITCOUNT UNDERRUN");
209 int do_ulong_bitfields2ul(ClientData cd, Tcl_Interp *ip,
211 int objc, Tcl_Obj *const *objv,
216 rc= do_bitfields(ip,1,&ok,result,objc,objv);
221 int do_ulong_ul2bitfields(ClientData cd, Tcl_Interp *ip,
223 int objc, Tcl_Obj *const *objv,
225 return do_bitfields(ip,0,result,&value,objc,objv);
230 int pat_ulong(Tcl_Interp *ip, Tcl_Obj *o, uint32_t *val) {
233 rc= Tcl_ConvertToType(ip,o,&ulong_type);
235 *val= *(const uint32_t*)&o->internalRep.longValue;
239 Tcl_Obj *ret_ulong(Tcl_Interp *ip, uint32_t val) {
243 Tcl_InvalidateStringRep(o);
244 *(uint32_t*)&o->internalRep.longValue= val;
245 o->typePtr= &ulong_type;
251 static void ulong_t_free(Tcl_Obj *o) { }
253 static void ulong_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
254 dup->internalRep= src->internalRep;
255 dup->typePtr= &ulong_type;
258 static void ulong_t_ustr(Tcl_Obj *o) {
262 val= *(const uint32_t*)&o->internalRep.longValue;
264 assert(val <= 0xffffffffUL);
265 snprintf(buf,sizeof(buf), "%08lx", (unsigned long)val);
267 obj_updatestr_vstringls(o, buf, sizeof(buf)-1, (char*)0);
270 static int ulong_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
274 if (o->typePtr == &hbytes_type) {
277 l= hbytes_len(OBJ_HBYTES(o));
279 staticerr(ip,"hbytes as ulong with length >4","HBYTES LENGTH OVERRUN");
281 memcpy((Byte*)&ul + 4 - l, hbytes_data(OBJ_HBYTES(o)), l);
286 str= Tcl_GetString(o);
288 if (str[0]=='0' && str[1]=='b' && str[2]) {
289 ul= strtoul(str+2,&ep,2);
290 } else if (str[0]=='0' && str[1]=='d' && str[2]) {
291 ul= strtoul(str+2,&ep,10);
293 ul= strtoul(str,&ep,16);
295 if (*ep || errno) return staticerr(ip, "bad unsigned long value", 0);
300 *(uint32_t*)&o->internalRep.longValue= ul;
301 o->typePtr= &ulong_type;
305 Tcl_ObjType ulong_type = {
307 ulong_t_free, ulong_t_dup, ulong_t_ustr, ulong_t_sfa