chiark / gitweb /
Fix ulong ustr!
[chiark-tcl.git] / hbytes / ulongs.c
1 /*
2  */
3
4 #include "hbytes.h"
5 #include "tables.h"
6
7 /* nice simple functions */
8
9 int do_ulong_int2ul(ClientData cd, Tcl_Interp *ip, int v,
10                     uint32_t *result) {
11   if (v<0) return
12     staticerr(ip,"cannot convert -ve integer to ulong","ULONG VALUE NEGATIVE");
13   *result= v;
14   return TCL_OK;
15 }
16   
17 int do_ulong_add(ClientData cd, Tcl_Interp *ip,
18                  uint32_t a, uint32_t b, uint32_t *result) {
19   *result= a + b;
20   return TCL_OK;
21 }
22   
23 int do_ulong_multiply(ClientData cd, Tcl_Interp *ip,
24                       uint32_t a, uint32_t b, uint32_t *result) {
25   *result= a * b;
26   return TCL_OK;
27 }
28   
29 int do_ulong_subtract(ClientData cd, Tcl_Interp *ip,
30                       uint32_t a, uint32_t b, uint32_t *result) {
31   *result= a - b;
32   return TCL_OK;
33 }
34   
35 int do_ulong_compare(ClientData cd, Tcl_Interp *ip,
36                      uint32_t a, uint32_t b, int *result) {
37   *result=
38     a == b ? 0 :
39     a < b ? -1 : 1;
40   return TCL_OK;
41 }
42
43 int do_ulong_ul2int(ClientData cd, Tcl_Interp *ip,
44                     uint32_t v, int *result) {
45   if (v>INT_MAX) return
46     staticerr(ip,"ulong too large to fit in an int", "ULONG VALUE OVERFLOW");
47   *result= v;
48   return TCL_OK;
49 }
50
51 int do_ulong_mask(ClientData cd, Tcl_Interp *ip,
52                   uint32_t a, uint32_t b, uint32_t *result) {
53   *result= a & b;
54   return TCL_OK;
55 }
56   
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; }
60   if (bits > 32) return
61     staticerr(ip,"shift out of range (32) bits", "ULONG BITCOUNT OVERRUN");
62   *result= (bits==32 ? 0 :
63             right ? v >> bits : v << bits);
64   return TCL_OK;
65 }
66
67 /* bitfields */
68
69 typedef struct {
70   const char *name;
71   int want_arg;
72   int (*reader_writer[2])(Tcl_Interp *ip, uint32_t *value_io,
73                           int *ok_io, Tcl_Obj *arg);
74 } BitFieldType;
75
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;
79   return TCL_OK;
80 }
81
82 static int bf_zero_write(Tcl_Interp *ip, uint32_t *value_io,
83                          int *ok_io, Tcl_Obj *arg) {
84   *value_io= 0;
85   return TCL_OK;
86 }
87
88 static int bf_ignore(Tcl_Interp *ip, uint32_t *value_io,
89                      int *ok_io, Tcl_Obj *arg) {
90   return TCL_OK;
91 }
92
93 static int bf_fixed_read(Tcl_Interp *ip, uint32_t *value_io,
94                          int *ok_io, Tcl_Obj *arg) {
95   uint32_t ul;
96   int rc;
97   
98   rc= pat_ulong(ip, arg, &ul);  if (rc) return rc;
99   if (*value_io != ul) *ok_io= 0;
100   return TCL_OK;
101 }
102
103 static int bf_ulong_write(Tcl_Interp *ip, uint32_t *value_io,
104                           int *ok_io, Tcl_Obj *arg) {
105   uint32_t ul;
106   int rc;
107   
108   rc= pat_ulong(ip, arg, &ul);  if (rc) return rc;
109   *value_io= ul;
110   return TCL_OK;
111 }
112
113 static int bf_var_read(Tcl_Interp *ip, Tcl_Obj *varname, Tcl_Obj *val) {
114   Tcl_Obj *rp;
115   rp= Tcl_ObjSetVar2(ip,varname,0,val,TCL_LEAVE_ERR_MSG);
116   if (!rp) return TCL_ERROR;
117   return TCL_OK;
118 }
119
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));
123 }
124
125 static int bf_uint_write(Tcl_Interp *ip, uint32_t *value_io,
126                          int *ok_io, Tcl_Obj *arg) {
127   int rc, v;
128   rc= pat_int(ip, arg, &v);  if (rc) return rc;
129   if (v<0) return
130     staticerr(ip,"value for bitfield is -ve", "ULONG VALUE NEGATIVE");
131   *value_io= v;
132   return TCL_OK;
133 }
134
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));
140 }
141
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  } },
149   { 0 }
150 };
151
152 static int do_bitfields(Tcl_Interp *ip, int writing, int *ok_r,
153                         uint32_t *value_io,
154                         int objc, Tcl_Obj *const *objv) {
155   const BitFieldType *ftype;
156   Tcl_Obj *arg;
157   int sz, pos, rc;
158   uint32_t value, sz_mask, this_mask, this_field;
159   
160   pos= 32;
161   value= *value_io;
162   *ok_r= 1;
163
164   while (--objc) {
165     rc= Tcl_GetIntFromObj(ip,*++objv,&sz);  if (rc) return rc;
166     if (!--objc) return staticerr(ip,"wrong # args: missing bitfield type",0);
167
168     if (sz<0)
169       return staticerr(ip,"bitfield size is -ve", "ULONG BITCOUNT NEGATIVE");
170     if (sz>pos) return
171       staticerr(ip,"total size of bitfields >32", "ULONG BITCOUNT OVERRUN");
172
173     pos -= sz;
174
175     sz_mask= ~(~0UL << sz);
176     this_mask= (sz_mask << pos);
177     this_field= (value & this_mask) >> pos;
178     
179     ftype= enum_lookup_cached(ip,*++objv,bitfieldtypes,"bitfield type");
180     if (!ftype) return TCL_ERROR;
181
182     if (ftype->want_arg) {
183       if (!--objc)
184         return staticerr(ip,"wrong # args: missing arg for bitfield",0);
185       arg= *++objv;
186     } else {
187       arg= 0;
188     }
189     rc= ftype->reader_writer[writing](ip, &this_field, ok_r, arg);
190     if (rc) return rc;
191
192     if (!*ok_r) return TCL_OK;
193
194     if (this_field & ~sz_mask)
195       return staticerr(ip,"bitfield value has more bits than bitfield",
196                        "ULONG VALUE OVERFLOW");
197     
198     value &= ~this_mask;
199     value |= (this_field << pos);
200   }
201
202   if (pos != 0) return
203     staticerr(ip,"bitfield sizes add up to <32","ULONG BITCOUNT UNDERRUN");
204
205   *value_io= value;
206   return TCL_OK;
207 }
208
209 int do_ulong_bitfields2ul(ClientData cd, Tcl_Interp *ip,
210                           uint32_t base,
211                           int objc, Tcl_Obj *const *objv,
212                           uint32_t *result) {
213   int ok, rc;
214   
215   *result= base;
216   rc= do_bitfields(ip,1,&ok,result,objc,objv);
217   assert(ok);
218   return rc;
219 }
220
221 int do_ulong_ul2bitfields(ClientData cd, Tcl_Interp *ip,
222                           uint32_t value,
223                           int objc, Tcl_Obj *const *objv,
224                           int *result) {
225   return do_bitfields(ip,0,result,&value,objc,objv);
226 }
227
228 /* Arg parsing */
229
230 int pat_ulong(Tcl_Interp *ip, Tcl_Obj *o, uint32_t *val) {
231   int rc;
232   
233   rc= Tcl_ConvertToType(ip,o,&ulong_type);
234   if (rc) return rc;
235   *val= *(const uint32_t*)&o->internalRep.longValue;
236   return TCL_OK;
237 }
238
239 Tcl_Obj *ret_ulong(Tcl_Interp *ip, uint32_t val) {
240   Tcl_Obj *o;
241
242   o= Tcl_NewObj();
243   Tcl_InvalidateStringRep(o);
244   *(uint32_t*)&o->internalRep.longValue= val;
245   o->typePtr= &ulong_type;
246   return o;
247 }
248
249 /* Tcl ulong type */
250
251 static void ulong_t_free(Tcl_Obj *o) { }
252
253 static void ulong_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
254   dup->internalRep= src->internalRep;
255   dup->typePtr= &ulong_type;
256 }
257
258 static void ulong_t_ustr(Tcl_Obj *o) {
259   uint32_t val;
260   char buf[9];
261
262   val= *(const uint32_t*)&o->internalRep.longValue;
263
264   assert(val <= 0xffffffffUL);
265   snprintf(buf,sizeof(buf), "%08lx", (unsigned long)val);
266
267   obj_updatestr_vstringls(o, buf, sizeof(buf)-1, (char*)0);
268 }
269
270 static int ulong_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
271   char *str, *ep;
272   uint32_t ul;
273
274   if (o->typePtr == &hbytes_type) {
275     int l;
276
277     l= hbytes_len(OBJ_HBYTES(o));
278     if (l > 4) return
279       staticerr(ip,"hbytes as ulong with length >4","HBYTES LENGTH OVERRUN");
280     ul= 0;
281     memcpy((Byte*)&ul + 4 - l, hbytes_data(OBJ_HBYTES(o)), l);
282     ul= htonl(ul);
283
284   } else {
285
286     str= Tcl_GetString(o);
287     errno=0;
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);
292     } else {
293       ul= strtoul(str,&ep,16);
294     }
295     if (*ep || errno) return staticerr(ip, "bad unsigned long value", 0);
296
297   }
298
299   objfreeir(o);
300   *(uint32_t*)&o->internalRep.longValue= ul;
301   o->typePtr= &ulong_type;
302   return TCL_OK;
303 }
304
305 Tcl_ObjType ulong_type = {
306   "ulong-nearly",
307   ulong_t_free, ulong_t_dup, ulong_t_ustr, ulong_t_sfa
308 };