chiark / gitweb /
hbytes compare
[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   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 static void hbytes_t_ustr(Tcl_Obj *o) {
98   obj_updatestr_array(o,
99                       hbytes_data(OBJ_HBYTES(o)),
100                       hbytes_len(OBJ_HBYTES(o)));
101 }
102
103 static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
104   char *str, *ep, *os;
105   Byte *startbytes, *bytes;
106   int l;
107   char cbuf[3];
108
109   os= str= Tcl_GetStringFromObj(o,&l);  assert(str);
110   objfreeir(o);
111
112   if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
113                               " odd length in hex");
114
115   startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
116
117   cbuf[2]= 0;
118   while (l>0) {
119     cbuf[0]= *str++;
120     cbuf[1]= *str++;
121     *bytes++= strtoul(cbuf,&ep,16);
122     if (ep != cbuf+2) {
123       hbytes_free(OBJ_HBYTES(o));
124       return staticerr(ip, "hbytes: conversion from hex:"
125                        " bad hex digit");
126     }
127     l -= 2;
128   }
129
130   o->typePtr = &hbytes_type;
131   return TCL_OK;
132 }
133
134 Tcl_ObjType hbytes_type = {
135   "hbytes",
136   hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
137 };
138
139 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
140                     Tcl_Obj *binary, HBytes_Value *result) {
141   const char *str;
142   int l;
143
144   str= Tcl_GetStringFromObj(binary,&l);
145   hbytes_array(result, str, l);
146   return TCL_OK;
147 }
148
149 int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
150                     HBytes_Value hex, Tcl_Obj **result) {
151   *result= Tcl_NewStringObj(hbytes_data(&hex), hbytes_len(&hex));
152   return TCL_OK;
153 }
154
155 int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
156                      HBytes_Value v, int *result) {
157   *result= hbytes_len(&v);
158   return TCL_OK;
159 }
160
161 int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
162                      int length, HBytes_Value *result) {
163   Byte *space;
164   int rc;
165   
166   space= hbytes_arrayspace(result, length);
167   rc= get_urandom(ip, space, length);
168   if (rc) { hbytes_free(result); return rc; }
169   return TCL_OK;
170 }  
171   
172 int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
173                      int length, HBytes_Value *result) {
174   Byte *space;
175   space= hbytes_arrayspace(result, length);
176   memset(space,0,length);
177   return TCL_OK;
178 }
179
180 int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
181                       HBytes_Value a, HBytes_Value b, int *result) {
182   int al, bl, minl, r;
183
184   al= hbytes_len(&a);
185   bl= hbytes_len(&b);
186   minl= al<bl ? al : bl;
187
188   r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
189   
190   if (r<0) *result= -2;
191   else if (r>0) *result= +2;
192   else {
193     if (al<bl) *result= -1;
194     else if (al>bl) *result= +1;
195     else *result= 0;
196   }
197   return TCL_OK;
198 }
199
200 int do__hbytes(ClientData cd, Tcl_Interp *ip,
201                const HBytes_SubCommand *subcmd,
202                int objc, Tcl_Obj *const *objv) {
203   return subcmd->func(0,ip,objc,objv);
204 }
205
206 int do__dgram_socket(ClientData cd, Tcl_Interp *ip,
207                      const DgramSocket_SubCommand *subcmd,
208                      int objc, Tcl_Obj *const *objv) {
209   return subcmd->func(0,ip,objc,objv);
210 }
211
212 #define URANDOM "/dev/urandom"
213
214 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
215   static FILE *urandom;
216
217   int r, esave;
218
219   if (!urandom) {
220     urandom= fopen(URANDOM,"rb");
221     if (!urandom) return posixerr(ip,errno,"open " URANDOM);
222   }
223   r= fread(buffer,1,l,urandom);
224   if (r==l) return 0;
225
226   esave= errno;
227   fclose(urandom); urandom=0;
228
229   if (ferror(urandom)) {
230     return posixerr(ip,errno,"read " URANDOM);
231   } else {
232     assert(feof(urandom));
233     return staticerr(ip, URANDOM " gave eof!");
234   }
235 }
236
237 int Hbytes_Init(Tcl_Interp *ip) {
238   Tcl_RegisterObjType(&hbytes_type);
239   Tcl_RegisterObjType(&blockcipherkey_type);
240   Tcl_RegisterObjType(&enum_nearlytype);
241   Tcl_RegisterObjType(&enum1_nearlytype);
242   Tcl_RegisterObjType(&sockaddr_type);
243   Tcl_RegisterObjType(&sockid_type);
244   Tcl_CreateObjCommand(ip, "hbytes",       pa__hbytes,       0,0);
245   Tcl_CreateObjCommand(ip, "dgram-socket", pa__dgram_socket, 0,0);
246   return TCL_OK;
247 }