Add a growable sprintf memory buffer library
[openocd.git] / src / helper / membuf.c
diff --git a/src/helper/membuf.c b/src/helper/membuf.c
new file mode 100644 (file)
index 0000000..af396ca
--- /dev/null
@@ -0,0 +1,238 @@
+/***************************************************************************\r
+ *   Copyright (C) 2009 By Duane Ellis                                     *\r
+ *   openocd@duaneellis.com                                                *\r
+ *                                                                         *\r
+ *   This program is free software; you can redistribute it and/or modify  *\r
+ *   it under the terms of the GNU General Public License as published by  *\r
+ *   the Free Software Foundation; either version 2 of the License, or     *\r
+ *   (at your option) any later version.                                   *\r
+ *                                                                         *\r
+ *   This program is distributed in the hope that it will be useful,       *\r
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *\r
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *\r
+ *   GNU General Public License for more details.                          *\r
+ *                                                                         *\r
+ *   You should have received a copy of the GNU General Public License     *\r
+ *   along with this program; if not, write to the                         *\r
+ *   Free Software Foundation, Inc.,                                       *\r
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *\r
+ ***************************************************************************/\r
+\r
+#include <stdio.h>\r
+#include <stdarg.h>\r
+#include <malloc.h>\r
+#include <string.h>\r
+\r
+#include "membuf.h"\r
+\r
+struct membuf {\r
+    // buflen is alway "+1" bigger then\r
+    // what is shown here, the +1 is for\r
+    // the NULL string terminator\r
+#define DEFAULT_BUFSIZE 100    \r
+    size_t maxlen; // allocated size\r
+    size_t curlen; // where we are inserting at\r
+    char *_strtoklast;\r
+    void *buf;\r
+};\r
+\r
+\r
+#define space_avail( pBuf )  (pBuf->maxlen - pBuf->curlen)\r
+#define dataend( pBuf )      ( ((char *)(pBuf->buf)) + pBuf->curlen )\r
+\r
+size_t \r
+membuf_len( struct membuf *pBuf )\r
+{\r
+    return pBuf->curlen;\r
+}\r
+\r
+const void *\r
+membuf_datapointer( struct membuf *pBuf )\r
+{\r
+    return ((void *)(pBuf->buf));\r
+}\r
+\r
+const char *\r
+membuf_strtok( struct membuf *pBuf, const char *sep, void **pLast )\r
+{\r
+    if( pBuf ){\r
+       pBuf->_strtoklast = NULL;\r
+       *pLast = pBuf;\r
+       return strtok_r( ((char *)(pBuf->buf)), sep, &(pBuf->_strtoklast) );\r
+    } else {\r
+       // recover our pBuf\r
+       pBuf = *((struct membuf **)(pLast));\r
+       return strtok_r( NULL, sep, &(pBuf->_strtoklast) );\r
+    }\r
+}\r
+       \r
+\r
+\r
+struct membuf *\r
+membuf_new(void)\r
+{\r
+    // by default - parameters are zero.\r
+    struct membuf *pBuf;\r
+\r
+    pBuf = calloc( 1, sizeof(*pBuf) );\r
+    if( pBuf ){\r
+       // we *ALWAYS* allocate +1 for null terminator.\r
+       pBuf->buf = calloc( DEFAULT_BUFSIZE+1, sizeof(char));\r
+       if( pBuf->buf == NULL ){\r
+           free(pBuf);\r
+           pBuf = NULL;\r
+       } else {\r
+           pBuf->maxlen = DEFAULT_BUFSIZE;\r
+       }\r
+    }\r
+    return pBuf;\r
+}\r
+\r
+\r
+struct membuf *\r
+membuf_grow( struct membuf *pBuf, int n )\r
+{\r
+    void *vp;\r
+    signed int newsize;\r
+\r
+    // this is a *SIGNED* value\r
+    newsize = ((int)(pBuf->maxlen)) + n;\r
+\r
+    // do not go negative, or too small\r
+    if( newsize < DEFAULT_BUFSIZE ){\r
+       newsize = DEFAULT_BUFSIZE;\r
+    }\r
+\r
+    // always alloc +1 for the null terminator\r
+    vp = realloc( pBuf->buf, newsize+1 );\r
+    if( vp ){\r
+       pBuf->buf    = vp;\r
+       pBuf->maxlen = newsize;\r
+       return pBuf;\r
+    } else {\r
+       return NULL;\r
+    }\r
+}\r
+\r
+\r
+void membuf_reset( struct membuf *pBuf )\r
+{\r
+    pBuf->curlen = 0;\r
+}\r
+\r
+\r
+void membuf_delete( struct membuf *pBuf )\r
+{\r
+    if( pBuf ){\r
+       if( pBuf->buf){\r
+           // wack data so it cannot be reused\r
+           memset(pBuf->buf,0,pBuf->maxlen);\r
+           free(pBuf->buf);\r
+       }\r
+       // wack dat so it cannot be reused\r
+       memset(pBuf,0,sizeof(pBuf));\r
+       free(pBuf);\r
+    }\r
+}\r
+\r
+int\r
+membuf_sprintf( struct membuf *pBuf , const char *fmt, ... )\r
+{\r
+    int r;\r
+    va_list ap;\r
+    va_start( ap, fmt );\r
+    r = membuf_vsprintf( pBuf, fmt, ap );\r
+    va_end(ap);\r
+    return r;\r
+}\r
+\r
+int\r
+membuf_vsprintf( struct membuf *pBuf, const char *fmt, va_list ap )\r
+{\r
+    int r;\r
+    size_t sa;\r
+    int grew;\r
+\r
+\r
+    grew = 0;\r
+    for(;;) {\r
+       sa = space_avail(pBuf);\r
+\r
+       // do work\r
+       r = vsnprintf( dataend( pBuf ),\r
+                      sa,\r
+                      fmt, \r
+                      ap );\r
+       if( (r > 0) && (((size_t)(r)) < sa) ){\r
+           // Success!\r
+           pBuf->curlen += ((size_t)(r));\r
+           // remember: We always alloc'ed +1\r
+           // so this does not overflow\r
+           ((char *)(pBuf->buf))[ pBuf->curlen ] = 0;\r
+           r = 0;\r
+           break;\r
+       }\r
+\r
+       // failure\r
+       if( r < 0 ){\r
+           // Option(A) format error\r
+           // Option(B) glibc2.0 bug\r
+           // assume (B).\r
+           r = (4 * DEFAULT_BUFSIZE);\r
+       }\r
+\r
+       // don't do this again\r
+       if( grew ){\r
+           r = -1;\r
+           break;\r
+       }\r
+       grew = 1;\r
+       pBuf = membuf_grow( pBuf, r );\r
+       if(pBuf == NULL){\r
+           // grow failed\r
+           r = -1;\r
+           break;\r
+       }\r
+    }\r
+    return r;\r
+}\r
+\r
+struct membuf *\r
+membuf_strcat( struct membuf *pBuf, const char *pStr )\r
+{\r
+    return membuf_append( pBuf, pStr, strlen( pStr ) );\r
+}\r
+\r
+struct membuf *\r
+membuf_append( struct membuf *pBuf, const void *pData, size_t len )\r
+{\r
+    size_t sa;\r
+    int r;\r
+\r
+    // how much room is there?\r
+    sa = space_avail( pBuf );\r
+\r
+    // will it fit?\r
+    if( sa < len ){\r
+       // if not, how much do we need?\r
+       r = ((int)(sa - len));\r
+       // do the grow.\r
+       pBuf = membuf_grow( pBuf, r );\r
+       // failed?\r
+       if(pBuf==NULL){\r
+           return pBuf;\r
+       }\r
+    }\r
+    // append\r
+    memcpy( dataend(pBuf),\r
+           pData,\r
+           len );\r
+    pBuf->curlen += len;\r
+    return pBuf;\r
+}\r
+\r
+\r
+\r
+\r
+\r
+\r

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)