4 #include "chiark_tcl_hbytes.h"
6 /*---------- operations on AddrMap_Entry ----------*/
8 static void ame_free(AddrMap_Entry *ame) {
9 TFREE(ame->start); ame->start=0;
10 if (ame->data) { Tcl_DecrRefCount(ame->data); ame->data=0; }
13 static const Byte *ame_parsecheck_addr(Tcl_Interp *ip, const AddrMap_Value *am,
14 const HBytes_Value *hb) {
15 int hbl= cht_hb_len(hb);
17 cht_staticerr(ip,"addr-map address too short","HBYTES ADDRMAP UNDERRUN");
21 cht_staticerr(ip,"addr-map address too long","HBYTES ADDRMAP OVERRUN");
24 return cht_hb_data(hb);
27 static int ame_parsecheck_range(Tcl_Interp *ip, const AddrMap_Value *am,
28 const HBytes_Value *starthb,
29 const HBytes_Value *endhb,
31 p_r[0]= ame_parsecheck_addr(ip,am,starthb); if (!p_r[0]) return TCL_ERROR;
32 p_r[1]= ame_parsecheck_addr(ip,am,endhb); if (!p_r[0]) return TCL_ERROR;
33 if (memcmp(p_r[0],p_r[1],am->byl) > 0)
34 return cht_staticerr(ip, "addr-map range start is after end",
35 "HBYTES ADDRMAP BADRANGE");
39 static int ame_ba_addsubtractone(Byte *out, const Byte *in, int byl,
40 unsigned signum, unsigned onoverflow) {
42 * *in is an array of byl bytes
43 * signum is 0xff or 0x01
44 * onoverflow is what counts as overflowed value,
45 * ie (for unsigned arith) 0x00 for add and 0xff for subtract
47 * *out is the resulting value (subject to overflow truncation)
48 * return value is TCL_OK, or TCL_ERROR if overflow happened
49 * (but interpreter result is not set on overflow)
53 for (j= byl, in += byl, out += byl;
56 *out = (*out) + signum;
57 if (*out != onoverflow)
63 /*---------- useful operations on AddrMap_Value etc. ----------*/
65 static void am_init0(AddrMap_Value *am, int byl) {
72 static void am_reallocentries(AddrMap_Value *am, int len) {
73 AddrMap_Entry *newentries;
75 assert(len >= am->space);
78 newentries= TREALLOC(am->entries, sizeof(*newentries)*len);
82 am->entries= newentries;
85 static void am_free(AddrMap_Value *am) {
91 for (i=0, ame=am->entries; i<am->used; i++, ame++)
98 /*---------- Tcl type and arg parsing functions ----------*/
100 int cht_pat_addrmapv(Tcl_Interp *ip, Tcl_Obj *var, AddrMap_Var *agg) {
102 rc= cht_pat_somethingv(ip,var,&agg->sth,&cht_addrmap_type);
104 agg->am= agg->sth.obj->internalRep.otherValuePtr;
108 static void addrmap_t_free(Tcl_Obj *o) {
109 AddrMap_Value *am= o->internalRep.otherValuePtr;
113 static void addrmap_t_dup(Tcl_Obj *sob, Tcl_Obj *dob) {
114 AddrMap_Value *sm= sob->internalRep.otherValuePtr;
116 AddrMap_Entry *sme, *dme;
119 assert(sob->typePtr == &cht_addrmap_type);
121 dm= TALLOC(sizeof(*dm));
123 am_init0(dm,sm->byl);
124 am_reallocentries(dm,sm->used);
126 for (i=0, sme=sm->entries, dme=dm->entries;
130 dme->start= TALLOC(sm->byl); assert(dme->start);
131 memcpy(dme->start, sme->start, sm->byl);
132 Tcl_IncrRefCount(dme->data);
134 dob->internalRep.otherValuePtr= dm;
135 dob->typePtr= &cht_addrmap_type;
138 static void addrmap_t_ustr(Tcl_Obj *so) {
139 AddrMap_Value *sm= so->internalRep.otherValuePtr;
140 Tcl_Obj **mainlobjsl, *surrogate;
142 int entnum, listlength;
144 assert(so->typePtr == &cht_addrmap_type);
145 mainlobjsl= TALLOC(sizeof(*mainlobjsl) * (sm->used+1)); assert(mainlobjsl);
146 mainlobjsl[0]= Tcl_NewIntObj(sm->byl * 8);
149 for (entnum=0, sme=sm->entries; entnum<sm->used; entnum++, sme++) {
151 Tcl_Obj *subl[3], *sublo;
153 if (!sme->data) continue;
155 cht_hb_array(&hb, sme->start, sm->byl);
156 subl[0]= cht_ret_hb(0, hb); assert(subl[0]);
158 if (entnum+1 < sm->used) {
159 ame_ba_addsubtractone(cht_hb_arrayspace(&hb, sm->byl),
160 (sme+1)->start, sm->byl,
161 /*subtract:*/ 0x0ffu, 0x0ffu);
163 memset(cht_hb_arrayspace(&hb, sm->byl),
167 subl[1]= cht_ret_hb(0, hb); assert(subl[1]);
170 sublo= Tcl_NewListObj(3,subl); assert(sublo);
171 mainlobjsl[listlength++]= sublo;
173 assert(listlength <= sm->used+1);
174 surrogate= Tcl_NewListObj(listlength,mainlobjsl); assert(surrogate);
177 so->bytes= Tcl_GetStringFromObj(surrogate, &so->length); assert(so->bytes);
178 surrogate->bytes= 0; surrogate->length= 0; /* we stole it */
181 static AddrMap_Entry *ame_sfa_alloc(AddrMap_Value *am) {
184 ame= am->entries + am->used;
187 assert(am->used <= am->space);
189 ame->start= TALLOC(am->byl); assert(ame->start);
194 static int addrmap_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
195 int rc, inlen, eol, innum, bitlen, cmp;
196 Tcl_Obj *eo, *starto, *endo;
197 HBytes_Value starthb, endhb;
198 const Byte *rangeptrs[2];
202 am= TALLOC(sizeof(*am)); assert(am);
205 rc= Tcl_ListObjLength(ip,o,&inlen); if (rc) goto x_badvalue_rc;
208 rc= cht_staticerr(ip, "addr-map overall length < 1", 0);
212 rc= Tcl_ListObjIndex(ip,o,0,&eo); if (rc) goto x_badvalue_rc;
213 rc= Tcl_GetIntFromObj(ip,eo,&bitlen); if (rc) goto x_badvalue_rc;
215 if (bitlen<0 || bitlen % 8) {
216 rc= cht_staticerr(ip, "addr-map overall length < 1", 0);
221 am_reallocentries(am, (inlen-1)*2+1);
223 ame= ame_sfa_alloc(am);
224 memset(ame->start,0,am->byl);
226 for (innum=1; innum < inlen; innum++) {
227 rc= Tcl_ListObjIndex(ip,o,innum,&eo); if (rc) goto x_badvalue_rc;
228 rc= Tcl_ListObjLength(ip,eo,&eol); if (rc) goto x_badvalue_rc;
231 rc= cht_staticerr(ip, "addr-map entry length != 3", 0);
234 rc= Tcl_ListObjIndex(ip,eo,0,&starto); if (rc) goto x_badvalue_rc;
235 rc= Tcl_ListObjIndex(ip,eo,1,&endo); if (rc) goto x_badvalue_rc;
237 rc= cht_pat_hb(ip,starto,&starthb); if (rc) goto x_badvalue_rc;
238 rc= cht_pat_hb(ip,endo,&endhb); if (rc) goto x_badvalue_rc;
240 rc= ame_parsecheck_range(ip,am,&starthb,&endhb,rangeptrs);
241 if (rc) goto x_badvalue_rc;
243 cmp= memcmp(ame->start, rangeptrs[0], am->byl);
245 rc= cht_staticerr(ip, "addr-map entries out of order", 0);
249 ame= ame_sfa_alloc(am);
250 memcpy(ame->start, rangeptrs[0], am->byl);
254 rc= Tcl_ListObjIndex(ip,eo,2,&ame->data); if (rc) goto x_badvalue_rc;
255 Tcl_IncrRefCount(ame->data);
257 ame= ame_sfa_alloc(am);
258 rc= ame_ba_addsubtractone(ame->start, rangeptrs[1], am->byl,
259 /*add:*/ 0x01u, 0x00u);
261 /* we've overflowed. it must have been ffffffff.... */
262 if (innum != inlen-1) {
263 rc= cht_staticerr(ip, "addr-map non-last entry end is all-bits-1", 0);
274 o->internalRep.otherValuePtr= am;
275 o->typePtr= &cht_addrmap_type;
280 Tcl_SetObjErrorCode(ip, Tcl_NewStringObj("HBYTES ADDRMAP VALUE", -1));
286 Tcl_ObjType cht_addrmap_type = {
288 addrmap_t_free, addrmap_t_dup, addrmap_t_ustr, addrmap_t_sfa