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));
15 int posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
20 em= Tcl_PosixError(ip);
21 Tcl_AppendResult(ip, m, ": ", em, (char*)0);
25 void objfreeir(Tcl_Obj *o) {
26 if (o->typePtr && o->typePtr->freeIntRepProc)
27 o->typePtr->freeIntRepProc(o);
31 int do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip,
32 Tcl_Obj *obj, Tcl_Obj **result) {
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);
42 if (HBYTES_ISEMPTY(v)) tn= "empty";
43 else if (HBYTES_ISSENTINEL(v)) tn= "sentinel!";
44 else if (HBYTES_ISSIMPLE(v)) tn= "simple";
46 HBytes_ComplexValue *cx= v->begin_complex;
48 nums[0]= cx->prespace;
49 nums[2]= cx->avail - cx->len;
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);
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;
71 static void hbytes_t_free(Tcl_Obj *o) {
72 hbytes_free(OBJ_HBYTES(o));
75 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
76 int l, const char *prefix) {
82 str= o->bytes= TALLOC(o->length+1);
84 memcpy(str,prefix,pl);
88 sprintf(str,"%02x",*byte);
94 void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
95 obj_updatestr_array_prefix(o,byte,l,"");
98 void obj_updatestr_vstringls(Tcl_Obj *o, ...) {
105 for (l=0; (part= va_arg(al, const char*)); )
110 o->bytes= TALLOC(l+1);
113 for (p= o->bytes; (part= va_arg(al, const char*)); p += pl) {
122 void obj_updatestr_string(Tcl_Obj *o, const char *str) {
123 obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
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)));
132 static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
134 Byte *startbytes, *bytes;
138 if (o->typePtr == &ulong_type) {
141 ul= htonl(*(const uint32_t*)&o->internalRep.longValue);
142 hbytes_array(OBJ_HBYTES(o), (const Byte*)&ul, 4);
146 os= str= Tcl_GetStringFromObj(o,&l); assert(str);
149 if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
150 " odd length in hex", "HBYTES SYNTAX");
152 startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
158 *bytes++= strtoul(cbuf,&ep,16);
160 hbytes_free(OBJ_HBYTES(o));
161 return staticerr(ip, "hbytes: conversion from hex:"
162 " bad hex digit", "HBYTES SYNTAX");
169 o->typePtr = &hbytes_type;
173 Tcl_ObjType hbytes_type = {
175 hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
178 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
179 Tcl_Obj *binary, HBytes_Value *result) {
180 const unsigned char *str;
183 str= Tcl_GetByteArrayFromObj(binary,&l);
184 hbytes_array(result, str, l);
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));
194 int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
195 HBytes_Value v, int *result) {
196 *result= hbytes_len(&v);
200 int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
201 int length, HBytes_Value *result) {
205 space= hbytes_arrayspace(result, length);
206 rc= get_urandom(ip, space, length);
207 if (rc) { hbytes_free(result); return rc; }
211 int do_hbytes_overwrite(ClientData cd, Tcl_Interp *ip,
212 HBytes_Var v, int start, HBytes_Value sub) {
215 sub_l= hbytes_len(&sub);
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);
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);
231 while (p<e && !*p) p++;
233 hbytes_unprepend(v.hb, p-o);
238 int do_hbytes_repeat(ClientData cd, Tcl_Interp *ip,
239 HBytes_Value sub, int count, HBytes_Value *result) {
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);
249 data= hbytes_arrayspace(result, sub_l*count);
250 sub_d= hbytes_data(&sub);
252 memcpy(data, sub_d, sub_l);
253 count--; data += sub_l;
258 int do_hbytes_xor(ClientData cd, Tcl_Interp *ip,
259 HBytes_Var v, HBytes_Value d) {
265 if (hbytes_len(&d) != l) return
266 staticerr(ip, "hbytes xor lengths do not match", "HBYTES LENGTH MISMATCH");
268 dest= hbytes_data(v.hb);
269 source= hbytes_data(&d);
270 memxor(dest,source,l);
274 int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
275 int length, HBytes_Value *result) {
277 space= hbytes_arrayspace(result, length);
278 memset(space,0,length);
282 int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
283 HBytes_Value a, HBytes_Value b, int *result) {
288 minl= al<bl ? al : bl;
290 r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
292 if (r<0) *result= -2;
293 else if (r>0) *result= +2;
295 if (al<bl) *result= -1;
296 else if (al>bl) *result= +1;
302 int do_hbytes_range(ClientData cd, Tcl_Interp *ip,
303 HBytes_Value v, int start, int size,
304 HBytes_Value *result) {
309 if (start<0 || size<0)
310 return staticerr(ip,"hbytes range subscript(s) -ve","HBYTES LENGTH RANGE");
312 return staticerr(ip, "hbytes range subscripts too big",
313 "HBYTES LENGTH UNDERRUN");
315 data= hbytes_data(&v);
316 hbytes_array(result, data+start, size);
320 /* hbytes representing uint16_t's */
322 int do_hbytes_h2ushort(ClientData cd, Tcl_Interp *ip,
323 HBytes_Value hex, long *result) {
329 return staticerr(ip, "hbytes h2ushort input more than 4 hex digits",
330 "HBYTES VALUE OVERFLOW");
332 data= hbytes_data(&hex);
333 *result= data[l-1] | (l>1 ? data[0]<<8 : 0);
337 int do_hbytes_ushort2h(ClientData cd, Tcl_Interp *ip,
338 long input, HBytes_Value *result) {
342 return staticerr(ip, "hbytes ushort2h input >2^16",
343 "HBYTES VALUE OVERFLOW");
346 hbytes_array(result,(const Byte*)&us,2);
350 /* toplevel functions */
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);
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);
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);
370 #define URANDOM "/dev/urandom"
372 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
373 static FILE *urandom;
378 urandom= fopen(URANDOM,"rb");
379 if (!urandom) return posixerr(ip,errno,"open " URANDOM);
381 r= fread(buffer,1,l,urandom);
385 fclose(urandom); urandom=0;
387 if (ferror(urandom)) {
388 return posixerr(ip,errno,"read " URANDOM);
390 assert(feof(urandom));
391 return staticerr(ip, URANDOM " gave eof!", 0);
395 int Hbytes_Init(Tcl_Interp *ip) {
396 const TopLevel_Command *cmd;
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);
406 for (cmd=toplevel_commands;
409 Tcl_CreateObjCommand(ip, cmd->name, cmd->func, 0,0);