+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");
+ if (start + sub_l > hbytes_len(v.hb))
+ return staticerr(ip, "hbytes overwrite out of range");
+ 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");
+ if (count > INT_MAX/sub_l) return staticerr(ip, "hbytes repeat too long");
+
+ 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_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 || l<start+size)
+ return staticerr(ip, "hbytes range subscripts out of range");
+
+ data= hbytes_data(&v);
+ hbytes_array(result, data+start, size);
+ return TCL_OK;
+}
+
+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_ulong(ClientData cd, Tcl_Interp *ip,
+ const ULong_SubCommand *subcmd,
+ int objc, Tcl_Obj *const *objv) {