chiark / gitweb /
hbytes xor
[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, const char *ec) {
10   Tcl_SetResult(ip, (char*)m, TCL_STATIC);
11   if (ec) Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(ec,-1));
12   return TCL_ERROR;
13 }
14
15 int posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
16   const char *em;
17   
18   Tcl_ResetResult(ip);
19   errno= errnoval;
20   em= Tcl_PosixError(ip);
21   Tcl_AppendResult(ip, m, ": ", em, (char*)0);
22   return TCL_ERROR;
23 }
24
25 void objfreeir(Tcl_Obj *o) {
26   if (o->typePtr && o->typePtr->freeIntRepProc)
27     o->typePtr->freeIntRepProc(o);
28   o->typePtr= 0;
29 }  
30
31 int do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip,
32                        Tcl_Obj *obj, Tcl_Obj **result) {
33   const char *tn;
34   int nums[3], i, lnl;
35   Tcl_Obj *objl[4];
36
37   if (obj->typePtr == &hbytes_type) {
38     HBytes_Value *v= OBJ_HBYTES(obj);
39     memset(nums,0,sizeof(nums));
40     nums[1]= hbytes_len(v);
41   
42     if (HBYTES_ISEMPTY(v)) tn= "empty";
43     else if (HBYTES_ISSENTINEL(v)) tn= "sentinel!";
44     else if (HBYTES_ISSIMPLE(v)) tn= "simple";
45     else {
46       HBytes_ComplexValue *cx= v->begin_complex;
47       tn= "complex";
48       nums[0]= cx->prespace;
49       nums[2]= cx->avail - cx->len;
50     }
51     lnl= 3;
52   } else {
53     tn= "other";
54     lnl= 0;
55   }
56     
57   objl[0]= Tcl_NewStringObj((char*)tn,-1);
58   for (i=0; i<lnl; i++) objl[i+1]= Tcl_NewIntObj(nums[i]);
59   *result= Tcl_NewListObj(lnl+1,objl);
60     
61   return TCL_OK;
62 }
63
64 static void hbytes_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
65   hbytes_array(OBJ_HBYTES(dup),
66                hbytes_data(OBJ_HBYTES(src)),
67                hbytes_len(OBJ_HBYTES(src)));
68   dup->typePtr= &hbytes_type;
69 }
70
71 static void hbytes_t_free(Tcl_Obj *o) {
72   hbytes_free(OBJ_HBYTES(o));
73 }
74
75 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
76                                 int l, const char *prefix) {
77   char *str;
78   int pl;
79
80   pl= strlen(prefix);
81   o->length= l*2+pl;
82   str= o->bytes= TALLOC(o->length+1);
83   
84   memcpy(str,prefix,pl);
85   str += pl;
86
87   while (l>0) {
88     sprintf(str,"%02x",*byte);
89     str+=2; byte++; l--;
90   }
91   *str= 0;
92 }
93
94 void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
95   obj_updatestr_array_prefix(o,byte,l,"");
96 }
97
98 void obj_updatestr_vstringls(Tcl_Obj *o, ...) {
99   va_list al;
100   char *p;
101   const char *part;
102   int l, pl;
103
104   va_start(al,o);
105   for (l=0; (part= va_arg(al, const char*)); )
106     l+= va_arg(al, int);
107   va_end(al);
108   
109   o->length= l;
110   o->bytes= TALLOC(l+1);
111
112   va_start(al,o);
113   for (p= o->bytes; (part= va_arg(al, const char*)); p += pl) {
114     pl= va_arg(al, int);
115     memcpy(p, part, pl);
116   }
117   va_end(al);
118
119   *p= 0;
120 }
121
122 void obj_updatestr_string(Tcl_Obj *o, const char *str) {
123   obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
124 }
125
126 static void hbytes_t_ustr(Tcl_Obj *o) {
127   obj_updatestr_array(o,
128                       hbytes_data(OBJ_HBYTES(o)),
129                       hbytes_len(OBJ_HBYTES(o)));
130 }
131
132 static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
133   char *str, *ep, *os;
134   Byte *startbytes, *bytes;
135   int l;
136   char cbuf[3];
137
138   if (o->typePtr == &ulong_type) {
139     uint32_t ul;
140
141     ul= htonl(*(const uint32_t*)&o->internalRep.longValue);
142     hbytes_array(OBJ_HBYTES(o), (const Byte*)&ul, 4);
143
144   } else {
145   
146     os= str= Tcl_GetStringFromObj(o,&l);  assert(str);
147     objfreeir(o);
148
149     if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
150                                 " odd length in hex", "HBYTES SYNTAX");
151
152     startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
153
154     cbuf[2]= 0;
155     while (l>0) {
156       cbuf[0]= *str++;
157       cbuf[1]= *str++;
158       *bytes++= strtoul(cbuf,&ep,16);
159       if (ep != cbuf+2) {
160         hbytes_free(OBJ_HBYTES(o));
161         return staticerr(ip, "hbytes: conversion from hex:"
162                          " bad hex digit", "HBYTES SYNTAX");
163       }
164       l -= 2;
165     }
166
167   }
168
169   o->typePtr = &hbytes_type;
170   return TCL_OK;
171 }
172
173 Tcl_ObjType hbytes_type = {
174   "hbytes",
175   hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
176 };
177
178 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
179                     Tcl_Obj *binary, HBytes_Value *result) {
180   const unsigned char *str;
181   int l;
182
183   str= Tcl_GetByteArrayFromObj(binary,&l);
184   hbytes_array(result, str, l);
185   return TCL_OK;
186 }
187
188 int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
189                     HBytes_Value hex, Tcl_Obj **result) {
190   *result= Tcl_NewByteArrayObj(hbytes_data(&hex), hbytes_len(&hex));
191   return TCL_OK;
192 }
193
194 int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
195                      HBytes_Value v, int *result) {
196   *result= hbytes_len(&v);
197   return TCL_OK;
198 }
199
200 int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
201                      int length, HBytes_Value *result) {
202   Byte *space;
203   int rc;
204   
205   space= hbytes_arrayspace(result, length);
206   rc= get_urandom(ip, space, length);
207   if (rc) { hbytes_free(result); return rc; }
208   return TCL_OK;
209 }  
210   
211 int do_hbytes_overwrite(ClientData cd, Tcl_Interp *ip,
212                         HBytes_Var v, int start, HBytes_Value sub) {
213   int sub_l;
214
215   sub_l= hbytes_len(&sub);
216   if (start < 0)
217     return staticerr(ip, "hbytes overwrite start -ve",
218                      "HBYTES LENGTH RANGE");
219   if (start + sub_l > hbytes_len(v.hb))
220     return staticerr(ip, "hbytes overwrite out of range",
221                      "HBYTES LENGTH UNDERRUN");
222   memcpy(hbytes_data(v.hb) + start, hbytes_data(&sub), sub_l);
223   return TCL_OK;
224 }
225
226 int do_hbytes_trimleft(ClientData cd, Tcl_Interp *ip, HBytes_Var v) {
227   const Byte *o, *p, *e;
228   o= p= hbytes_data(v.hb);
229   e= p + hbytes_len(v.hb);
230
231   while (p<e && !*p) p++;
232   if (p != o)
233     hbytes_unprepend(v.hb, p-o);
234
235   return TCL_OK;
236 }
237
238 int do_hbytes_repeat(ClientData cd, Tcl_Interp *ip,
239                      HBytes_Value sub, int count, HBytes_Value *result) {
240   int sub_l;
241   Byte *data;
242   const Byte *sub_d;
243
244   sub_l= hbytes_len(&sub);
245   if (count < 0) return staticerr(ip, "hbytes repeat count -ve",
246                                   "HBYTES LENGTH RANGE");
247   if (count > INT_MAX/sub_l) return staticerr(ip, "hbytes repeat too long", 0);
248
249   data= hbytes_arrayspace(result, sub_l*count);
250   sub_d= hbytes_data(&sub);
251   while (count) {
252     memcpy(data, sub_d, sub_l);
253     count--; data += sub_l;
254   }
255   return TCL_OK;
256 }  
257
258 int do_hbytes_xor(ClientData cd, Tcl_Interp *ip,
259                   HBytes_Var v, HBytes_Value d) {
260   int l;
261   Byte *dest;
262   const Byte *source;
263
264   l= hbytes_len(v.hb);
265   if (hbytes_len(&d) != l) return
266     staticerr(ip, "hbytes xor lengths do not match", "HBYTES LENGTH MISMATCH");
267
268   dest= hbytes_data(v.hb);
269   source= hbytes_data(&d);
270   memxor(dest,source,l);
271   return TCL_OK;
272 }
273   
274 int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
275                      int length, HBytes_Value *result) {
276   Byte *space;
277   space= hbytes_arrayspace(result, length);
278   memset(space,0,length);
279   return TCL_OK;
280 }
281
282 int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
283                       HBytes_Value a, HBytes_Value b, int *result) {
284   int al, bl, minl, r;
285
286   al= hbytes_len(&a);
287   bl= hbytes_len(&b);
288   minl= al<bl ? al : bl;
289
290   r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
291   
292   if (r<0) *result= -2;
293   else if (r>0) *result= +2;
294   else {
295     if (al<bl) *result= -1;
296     else if (al>bl) *result= +1;
297     else *result= 0;
298   }
299   return TCL_OK;
300 }
301
302 int do_hbytes_range(ClientData cd, Tcl_Interp *ip,
303                     HBytes_Value v, int start, int size,
304                     HBytes_Value *result) {
305   const Byte *data;
306   int l;
307
308   l= hbytes_len(&v);
309   if (start<0 || size<0)
310     return staticerr(ip,"hbytes range subscript(s) -ve","HBYTES LENGTH RANGE");
311   if (l<start+size)
312     return staticerr(ip, "hbytes range subscripts too big",
313                      "HBYTES LENGTH UNDERRUN");
314
315   data= hbytes_data(&v);
316   hbytes_array(result, data+start, size);
317   return TCL_OK;
318 }
319
320 /* hbytes representing uint16_t's */
321
322 int do_hbytes_h2ushort(ClientData cd, Tcl_Interp *ip,
323                        HBytes_Value hex, long *result) {
324   const Byte *data;
325   int l;
326
327   l= hbytes_len(&hex);
328   if (l>2)
329     return staticerr(ip, "hbytes h2ushort input more than 4 hex digits",
330                      "HBYTES VALUE OVERFLOW");
331
332   data= hbytes_data(&hex);
333   *result= data[l-1] | (l>1 ? data[0]<<8 : 0);
334   return TCL_OK;
335 }
336
337 int do_hbytes_ushort2h(ClientData cd, Tcl_Interp *ip,
338                        long input, HBytes_Value *result) {
339   uint16_t us;
340
341   if (input > 0x0ffff)
342     return staticerr(ip, "hbytes ushort2h input >2^16",
343                      "HBYTES VALUE OVERFLOW");
344
345   us= htons(input);
346   hbytes_array(result,(const Byte*)&us,2);
347   return TCL_OK;
348 }
349
350 /* toplevel functions */
351
352 int do_toplevel_hbytes(ClientData cd, Tcl_Interp *ip,
353                        const HBytes_SubCommand *subcmd,
354                        int objc, Tcl_Obj *const *objv) {
355   return subcmd->func(0,ip,objc,objv);
356 }
357
358 int do_toplevel_dgram_socket(ClientData cd, Tcl_Interp *ip,
359                              const DgramSocket_SubCommand *subcmd,
360                              int objc, Tcl_Obj *const *objv) {
361   return subcmd->func(0,ip,objc,objv);
362 }
363
364 int do_toplevel_ulong(ClientData cd, Tcl_Interp *ip,
365                       const ULong_SubCommand *subcmd,
366                       int objc, Tcl_Obj *const *objv) {
367   return subcmd->func(0,ip,objc,objv);
368 }
369
370 #define URANDOM "/dev/urandom"
371
372 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
373   static FILE *urandom;
374
375   int r, esave;
376
377   if (!urandom) {
378     urandom= fopen(URANDOM,"rb");
379     if (!urandom) return posixerr(ip,errno,"open " URANDOM);
380   }
381   r= fread(buffer,1,l,urandom);
382   if (r==l) return 0;
383
384   esave= errno;
385   fclose(urandom); urandom=0;
386
387   if (ferror(urandom)) {
388     return posixerr(ip,errno,"read " URANDOM);
389   } else {
390     assert(feof(urandom));
391     return staticerr(ip, URANDOM " gave eof!", 0);
392   }
393 }
394
395 int Hbytes_Init(Tcl_Interp *ip) {
396   const TopLevel_Command *cmd;
397
398   Tcl_RegisterObjType(&hbytes_type);
399   Tcl_RegisterObjType(&blockcipherkey_type);
400   Tcl_RegisterObjType(&enum_nearlytype);
401   Tcl_RegisterObjType(&enum1_nearlytype);
402   Tcl_RegisterObjType(&sockaddr_type);
403   Tcl_RegisterObjType(&dgramsockid_type);
404   Tcl_RegisterObjType(&ulong_type);
405
406   for (cmd=toplevel_commands;
407        cmd->name;
408        cmd++)
409     Tcl_CreateObjCommand(ip, cmd->name, cmd->func, 0,0);
410
411   return TCL_OK;
412 }