2 /* WARNING - FILE COPIED IN REPO TO CHIARK-TCL THEN
3 EDITED - THIS VERSION IS OBSOLETE */
11 int staticerr(Tcl_Interp *ip, const char *m, const char *ec) {
12 Tcl_SetResult(ip, (char*)m, TCL_STATIC);
13 if (ec) Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(ec,-1));
17 int posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
22 em= Tcl_PosixError(ip);
23 Tcl_AppendResult(ip, m, ": ", em, (char*)0);
27 int newfdposixerr(Tcl_Interp *ip, int fd, const char *m) {
31 return posixerr(ip,e,m);
34 void objfreeir(Tcl_Obj *o) {
35 if (o->typePtr && o->typePtr->freeIntRepProc)
36 o->typePtr->freeIntRepProc(o);
40 int do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip,
41 Tcl_Obj *obj, Tcl_Obj **result) {
46 if (obj->typePtr == &hbytes_type) {
47 HBytes_Value *v= OBJ_HBYTES(obj);
48 memset(nums,0,sizeof(nums));
49 nums[1]= hbytes_len(v);
51 if (HBYTES_ISEMPTY(v)) tn= "empty";
52 else if (HBYTES_ISSENTINEL(v)) tn= "sentinel!";
53 else if (HBYTES_ISSIMPLE(v)) tn= "simple";
55 HBytes_ComplexValue *cx= v->begin_complex;
57 nums[0]= cx->prespace;
58 nums[2]= cx->avail - cx->len;
66 objl[0]= Tcl_NewStringObj((char*)tn,-1);
67 for (i=0; i<lnl; i++) objl[i+1]= Tcl_NewIntObj(nums[i]);
68 *result= Tcl_NewListObj(lnl+1,objl);
73 static void hbytes_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
74 hbytes_array(OBJ_HBYTES(dup),
75 hbytes_data(OBJ_HBYTES(src)),
76 hbytes_len(OBJ_HBYTES(src)));
77 dup->typePtr= &hbytes_type;
80 static void hbytes_t_free(Tcl_Obj *o) {
81 hbytes_free(OBJ_HBYTES(o));
84 void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
85 int l, const char *prefix) {
91 str= o->bytes= TALLOC(o->length+1);
93 memcpy(str,prefix,pl);
97 sprintf(str,"%02x",*byte);
103 void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
104 obj_updatestr_array_prefix(o,byte,l,"");
107 void obj_updatestr_vstringls(Tcl_Obj *o, ...) {
114 for (l=0; (part= va_arg(al, const char*)); )
119 o->bytes= TALLOC(l+1);
122 for (p= o->bytes; (part= va_arg(al, const char*)); p += pl) {
131 void obj_updatestr_string(Tcl_Obj *o, const char *str) {
132 obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
135 static void hbytes_t_ustr(Tcl_Obj *o) {
136 obj_updatestr_array(o,
137 hbytes_data(OBJ_HBYTES(o)),
138 hbytes_len(OBJ_HBYTES(o)));
141 static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
143 Byte *startbytes, *bytes;
147 if (o->typePtr == &ulong_type) {
150 ul= htonl(*(const uint32_t*)&o->internalRep.longValue);
151 hbytes_array(OBJ_HBYTES(o), (const Byte*)&ul, 4);
155 os= str= Tcl_GetStringFromObj(o,&l); assert(str);
158 if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
159 " odd length in hex", "HBYTES SYNTAX");
161 startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
167 *bytes++= strtoul(cbuf,&ep,16);
169 hbytes_free(OBJ_HBYTES(o));
170 return staticerr(ip, "hbytes: conversion from hex:"
171 " bad hex digit", "HBYTES SYNTAX");
178 o->typePtr = &hbytes_type;
182 Tcl_ObjType hbytes_type = {
184 hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
187 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
188 Tcl_Obj *binary, HBytes_Value *result) {
189 const unsigned char *str;
192 str= Tcl_GetByteArrayFromObj(binary,&l);
193 hbytes_array(result, str, l);
197 int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
198 HBytes_Value hex, Tcl_Obj **result) {
199 *result= Tcl_NewByteArrayObj(hbytes_data(&hex), hbytes_len(&hex));
203 int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
204 HBytes_Value v, int *result) {
205 *result= hbytes_len(&v);
209 int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
210 int length, HBytes_Value *result) {
214 space= hbytes_arrayspace(result, length);
215 rc= get_urandom(ip, space, length);
216 if (rc) { hbytes_free(result); return rc; }
220 int do_hbytes_overwrite(ClientData cd, Tcl_Interp *ip,
221 HBytes_Var v, int start, HBytes_Value sub) {
224 sub_l= hbytes_len(&sub);
226 return staticerr(ip, "hbytes overwrite start -ve",
227 "HBYTES LENGTH RANGE");
228 if (start + sub_l > hbytes_len(v.hb))
229 return staticerr(ip, "hbytes overwrite out of range",
230 "HBYTES LENGTH UNDERRUN");
231 memcpy(hbytes_data(v.hb) + start, hbytes_data(&sub), sub_l);
235 int do_hbytes_trimleft(ClientData cd, Tcl_Interp *ip, HBytes_Var v) {
236 const Byte *o, *p, *e;
237 o= p= hbytes_data(v.hb);
238 e= p + hbytes_len(v.hb);
240 while (p<e && !*p) p++;
242 hbytes_unprepend(v.hb, p-o);
247 int do_hbytes_repeat(ClientData cd, Tcl_Interp *ip,
248 HBytes_Value sub, int count, HBytes_Value *result) {
253 sub_l= hbytes_len(&sub);
254 if (count < 0) return staticerr(ip, "hbytes repeat count -ve",
255 "HBYTES LENGTH RANGE");
256 if (count > INT_MAX/sub_l) return staticerr(ip, "hbytes repeat too long", 0);
258 data= hbytes_arrayspace(result, sub_l*count);
259 sub_d= hbytes_data(&sub);
261 memcpy(data, sub_d, sub_l);
262 count--; data += sub_l;
267 int do_hbytes_xor(ClientData cd, Tcl_Interp *ip,
268 HBytes_Var v, HBytes_Value d) {
274 if (hbytes_len(&d) != l) return
275 staticerr(ip, "hbytes xor lengths do not match", "HBYTES LENGTH MISMATCH");
277 dest= hbytes_data(v.hb);
278 source= hbytes_data(&d);
279 memxor(dest,source,l);
283 int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
284 int length, HBytes_Value *result) {
286 space= hbytes_arrayspace(result, length);
287 memset(space,0,length);
291 int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
292 HBytes_Value a, HBytes_Value b, int *result) {
297 minl= al<bl ? al : bl;
299 r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
301 if (r<0) *result= -2;
302 else if (r>0) *result= +2;
304 if (al<bl) *result= -1;
305 else if (al>bl) *result= +1;
311 int do_hbytes_range(ClientData cd, Tcl_Interp *ip,
312 HBytes_Value v, int start, int size,
313 HBytes_Value *result) {
318 if (start<0 || size<0)
319 return staticerr(ip,"hbytes range subscript(s) -ve","HBYTES LENGTH RANGE");
321 return staticerr(ip, "hbytes range subscripts too big",
322 "HBYTES LENGTH UNDERRUN");
324 data= hbytes_data(&v);
325 hbytes_array(result, data+start, size);
329 int do_hbytes_addr_map(ClientData cd, Tcl_Interp *ip,
330 const AddrMap_SubCommand *subcmd,
331 int objc, Tcl_Obj *const *objv) {
332 return subcmd->func(0,ip,objc,objv);
335 /* hbytes representing uint16_t's */
337 int do_hbytes_h2ushort(ClientData cd, Tcl_Interp *ip,
338 HBytes_Value hex, long *result) {
344 return staticerr(ip, "hbytes h2ushort input more than 4 hex digits",
345 "HBYTES VALUE OVERFLOW");
347 data= hbytes_data(&hex);
348 *result= data[l-1] | (l>1 ? data[0]<<8 : 0);
352 int do_hbytes_ushort2h(ClientData cd, Tcl_Interp *ip,
353 long input, HBytes_Value *result) {
357 return staticerr(ip, "hbytes ushort2h input >2^16",
358 "HBYTES VALUE OVERFLOW");
361 hbytes_array(result,(const Byte*)&us,2);
365 /* toplevel functions */
367 int do_toplevel_hbytes(ClientData cd, Tcl_Interp *ip,
368 const HBytes_SubCommand *subcmd,
369 int objc, Tcl_Obj *const *objv) {
370 return subcmd->func(0,ip,objc,objv);
373 int do_toplevel_dgram_socket(ClientData cd, Tcl_Interp *ip,
374 const DgramSocket_SubCommand *subcmd,
375 int objc, Tcl_Obj *const *objv) {
376 return subcmd->func(0,ip,objc,objv);
379 int do_toplevel_tuntap_socket_raw(ClientData cd, Tcl_Interp *ip,
380 const TunSocket_SubCommand *subcmd,
381 int objc, Tcl_Obj *const *objv) {
382 return subcmd->func(0,ip,objc,objv);
385 int do_toplevel_ulong(ClientData cd, Tcl_Interp *ip,
386 const ULong_SubCommand *subcmd,
387 int objc, Tcl_Obj *const *objv) {
388 return subcmd->func(0,ip,objc,objv);
391 int do_toplevel_adns(ClientData cd, Tcl_Interp *ip,
392 const Adns_SubCommand *subcmd,
393 int objc, Tcl_Obj *const *objv) {
394 return subcmd->func(0,ip,objc,objv);
397 #define URANDOM "/dev/urandom"
399 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
400 static FILE *urandom;
405 urandom= fopen(URANDOM,"rb");
406 if (!urandom) return posixerr(ip,errno,"open " URANDOM);
408 r= fread(buffer,1,l,urandom);
412 fclose(urandom); urandom=0;
414 if (ferror(urandom)) {
415 return posixerr(ip,errno,"read " URANDOM);
417 assert(feof(urandom));
418 return staticerr(ip, URANDOM " gave eof!", 0);
422 int Hbytes_Init(Tcl_Interp *ip) {
423 const TopLevel_Command *cmd;
425 Tcl_RegisterObjType(&hbytes_type);
426 Tcl_RegisterObjType(&blockcipherkey_type);
427 Tcl_RegisterObjType(&enum_nearlytype);
428 Tcl_RegisterObjType(&enum1_nearlytype);
429 Tcl_RegisterObjType(&sockaddr_type);
430 Tcl_RegisterObjType(&tabledataid_nearlytype);
431 Tcl_RegisterObjType(&ulong_type);
432 Tcl_RegisterObjType(&addrmap_type);
434 for (cmd=toplevel_commands;
437 Tcl_CreateObjCommand(ip, (char*)cmd->name, cmd->func, 0,0);