10 Byte *start; /* byl bytes */
11 Tcl_Obj *data; /* may be 0 to mean empty */
14 struct AddrMap_Value {
16 AddrMap_Entry *entries;
17 /* Entries are sorted by start. Each entry gives value (or lack of
18 * it) for all A st START <= A < NEXT-START. Last entry has value
19 * (or lack of it) for all A >= START. First entry is always
20 * present and always has start all-bits-0. */
21 }; /* internalRep.otherValuePtr */
23 /*---------- operations on AddrMap_Entry ----------*/
25 static void ame_free(AddrMap_Entry *ame) {
26 TFREE(ame->start); ame->start=0;
27 if (ame->data) { Tcl_DecrRefCount(ame->data); ame->data=0; }
30 static const Byte *ame_parsecheck_addr(Tcl_Interp *ip, const AddrMap_Value *am,
31 const HBytes_Value *hb) {
32 int hbl= hbytes_len(hb);
34 staticerr(ip,"addr-map address too short","HBYTES ADDRMAP UNDERRUN");
38 staticerr(ip,"addr-map address too long","HBYTES ADDRMAP OVERRUN");
41 return hbytes_data(hb);
44 static int ame_parsecheck_range(Tcl_Interp *ip, const AddrMap_Value *am,
45 const HBytes_Value *starthb,
46 const HBytes_Value *endhb,
48 p_r[0]= ame_parsecheck_addr(ip,am,starthb); if (!p_r[0]) return TCL_ERROR;
49 p_r[1]= ame_parsecheck_addr(ip,am,endhb); if (!p_r[0]) return TCL_ERROR;
50 if (memcmp(p_r[0],p_r[1],am->byl) > 0)
51 return staticerr(ip, "addr-map range start is after end",
52 "HBYTES ADDRMAP BADRANGE");
56 static int ame_ba_addsubtractone(Byte *out, const Byte *in, int byl,
57 unsigned signum, unsigned onoverflow) {
59 * *in is an array of byl bytes
60 * signum is 0xff or 0x01
61 * onoverflow is what counts as overflowed value,
62 * ie (for unsigned arith) 0x00 for add and 0xff for subtract
64 * *out is the resulting value (subject to overflow truncation)
65 * return value is TCL_OK, or TCL_ERROR if overflow happened
66 * (but interpreter result is not set on overflow)
70 for (j= byl, in += byl, out += byl;
73 *out = (*out) + signum;
74 if (*out != onoverflow)
80 /*---------- useful operations on AddrMap_Value etc. ----------*/
82 static void am_init0(AddrMap_Value *am, int byl) {
89 static void am_reallocentries(AddrMap_Value *am, int len) {
90 AddrMap_Entry *newentries;
92 assert(len >= am->space);
95 newentries= TREALLOC(am->entries, sizeof(*newentries)*len);
99 am->entries= newentries;
102 static void am_free(AddrMap_Value *am) {
108 for (i=0, ame=am->entries; i<am->used; i++, ame++)
115 /*---------- Tcl type and arg parsing functions ----------*/
117 int pat_addrmapv(Tcl_Interp *ip, Tcl_Obj *var, AddrMap_Var *agg) {
119 rc= pat_somethingv(ip,var,&agg->sth,&addrmap_type); if (rc) return rc;
120 agg->am= agg->sth.obj->internalRep.otherValuePtr;
124 static void addrmap_t_free(Tcl_Obj *o) {
125 AddrMap_Value *am= o->internalRep.otherValuePtr;
129 static void addrmap_t_dup(Tcl_Obj *sob, Tcl_Obj *dob) {
130 AddrMap_Value *sm= sob->internalRep.otherValuePtr;
132 AddrMap_Entry *sme, *dme;
135 assert(sob->typePtr == &addrmap_type);
137 dm= TALLOC(sizeof(*dm));
139 am_init0(dm,sm->byl);
140 am_reallocentries(dm,sm->used);
142 for (i=0, sme=sm->entries, dme=dm->entries;
146 dme->start= TALLOC(sm->byl); assert(dme->start);
147 memcpy(dme->start, sme->start, sm->byl);
148 Tcl_IncrRefCount(dme->data);
150 dob->internalRep.otherValuePtr= dm;
151 dob->typePtr= &addrmap_type;
154 static void addrmap_t_ustr(Tcl_Obj *so) {
155 AddrMap_Value *sm= so->internalRep.otherValuePtr;
156 Tcl_Obj **mainlobjsl, *surrogate;
158 int entnum, listlength;
160 assert(so->typePtr == &addrmap_type);
161 mainlobjsl= TALLOC(sizeof(*mainlobjsl) * (sm->used+1)); assert(mainlobjsl);
162 mainlobjsl[0]= Tcl_NewIntObj(sm->byl * 8);
165 for (entnum=0, sme=sm->entries; entnum<sm->used; entnum++, sme++) {
167 Tcl_Obj *subl[3], *sublo;
169 if (!sme->data) continue;
171 hbytes_array(&hb, sme->start, sm->byl);
172 subl[0]= ret_hb(0, hb); assert(subl[0]);
174 if (entnum+1 < sm->used) {
175 ame_ba_addsubtractone(hbytes_arrayspace(&hb, sm->byl),
176 (sme+1)->start, sm->byl,
177 /*subtract:*/ 0x0ffu, 0x0ffu);
179 memset(hbytes_arrayspace(&hb, sm->byl),
183 subl[1]= ret_hb(0, hb); assert(subl[1]);
186 sublo= Tcl_NewListObj(3,subl); assert(sublo);
187 mainlobjsl[listlength++]= sublo;
189 assert(listlength <= sm->used+1);
190 surrogate= Tcl_NewListObj(listlength,mainlobjsl); assert(surrogate);
193 so->bytes= Tcl_GetStringFromObj(surrogate, &so->length); assert(so->bytes);
194 surrogate->bytes= 0; surrogate->length= 0; /* we stole it */
197 static AddrMap_Entry *ame_sfa_alloc(AddrMap_Value *am) {
200 ame= am->entries + am->used;
203 assert(am->used <= am->space);
205 ame->start= TALLOC(am->byl); assert(ame->start);
210 static int addrmap_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
211 int rc, inlen, eol, innum, bitlen, cmp;
212 Tcl_Obj *eo, *starto, *endo;
213 HBytes_Value starthb, endhb;
214 const Byte *rangeptrs[2];
218 am= TALLOC(sizeof(*am)); assert(am);
221 rc= Tcl_ListObjLength(ip,o,&inlen); if (rc) goto x_badvalue_rc;
224 rc= staticerr(ip, "addr-map overall length < 1", 0);
228 rc= Tcl_ListObjIndex(ip,o,0,&eo); if (rc) goto x_badvalue_rc;
229 rc= Tcl_GetIntFromObj(ip,eo,&bitlen); if (rc) goto x_badvalue_rc;
231 if (bitlen<0 || bitlen % 8) {
232 rc= staticerr(ip, "addr-map overall length < 1", 0);
237 am_reallocentries(am, (inlen-1)*2+1);
239 ame= ame_sfa_alloc(am);
240 memset(ame->start,0,am->byl);
242 for (innum=1; innum < inlen; innum++) {
243 rc= Tcl_ListObjIndex(ip,o,innum,&eo); if (rc) goto x_badvalue_rc;
244 rc= Tcl_ListObjLength(ip,eo,&eol); if (rc) goto x_badvalue_rc;
247 rc= staticerr(ip, "addr-map entry length != 3", 0);
250 rc= Tcl_ListObjIndex(ip,eo,0,&starto); if (rc) goto x_badvalue_rc;
251 rc= Tcl_ListObjIndex(ip,eo,1,&endo); if (rc) goto x_badvalue_rc;
253 rc= pat_hb(ip,starto,&starthb); if (rc) goto x_badvalue_rc;
254 rc= pat_hb(ip,endo,&endhb); if (rc) goto x_badvalue_rc;
256 rc= ame_parsecheck_range(ip,am,&starthb,&endhb,rangeptrs);
257 if (rc) goto x_badvalue_rc;
259 cmp= memcmp(ame->start, rangeptrs[0], am->byl);
261 rc= staticerr(ip, "addr-map entries out of order", 0);
265 ame= ame_sfa_alloc(am);
266 memcpy(ame->start, rangeptrs[0], am->byl);
270 rc= Tcl_ListObjIndex(ip,eo,2,&ame->data); if (rc) goto x_badvalue_rc;
271 Tcl_IncrRefCount(ame->data);
273 ame= ame_sfa_alloc(am);
274 rc= ame_ba_addsubtractone(ame->start, rangeptrs[1], am->byl,
275 /*add:*/ 0x01u, 0x00u);
277 /* we've overflowed. it must have been ffffffff.... */
278 if (innum != inlen-1) {
279 rc= staticerr(ip, "addr-map non-last entry end is all-bits-1", 0);
290 o->internalRep.otherValuePtr= am;
291 o->typePtr= &addrmap_type;
296 Tcl_SetObjErrorCode(ip, Tcl_NewStringObj("HBYTES ADDRMAP VALUE", -1));
302 Tcl_ObjType addrmap_type = {
304 addrmap_t_free, addrmap_t_dup, addrmap_t_ustr, addrmap_t_sfa