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_subtract(ClientData cd, Tcl_Interp *ip,
24 uint32_t a, uint32_t b, uint32_t *result) {
29 int do_ulong_compare(ClientData cd, Tcl_Interp *ip,
30 uint32_t a, uint32_t b, int *result) {
37 int do_ulong_ul2int(ClientData cd, Tcl_Interp *ip,
38 uint32_t v, int *result) {
40 staticerr(ip,"ulong too large to fit in an int", "ULONG VALUE OVERFLOW");
45 int do_ulong_mask(ClientData cd, Tcl_Interp *ip,
46 uint32_t a, uint32_t b, uint32_t *result) {
51 int do_ulong_shift(ClientData cd, Tcl_Interp *ip, int right,
52 uint32_t v, int bits, uint32_t *result) {
53 if (bits < 0) { bits= -bits; right= !right; }
55 staticerr(ip,"shift out of range (32) bits", "ULONG BITCOUNT OVERRUN");
56 *result= (bits==32 ? 0 :
57 right ? v >> bits : v << bits);
66 int (*reader_writer[2])(Tcl_Interp *ip, uint32_t *value_io,
67 int *ok_io, Tcl_Obj *arg);
70 static int bf_zero_read(Tcl_Interp *ip, uint32_t *value_io,
71 int *ok_io, Tcl_Obj *arg) {
72 if (*value_io) *ok_io= 0;
76 static int bf_zero_write(Tcl_Interp *ip, uint32_t *value_io,
77 int *ok_io, Tcl_Obj *arg) {
82 static int bf_ignore(Tcl_Interp *ip, uint32_t *value_io,
83 int *ok_io, Tcl_Obj *arg) {
87 static int bf_fixed_read(Tcl_Interp *ip, uint32_t *value_io,
88 int *ok_io, Tcl_Obj *arg) {
92 rc= pat_ulong(ip, arg, &ul); if (rc) return rc;
93 if (*value_io != ul) *ok_io= 0;
97 static int bf_ulong_write(Tcl_Interp *ip, uint32_t *value_io,
98 int *ok_io, Tcl_Obj *arg) {
102 rc= pat_ulong(ip, arg, &ul); if (rc) return rc;
107 static int bf_var_read(Tcl_Interp *ip, Tcl_Obj *varname, Tcl_Obj *val) {
109 rp= Tcl_ObjSetVar2(ip,varname,0,val,TCL_LEAVE_ERR_MSG);
110 if (!rp) return TCL_ERROR;
114 static int bf_ulong_read(Tcl_Interp *ip, uint32_t *value_io,
115 int *ok_io, Tcl_Obj *arg) {
116 return bf_var_read(ip,arg, ret_ulong(ip,*value_io));
119 static int bf_uint_write(Tcl_Interp *ip, uint32_t *value_io,
120 int *ok_io, Tcl_Obj *arg) {
122 rc= pat_int(ip, arg, &v); if (rc) return rc;
124 staticerr(ip,"value for bitfield is -ve", "ULONG VALUE NEGATIVE");
129 static int bf_uint_read(Tcl_Interp *ip, uint32_t *value_io,
130 int *ok_io, Tcl_Obj *arg) {
131 if (*value_io > INT_MAX) return
132 staticerr(ip,"value from bitfield exceeds INT_MAX","ULONG VALUE OVERFLOW");
133 return bf_var_read(ip,arg, ret_int(ip,*value_io));
136 #define BFT(t,a) { #t, a, { bf_read_##t, bf_write_##t } }
137 static const BitFieldType bitfieldtypes[]= {
138 { "zero", 0, { bf_zero_read, bf_zero_write } },
139 { "ignore", 0, { bf_ignore, bf_ignore } },
140 { "fixed", 1, { bf_fixed_read, bf_ulong_write } },
141 { "ulong", 1, { bf_ulong_read, bf_ulong_write } },
142 { "uint", 1, { bf_uint_read, bf_uint_write } },
146 static int do_bitfields(Tcl_Interp *ip, int writing, int *ok_r,
148 int objc, Tcl_Obj *const *objv) {
149 const BitFieldType *ftype;
152 uint32_t value, sz_mask, this_mask, this_field;
159 rc= Tcl_GetIntFromObj(ip,*++objv,&sz); if (rc) return rc;
160 if (!--objc) return staticerr(ip,"wrong # args: missing bitfield type",0);
163 return staticerr(ip,"bitfield size is -ve", "ULONG BITCOUNT NEGATIVE");
165 staticerr(ip,"total size of bitfields >32", "ULONG BITCOUNT OVERRUN");
169 sz_mask= ~(~0UL << sz);
170 this_mask= (sz_mask << pos);
171 this_field= (value & this_mask) >> pos;
173 ftype= enum_lookup_cached(ip,*++objv,bitfieldtypes,"bitfield type");
174 if (!ftype) return TCL_ERROR;
176 if (ftype->want_arg) {
178 return staticerr(ip,"wrong # args: missing arg for bitfield",0);
183 rc= ftype->reader_writer[writing](ip, &this_field, ok_r, arg);
186 if (!*ok_r) return TCL_OK;
188 if (this_field & ~sz_mask)
189 return staticerr(ip,"bitfield value has more bits than bitfield",
190 "ULONG VALUE OVERFLOW");
193 value |= (this_field << pos);
197 staticerr(ip,"bitfield sizes add up to <32","ULONG BITCOUNT UNDERRUN");
203 int do_ulong_bitfields2ul(ClientData cd, Tcl_Interp *ip,
205 int objc, Tcl_Obj *const *objv,
210 rc= do_bitfields(ip,1,&ok,result,objc,objv);
215 int do_ulong_ul2bitfields(ClientData cd, Tcl_Interp *ip,
217 int objc, Tcl_Obj *const *objv,
219 return do_bitfields(ip,0,result,&value,objc,objv);
224 int pat_ulong(Tcl_Interp *ip, Tcl_Obj *o, uint32_t *val) {
227 rc= Tcl_ConvertToType(ip,o,&ulong_type);
229 *val= *(const uint32_t*)&o->internalRep.longValue;
233 Tcl_Obj *ret_ulong(Tcl_Interp *ip, uint32_t val) {
237 Tcl_InvalidateStringRep(o);
238 *(uint32_t*)&o->internalRep.longValue= val;
239 o->typePtr= &ulong_type;
245 static void ulong_t_free(Tcl_Obj *o) { }
247 static void ulong_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
248 dup->internalRep= src->internalRep;
249 dup->typePtr= &ulong_type;
252 static void ulong_t_ustr(Tcl_Obj *o) {
256 val= *(const uint32_t*)&o->internalRep.longValue;
258 assert(val <= 0xffffffffUL);
259 snprintf(buf,sizeof(buf), "%08lx", (unsigned long)val);
261 obj_updatestr_vstringls(o, buf, sizeof(buf)-1, (char*)0);
264 static int ulong_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
268 if (o->typePtr == &hbytes_type) {
271 l= hbytes_len(OBJ_HBYTES(o));
273 staticerr(ip,"hbytes as ulong with length >4","HBYTES LENGTH OVERRUN");
275 memcpy((Byte*)&ul + 4 - l, hbytes_data(OBJ_HBYTES(o)), l);
280 str= Tcl_GetString(o);
282 if (str[0]=='0' && str[1]=='b' && str[2]) {
283 ul= strtoul(str+2,&ep,2);
284 } else if (str[0]=='0' && str[1]=='d' && str[2]) {
285 ul= strtoul(str+2,&ep,10);
287 ul= strtoul(str,&ep,16);
289 if (*ep || errno) return staticerr(ip, "bad unsigned long value", 0);
294 *(uint32_t*)&o->internalRep.longValue= ul;
295 o->typePtr= &ulong_type;
299 Tcl_ObjType ulong_type = {
301 ulong_t_free, ulong_t_dup, ulong_t_ustr, ulong_t_sfa