chiark / gitweb /
hbytes range
[chiark-tcl.git] / base / hook.c
index 68df783561431b14b411c22fa274111b5e16fa99..0efc5f1607aabb7539727d681662ce29c46cc089 100644 (file)
@@ -71,11 +71,18 @@ static void hbytes_t_free(Tcl_Obj *o) {
   hbytes_free(OBJ_HBYTES(o));
 }
 
-void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
+void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
+                               int l, const char *prefix) {
   char *str;
+  int pl;
+
+  pl= strlen(prefix);
+  o->length= l*2+pl;
+  str= o->bytes= TALLOC(o->length+1);
+  
+  memcpy(str,prefix,pl);
+  str += pl;
 
-  str= o->bytes= TALLOC(l*2+1);
-  o->length= l*2;
   while (l>0) {
     sprintf(str,"%02x",*byte);
     str+=2; byte++; l--;
@@ -83,6 +90,10 @@ void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
   *str= 0;
 }
 
+void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
+  obj_updatestr_array_prefix(o,byte,l,"");
+}
+
 static void hbytes_t_ustr(Tcl_Obj *o) {
   obj_updatestr_array(o,
                      hbytes_data(OBJ_HBYTES(o)),
@@ -147,12 +158,72 @@ int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
   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_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__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__dgram_socket(ClientData cd, Tcl_Interp *ip,
+                    const DgramSocket_SubCommand *subcmd,
+                    int objc, Tcl_Obj *const *objv) {
+  return subcmd->func(0,ip,objc,objv);
+}
+
 #define URANDOM "/dev/urandom"
 
 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
@@ -183,6 +254,9 @@ int Hbytes_Init(Tcl_Interp *ip) {
   Tcl_RegisterObjType(&blockcipherkey_type);
   Tcl_RegisterObjType(&enum_nearlytype);
   Tcl_RegisterObjType(&enum1_nearlytype);
-  Tcl_CreateObjCommand(ip,"hbytes", pa__hbytes,0,0);
+  Tcl_RegisterObjType(&sockaddr_type);
+  Tcl_RegisterObjType(&sockid_type);
+  Tcl_CreateObjCommand(ip, "hbytes",       pa__hbytes,       0,0);
+  Tcl_CreateObjCommand(ip, "dgram-socket", pa__dgram_socket, 0,0);
   return TCL_OK;
 }