chiark / gitweb /
check for extra bits
[chiark-tcl.git] / maskmap / maskmap.c
1 /*
2  */
3
4 #include <string.h>
5
6 #include "tables.h"
7 #include "hbytes.h"
8
9 typedef struct {
10   int prefixlen; /* there may be some empty slots with prefixlen==-1 at end */
11   Byte *prefix; /* ceil(prefixlen/8) bytes */
12   Tcl_Obj *data;
13 } MaskMap_Entry;
14
15 struct MaskMap_Value {
16   int allocd;
17   MaskMap_Entry *entries;
18 }; /* overlays internalRep */
19
20 /*---------- useful stuff ----------*/
21
22 static int prefix_bytes (int prefixlen) {
23   return (prefixlen + 7)/8;
24 }
25
26 /*---------- operations on MaskMap_Entry ----------*/
27
28 static void mme_init(MaskMap_Entry *mme) {
29   mme->prefix= 0;
30   mme->data= 0;
31 }
32
33 static void mme_free(MaskMap_Entry *mme) {
34   TFREE(mme->prefix);  mme->prefix=0;
35   if (mme->data) { Tcl_DecrRefCount(mme->data); mme->data=0; }
36 }
37
38 static int mme_parsekey(Tcl_Interp *ip, MaskMap_Entry *mme,
39                         Tcl_Obj *prefixo, Tcl_Obj *prefixbitso,
40                         int inmap) {
41  /* *mme should be blank entry; after exit (even error exit) it will be valid
42   * - on errors, it will be blank.  inmap is 1 if we're parsing an existing
43   * map or 0 if it's an entry to be added or modified. */
44   HBytes_Value prefix;
45   int suppliedprefixbytes, prefixbits, wantprefixbytes, sparebits;
46   const Byte *data;
47   int rc;
48
49   hbytes_empty(&prefix);
50   
51   rc= pat_hb(ip,prefixo,&prefix);  if (rc) goto x_rc;
52   rc= pat_int(ip,prefixbitso,&prefixbits);  if (rc) goto x_rc;
53
54   wantprefixbytes= prefix_bytes(prefixbits);
55   suppliedprefixbytes= hbytes_len(&prefix);
56
57   if (suppliedprefixbytes < wantprefixbytes) {
58     rc= staticerr(ip, "mask-map entry PREFIX too short for PREFIX-LEN",
59                   "HBYTES MASKMAP SYNTAX UNDERRUN");
60     goto x_rc;
61   }
62   if (inmap && suppliedprefixbytes > wantprefixbytes) {
63     rc= staticerr(ip, "mask-map existing entry PREFIX too long for PREFIX-LEN",
64                   "HBYTES MASKMAP SYNTAX OVERRUN");
65     goto x_rc;
66   }
67   sparebits= wantprefixbytes*8 - prefixbits;
68   data= hbytes_data(&prefix);
69   if (sparebits && (data[wantprefixbytes-1] & ((1u << sparebits)-1))) {
70     rc= staticerr(ip, "mask-map entry PREFIX contains bits excluded"
71                   " by PREFIX-LEN", "HBYTES MASKMAP SYNTAX EXCLBITS");
72     goto x_rc;
73   }
74
75   mme->prefixlen= prefixbits;
76   mme->prefix= TALLOC(wantprefixbytes);  assert(mme->prefix);
77   memcpy(mme->prefix, data, wantprefixbytes);
78   return TCL_OK;
79
80  x_rc:
81   mme_free(mme);
82   return rc;
83 }
84
85 static int mme_ordercompare(MaskMap_Entry *a, MaskMap_Entry *b) {
86   if (a->prefixlen != b->prefixlen)
87     return a->prefixlen - b->prefixlen;
88   else
89     return memcmp(b->prefix, a->prefix, prefix_bytes(a->prefixlen));
90 }
91
92 /*---------- useful operations on MaskMap_Value etc. ----------*/
93
94 static int mm_count(const MaskMap_Value *mm) {
95   int i;
96   for (i=0; i<mm->allocd && mm->entries[i].prefixlen != -1; i++);
97   return i;
98 }
99
100 static void mm_allocentries(MaskMap_Value *mm, int len) {
101   int i;
102   mm->allocd= len;
103   mm->entries= TALLOC(sizeof(*mm->entries) * len);
104   assert(mm->entries);
105   for (i=0; i < len; i++)
106     mme_init(&mm->entries[i]);
107 }
108   
109 /*---------- substantial operations on mask maps ----------*/
110
111 int do_maskmap_amend(ClientData cd, Tcl_Interp *ip,
112                      MaskMap_Var map, HBytes_Value prefix,
113                      int preflen, Tcl_Obj *data) {
114   
115   return TCL_OK;
116 }
117
118 int do_maskmap_lookup(ClientData cd, Tcl_Interp *ip,
119                       Tcl_Obj *mapo, HBytes_Value addr, Tcl_Obj *def,
120                       Tcl_Obj **result) {
121   /*MaskMap_Var *map= (void*)&mapo->internalRep;*/
122   *result= Tcl_NewIntObj(42);
123   return TCL_OK; /*fixme*/
124 }
125
126 /*---------- Tcl type and arg parsing functions ----------*/
127
128 int pat_maskmapv(Tcl_Interp *ip, Tcl_Obj *var, MaskMap_Var *agg) {
129   int rc;
130   rc= pat_somethingv(ip,var,&agg->sth,&maskmap_type);  if (rc) return rc;
131   agg->mm= (void*)&agg->sth.obj->internalRep;
132   return TCL_OK;
133 }
134
135 static void maskmap_t_free(Tcl_Obj *o) {
136   MaskMap_Value *mm= (void*)&o->internalRep;
137   MaskMap_Entry *mme;
138   int i;
139   
140   for (i=0, mme=mm->entries; i<mm->allocd; i++, mme++) {
141     if (mme->prefixlen==-1) break;
142     mme_free(mme);
143   }
144 }
145
146 static void maskmap_t_dup(Tcl_Obj *sob, Tcl_Obj *dob) {
147   MaskMap_Value *sm= (void*)&sob->internalRep;
148   MaskMap_Value *dm= (void*)&dob->internalRep;
149   MaskMap_Entry *sme, *dme;
150   int l, i, nbytes;
151
152   assert(sob->typePtr == &maskmap_type);
153   objfreeir(dob);
154   l= mm_count(sm);
155
156   mm_allocentries(dm,l);
157   for (i=0, sme=sm->entries, dme=dm->entries;
158        i<dm->allocd;
159        i++, sme++, dme++) {
160     *dme= *sme;
161     nbytes= prefix_bytes(sme->prefixlen);
162     dme->prefix= TALLOC(nbytes);  assert(dme->prefix);
163     memcpy(dme->prefix, sme->prefix, nbytes);
164     Tcl_IncrRefCount(dme->data);
165   }
166   dob->typePtr= &maskmap_type;
167 }
168
169 static void maskmap_t_ustr(Tcl_Obj *so) {
170   MaskMap_Value *sm= (void*)&so->internalRep;
171   Tcl_Obj **mainlobjsl, *mainobj;
172   int i, l;
173
174   assert(so->typePtr == &maskmap_type);
175   l= mm_count(sm);
176   mainlobjsl= TALLOC(sizeof(*mainlobjsl) * l);  assert(mainlobjsl);
177
178   for (i=0; i<l; i++) {
179     MaskMap_Entry *sme= &sm->entries[i];
180     HBytes_Value hb;
181     Tcl_Obj *subl[3], *sublo;
182
183     hbytes_array(&hb, sme->prefix, prefix_bytes(sme->prefixlen));
184     subl[0]= ret_hb(0, hb);  assert(subl[0]);
185     subl[1]= Tcl_NewIntObj(sme->prefixlen);  assert(subl[1]);
186     subl[2]= sme->data;
187     
188     sublo= Tcl_NewListObj(3,subl);  assert(sublo);
189     mainlobjsl[i]= sublo;
190   }
191
192   mainobj= Tcl_NewListObj(l,mainlobjsl);  assert(mainobj);
193   so->bytes= Tcl_GetStringFromObj(mainobj, &so->length);  assert(so->bytes);
194   mainobj->bytes= 0; mainobj->length= 0; /* we stole it */
195 }
196
197 static int maskmap_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
198   int rc, len, eol, i;
199   MaskMap_Value mm;
200   Tcl_Obj *eo, *prefixo, *prefixleno;
201
202   mm.allocd= 0;
203   mm.entries= 0;
204   
205   rc= Tcl_ListObjLength(ip,o,&len);  if (rc) goto x_rc;
206   mm_allocentries(&mm, len);
207   
208   for (i=0; i<len; i++) {
209     rc= Tcl_ListObjIndex(ip,o,i,&eo);  if (rc) goto x_rc;
210     rc= Tcl_ListObjLength(ip,eo,&eol);  if (rc) goto x_rc;
211     if (eol != 3) {
212       rc= staticerr(ip, "mask-map entry length != 3",
213                     "HBYTES MASKMAP SYNTAX LLENGTH");
214       goto x_rc;
215     }
216     rc= Tcl_ListObjIndex(ip,eo,0,&prefixo);  if (rc) goto x_rc;
217     rc= Tcl_ListObjIndex(ip,eo,1,&prefixleno);  if (rc) goto x_rc;
218     rc= Tcl_ListObjIndex(ip,eo,2,&mm.entries[i].data);  if (rc) goto x_rc;
219     Tcl_IncrRefCount(mm.entries[i].data);
220
221     rc= mme_parsekey(ip, &mm.entries[i], prefixo, prefixleno, 1);
222     if (rc) goto x_rc;
223
224     if (i>0) {
225       if (mme_ordercompare(&mm.entries[i-1], &mm.entries[i]) <= 0) {
226         rc= staticerr(ip, "mask-map entries out of order",
227                       "HBYTES MASKMAP SYNTAX ORDER");
228         goto x_rc;
229       }
230     }
231   }
232
233   /* we commit now */
234   assert(sizeof(mm) <= sizeof(o->internalRep));
235   objfreeir(o);
236   memcpy(&o->internalRep, &mm, sizeof(mm));
237   o->typePtr= &maskmap_type;
238   return TCL_OK;
239
240 x_rc:
241   if (mm.entries) {
242     for (i=0; i<len; i++)
243       mme_free(&mm.entries[i]);
244     TFREE(mm.entries);
245   }
246   return rc;
247 }
248
249 Tcl_ObjType maskmap_type = {
250   "mask-map",
251   maskmap_t_free, maskmap_t_dup, maskmap_t_ustr, maskmap_t_sfa
252 };