chiark / gitweb /
all compiles again. more pat to do.
[chiark-tcl.git] / hbytes / hbytes.c
index 99bddfe986acb222f953b40d9dbd5fecdd1217fe..626b2da6cdd497dfbece6a6ef8cc8d51f183ed5a 100644 (file)
@@ -3,26 +3,31 @@
  */
 
 #include "hbytes.h"
+#include "tables.h"
 
 int staticerr(Tcl_Interp *ip, const char *m) {
   Tcl_SetResult(ip, (char*)m, TCL_STATIC);
   return TCL_ERROR;
 }
 
-static void hbytes_setintern(Tcl_Obj *o, const Byte *array, int l) {
+void hbytes_set(HBytes_Value *upd, const Byte *array, int l) {
   Byte *np;
-    
-  HBYTES(o)->start= np= l ? TALLOC(l) : 0;
+
+  upd->start= np= l ? TALLOC(l) : 0;
   memcpy(np, array, l);
-  HBYTES(o)->end= np + l;
+  upd->end= np + l;
+}
+
+static void hbytes_setintern(Tcl_Obj *o, const Byte *array, int l) {
+  hbytes_set(&HBYTES(o), array, l);
   o->typePtr = &hbytes_type;
 }
 
 static void hbytes_t_dup(Tcl_Obj *src, Tcl_Obj *dup) {
-  hbytes_setintern(src, HBYTES(src)->start, HBYTES_LEN(src));
+  hbytes_setintern(src, HBYTES(src).start, HBYTES_LEN(HBYTES(src)));
 }
 
-Tcl_Obj *hbytes_set(Tcl_Obj *overwrite, const Byte *array, int l) {
+Tcl_Obj *hbytes_set_obj(Tcl_Obj *overwrite, const Byte *array, int l) {
   if (!overwrite) overwrite= Tcl_NewObj();
   objfreeir(overwrite);
   Tcl_InvalidateStringRep(overwrite);
@@ -31,7 +36,7 @@ Tcl_Obj *hbytes_set(Tcl_Obj *overwrite, const Byte *array, int l) {
 }
 
 static void hbytes_t_free(Tcl_Obj *o) {
-  TFREE(HBYTES(o)->start);
+  TFREE(HBYTES(o).start);
 }
 
 static void hbytes_t_ustr(Tcl_Obj *o) {
@@ -39,8 +44,8 @@ static void hbytes_t_ustr(Tcl_Obj *o) {
   char *str;
   const Byte *byte;
 
-  l= HBYTES_LEN(o);
-  byte= HBYTES(o)->start;
+  l= HBYTES_LEN(HBYTES(o));
+  byte= HBYTES(o).start;
   str= o->bytes= TALLOC(l*2+1);
   o->length= l*2;
   while (l>0) {
@@ -80,8 +85,8 @@ fprintf(stderr,">%d|%s|%s<\n",l,os,cbuf);
   }
   objfreeir(o);
 
-  HBYTES(o)->start= startbytes;
-  HBYTES(o)->end= bytes;
+  HBYTES(o).start= startbytes;
+  HBYTES(o).end= bytes;
   o->typePtr = &hbytes_type;
   return TCL_OK;
 }
@@ -91,75 +96,60 @@ Tcl_ObjType hbytes_type = {
   hbytes_t_free, hbytes_t_dup, hbytes_t_ustr, hbytes_t_sfa
 };
 
-static Tcl_Obj *hb_getvar(Tcl_Interp *ip, Tcl_Obj *varname) {
-  int ec;
-  Tcl_Obj *value;
-  
-  value= Tcl_ObjGetVar2(ip,varname,0,TCL_LEAVE_ERR_MSG);
-  if (!value) return 0;
-
-  ec= Tcl_ConvertToType(ip,value,&hbytes_type);
-  if (ec) return 0;
-
-  return value;
-}
-
-static int hc_bin(ClientData cd, Tcl_Interp *ip, int objc,
-                 Tcl_Obj *const *objv) {
-  Tcl_Obj *varname, *value, *result;
+int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip,
+                   Tcl_Obj *binary, HBytes_Value *result) {
   const char *str;
   int l;
 
-  varname= objv[0];
-  switch (objc) {
-  case 1:
-    value= hb_getvar(ip,varname);  if (!value) return TCL_ERROR;
-    result= Tcl_NewStringObj(HBYTES(value)->start, HBYTES_LEN(value));
-    assert(result);
-    Tcl_SetObjResult(ip,result);
-    return TCL_OK;
-  case 2:
-    value= objv[1];
-    str= Tcl_GetStringFromObj(value,&l);
-    value= hbytes_set(0,str,l);
-    value= Tcl_ObjSetVar2(ip,varname,0, value, TCL_LEAVE_ERR_MSG);
-    if (!value) return TCL_ERROR;
-    Tcl_ResetResult(ip);
-    return TCL_OK;
-  }
-  abort();
+  str= Tcl_GetStringFromObj(binary,&l);
+  hbytes_set(result, str, l);
+  return TCL_OK;
 }
 
-typedef struct {
-  const char *name;
-  int minargs, maxargs;
-  Tcl_ObjCmdProc *func;
-} SubCommand;
+int do_hbytes_h2raw(ClientData cd, Tcl_Interp *ip,
+                   HBytes_Value hex, Tcl_Obj **result) {
+  *result= Tcl_NewStringObj(hex.start, HBYTES_LEN(hex));
+  return TCL_OK;
+}
 
-static const SubCommand subcommands[] = {
-  { "bin", 1, 2, hc_bin },
-  { 0 }
-};
+#if 0
+HC_DEFINE(pkcs5) {
+  static const PadKindInfo padkindinfos[0]= {
+    { "pa", 1, 1 },
+    { "pn", 1, 0 },
+    { "ua", 0, 1 },
+    { "un", 0, 0 },
+    { 0 }
+  };
+
+  HC_DECLS_HBV;
+  Tcl_Obj *v;
+  int blocksize;
+  const PadKindInfo *pk;
+  const BlockCipherInfo *bc;
+
+  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;
+
+  /* do nothing :-) */
+
+  HC_FINI_HBV;
+}
+#endif
 
-static int hb_proc(ClientData cd, Tcl_Interp *ip, int objc,
-                  Tcl_Obj *const *objv) {
-  const SubCommand *sc;
-
-  if (objc<2) return staticerr(ip, "hbytes: need subcommand");
-  sc= enum_lookup_cached(ip,objv[1],subcommands,"hbytes subcommand");
-  if (!sc) return TCL_ERROR;
-  objc -= 2;
-  objv += 2;
-  if (objc < sc->minargs)
-    return staticerr(ip, "too few args");
-  if (sc->maxargs >=0 && objc > sc->maxargs)
-    return staticerr(ip,"too many args");
-  return sc->func((void*)sc,ip,objc,objv);
+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 Hbytes_Init(Tcl_Interp *ip) {
   Tcl_RegisterObjType(&hbytes_type);
   Tcl_RegisterObjType(&enum_nearlytype);
-  Tcl_CreateObjCommand(ip,"hbytes", hb_proc,0,0);
+  Tcl_RegisterObjType(&enum1_nearlytype);
+  Tcl_CreateObjCommand(ip,"hbytes", pa__hbytes,0,0);
   return TCL_OK;
 }