chiark / gitweb /
61593aba5a1bd5abb6a40c7040308772a2ce9978
[chiark-tcl.git] / hbytes / 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   hbytes_array(OBJ_HBYTES(dup),
65                hbytes_data(OBJ_HBYTES(src)),
66                hbytes_len(OBJ_HBYTES(src)));
67   dup->typePtr= &hbytes_type;
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   if (o->typePtr == &ulong_type) {
138     uint32_t ul;
139
140     ul= htonl(*(const uint32_t*)&o->internalRep.longValue);
141     hbytes_array(OBJ_HBYTES(o), (const Byte*)&ul, 4);
142
143   } else {
144   
145     os= str= Tcl_GetStringFromObj(o,&l);  assert(str);
146     objfreeir(o);
147
148     if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
149                                 " odd length in hex");
150
151     startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
152
153     cbuf[2]= 0;
154     while (l>0) {
155       cbuf[0]= *str++;
156       cbuf[1]= *str++;
157       *bytes++= strtoul(cbuf,&ep,16);
158       if (ep != cbuf+2) {
159         hbytes_free(OBJ_HBYTES(o));
160         return staticerr(ip, "hbytes: conversion from hex:"
161                          " bad hex digit");
162       }
163       l -= 2;
164     }
165
166   }
167
168   o->typePtr = &hbytes_type;
169   return TCL_OK;
170 }
171
172 Tcl_ObjType hbytes_type = {
173   "hbytes",
174   hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
175 };
176
177 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
178                     Tcl_Obj *binary, HBytes_Value *result) {
179   const unsigned char *str;
180   int l;
181
182   str= Tcl_GetByteArrayFromObj(binary,&l);
183   hbytes_array(result, str, l);
184   return TCL_OK;
185 }
186
187 int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
188                     HBytes_Value hex, Tcl_Obj **result) {
189   *result= Tcl_NewByteArrayObj(hbytes_data(&hex), hbytes_len(&hex));
190   return TCL_OK;
191 }
192
193 int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
194                      HBytes_Value v, int *result) {
195   *result= hbytes_len(&v);
196   return TCL_OK;
197 }
198
199 int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
200                      int length, HBytes_Value *result) {
201   Byte *space;
202   int rc;
203   
204   space= hbytes_arrayspace(result, length);
205   rc= get_urandom(ip, space, length);
206   if (rc) { hbytes_free(result); return rc; }
207   return TCL_OK;
208 }  
209   
210 int do_hbytes_overwrite(ClientData cd, Tcl_Interp *ip,
211                         HBytes_Var v, int start, HBytes_Value sub) {
212   int sub_l;
213
214   sub_l= hbytes_len(&sub);
215   if (start < 0)
216     return staticerr(ip, "hbytes overwrite start -ve");
217   if (start + sub_l > hbytes_len(v.hb))
218     return staticerr(ip, "hbytes overwrite out of range");
219   memcpy(hbytes_data(v.hb) + start, hbytes_data(&sub), sub_l);
220   return TCL_OK;
221 }
222
223 int do_hbytes_trimleft(ClientData cd, Tcl_Interp *ip, HBytes_Var v) {
224   const Byte *o, *p, *e;
225   o= p= hbytes_data(v.hb);
226   e= p + hbytes_len(v.hb);
227
228   while (p<e && !*p) p++;
229   if (p != o)
230     hbytes_unprepend(v.hb, p-o);
231
232   return TCL_OK;
233 }
234
235 int do_hbytes_repeat(ClientData cd, Tcl_Interp *ip,
236                      HBytes_Value sub, int count, HBytes_Value *result) {
237   int sub_l;
238   Byte *data;
239   const Byte *sub_d;
240
241   sub_l= hbytes_len(&sub);
242   if (count < 0) return staticerr(ip, "hbytes repeat count -ve");
243   if (count > INT_MAX/sub_l) return staticerr(ip, "hbytes repeat too long");
244
245   data= hbytes_arrayspace(result, sub_l*count);
246   sub_d= hbytes_data(&sub);
247   while (count) {
248     memcpy(data, sub_d, sub_l);
249     count--; data += sub_l;
250   }
251   return TCL_OK;
252 }  
253
254 int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
255                      int length, HBytes_Value *result) {
256   Byte *space;
257   space= hbytes_arrayspace(result, length);
258   memset(space,0,length);
259   return TCL_OK;
260 }
261
262 int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
263                       HBytes_Value a, HBytes_Value b, int *result) {
264   int al, bl, minl, r;
265
266   al= hbytes_len(&a);
267   bl= hbytes_len(&b);
268   minl= al<bl ? al : bl;
269
270   r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
271   
272   if (r<0) *result= -2;
273   else if (r>0) *result= +2;
274   else {
275     if (al<bl) *result= -1;
276     else if (al>bl) *result= +1;
277     else *result= 0;
278   }
279   return TCL_OK;
280 }
281
282 int do_hbytes_range(ClientData cd, Tcl_Interp *ip,
283                     HBytes_Value v, int start, int size,
284                     HBytes_Value *result) {
285   const Byte *data;
286   int l;
287
288   l= hbytes_len(&v);
289   if (start<0 || size<0 || l<start+size)
290     return staticerr(ip, "hbytes range subscripts out of range");
291
292   data= hbytes_data(&v);
293   hbytes_array(result, data+start, size);
294   return TCL_OK;
295 }
296
297 /* hbytes representing uint16_t's */
298
299 int do_hbytes_h2ushort(ClientData cd, Tcl_Interp *ip,
300                        HBytes_Value hex, long *result) {
301   const Byte *data;
302   int l;
303
304   l= hbytes_len(&hex);
305   if (l>2)
306     return staticerr(ip, "hbytes h2ushort input more than 4 hex digits");
307
308   data= hbytes_data(&hex);
309   *result= data[l-1] | (l>1 ? data[0]<<8 : 0);
310   return TCL_OK;
311 }
312
313 int do_hbytes_ushort2h(ClientData cd, Tcl_Interp *ip,
314                        long input, HBytes_Value *result) {
315   uint16_t us;
316
317   if (input > 0x0ffff)
318     return staticerr(ip, "hbytes ushort2h input >2^16");
319
320   us= htons(input);
321   hbytes_array(result,(const Byte*)&us,2);
322   return TCL_OK;
323 }
324
325 /* toplevel functions */
326
327 int do_toplevel_hbytes(ClientData cd, Tcl_Interp *ip,
328                        const HBytes_SubCommand *subcmd,
329                        int objc, Tcl_Obj *const *objv) {
330   return subcmd->func(0,ip,objc,objv);
331 }
332
333 int do_toplevel_dgram_socket(ClientData cd, Tcl_Interp *ip,
334                              const DgramSocket_SubCommand *subcmd,
335                              int objc, Tcl_Obj *const *objv) {
336   return subcmd->func(0,ip,objc,objv);
337 }
338
339 int do_toplevel_ulong(ClientData cd, Tcl_Interp *ip,
340                       const ULong_SubCommand *subcmd,
341                       int objc, Tcl_Obj *const *objv) {
342   return subcmd->func(0,ip,objc,objv);
343 }
344
345 #define URANDOM "/dev/urandom"
346
347 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
348   static FILE *urandom;
349
350   int r, esave;
351
352   if (!urandom) {
353     urandom= fopen(URANDOM,"rb");
354     if (!urandom) return posixerr(ip,errno,"open " URANDOM);
355   }
356   r= fread(buffer,1,l,urandom);
357   if (r==l) return 0;
358
359   esave= errno;
360   fclose(urandom); urandom=0;
361
362   if (ferror(urandom)) {
363     return posixerr(ip,errno,"read " URANDOM);
364   } else {
365     assert(feof(urandom));
366     return staticerr(ip, URANDOM " gave eof!");
367   }
368 }
369
370 int Hbytes_Init(Tcl_Interp *ip) {
371   const TopLevel_Command *cmd;
372
373   Tcl_RegisterObjType(&hbytes_type);
374   Tcl_RegisterObjType(&blockcipherkey_type);
375   Tcl_RegisterObjType(&enum_nearlytype);
376   Tcl_RegisterObjType(&enum1_nearlytype);
377   Tcl_RegisterObjType(&sockaddr_type);
378   Tcl_RegisterObjType(&dgramsockid_type);
379   Tcl_RegisterObjType(&ulong_type);
380
381   for (cmd=toplevel_commands;
382        cmd->name;
383        cmd++)
384     Tcl_CreateObjCommand(ip, cmd->name, cmd->func, 0,0);
385
386   return TCL_OK;
387 }