-void obj_updatestr_string(Tcl_Obj *o, const char *str) {
- obj_updatestr_vstringls(o, str, strlen(str), (char*)0);
-}
-
-static void hbytes_t_ustr(Tcl_Obj *o) {
- obj_updatestr_array(o,
- hbytes_data(OBJ_HBYTES(o)),
- hbytes_len(OBJ_HBYTES(o)));
-}
-
-static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
- char *str, *ep, *os;
- Byte *startbytes, *bytes;
- int l;
- char cbuf[3];
-
- if (o->typePtr == &ulong_type) {
- uint32_t ul;
-
- ul= htonl(*(const uint32_t*)&o->internalRep.longValue);
- hbytes_array(OBJ_HBYTES(o), (const Byte*)&ul, 4);
-
- } else {
-
- os= str= Tcl_GetStringFromObj(o,&l); assert(str);
- objfreeir(o);
-
- if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
- " odd length in hex", "HBYTES SYNTAX");
-
- startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
-
- cbuf[2]= 0;
- while (l>0) {
- cbuf[0]= *str++;
- cbuf[1]= *str++;
- *bytes++= strtoul(cbuf,&ep,16);
- if (ep != cbuf+2) {
- hbytes_free(OBJ_HBYTES(o));
- return staticerr(ip, "hbytes: conversion from hex:"
- " bad hex digit", "HBYTES SYNTAX");
- }
- l -= 2;
- }
-
- }
-
- o->typePtr = &hbytes_type;
- return TCL_OK;
-}
-
-Tcl_ObjType hbytes_type = {
- "hbytes",
- hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
-};
-
-int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
- Tcl_Obj *binary, HBytes_Value *result) {
- const unsigned char *str;
- int l;
-
- str= Tcl_GetByteArrayFromObj(binary,&l);
- hbytes_array(result, str, l);
- return TCL_OK;
-}
-
-int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
- HBytes_Value hex, Tcl_Obj **result) {
- *result= Tcl_NewByteArrayObj(hbytes_data(&hex), hbytes_len(&hex));
- return TCL_OK;
-}
-
-int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
- HBytes_Value v, int *result) {
- *result= hbytes_len(&v);
- return TCL_OK;
-}
-
-int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
- int length, HBytes_Value *result) {
- Byte *space;
- int rc;
-
- space= hbytes_arrayspace(result, length);
- rc= get_urandom(ip, space, length);
- if (rc) { hbytes_free(result); return rc; }
- return TCL_OK;
-}
-
-int do_hbytes_overwrite(ClientData cd, Tcl_Interp *ip,
- HBytes_Var v, int start, HBytes_Value sub) {
- int sub_l;
-
- sub_l= hbytes_len(&sub);
- if (start < 0)
- return staticerr(ip, "hbytes overwrite start -ve",
- "HBYTES LENGTH RANGE");
- if (start + sub_l > hbytes_len(v.hb))
- return staticerr(ip, "hbytes overwrite out of range",
- "HBYTES LENGTH UNDERRUN");
- memcpy(hbytes_data(v.hb) + start, hbytes_data(&sub), sub_l);
- return TCL_OK;
-}
-
-int do_hbytes_trimleft(ClientData cd, Tcl_Interp *ip, HBytes_Var v) {
- const Byte *o, *p, *e;
- o= p= hbytes_data(v.hb);
- e= p + hbytes_len(v.hb);
-
- while (p<e && !*p) p++;
- if (p != o)
- hbytes_unprepend(v.hb, p-o);
-
- return TCL_OK;
-}
-
-int do_hbytes_repeat(ClientData cd, Tcl_Interp *ip,
- HBytes_Value sub, int count, HBytes_Value *result) {
- int sub_l;
- Byte *data;
- const Byte *sub_d;
-
- sub_l= hbytes_len(&sub);
- if (count < 0) return staticerr(ip, "hbytes repeat count -ve",
- "HBYTES LENGTH RANGE");
- if (count > INT_MAX/sub_l) return staticerr(ip, "hbytes repeat too long", 0);
-
- data= hbytes_arrayspace(result, sub_l*count);
- sub_d= hbytes_data(&sub);
- while (count) {
- memcpy(data, sub_d, sub_l);
- count--; data += sub_l;
- }
- return TCL_OK;
-}
-
-int do_hbytes_xor(ClientData cd, Tcl_Interp *ip,
- HBytes_Var v, HBytes_Value d) {
- int l;
- Byte *dest;
- const Byte *source;
-
- l= hbytes_len(v.hb);
- if (hbytes_len(&d) != l) return
- staticerr(ip, "hbytes xor lengths do not match", "HBYTES LENGTH MISMATCH");
-
- dest= hbytes_data(v.hb);
- source= hbytes_data(&d);
- memxor(dest,source,l);
- return TCL_OK;
-}
-
-int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
- int length, HBytes_Value *result) {
- Byte *space;
- space= hbytes_arrayspace(result, length);
- memset(space,0,length);
- return TCL_OK;
-}
-
-int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
- HBytes_Value a, HBytes_Value b, int *result) {
- int al, bl, minl, r;
-
- al= hbytes_len(&a);
- bl= hbytes_len(&b);
- minl= al<bl ? al : bl;
-
- r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
-
- if (r<0) *result= -2;
- else if (r>0) *result= +2;
- else {
- if (al<bl) *result= -1;
- else if (al>bl) *result= +1;
- else *result= 0;
- }
- return TCL_OK;
-}
-
-int do_hbytes_range(ClientData cd, Tcl_Interp *ip,
- HBytes_Value v, int start, int size,
- HBytes_Value *result) {
- const Byte *data;
- int l;
-
- l= hbytes_len(&v);
- if (start<0 || size<0)
- return staticerr(ip,"hbytes range subscript(s) -ve","HBYTES LENGTH RANGE");
- if (l<start+size)
- return staticerr(ip, "hbytes range subscripts too big",
- "HBYTES LENGTH UNDERRUN");
-
- data= hbytes_data(&v);
- hbytes_array(result, data+start, size);
- return TCL_OK;
-}
-
-/* hbytes representing uint16_t's */
-
-int do_hbytes_h2ushort(ClientData cd, Tcl_Interp *ip,
- HBytes_Value hex, long *result) {
- const Byte *data;
- int l;
-
- l= hbytes_len(&hex);
- if (l>2)
- return staticerr(ip, "hbytes h2ushort input more than 4 hex digits",
- "HBYTES VALUE OVERFLOW");
-
- data= hbytes_data(&hex);
- *result= data[l-1] | (l>1 ? data[0]<<8 : 0);
- return TCL_OK;
-}
-
-int do_hbytes_ushort2h(ClientData cd, Tcl_Interp *ip,
- long input, HBytes_Value *result) {
- uint16_t us;
-
- if (input > 0x0ffff)
- return staticerr(ip, "hbytes ushort2h input >2^16",
- "HBYTES VALUE OVERFLOW");
-
- us= htons(input);
- hbytes_array(result,(const Byte*)&us,2);
- return TCL_OK;
-}
-
-/* toplevel functions */
-
-int do_toplevel_hbytes(ClientData cd, Tcl_Interp *ip,
- const HBytes_SubCommand *subcmd,
- int objc, Tcl_Obj *const *objv) {
- return subcmd->func(0,ip,objc,objv);
-}
-
-int do_toplevel_dgram_socket(ClientData cd, Tcl_Interp *ip,
- const DgramSocket_SubCommand *subcmd,
- int objc, Tcl_Obj *const *objv) {
- return subcmd->func(0,ip,objc,objv);
-}
-
-int do_toplevel_tuntap_socket(ClientData cd, Tcl_Interp *ip,
- const TunSocket_SubCommand *subcmd,
- int objc, Tcl_Obj *const *objv) {
- return subcmd->func(0,ip,objc,objv);
-}
-
-int do_toplevel_ulong(ClientData cd, Tcl_Interp *ip,
- const ULong_SubCommand *subcmd,
- int objc, Tcl_Obj *const *objv) {
- return subcmd->func(0,ip,objc,objv);