chiark / gitweb /
many improvements: use Get/SetAssocData for idtables to avoid globals, and adns bindi...
[chiark-tcl.git] / hbytes / hook.c
index 61593aba5a1bd5abb6a40c7040308772a2ce9978..b65741f38b09865e4e25b3b5651993d9ac1ac4d7 100644 (file)
@@ -6,8 +6,9 @@
 #include "hbytes.h"
 #include "tables.h"
 
 #include "hbytes.h"
 #include "tables.h"
 
-int staticerr(Tcl_Interp *ip, const char *m) {
+int staticerr(Tcl_Interp *ip, const char *m, const char *ec) {
   Tcl_SetResult(ip, (char*)m, TCL_STATIC);
   Tcl_SetResult(ip, (char*)m, TCL_STATIC);
+  if (ec) Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(ec,-1));
   return TCL_ERROR;
 }
 
   return TCL_ERROR;
 }
 
@@ -21,6 +22,13 @@ int posixerr(Tcl_Interp *ip, int errnoval, const char *m) {
   return TCL_ERROR;
 }
 
   return TCL_ERROR;
 }
 
+int newfdposixerr(Tcl_Interp *ip, int fd, const char *m) {
+  int e;
+  e= errno;
+  close(fd);
+  return posixerr(ip,e,m);
+}
+
 void objfreeir(Tcl_Obj *o) {
   if (o->typePtr && o->typePtr->freeIntRepProc)
     o->typePtr->freeIntRepProc(o);
 void objfreeir(Tcl_Obj *o) {
   if (o->typePtr && o->typePtr->freeIntRepProc)
     o->typePtr->freeIntRepProc(o);
@@ -146,7 +154,7 @@ static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
     objfreeir(o);
 
     if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
     objfreeir(o);
 
     if (l & 1) return staticerr(ip, "hbytes: conversion from hex:"
-                               " odd length in hex");
+                               " odd length in hex", "HBYTES SYNTAX");
 
     startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
 
 
     startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2);
 
@@ -158,7 +166,7 @@ static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
       if (ep != cbuf+2) {
        hbytes_free(OBJ_HBYTES(o));
        return staticerr(ip, "hbytes: conversion from hex:"
       if (ep != cbuf+2) {
        hbytes_free(OBJ_HBYTES(o));
        return staticerr(ip, "hbytes: conversion from hex:"
-                        " bad hex digit");
+                        " bad hex digit", "HBYTES SYNTAX");
       }
       l -= 2;
     }
       }
       l -= 2;
     }
@@ -213,9 +221,11 @@ int do_hbytes_overwrite(ClientData cd, Tcl_Interp *ip,
 
   sub_l= hbytes_len(&sub);
   if (start < 0)
 
   sub_l= hbytes_len(&sub);
   if (start < 0)
-    return staticerr(ip, "hbytes overwrite start -ve");
+    return staticerr(ip, "hbytes overwrite start -ve",
+                    "HBYTES LENGTH RANGE");
   if (start + sub_l > hbytes_len(v.hb))
   if (start + sub_l > hbytes_len(v.hb))
-    return staticerr(ip, "hbytes overwrite out of range");
+    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;
 }
   memcpy(hbytes_data(v.hb) + start, hbytes_data(&sub), sub_l);
   return TCL_OK;
 }
@@ -239,8 +249,9 @@ int do_hbytes_repeat(ClientData cd, Tcl_Interp *ip,
   const Byte *sub_d;
 
   sub_l= hbytes_len(&sub);
   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");
+  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);
 
   data= hbytes_arrayspace(result, sub_l*count);
   sub_d= hbytes_data(&sub);
@@ -251,6 +262,22 @@ int do_hbytes_repeat(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }  
 
   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;
 int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
                     int length, HBytes_Value *result) {
   Byte *space;
@@ -286,14 +313,23 @@ int do_hbytes_range(ClientData cd, Tcl_Interp *ip,
   int l;
 
   l= hbytes_len(&v);
   int l;
 
   l= hbytes_len(&v);
-  if (start<0 || size<0 || l<start+size)
-    return staticerr(ip, "hbytes range subscripts out of range");
+  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;
 }
 
 
   data= hbytes_data(&v);
   hbytes_array(result, data+start, size);
   return TCL_OK;
 }
 
+int do_hbytes_mask_map(ClientData cd, Tcl_Interp *ip,
+                     const MaskMap_SubCommand *subcmd,
+                     int objc, Tcl_Obj *const *objv) {
+  return subcmd->func(0,ip,objc,objv);
+}
+
 /* hbytes representing uint16_t's */
 
 int do_hbytes_h2ushort(ClientData cd, Tcl_Interp *ip,
 /* hbytes representing uint16_t's */
 
 int do_hbytes_h2ushort(ClientData cd, Tcl_Interp *ip,
@@ -303,7 +339,8 @@ int do_hbytes_h2ushort(ClientData cd, Tcl_Interp *ip,
 
   l= hbytes_len(&hex);
   if (l>2)
 
   l= hbytes_len(&hex);
   if (l>2)
-    return staticerr(ip, "hbytes h2ushort input more than 4 hex digits");
+    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);
 
   data= hbytes_data(&hex);
   *result= data[l-1] | (l>1 ? data[0]<<8 : 0);
@@ -315,7 +352,8 @@ int do_hbytes_ushort2h(ClientData cd, Tcl_Interp *ip,
   uint16_t us;
 
   if (input > 0x0ffff)
   uint16_t us;
 
   if (input > 0x0ffff)
-    return staticerr(ip, "hbytes ushort2h input >2^16");
+    return staticerr(ip, "hbytes ushort2h input >2^16",
+                    "HBYTES VALUE OVERFLOW");
 
   us= htons(input);
   hbytes_array(result,(const Byte*)&us,2);
 
   us= htons(input);
   hbytes_array(result,(const Byte*)&us,2);
@@ -336,12 +374,24 @@ int do_toplevel_dgram_socket(ClientData cd, Tcl_Interp *ip,
   return subcmd->func(0,ip,objc,objv);
 }
 
   return subcmd->func(0,ip,objc,objv);
 }
 
+int do_toplevel_tuntap_socket_raw(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);
 }
 
 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);
 }
 
+int do_toplevel_adns(ClientData cd, Tcl_Interp *ip,
+                     const Adns_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) {
 #define URANDOM "/dev/urandom"
 
 int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
@@ -363,7 +413,7 @@ int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) {
     return posixerr(ip,errno,"read " URANDOM);
   } else {
     assert(feof(urandom));
     return posixerr(ip,errno,"read " URANDOM);
   } else {
     assert(feof(urandom));
-    return staticerr(ip, URANDOM " gave eof!");
+    return staticerr(ip, URANDOM " gave eof!", 0);
   }
 }
 
   }
 }
 
@@ -375,13 +425,14 @@ int Hbytes_Init(Tcl_Interp *ip) {
   Tcl_RegisterObjType(&enum_nearlytype);
   Tcl_RegisterObjType(&enum1_nearlytype);
   Tcl_RegisterObjType(&sockaddr_type);
   Tcl_RegisterObjType(&enum_nearlytype);
   Tcl_RegisterObjType(&enum1_nearlytype);
   Tcl_RegisterObjType(&sockaddr_type);
-  Tcl_RegisterObjType(&dgramsockid_type);
+  Tcl_RegisterObjType(&tabledataid_nearlytype);
   Tcl_RegisterObjType(&ulong_type);
   Tcl_RegisterObjType(&ulong_type);
+  Tcl_RegisterObjType(&maskmap_type);
 
   for (cmd=toplevel_commands;
        cmd->name;
        cmd++)
 
   for (cmd=toplevel_commands;
        cmd->name;
        cmd++)
-    Tcl_CreateObjCommand(ip, cmd->name, cmd->func, 0,0);
+    Tcl_CreateObjCommand(ip, (char*)cmd->name, cmd->func, 0,0);
 
   return TCL_OK;
 }
 
   return TCL_OK;
 }