From 29d695bbfb0f42019af456fc8a7a3137e9745806 Mon Sep 17 00:00:00 2001 From: ian Date: Tue, 10 Sep 2002 23:47:13 +0000 Subject: [PATCH] hbytes overwrite; hbytes repeat --- base/chiark-tcl.h | 2 ++ base/hook.c | 32 ++++++++++++++++++++++++++++++++ base/tables-examples.tct | 20 ++++++++++++++------ hbytes/hbytes.h | 2 ++ hbytes/hook.c | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 6 deletions(-) diff --git a/base/chiark-tcl.h b/base/chiark-tcl.h index 864019e..23cad20 100644 --- a/base/chiark-tcl.h +++ b/base/chiark-tcl.h @@ -14,6 +14,8 @@ * (too short? error) * * hbytes range VALUE START SIZE => substring (or error) + * hbytes overwrite VAR START VALUE + * hbytes repeat VALUE COUNT => COUNT copies of VALUE * * hbytes h2ulong HEX => ulong (HEX len must be 4) * hbytes ulong2h UL => hex diff --git a/base/hook.c b/base/hook.c index 7f889fb..f364978 100644 --- a/base/hook.c +++ b/base/hook.c @@ -197,6 +197,38 @@ 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_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; diff --git a/base/tables-examples.tct b/base/tables-examples.tct index f0f2235..f2146a5 100644 --- a/base/tables-examples.tct +++ b/base/tables-examples.tct @@ -108,6 +108,20 @@ Table hbytes HBytes_SubCommand v hbv length int => hb + overwrite + v hbv + start int + sub hb + zeroes + length int + => hb + repeat + v hb + count int + => hb + random + length int + => hb pkcs5 meth enum(PadMethod, "hbytes pad subcommand") v hbv @@ -131,12 +145,6 @@ Table hbytes HBytes_SubCommand key obj ?maclen obj => hb - zeroes - length int - => hb - random - length int - => hb Table dgram_socket DgramSocket_SubCommand create diff --git a/hbytes/hbytes.h b/hbytes/hbytes.h index 864019e..23cad20 100644 --- a/hbytes/hbytes.h +++ b/hbytes/hbytes.h @@ -14,6 +14,8 @@ * (too short? error) * * hbytes range VALUE START SIZE => substring (or error) + * hbytes overwrite VAR START VALUE + * hbytes repeat VALUE COUNT => COUNT copies of VALUE * * hbytes h2ulong HEX => ulong (HEX len must be 4) * hbytes ulong2h UL => hex diff --git a/hbytes/hook.c b/hbytes/hook.c index 7f889fb..f364978 100644 --- a/hbytes/hook.c +++ b/hbytes/hook.c @@ -197,6 +197,38 @@ 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_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; -- 2.30.2