From: John Wolfe Date: Mon, 4 Apr 2022 19:58:41 +0000 (-0700) Subject: DynBuf: Implement new function DynBuf_Insert. X-Git-Tag: stable-12.1.0~105 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d59878b05d57393770f1cf064141a17c9a8af3f4;p=thirdparty%2Fopen-vm-tools.git DynBuf: Implement new function DynBuf_Insert. - DynBuf_Insert(): Insert data at a given offset within a dynamic buffer. --- diff --git a/open-vm-tools/lib/include/dynbuf.h b/open-vm-tools/lib/include/dynbuf.h index 6798cdc75..0d87d87cb 100644 --- a/open-vm-tools/lib/include/dynbuf.h +++ b/open-vm-tools/lib/include/dynbuf.h @@ -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 diff --git a/open-vm-tools/lib/misc/dynbuf.c b/open-vm-tools/lib/misc/dynbuf.c index f5b093f31..ef7663e1a 100644 --- a/open-vm-tools/lib/misc/dynbuf.c +++ b/open-vm-tools/lib/misc/dynbuf.c @@ -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); +} + + /* *----------------------------------------------------------------------------- *