chiark / gitweb /
Abolish serpent-l. Use PKCS#1 v1.5 RSA padding but without hash OID for secnet.
[chiark-tcl.git] / base / hook.c
index 8774f7b6a3fb174a5588305052cb65dc1f8dff93..0adb6b5d5804bb54e7669ef485987a69fb38a9a9 100644 (file)
@@ -94,6 +94,34 @@ void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) {
   obj_updatestr_array_prefix(o,byte,l,"");
 }
 
+void obj_updatestr_vstringls(Tcl_Obj *o, ...) {
+  va_list al;
+  char *p;
+  const char *part;
+  int l, pl;
+
+  va_start(al,o);
+  for (l=0; (part= va_arg(al, const char*)); )
+    l+= va_arg(al, int);
+  va_end(al);
+  
+  o->length= l;
+  o->bytes= TALLOC(l+1);
+
+  va_start(al,o);
+  for (p= o->bytes; (part= va_arg(al, const char*)); p += pl) {
+    pl= va_arg(al, int);
+    memcpy(p, part, pl);
+  }
+  va_end(al);
+
+  *p= 0;
+}
+
+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)),
@@ -138,17 +166,17 @@ Tcl_ObjType hbytes_type = {
 
 int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
                    Tcl_Obj *binary, HBytes_Value *result) {
-  const char *str;
+  const unsigned char *str;
   int l;
 
-  str= Tcl_GetStringFromObj(binary,&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_NewStringObj(hbytes_data(&hex), hbytes_len(&hex));
+  *result= Tcl_NewByteArrayObj(hbytes_data(&hex), hbytes_len(&hex));
   return TCL_OK;
 }
 
@@ -169,6 +197,50 @@ int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
   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;
@@ -197,15 +269,36 @@ int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }
 
-int do__hbytes(ClientData cd, Tcl_Interp *ip,
-              const HBytes_SubCommand *subcmd,
-              int objc, Tcl_Obj *const *objv) {
+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__dgram_socket(ClientData cd, Tcl_Interp *ip,
-                    const DgramSocket_SubCommand *subcmd,
-                    int objc, Tcl_Obj *const *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);
 }
 
@@ -235,13 +328,20 @@ int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
 }
 
 int Hbytes_Init(Tcl_Interp *ip) {
+  const TopLevel_Command *cmd;
+
   Tcl_RegisterObjType(&hbytes_type);
   Tcl_RegisterObjType(&blockcipherkey_type);
   Tcl_RegisterObjType(&enum_nearlytype);
   Tcl_RegisterObjType(&enum1_nearlytype);
   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);
+  Tcl_RegisterObjType(&dgramsockid_type);
+  Tcl_RegisterObjType(&ulong_type);
+
+  for (cmd=toplevel_commands;
+       cmd->name;
+       cmd++)
+    Tcl_CreateObjCommand(ip, cmd->name, cmd->func, 0,0);
+
   return TCL_OK;
 }