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 int newfdposixerr(Tcl_Interp *ip, int fd, const char *m) {
29 return posixerr(ip,e,m);
32 void objfreeir(Tcl_Obj *o) {
33 if (o->typePtr && o->typePtr->freeIntRepProc)
34 o->typePtr->freeIntRepProc(o);
38 int do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip,
39 Tcl_Obj *obj, Tcl_Obj **result) {
44 if (obj->typePtr == &hbytes_type) {
45 HBytes_Value *v= OBJ_HBYTES(obj);
46 memset(nums,0,sizeof(nums));
47 nums[1]= hbytes_len(v);
49 if (HBYTES_ISEMPTY(v)) tn= "empty";
50 else if (HBYTES_ISSENTINEL(v)) tn= "sentinel!";
51 else if (HBYTES_ISSIMPLE(v)) tn= "simple";
53 HBytes_ComplexValue *cx= v->begin_complex;
55 nums[0]= cx->prespace;
56 nums[2]= cx->avail - cx->len;
64 objl[0]= Tcl_NewStringObj((char*)tn,-1);
65 for (i=0; i<lnl; i++) objl[i+1]= Tcl_NewIntObj(nums[i]);
66 *result= Tcl_NewListObj(lnl+1,objl);
71 static void hbytes_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
72 hbytes_array(OBJ_HBYTES(dup),
73 hbytes_data(OBJ_HBYTES(src)),
74 hbytes_len(OBJ_HBYTES(src)));
75 dup->typePtr= &hbytes_type;
78 static void hbytes_t_free(Tcl_Obj *o) {
79 hbytes_free(OBJ_HBYTES(o));
82 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
83 int l, const char *prefix) {
89 str= o->bytes= TALLOC(o->length+1);
91 memcpy(str,prefix,pl);
95 sprintf(str,"%02x",*byte);
101 void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
102 obj_updatestr_array_prefix(o,byte,l,"");
105 void obj_updatestr_vstringls(Tcl_Obj *o, ...) {
112 for (l=0; (part= va_arg(al, const char*)); )
117 o->bytes= TALLOC(l+1);
120 for (p= o->bytes; (part= va_arg(al, const char*)); p += pl) {
129 void obj_updatestr_string(Tcl_Obj *o, const char *str) {
130 obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
133 static void hbytes_t_ustr(Tcl_Obj *o) {
134 obj_updatestr_array(o,
135 hbytes_data(OBJ_HBYTES(o)),
136 hbytes_len(OBJ_HBYTES(o)));
139 static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
141 Byte *startbytes, *bytes;
145 if (o->typePtr == &ulong_type) {
148 ul= htonl(*(const uint32_t*)&o->internalRep.longValue);
149 hbytes_array(OBJ_HBYTES(o), (const Byte*)&ul, 4);
153 os= str= Tcl_GetStringFromObj(o,&l); assert(str);
156 if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
157 " odd length in hex", "HBYTES SYNTAX");
159 startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
165 *bytes++= strtoul(cbuf,&ep,16);
167 hbytes_free(OBJ_HBYTES(o));
168 return staticerr(ip, "hbytes: conversion from hex:"
169 " bad hex digit", "HBYTES SYNTAX");
176 o->typePtr = &hbytes_type;
180 Tcl_ObjType hbytes_type = {
182 hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
185 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
186 Tcl_Obj *binary, HBytes_Value *result) {
187 const unsigned char *str;
190 str= Tcl_GetByteArrayFromObj(binary,&l);
191 hbytes_array(result, str, l);
195 int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
196 HBytes_Value hex, Tcl_Obj **result) {
197 *result= Tcl_NewByteArrayObj(hbytes_data(&hex), hbytes_len(&hex));
201 int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
202 HBytes_Value v, int *result) {
203 *result= hbytes_len(&v);
207 int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
208 int length, HBytes_Value *result) {
212 space= hbytes_arrayspace(result, length);
213 rc= get_urandom(ip, space, length);
214 if (rc) { hbytes_free(result); return rc; }
218 int do_hbytes_overwrite(ClientData cd, Tcl_Interp *ip,
219 HBytes_Var v, int start, HBytes_Value sub) {
222 sub_l= hbytes_len(&sub);
224 return staticerr(ip, "hbytes overwrite start -ve",
225 "HBYTES LENGTH RANGE");
226 if (start + sub_l > hbytes_len(v.hb))
227 return staticerr(ip, "hbytes overwrite out of range",
228 "HBYTES LENGTH UNDERRUN");
229 memcpy(hbytes_data(v.hb) + start, hbytes_data(&sub), sub_l);
233 int do_hbytes_trimleft(ClientData cd, Tcl_Interp *ip, HBytes_Var v) {
234 const Byte *o, *p, *e;
235 o= p= hbytes_data(v.hb);
236 e= p + hbytes_len(v.hb);
238 while (p<e && !*p) p++;
240 hbytes_unprepend(v.hb, p-o);
245 int do_hbytes_repeat(ClientData cd, Tcl_Interp *ip,
246 HBytes_Value sub, int count, HBytes_Value *result) {
251 sub_l= hbytes_len(&sub);
252 if (count < 0) return staticerr(ip, "hbytes repeat count -ve",
253 "HBYTES LENGTH RANGE");
254 if (count > INT_MAX/sub_l) return staticerr(ip, "hbytes repeat too long", 0);
256 data= hbytes_arrayspace(result, sub_l*count);
257 sub_d= hbytes_data(&sub);
259 memcpy(data, sub_d, sub_l);
260 count--; data += sub_l;
265 int do_hbytes_xor(ClientData cd, Tcl_Interp *ip,
266 HBytes_Var v, HBytes_Value d) {
272 if (hbytes_len(&d) != l) return
273 staticerr(ip, "hbytes xor lengths do not match", "HBYTES LENGTH MISMATCH");
275 dest= hbytes_data(v.hb);
276 source= hbytes_data(&d);
277 memxor(dest,source,l);
281 int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
282 int length, HBytes_Value *result) {
284 space= hbytes_arrayspace(result, length);
285 memset(space,0,length);
289 int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
290 HBytes_Value a, HBytes_Value b, int *result) {
295 minl= al<bl ? al : bl;
297 r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
299 if (r<0) *result= -2;
300 else if (r>0) *result= +2;
302 if (al<bl) *result= -1;
303 else if (al>bl) *result= +1;
309 int do_hbytes_range(ClientData cd, Tcl_Interp *ip,
310 HBytes_Value v, int start, int size,
311 HBytes_Value *result) {
316 if (start<0 || size<0)
317 return staticerr(ip,"hbytes range subscript(s) -ve","HBYTES LENGTH RANGE");
319 return staticerr(ip, "hbytes range subscripts too big",
320 "HBYTES LENGTH UNDERRUN");
322 data= hbytes_data(&v);
323 hbytes_array(result, data+start, size);
327 /* hbytes representing uint16_t's */
329 int do_hbytes_h2ushort(ClientData cd, Tcl_Interp *ip,
330 HBytes_Value hex, long *result) {
336 return staticerr(ip, "hbytes h2ushort input more than 4 hex digits",
337 "HBYTES VALUE OVERFLOW");
339 data= hbytes_data(&hex);
340 *result= data[l-1] | (l>1 ? data[0]<<8 : 0);
344 int do_hbytes_ushort2h(ClientData cd, Tcl_Interp *ip,
345 long input, HBytes_Value *result) {
349 return staticerr(ip, "hbytes ushort2h input >2^16",
350 "HBYTES VALUE OVERFLOW");
353 hbytes_array(result,(const Byte*)&us,2);
357 /* toplevel functions */
359 int do_toplevel_hbytes(ClientData cd, Tcl_Interp *ip,
360 const HBytes_SubCommand *subcmd,
361 int objc, Tcl_Obj *const *objv) {
362 return subcmd->func(0,ip,objc,objv);
365 int do_toplevel_dgram_socket(ClientData cd, Tcl_Interp *ip,
366 const DgramSocket_SubCommand *subcmd,
367 int objc, Tcl_Obj *const *objv) {
368 return subcmd->func(0,ip,objc,objv);
371 int do_toplevel_tuntap_socket_raw(ClientData cd, Tcl_Interp *ip,
372 const TunSocket_SubCommand *subcmd,
373 int objc, Tcl_Obj *const *objv) {
374 return subcmd->func(0,ip,objc,objv);
377 int do_toplevel_ulong(ClientData cd, Tcl_Interp *ip,
378 const ULong_SubCommand *subcmd,
379 int objc, Tcl_Obj *const *objv) {
380 return subcmd->func(0,ip,objc,objv);
383 int do_toplevel_mask_map(ClientData cd, Tcl_Interp *ip,
384 const MaskMap_SubCommand *subcmd,
385 int objc, Tcl_Obj *const *objv) {
386 return subcmd->func(0,ip,objc,objv);
389 #define URANDOM "/dev/urandom"
391 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
392 static FILE *urandom;
397 urandom= fopen(URANDOM,"rb");
398 if (!urandom) return posixerr(ip,errno,"open " URANDOM);
400 r= fread(buffer,1,l,urandom);
404 fclose(urandom); urandom=0;
406 if (ferror(urandom)) {
407 return posixerr(ip,errno,"read " URANDOM);
409 assert(feof(urandom));
410 return staticerr(ip, URANDOM " gave eof!", 0);
414 int Hbytes_Init(Tcl_Interp *ip) {
415 const TopLevel_Command *cmd;
417 Tcl_RegisterObjType(&hbytes_type);
418 Tcl_RegisterObjType(&blockcipherkey_type);
419 Tcl_RegisterObjType(&enum_nearlytype);
420 Tcl_RegisterObjType(&enum1_nearlytype);
421 Tcl_RegisterObjType(&sockaddr_type);
422 Tcl_RegisterObjType(&tabledataid_nearlytype);
423 Tcl_RegisterObjType(&ulong_type);
425 for (cmd=toplevel_commands;
428 Tcl_CreateObjCommand(ip, (char*)cmd->name, cmd->func, 0,0);