chiark / gitweb /
e0c7f90ee6aa1b44e27004449aba93a23755c176
[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 *prefixleno,
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, prefixlen, wantprefixbytes;
46   int rc;
47
48   hbytes_empty(&prefix);
49   
50   rc= pat_hb(ip,prefixo,&prefix);  if (rc) goto x_rc;
51   rc= pat_int(ip,prefixleno,&prefixlen);  if (rc) goto x_rc;
52
53   wantprefixbytes= prefix_bytes(prefixlen);
54   suppliedprefixbytes= hbytes_len(&prefix);
55
56   if (suppliedprefixbytes < wantprefixbytes) {
57     rc= staticerr(ip, "mask-map entry PREFIX too short for PREFIX-LEN",
58                   "HBYTES MASKMAP SYNTAX UNDERRUN");
59     goto x_rc;
60   }
61   if (inmap && suppliedprefixbytes > wantprefixbytes) {
62     rc= staticerr(ip, "mask-map existing entry PREFIX too long for PREFIX-LEN",
63                   "HBYTES MASKMAP SYNTAX OVERRUN");
64     goto x_rc;
65   }
66   mme->prefixlen= prefixlen;
67   mme->prefix= TALLOC(wantprefixbytes);  assert(mme->prefix);
68   memcpy(mme->prefix, hbytes_data(&prefix), wantprefixbytes);
69   return TCL_OK;
70
71  x_rc:
72   mme_free(mme);
73   return rc;
74 }
75
76 static int mme_ordercompare(MaskMap_Entry *a, MaskMap_Entry *b) {
77   if (a->prefixlen != b->prefixlen)
78     return a->prefixlen - b->prefixlen;
79   else
80     return memcmp(b->prefix, a->prefix, prefix_bytes(a->prefixlen));
81 }
82
83 /*---------- useful operations on MaskMap_Value etc. ----------*/
84
85 static int mm_count(const MaskMap_Value *mm) {
86   int i;
87   for (i=0; i<mm->allocd && mm->entries[i].prefixlen != -1; i++);
88   return i;
89 }
90
91 static void mm_allocentries(MaskMap_Value *mm, int len) {
92   int i;
93   mm->allocd= len;
94   mm->entries= TALLOC(sizeof(*mm->entries) * len);
95   assert(mm->entries);
96   for (i=0; i < len; i++)
97     mme_init(&mm->entries[i]);
98 }
99   
100 /*---------- substantial operations on mask maps ----------*/
101
102 int do_maskmap_amend(ClientData cd, Tcl_Interp *ip,
103                      MaskMap_Var map, HBytes_Value prefix,
104                      int preflen, Tcl_Obj *data) {
105   return TCL_OK; /*fixme*/
106 }
107
108 int do_maskmap_lookup(ClientData cd, Tcl_Interp *ip,
109                       MaskMap_Var map, HBytes_Value addr, Tcl_Obj *def,
110                       Tcl_Obj **result) {
111   *result= Tcl_NewIntObj(42);
112   return TCL_OK; /*fixme*/
113 }
114
115 /*---------- Tcl type and arg parsing functions ----------*/
116
117 int pat_maskmapv(Tcl_Interp *ip, Tcl_Obj *var, MaskMap_Var *agg) {
118   int rc;
119   rc= pat_somethingv(ip,var,&agg->sth,&maskmap_type);  if (rc) return rc;
120   agg->mm= (void*)&agg->sth.obj->internalRep;
121   return TCL_OK;
122 }
123
124 static void maskmap_t_free(Tcl_Obj *o) {
125   MaskMap_Value *mm= (void*)&o->internalRep;
126   MaskMap_Entry *mme;
127   int i;
128   
129   for (i=0, mme=mm->entries; i<mm->allocd; i++, mme++) {
130     if (mme->prefixlen==-1) break;
131     mme_free(mme);
132   }
133 }
134
135 static void maskmap_t_dup(Tcl_Obj *sob, Tcl_Obj *dob) {
136   MaskMap_Value *sm= (void*)&sob->internalRep;
137   MaskMap_Value *dm= (void*)&dob->internalRep;
138   MaskMap_Entry *sme, *dme;
139   int l, i, nbytes;
140
141   assert(sob->typePtr == &maskmap_type);
142   objfreeir(dob);
143   l= mm_count(sm);
144
145   mm_allocentries(dm,l);
146   for (i=0, sme=sm->entries, dme=dm->entries;
147        i<dm->allocd;
148        i++, sme++, dme++) {
149     *dme= *sme;
150     nbytes= prefix_bytes(sme->prefixlen);
151     dme->prefix= TALLOC(nbytes);  assert(dme->prefix);
152     memcpy(dme->prefix, sme->prefix, nbytes);
153     Tcl_IncrRefCount(dme->data);
154   }
155   dob->typePtr= &maskmap_type;
156 }
157
158 static void maskmap_t_ustr(Tcl_Obj *so) {
159   MaskMap_Value *sm= (void*)&so->internalRep;
160   Tcl_Obj **mainlobjsl, *mainobj;
161   int i, l;
162
163   assert(so->typePtr == &maskmap_type);
164   l= mm_count(sm);
165   mainlobjsl= TALLOC(sizeof(*mainlobjsl) * l);  assert(mainlobjsl);
166
167   for (i=0; i<l; i++) {
168     MaskMap_Entry *sme= &sm->entries[i];
169     HBytes_Value hb;
170     Tcl_Obj *subl[3], *sublo;
171
172     hbytes_array(&hb, sme->prefix, prefix_bytes(sme->prefixlen));
173     subl[0]= ret_hb(0, hb);  assert(subl[0]);
174     subl[1]= Tcl_NewIntObj(sme->prefixlen);  assert(subl[1]);
175     subl[2]= sme->data;
176     
177     sublo= Tcl_NewListObj(3,subl);  assert(sublo);
178     mainlobjsl[i]= sublo;
179   }
180
181   mainobj= Tcl_NewListObj(l,mainlobjsl);  assert(mainobj);
182   so->bytes= Tcl_GetStringFromObj(mainobj, &so->length);  assert(so->bytes);
183   mainobj->bytes= 0; mainobj->length= 0; /* we stole it */
184 }
185
186 static int maskmap_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
187   int rc, len, eol, i;
188   MaskMap_Value mm;
189   Tcl_Obj *eo, *prefixo, *prefixleno;
190
191   mm.allocd= 0;
192   mm.entries= 0;
193   
194   rc= Tcl_ListObjLength(ip,o,&len);  if (rc) goto x_rc;
195   mm_allocentries(&mm, len);
196   
197   for (i=0; i<len; i++) {
198     rc= Tcl_ListObjIndex(ip,o,i,&eo);  if (rc) goto x_rc;
199     rc= Tcl_ListObjLength(ip,eo,&eol);  if (rc) goto x_rc;
200     if (eol != 3) {
201       rc= staticerr(ip, "mask-map entry length != 3",
202                     "HBYTES MASKMAP SYNTAX LLENGTH");
203       goto x_rc;
204     }
205     rc= Tcl_ListObjIndex(ip,eo,0,&prefixo);  if (rc) goto x_rc;
206     rc= Tcl_ListObjIndex(ip,eo,1,&prefixleno);  if (rc) goto x_rc;
207     rc= Tcl_ListObjIndex(ip,eo,2,&mm.entries[i].data);  if (rc) goto x_rc;
208     Tcl_IncrRefCount(mm.entries[i].data);
209
210     rc= mme_parsekey(ip, &mm.entries[i], prefixo, prefixleno, 1);
211     if (rc) goto x_rc;
212
213     if (i>0) {
214       if (mme_ordercompare(&mm.entries[i-1], &mm.entries[i]) <= 0) {
215         rc= staticerr(ip, "mask-map entries out of order",
216                       "HBYTES MASKMAP SYNTAX ORDER");
217         goto x_rc;
218       }
219     }
220   }
221
222   /* we commit now */
223   assert(sizeof(mm) <= sizeof(o->internalRep));
224   objfreeir(o);
225   memcpy(&o->internalRep, &mm, sizeof(mm));
226   o->typePtr= &maskmap_type;
227   return TCL_OK;
228
229 x_rc:
230   if (mm.entries) {
231     for (i=0; i<len; i++)
232       mme_free(&mm.entries[i]);
233     TFREE(mm.entries);
234   }
235   return rc;
236 }
237
238 Tcl_ObjType maskmap_type = {
239   "mask-map",
240   maskmap_t_free, maskmap_t_dup, maskmap_t_ustr, maskmap_t_sfa
241 };