chiark / gitweb /
hbytes range
[chiark-tcl.git] / base / hook.c
index 08e055d6426379687d01c580a3e8caf6e099c121..0efc5f1607aabb7539727d681662ce29c46cc089 100644 (file)
@@ -1,6 +1,8 @@
 /*
  */
 
+#include <errno.h>
+
 #include "hbytes.h"
 #include "tables.h"
 
@@ -9,6 +11,16 @@ int staticerr(Tcl_Interp *ip, const char *m) {
   return TCL_ERROR;
 }
 
+int posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
+  const char *em;
+  
+  Tcl_ResetResult(ip);
+  errno= errnoval;
+  em= Tcl_PosixError(ip);
+  Tcl_AppendResult(ip, m, ": ", em, (char*)0);
+  return TCL_ERROR;
+}
+
 void objfreeir(Tcl_Obj *o) {
   if (o->typePtr && o->typePtr->freeIntRepProc)
     o->typePtr->freeIntRepProc(o);
@@ -16,27 +28,35 @@ void objfreeir(Tcl_Obj *o) {
 }  
 
 int do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip,
-                      HBytes_Value v, Tcl_Obj **result) {
+                      Tcl_Obj *obj, Tcl_Obj **result) {
   const char *tn;
-  int nums[3], i;
+  int nums[3], i, lnl;
   Tcl_Obj *objl[4];
 
-  memset(nums,0,sizeof(nums));
-  nums[1]= hbytes_len(&v);
+  if (obj->typePtr == &hbytes_type) {
+    HBytes_Value *v= OBJ_HBYTES(obj);
+    memset(nums,0,sizeof(nums));
+    nums[1]= hbytes_len(v);
   
-  if (HBYTES_ISEMPTY(&v)) tn= "empty";
-  else if (HBYTES_ISSENTINEL(&v)) tn= "sentinel!";
-  else if (HBYTES_ISSIMPLE(&v)) tn= "simple";
-  else {
-    HBytes_ComplexValue *cx= v.begin_complex;
-    tn= "complex";
-    nums[0]= cx->prespace;
-    nums[2]= cx->avail - cx->len;
+    if (HBYTES_ISEMPTY(v)) tn= "empty";
+    else if (HBYTES_ISSENTINEL(v)) tn= "sentinel!";
+    else if (HBYTES_ISSIMPLE(v)) tn= "simple";
+    else {
+      HBytes_ComplexValue *cx= v->begin_complex;
+      tn= "complex";
+      nums[0]= cx->prespace;
+      nums[2]= cx->avail - cx->len;
+    }
+    lnl= 3;
+  } else {
+    tn= "other";
+    lnl= 0;
   }
     
   objl[0]= Tcl_NewStringObj((char*)tn,-1);
-  for (i=0; i<3; i++) objl[i+1]= Tcl_NewIntObj(nums[i]);
-  *result= Tcl_NewListObj(4,objl);
+  for (i=0; i<lnl; i++) objl[i+1]= Tcl_NewIntObj(nums[i]);
+  *result= Tcl_NewListObj(lnl+1,objl);
+    
   return TCL_OK;
 }
 
@@ -51,15 +71,18 @@ static void hbytes_t_free(Tcl_Obj *o) {
   hbytes_free(OBJ_HBYTES(o));
 }
 
-static void hbytes_t_ustr(Tcl_Obj *o) {
-  int l;
+void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte,
+                               int l, const char *prefix) {
   char *str;
-  const Byte *byte;
+  int pl;
+
+  pl= strlen(prefix);
+  o->length= l*2+pl;
+  str= o->bytes= TALLOC(o->length+1);
+  
+  memcpy(str,prefix,pl);
+  str += pl;
 
-  byte= hbytes_data(OBJ_HBYTES(o));
-  l= hbytes_len(OBJ_HBYTES(o));
-  str= o->bytes= TALLOC(l*2+1);
-  o->length= l*2;
   while (l>0) {
     sprintf(str,"%02x",*byte);
     str+=2; byte++; l--;
@@ -67,6 +90,16 @@ static void hbytes_t_ustr(Tcl_Obj *o) {
   *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)),
+                     hbytes_len(OBJ_HBYTES(o)));
+}
+
 static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
   char *str, *ep, *os;
   Byte *startbytes, *bytes;
@@ -119,44 +152,111 @@ int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }
 
-#if 0
-HC_DEFINE(pkcs5) {
-  static const PadKindInfo padkindinfos[0]= {
-    { "pa", 1, 1 },
-    { "pn", 1, 0 },
-    { "ua", 0, 1 },
-    { "un", 0, 0 },
-    { 0 }
-  };
+int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
+                    HBytes_Value v, int *result) {
+  *result= hbytes_len(&v);
+  return TCL_OK;
+}
 
-  HC_DECLS_HBV;
-  Tcl_Obj *v;
-  int blocksize;
-  const PadKindInfo *pk;
-  const BlockCipherInfo *bc;
+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;
+}
 
-  HC_ARG_ENUM(pk, padkindinfos);
-  HC_ARG_HBV;
-  if (!pk->algname) HC_ARG_INTRANGE(blocksize, 1,255);
-  else { HC_ARG_ENUM(bc, blockciphers); blocksize= bc->blocksize; }
-  HC_ARGS_E;
+int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
+                     HBytes_Value a, HBytes_Value b, int *result) {
+  int al, bl, minl, r;
 
-  /* do nothing :-) */
+  al= hbytes_len(&a);
+  bl= hbytes_len(&b);
+  minl= al<bl ? al : bl;
 
-  HC_FINI_HBV;
+  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;
 }
-#endif
 
+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) {
+  static FILE *urandom;
+
+  int r, esave;
+
+  if (!urandom) {
+    urandom= fopen(URANDOM,"rb");
+    if (!urandom) return posixerr(ip,errno,"open " URANDOM);
+  }
+  r= fread(buffer,1,l,urandom);
+  if (r==l) return 0;
+
+  esave= errno;
+  fclose(urandom); urandom=0;
+
+  if (ferror(urandom)) {
+    return posixerr(ip,errno,"read " URANDOM);
+  } else {
+    assert(feof(urandom));
+    return staticerr(ip, URANDOM " gave eof!");
+  }
+}
+
 int Hbytes_Init(Tcl_Interp *ip) {
   Tcl_RegisterObjType(&hbytes_type);
+  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;
 }