X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-tcl.git;a=blobdiff_plain;f=hbytes%2Fhbytes.c;h=d095531a03c47325d26be59467cde9a99cdea93b;hp=d4a33f5082c056a1df90a0464e7323c5138cb1cf;hb=310a482133795d0bee9621003c0a0b3813a47037;hpb=9f10950ab67992a0ff2304639673c2fc97744e0d diff --git a/hbytes/hbytes.c b/hbytes/hbytes.c index d4a33f5..d095531 100644 --- a/hbytes/hbytes.c +++ b/hbytes/hbytes.c @@ -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,48 +96,24 @@ 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; -} - -HC_DEFINE(raw2h) { - HC_DECLS; - Tcl_Obj *raw, *value; +int do_hbytes_raw2h(ClientData cd, Tcl_Interp *ip, + Tcl_Obj *binary, HBytes_Value *result) { const char *str; int l; - HC_ARG_O(raw); - HC_ARGS_E; - str= Tcl_GetStringFromObj(raw,&l); - value= hbytes_set(0,str,l); - Tcl_SetObjResult(ip,value); - HC_FINI; + str= Tcl_GetStringFromObj(binary,&l); + hbytes_set(result, str, l); + return TCL_OK; } -HC_DEFINE(h2raw) { - Tcl_Obj *value, *result; - - HC_ARG_H(value); - HC_ARGS_E; - result= Tcl_NewStringObj(HBYTES(value)->start, HBYTES_LEN(value)); - Tcl_SetObjResult(ip,result); - HC_FINI; +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; } +#if 0 HC_DEFINE(pkcs5) { - typedef struct { - const char *spec; - int pad, algname; - } PadKindInfo; static const PadKindInfo padkindinfos[0]= { { "pa", 1, 1 }, { "pn", 1, 0 }, @@ -157,67 +138,17 @@ HC_DEFINE(pkcs5) { HC_FINI_HBV; } - -static int hc_raw2h(ClientData cd, Tcl_Interp *ip, int objc, - Tcl_Obj *const *objv) { - - Tcl_Obj *varname, *value, *result; - - varname= objv[0]; - switch (objc) { - case 1: - value= hb_getvar(ip,varname); if (!value) return TCL_ERROR; - assert(result); - Tcl_SetObjResult(ip,result); - return TCL_OK; - case 2: - value= objv[1]; - HC_MINARGS(1); - - value= Tcl_ObjSetVar2(ip,varname,0, value, TCL_LEAVE_ERR_MSG); - if (!value) return TCL_ERROR; - Tcl_ResetResult(ip); - return TCL_OK; - } - abort(); -} - -typedef struct { - const char *name; - int minargs, maxargs; - Tcl_ObjCmdProc *func; -} SubCommand; - -#define SUBCOMMANDS \ - DO(raw2h) \ - DO(h2raw) \ - DO(pkcs5) - -static const SubCommand subcommands[] = { -#define DO(c) { #c, hc_##c }, - SUBCOMMANDS - { 0 } -}; +#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 ParserCommandTableEntry *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_CreateObjCommand(ip,"hbytes", pa__hbytes,0,0); return TCL_OK; }