chiark / gitweb /
Can parse a key file. Cleared up raw confusion.
[chiark-tcl.git] / base / hook.c
1 /*
2  */
3
4 #include <errno.h>
5
6 #include "hbytes.h"
7 #include "tables.h"
8
9 int staticerr(Tcl_Interp *ip, const char *m) {
10   Tcl_SetResult(ip, (char*)m, TCL_STATIC);
11   return TCL_ERROR;
12 }
13
14 int posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
15   const char *em;
16   
17   Tcl_ResetResult(ip);
18   errno= errnoval;
19   em= Tcl_PosixError(ip);
20   Tcl_AppendResult(ip, m, ": ", em, (char*)0);
21   return TCL_ERROR;
22 }
23
24 void objfreeir(Tcl_Obj *o) {
25   if (o->typePtr && o->typePtr->freeIntRepProc)
26     o->typePtr->freeIntRepProc(o);
27   o->typePtr= 0;
28 }  
29
30 int do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip,
31                        Tcl_Obj *obj, Tcl_Obj **result) {
32   const char *tn;
33   int nums[3], i, lnl;
34   Tcl_Obj *objl[4];
35
36   if (obj->typePtr == &hbytes_type) {
37     HBytes_Value *v= OBJ_HBYTES(obj);
38     memset(nums,0,sizeof(nums));
39     nums[1]= hbytes_len(v);
40   
41     if (HBYTES_ISEMPTY(v)) tn= "empty";
42     else if (HBYTES_ISSENTINEL(v)) tn= "sentinel!";
43     else if (HBYTES_ISSIMPLE(v)) tn= "simple";
44     else {
45       HBytes_ComplexValue *cx= v->begin_complex;
46       tn= "complex";
47       nums[0]= cx->prespace;
48       nums[2]= cx->avail - cx->len;
49     }
50     lnl= 3;
51   } else {
52     tn= "other";
53     lnl= 0;
54   }
55     
56   objl[0]= Tcl_NewStringObj((char*)tn,-1);
57   for (i=0; i<lnl; i++) objl[i+1]= Tcl_NewIntObj(nums[i]);
58   *result= Tcl_NewListObj(lnl+1,objl);
59     
60   return TCL_OK;
61 }
62
63 static void hbytes_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
64   objfreeir(dup);
65   hbytes_array(OBJ_HBYTES(dup),
66                hbytes_data(OBJ_HBYTES(src)),
67                hbytes_len(OBJ_HBYTES(src)));
68 }
69
70 static void hbytes_t_free(Tcl_Obj *o) {
71   hbytes_free(OBJ_HBYTES(o));
72 }
73
74 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
75                                 int l, const char *prefix) {
76   char *str;
77   int pl;
78
79   pl= strlen(prefix);
80   o->length= l*2+pl;
81   str= o->bytes= TALLOC(o->length+1);
82   
83   memcpy(str,prefix,pl);
84   str += pl;
85
86   while (l>0) {
87     sprintf(str,"%02x",*byte);
88     str+=2; byte++; l--;
89   }
90   *str= 0;
91 }
92
93 void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
94   obj_updatestr_array_prefix(o,byte,l,"");
95 }
96
97 void obj_updatestr_vstringls(Tcl_Obj *o, ...) {
98   va_list al;
99   char *p;
100   const char *part;
101   int l, pl;
102
103   va_start(al,o);
104   for (l=0; (part= va_arg(al, const char*)); )
105     l+= va_arg(al, int);
106   va_end(al);
107   
108   o->length= l;
109   o->bytes= TALLOC(l+1);
110
111   va_start(al,o);
112   for (p= o->bytes; (part= va_arg(al, const char*)); p += pl) {
113     pl= va_arg(al, int);
114     memcpy(p, part, pl);
115   }
116   va_end(al);
117
118   *p= 0;
119 }
120
121 void obj_updatestr_string(Tcl_Obj *o, const char *str) {
122   obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
123 }
124
125 static void hbytes_t_ustr(Tcl_Obj *o) {
126   obj_updatestr_array(o,
127                       hbytes_data(OBJ_HBYTES(o)),
128                       hbytes_len(OBJ_HBYTES(o)));
129 }
130
131 static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
132   char *str, *ep, *os;
133   Byte *startbytes, *bytes;
134   int l;
135   char cbuf[3];
136
137   os= str= Tcl_GetStringFromObj(o,&l);  assert(str);
138   objfreeir(o);
139
140   if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
141                               " odd length in hex");
142
143   startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
144
145   cbuf[2]= 0;
146   while (l>0) {
147     cbuf[0]= *str++;
148     cbuf[1]= *str++;
149     *bytes++= strtoul(cbuf,&ep,16);
150     if (ep != cbuf+2) {
151       hbytes_free(OBJ_HBYTES(o));
152       return staticerr(ip, "hbytes: conversion from hex:"
153                        " bad hex digit");
154     }
155     l -= 2;
156   }
157
158   o->typePtr = &hbytes_type;
159   return TCL_OK;
160 }
161
162 Tcl_ObjType hbytes_type = {
163   "hbytes",
164   hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
165 };
166
167 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
168                     Tcl_Obj *binary, HBytes_Value *result) {
169   const unsigned char *str;
170   int l;
171
172   str= Tcl_GetByteArrayFromObj(binary,&l);
173   hbytes_array(result, str, l);
174   return TCL_OK;
175 }
176
177 int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
178                     HBytes_Value hex, Tcl_Obj **result) {
179   *result= Tcl_NewByteArrayObj(hbytes_data(&hex), hbytes_len(&hex));
180   return TCL_OK;
181 }
182
183 int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
184                      HBytes_Value v, int *result) {
185   *result= hbytes_len(&v);
186   return TCL_OK;
187 }
188
189 int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
190                      int length, HBytes_Value *result) {
191   Byte *space;
192   int rc;
193   
194   space= hbytes_arrayspace(result, length);
195   rc= get_urandom(ip, space, length);
196   if (rc) { hbytes_free(result); return rc; }
197   return TCL_OK;
198 }  
199   
200 int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
201                      int length, HBytes_Value *result) {
202   Byte *space;
203   space= hbytes_arrayspace(result, length);
204   memset(space,0,length);
205   return TCL_OK;
206 }
207
208 int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
209                       HBytes_Value a, HBytes_Value b, int *result) {
210   int al, bl, minl, r;
211
212   al= hbytes_len(&a);
213   bl= hbytes_len(&b);
214   minl= al<bl ? al : bl;
215
216   r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
217   
218   if (r<0) *result= -2;
219   else if (r>0) *result= +2;
220   else {
221     if (al<bl) *result= -1;
222     else if (al>bl) *result= +1;
223     else *result= 0;
224   }
225   return TCL_OK;
226 }
227
228 int do_hbytes_range(ClientData cd, Tcl_Interp *ip,
229                     HBytes_Value v, int start, int size,
230                     HBytes_Value *result) {
231   const Byte *data;
232   int l;
233
234   l= hbytes_len(&v);
235   if (start<0 || size<0 || l<start+size)
236     return staticerr(ip, "hbytes range subscripts out of range");
237
238   data= hbytes_data(&v);
239   hbytes_array(result, data+start, size);
240   return TCL_OK;
241 }
242   
243 int do_toplevel_hbytes(ClientData cd, Tcl_Interp *ip,
244                        const HBytes_SubCommand *subcmd,
245                        int objc, Tcl_Obj *const *objv) {
246   return subcmd->func(0,ip,objc,objv);
247 }
248
249 int do_toplevel_dgram_socket(ClientData cd, Tcl_Interp *ip,
250                              const DgramSocket_SubCommand *subcmd,
251                              int objc, Tcl_Obj *const *objv) {
252   return subcmd->func(0,ip,objc,objv);
253 }
254
255 int do_toplevel_ulong(ClientData cd, Tcl_Interp *ip,
256                       const ULong_SubCommand *subcmd,
257                       int objc, Tcl_Obj *const *objv) {
258   return subcmd->func(0,ip,objc,objv);
259 }
260
261 #define URANDOM "/dev/urandom"
262
263 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
264   static FILE *urandom;
265
266   int r, esave;
267
268   if (!urandom) {
269     urandom= fopen(URANDOM,"rb");
270     if (!urandom) return posixerr(ip,errno,"open " URANDOM);
271   }
272   r= fread(buffer,1,l,urandom);
273   if (r==l) return 0;
274
275   esave= errno;
276   fclose(urandom); urandom=0;
277
278   if (ferror(urandom)) {
279     return posixerr(ip,errno,"read " URANDOM);
280   } else {
281     assert(feof(urandom));
282     return staticerr(ip, URANDOM " gave eof!");
283   }
284 }
285
286 int Hbytes_Init(Tcl_Interp *ip) {
287   const TopLevel_Command *cmd;
288
289   Tcl_RegisterObjType(&hbytes_type);
290   Tcl_RegisterObjType(&blockcipherkey_type);
291   Tcl_RegisterObjType(&enum_nearlytype);
292   Tcl_RegisterObjType(&enum1_nearlytype);
293   Tcl_RegisterObjType(&sockaddr_type);
294   Tcl_RegisterObjType(&dgramsockid_type);
295   Tcl_RegisterObjType(&ulong_type);
296
297   for (cmd=toplevel_commands;
298        cmd->name;
299        cmd++)
300     Tcl_CreateObjCommand(ip, cmd->name, cmd->func, 0,0);
301
302   return TCL_OK;
303 }