]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
DynBuf: Implement new function DynBuf_Insert.
authorJohn Wolfe <jwolfe@vmware.com>
Mon, 4 Apr 2022 19:58:41 +0000 (12:58 -0700)
committerJohn Wolfe <jwolfe@vmware.com>
Mon, 4 Apr 2022 19:58:41 +0000 (12:58 -0700)
 - DynBuf_Insert(): Insert data at a given offset within a dynamic buffer.

open-vm-tools/lib/include/dynbuf.h
open-vm-tools/lib/misc/dynbuf.c

index 6798cdc7585646bad8c853e3d4d1bc4c5ea13793..0d87d87cb8ac18f57e556c56787491de07a116c4 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 1998-2019 VMware, Inc. All rights reserved.
+ * Copyright (C) 1998-2022 VMware, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -75,6 +75,12 @@ DynBuf_Append(DynBuf *b,        // IN/OUT
               void const *data, // IN
               size_t size);     // IN
 
+Bool
+DynBuf_Insert(DynBuf *b,        // IN/OUT
+              size_t offset,    // IN
+              void const *data, // IN
+              size_t size);     // IN
+
 Bool
 DynBuf_Trim(DynBuf *b); // IN/OUT
 
index f5b093f31d5d76526d984a62860b53b1e1bf7861..ef7663e1a056aa218c99d2af6d82bb4c512ecfe6 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 1998-2019 VMware, Inc. All rights reserved.
+ * Copyright (C) 1998-2022 VMware, Inc. All rights reserved.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -376,10 +376,10 @@ DynBuf_SafeInternalEnlarge(DynBuf *b,           // IN/OUT:
 /*
  *-----------------------------------------------------------------------------
  *
- * DynBuf_Append --
+ * DynBuf_Insert --
  *
- *      Append data at the end of a dynamic buffer. 'size' is the size of the
- *      data. If it is <= 0, no operation is performed --hpreg
+ *      Insert data at a given offset within a dynamic buffer. 'size' is the
+ *      size of the data. If it is <= 0, no operation is performed.
  *
  * Results:
  *      TRUE on success
@@ -392,13 +392,15 @@ DynBuf_SafeInternalEnlarge(DynBuf *b,           // IN/OUT:
  */
 
 Bool
-DynBuf_Append(DynBuf *b,        // IN/OUT:
+DynBuf_Insert(DynBuf *b,        // IN/OUT:
+              size_t offset,    // IN:
               void const *data, // IN:
               size_t size)      // IN:
 {
    size_t new_size;
 
    ASSERT(b);
+   ASSERT(offset <= b->size);
 
    if (size <= 0) {
       return TRUE;
@@ -419,13 +421,41 @@ DynBuf_Append(DynBuf *b,        // IN/OUT:
       }
    }
 
-   memcpy(b->data + b->size, data, size);
+   memmove(b->data + offset + size, b->data + offset, b->size - offset);
+   memcpy(b->data + offset, data, size);
    b->size = new_size;
 
    return TRUE;
 }
 
 
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * DynBuf_Append --
+ *
+ *      Append data at the end of a dynamic buffer. 'size' is the size of the
+ *      data. If it is <= 0, no operation is performed.
+ *
+ * Results:
+ *      TRUE on success
+ *      FALSE on failure (not enough memory)
+ *
+ * Side effects:
+ *      None
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+Bool
+DynBuf_Append(DynBuf *b,        // IN/OUT:
+              void const *data, // IN:
+              size_t size)      // IN:
+{
+   return DynBuf_Insert(b, b->size, data, size);
+}
+
+
 /*
  *-----------------------------------------------------------------------------
  *