chiark / gitweb /
hbytes overwrite; hbytes repeat
authorian <ian>
Tue, 10 Sep 2002 23:47:13 +0000 (23:47 +0000)
committerian <ian>
Tue, 10 Sep 2002 23:47:13 +0000 (23:47 +0000)
base/chiark-tcl.h
base/hook.c
base/tables-examples.tct
hbytes/hbytes.h
hbytes/hook.c

index 864019e50d82a9edfb297cbf335b6a5953872bce..23cad209c11840758e99052d62198efd632bca04 100644 (file)
@@ -14,6 +14,8 @@
  *                                                  (too short? error)
  *
  *  hbytes range VALUE START SIZE                => substring (or error)
  *                                                  (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
  *
  *  hbytes h2ulong HEX                           => ulong  (HEX len must be 4)
  *  hbytes ulong2h UL                            => hex
index 7f889fbdc6077632ada45be5e5a6adca703df142..f364978c200db68dde9b2ba53c0a3e8faa898533 100644 (file)
@@ -197,6 +197,38 @@ int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }  
   
   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;
 int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
                     int length, HBytes_Value *result) {
   Byte *space;
index f0f22354400958e7102e1ea56c99ec33c7a1d13b..f2146a552c6dc23b1c3518ce7604a415c27ca09a 100644 (file)
@@ -108,6 +108,20 @@ Table hbytes HBytes_SubCommand
                v       hbv
                length  int
                =>      hb
                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
        pkcs5
                meth    enum(PadMethod, "hbytes pad subcommand")
                v       hbv
@@ -131,12 +145,6 @@ Table hbytes HBytes_SubCommand
                key     obj
                ?maclen obj
                =>      hb
                key     obj
                ?maclen obj
                =>      hb
-       zeroes
-               length  int
-               =>      hb
-       random
-               length  int
-               =>      hb
 
 Table dgram_socket DgramSocket_SubCommand
        create
 
 Table dgram_socket DgramSocket_SubCommand
        create
index 864019e50d82a9edfb297cbf335b6a5953872bce..23cad209c11840758e99052d62198efd632bca04 100644 (file)
@@ -14,6 +14,8 @@
  *                                                  (too short? error)
  *
  *  hbytes range VALUE START SIZE                => substring (or error)
  *                                                  (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
  *
  *  hbytes h2ulong HEX                           => ulong  (HEX len must be 4)
  *  hbytes ulong2h UL                            => hex
index 7f889fbdc6077632ada45be5e5a6adca703df142..f364978c200db68dde9b2ba53c0a3e8faa898533 100644 (file)
@@ -197,6 +197,38 @@ int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }  
   
   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;
 int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
                     int length, HBytes_Value *result) {
   Byte *space;