X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-tcl.git;a=blobdiff_plain;f=hbytes%2Fhook.c;h=b84306a1e12c8a516b21aedf9ca27ae200ca3751;hp=b6c0c9e2128e54ad06b137cb3f1d8928a87b2329;hb=ed7354bcb695fcf3ee3f5cea14be209fbb99f161;hpb=b78c59b29c46210ccbfc0fe4815b4e49fba8817b diff --git a/hbytes/hook.c b/hbytes/hook.c index b6c0c9e..b84306a 100644 --- a/hbytes/hook.c +++ b/hbytes/hook.c @@ -6,8 +6,9 @@ #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); + if (ec) Tcl_SetObjErrorCode(ip, Tcl_NewStringObj(ec,-1)); return TCL_ERROR; } @@ -21,6 +22,13 @@ int posixerr(Tcl_Interp *ip, int errnoval, const char *m) { 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); @@ -61,21 +69,28 @@ int do_hbytes_rep_info(ClientData cd, Tcl_Interp *ip, } static void hbytes_t_dup(Tcl_Obj *src, Tcl_Obj *dup) { - objfreeir(dup); hbytes_array(OBJ_HBYTES(dup), hbytes_data(OBJ_HBYTES(src)), hbytes_len(OBJ_HBYTES(src))); + dup->typePtr= &hbytes_type; } static void hbytes_t_free(Tcl_Obj *o) { hbytes_free(OBJ_HBYTES(o)); } -void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) { +void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte, + int l, const char *prefix) { char *str; + int pl; + + pl= strlen(prefix); + o->length= l*2+pl; + str= o->bytes= TALLOC(o->length+1); + + memcpy(str,prefix,pl); + str += pl; - str= o->bytes= TALLOC(l*2+1); - o->length= l*2; while (l>0) { sprintf(str,"%02x",*byte); str+=2; byte++; l--; @@ -83,6 +98,38 @@ void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) { *str= 0; } +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)), @@ -95,25 +142,35 @@ static int hbytes_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) { int l; char cbuf[3]; - os= str= Tcl_GetStringFromObj(o,&l); assert(str); - objfreeir(o); - - if (l & 1) return staticerr(ip, "hbytes: conversion from hex:" - " odd length in hex"); + if (o->typePtr == &ulong_type) { + uint32_t ul; - startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2); + ul= htonl(*(const uint32_t*)&o->internalRep.longValue); + hbytes_array(OBJ_HBYTES(o), (const Byte*)&ul, 4); - cbuf[2]= 0; - while (l>0) { - cbuf[0]= *str++; - cbuf[1]= *str++; - *bytes++= strtoul(cbuf,&ep,16); - if (ep != cbuf+2) { - hbytes_free(OBJ_HBYTES(o)); - return staticerr(ip, "hbytes: conversion from hex:" - " bad hex digit"); + } else { + + os= str= Tcl_GetStringFromObj(o,&l); assert(str); + objfreeir(o); + + if (l & 1) return staticerr(ip, "hbytes: conversion from hex:" + " odd length in hex", "HBYTES SYNTAX"); + + startbytes= bytes= hbytes_arrayspace(OBJ_HBYTES(o), l/2); + + cbuf[2]= 0; + while (l>0) { + cbuf[0]= *str++; + cbuf[1]= *str++; + *bytes++= strtoul(cbuf,&ep,16); + if (ep != cbuf+2) { + hbytes_free(OBJ_HBYTES(o)); + return staticerr(ip, "hbytes: conversion from hex:" + " bad hex digit", "HBYTES SYNTAX"); + } + l -= 2; } - l -= 2; + } o->typePtr = &hbytes_type; @@ -127,17 +184,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; } @@ -158,6 +215,69 @@ 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", + "HBYTES LENGTH RANGE"); + if (start + sub_l > hbytes_len(v.hb)) + 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; +} + +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 INT_MAX/sub_l) return staticerr(ip, "hbytes repeat too long", 0); + + 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_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; @@ -166,9 +286,97 @@ int do_hbytes_zeroes(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_compare(ClientData cd, Tcl_Interp *ip, + HBytes_Value a, HBytes_Value b, int *result) { + int al, bl, minl, r; + + al= hbytes_len(&a); + bl= hbytes_len(&b); + minl= al0) *result= +2; + else { + if (albl) *result= +1; + else *result= 0; + } + return TCL_OK; +} + +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) + return staticerr(ip,"hbytes range subscript(s) -ve","HBYTES LENGTH RANGE"); + if (l2) + 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); + return TCL_OK; +} + +int do_hbytes_ushort2h(ClientData cd, Tcl_Interp *ip, + long input, HBytes_Value *result) { + uint16_t us; + + if (input > 0x0ffff) + return staticerr(ip, "hbytes ushort2h input >2^16", + "HBYTES VALUE OVERFLOW"); + + us= htons(input); + hbytes_array(result,(const Byte*)&us,2); + return TCL_OK; +} + +/* toplevel functions */ + +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_toplevel_tuntap_socket(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); } @@ -193,15 +401,25 @@ int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) { return posixerr(ip,errno,"read " URANDOM); } else { assert(feof(urandom)); - return staticerr(ip, URANDOM " gave eof!"); + return staticerr(ip, URANDOM " gave eof!", 0); } } 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_CreateObjCommand(ip,"hbytes", pa__hbytes,0,0); + Tcl_RegisterObjType(&sockaddr_type); + Tcl_RegisterObjType(&tabledataid_nearlytype); + Tcl_RegisterObjType(&ulong_type); + + for (cmd=toplevel_commands; + cmd->name; + cmd++) + Tcl_CreateObjCommand(ip, cmd->name, cmd->func, 0,0); + return TCL_OK; }