diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2005-09-07 16:04:24 +0000 |
---|---|---|
committer | Rainer Gerhards <rgerhards@adiscon.com> | 2005-09-07 16:04:24 +0000 |
commit | 602fb17d3307d3dbe619ec559688c5b0c2ebc47c (patch) | |
tree | d365a4c0925565262839106c5fbb25dafefb14d7 | |
parent | 8febc25442bcd8fb5ce65c072404ca3d62602cef (diff) | |
download | rsyslog-602fb17d3307d3dbe619ec559688c5b0c2ebc47c.tar.gz rsyslog-602fb17d3307d3dbe619ec559688c5b0c2ebc47c.tar.xz rsyslog-602fb17d3307d3dbe619ec559688c5b0c2ebc47c.zip |
begin adding counted string class - does NOT compile yet!
-rw-r--r-- | NEWS | 4 | ||||
-rwxr-xr-x | liblogging-stub.h | 7 | ||||
-rwxr-xr-x | stringbuf.c | 150 | ||||
-rwxr-xr-x | stringbuf.h | 77 | ||||
-rw-r--r-- | syslogd.c | 34 | ||||
-rw-r--r-- | version.h | 2 |
6 files changed, 138 insertions, 136 deletions
@@ -1,4 +1,8 @@ --------------------------------------------------------------------------- +Version 1.10.0 (RGer), 2005-09-?? + REMINDER: 1.10 is the first unstable version if the 1.x series! +- changed stringbuf into a new counted string class +--------------------------------------------------------------------------- Version 1.0.0 (RGer), 2005-09-12 - changed install doc to cover daily cron scripts - a trouble source - added rc script for slackware (provided by Chris Elvidge - thanks!) diff --git a/liblogging-stub.h b/liblogging-stub.h index f618a967..3a9ada3f 100755 --- a/liblogging-stub.h +++ b/liblogging-stub.h @@ -195,7 +195,12 @@ enum srObjectID OIDsbStrB = 0xCDAB000B,
OIDsbLstn = 0xCDAB000C,
OIDsbPSSR = 0xCDAB000D,
- OIDsbPSRC = 0xCDAB000E
+ OIDsbPSRC = 0xCDAB000E,
+
+ /* stringbuf extensions (must be kept after merge with liblogging...)
+ * rgerhards 2005-9-07
+ */
+ OIDrsCStr = 0xABCD0001
};
typedef enum srObjectID srObjID;
diff --git a/stringbuf.c b/stringbuf.c index b91c05a4..11a1f5e4 100755 --- a/stringbuf.c +++ b/stringbuf.c @@ -1,41 +1,12 @@ -/*! \file stringbuf.c - * \brief Implemetation of the dynamic string buffer helper object. - * - * \author Rainer Gerhards <rgerhards@adiscon.com> - * \date 2003-08-08 - * Initial version begun. - * - * Copyright 2002-2003 - * Rainer Gerhards and Adiscon GmbH. All Rights Reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * * Neither the name of Adiscon GmbH or Rainer Gerhards - * nor the names of its contributors may be used to - * endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +/* This is the byte-counted string class for rsyslog. It is a replacement + * for classical \0 terminated string functions. We introduce it in + * the hope it will make the program more secure, obtain some performance + * and, most importantly, lay they foundation for syslog-protocol, which + * requires strings to be able to handle embedded \0 characters. + * Please see syslogd.c for license information. + * All functions in this "class" start with rsCStr (rsyslog Counted String). + * This code is placed under the GPL. + * begun 2005-09-07 rgerhards */ #include <stdlib.h> #include <assert.h> @@ -55,24 +26,26 @@ * ################################################################# */ -sbStrBObj *sbStrBConstruct(void) +rsCStrObj *rsCStrConstruct(void) { - sbStrBObj *pThis; + rsCStrObj *pThis; - if((pThis = (sbStrBObj*) calloc(1, sizeof(sbStrBObj))) == NULL) + if((pThis = (rsCStrObj*) calloc(1, sizeof(rsCStrObj))) == NULL) return NULL; - pThis->OID = OIDsbStrB; + pThis->OID = OIDrsCStr; pThis->pBuf = NULL; + pThis->pszBuf = NULL; pThis->iBufSize = 0; pThis->iBufPtr = 0; + pThis->iStrLen = 0; pThis->iAllocIncrement = STRINGBUF_ALLOC_INCREMENT; return pThis; } -void sbStrBDestruct(sbStrBObj *pThis) +void rsCStrDestruct(rsCStrObj *pThis) { # if STRINGBUF_TRIM_ALLOCSIZE == 1 /* in this mode, a new buffer already was allocated, @@ -83,11 +56,15 @@ void sbStrBDestruct(sbStrBObj *pThis) } # endif + if(pThis->pszBuf != NULL) { + free(pThis->pszBuf); + } + SRFREEOBJ(pThis); } -srRetVal sbStrBAppendStr(sbStrBObj *pThis, char* psz) +srRetVal rsCStrAppendStr(rsCStrObj *pThis, char* psz) { srRetVal iRet; @@ -95,14 +72,14 @@ srRetVal sbStrBAppendStr(sbStrBObj *pThis, char* psz) assert(psz != NULL); while(*psz) - if((iRet = sbStrBAppendChar(pThis, *psz++)) != SR_RET_OK) + if((iRet = rsCStrAppendChar(pThis, *psz++)) != SR_RET_OK) return iRet; return SR_RET_OK; } -srRetVal sbStrBAppendInt(sbStrBObj *pThis, int i) +srRetVal rsCStrAppendInt(rsCStrObj *pThis, int i) { srRetVal iRet; char szBuf[32]; @@ -112,11 +89,11 @@ srRetVal sbStrBAppendInt(sbStrBObj *pThis, int i) if((iRet = srUtilItoA(szBuf, sizeof(szBuf), i)) != SR_RET_OK) return iRet; - return sbStrBAppendStr(pThis, szBuf); + return rsCStrAppendStr(pThis, szBuf); } -srRetVal sbStrBAppendChar(sbStrBObj *pThis, char c) +srRetVal rsCStrAppendChar(rsCStrObj *pThis, char c) { char* pNewBuf; @@ -136,18 +113,63 @@ srRetVal sbStrBAppendChar(sbStrBObj *pThis, char c) /* ok, when we reach this, we have sufficient memory */ *(pThis->pBuf + pThis->iBufPtr++) = c; + pThis->iStrLen++; return SR_RET_OK; } -char* sbStrBFinish(sbStrBObj *pThis) +/* Converts the CStr object to a classical zero-terminated C string, + * returns that string and destroys the CStr object. The returned string + * MUST be freed by the caller. The function might return NULL if + * no memory can be allocated. + * + * TODO: + * This function should at some time become special. The base idea is to + * add one extra byte to the end of the regular buffer, so that we can + * convert it to an szString without the need to copy. The extra memory + * footprint is not hefty, but the performance gain is potentially large. + * To get it done now, I am not doing the optimiziation right now. + * + * rgerhards, 2005-09-07 + */ +char* rsCStrConvSzStrAndDestruct(rsCStrObj *pThis) { char* pRetBuf; sbSTRBCHECKVALIDOBJECT(pThis); - sbStrBAppendChar(pThis, '\0'); + if(pThis->pszBuf == NULL) { + /* we do not yet have a usable sz version - so create it... */ + if((pThis->pszBuf = malloc(pThis->iStrLen + 1 * sizeof(char))) == NULL) { + /* TODO: think about what to do - so far, I have no bright + * idea... rgerhards 2005-09-07 + */ + } + else { + /* we can create the sz String */ + if(pThis->pBuf != NULL) + memcpy(pThis->pszBuf, pThis->pBuf, pThis->iStrLen); + *(pThis->pszBuf + pThis->iStrLen) = '\0'; + } + } + + /* We got it, now free the object ourselfs. Please note + * that we can NOT use the rsCStrDestruct function as it would + * also free the sz String buffer, which we pass on to the user. + */ + pRetBuf = pThis->pszBuf; + if(pThis->pBuf != NULL) + free(pThis->pBuf); + SRFREEOBJ(pThis); + + return(pRetBuf); +} + + +void rsCStrFinish(rsCStrObj *pThis) +{ + sbSTRBCHECKVALIDOBJECT(pThis); # if STRINGBUF_TRIM_ALLOCSIZE == 1 /* in this mode, we need to trim the string. To do @@ -155,34 +177,36 @@ char* sbStrBFinish(sbStrBObj *pThis) * string size, and then copy the old one over. * This new buffer is then to be returned. */ - if((pRetBuf = malloc((pThis->iBufSize + 1) * sizeof(char))) == NULL) + if((pRetBuf = malloc((pThis->iBufSize) * sizeof(char))) == NULL) { /* OK, in this case we use the previous buffer. At least * we have it ;) */ - pRetBuf = pThis->pBuf; } else { /* got the new buffer, so let's use it */ - memcpy(pRetBuf, pThis->pBuf, pThis->iBufPtr + 1); + char* pBuf; + memcpy(pBuf, pThis->pBuf, pThis->iBufPtr + 1); + pThis->pBuf = pBuf; } # else - /* here, we can simply return a pointer to the - * currently existing buffer. We don't care about - * the extra memory, as we achieve a big performance - * gain. + /* here, we need to do ... nothing ;) */ - pRetBuf = pThis->pBuf; # endif - - sbStrBDestruct(pThis); - - return(pRetBuf); } -void sbStrBSetAllocIncrement(sbStrBObj *pThis, int iNewIncrement) +void rsCStrSetAllocIncrement(rsCStrObj *pThis, int iNewIncrement) { sbSTRBCHECKVALIDOBJECT(pThis); assert(iNewIncrement > 0); pThis->iAllocIncrement = iNewIncrement; } + +/* + * Local variables: + * c-indent-level: 8 + * c-basic-offset: 8 + * tab-width: 8 + * End: + * vi:set ai: + */ diff --git a/stringbuf.h b/stringbuf.h index f456214b..c53ff2d5 100755 --- a/stringbuf.h +++ b/stringbuf.h @@ -1,84 +1,53 @@ /*! \file stringbuf.h
- * \brief The dynamic stringt buffer helper object.
+ * \brief The counted string object
*
- * The string buffer object is a slim string handler. It implements
- * a dynamically growing string and can be used whereever data needs
- * to be appended to a string AND it is not known how large the
- * resulting structure is. If you know the string size or can
- * retrieve it quickly, you should NOT use the string buffer
- * object - because it has some overhead not associated with direct
- * string manipulations.
- *
- * This object is used to grow a string. For performance reasons,
- * the termination \0 byte is only written after the caller
- * indicates the string is completed.
+ * This is the byte-counted string class for rsyslog. It is a replacement
+ * for classical \0 terminated string functions. We introduce it in
+ * the hope it will make the program more secure, obtain some performance
+ * and, most importantly, lay they foundation for syslog-protocol, which
+ * requires strings to be able to handle embedded \0 characters.
*
* \author Rainer Gerhards <rgerhards@adiscon.com>
- * \date 2003-08-08
+ * \date 2005-09-07
* Initial version begun.
*
- * Copyright 2002-2003
+ * All functions in this "class" start with rsCStr (rsyslog Counted String).
+ * Copyright 2005
* Rainer Gerhards and Adiscon GmbH. All Rights Reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * * Neither the name of Adiscon GmbH or Rainer Gerhards
- * nor the names of its contributors may be used to
- * endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
- * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
- * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * This code is placed under the GPL.
*/
#ifndef __LIB3195_STRINGBUF_H_INCLUDED__
#define __LIB3195_STRINGBUF_H_INCLUDED__ 1
-#define sbSTRBCHECKVALIDOBJECT(x) {assert(x != NULL); assert(x->OID == OIDsbStrB);}
+#define sbSTRBCHECKVALIDOBJECT(x) {assert(x != NULL); assert(x->OID == OIDrsCStr);}
/**
* The dynamic string buffer object.
*
*/
-struct sbStrBObject
+struct rsCStrObject
{
srObjID OID; /**< object ID */
char *pBuf; /**< pointer to the string buffer, may be NULL if string is empty */
+ char *pszBuf; /**< pointer to the sz version of the string (after it has been created )*/
int iBufSize; /**< current maximum size of the string buffer */
int iBufPtr; /**< pointer (index) of next character position to be written to. */
+ int iStrLen; /**< length of the string in characters. */
int iAllocIncrement; /**< the amount of bytes the string should be expanded if it needs to */
};
-typedef struct sbStrBObject sbStrBObj;
+typedef struct rsCStrObject rsCStrObj;
/**
- * Construct a sbStrB object.
+ * Construct a rsCStr object.
*/
-sbStrBObj *sbStrBConstruct(void);
+rsCStrObj *rsCStrConstruct(void);
/**
* Destruct the string buffer object.
*/
-void sbStrBDestruct(sbStrBObj *pThis);
+void rsCStrDestruct(rsCStrObj *pThis);
/**
* Append a character to an existing string. If necessary, the
@@ -86,7 +55,7 @@ void sbStrBDestruct(sbStrBObj *pThis); *
* \param c Character to append to string.
*/
-srRetVal sbStrBAppendChar(sbStrBObj *pThis, char c);
+srRetVal rsCStrAppendChar(rsCStrObj *pThis, char c);
/**
* Finish the string buffer. That means, the string
@@ -101,14 +70,14 @@ srRetVal sbStrBAppendChar(sbStrBObj *pThis, char c); * \retval pointer to \0 terminated string. May be NULL
* (empty string) and MUST be free()ed by caller.
*/
-char* sbStrBFinish(sbStrBObj *pThis);
+void rsCStrFinish(rsCStrObj *pThis);
/**
* Append a string to the buffer.
*
* \param psz pointer to string to be appended. Must not be NULL.
*/
-srRetVal sbStrBAppendStr(sbStrBObj *pThis, char* psz);
+srRetVal rsCStrAppendStr(rsCStrObj *pThis, char* psz);
/**
* Set a new allocation incremet. This will influence
@@ -124,13 +93,13 @@ srRetVal sbStrBAppendStr(sbStrBObj *pThis, char* psz); * advise not to use an increment below 32 bytes, except
* if you are very well aware why you are doing it ;)
*/
-void sbStrBSetAllocIncrement(sbStrBObj *pThis, int iNewIncrement);
+void rsCStrSetAllocIncrement(rsCStrObj *pThis, int iNewIncrement);
/**
* Append an integer to the string. No special formatting is
* done.
*/
-srRetVal sbStrBAppendInt(sbStrBObj *pThis, int i);
+srRetVal rsCStrAppendInt(rsCStrObj *pThis, int i);
#endif
@@ -3166,7 +3166,7 @@ void logmsg(pri, pMsg, flags) char *p2parse; char *pBuf; char *pWork; - sbStrBObj *pStrB; + rsCStrObj *pStrB; int iCnt; int bContParse = 1; @@ -3252,20 +3252,20 @@ void logmsg(pri, pMsg, flags) * is the max size ;) we need to shuffle the code again... Just for * the records: the code is currently clean, but we could optimize it! */ if(bContParse) { - if((pStrB = sbStrBConstruct()) == NULL) + if((pStrB = rsCStrConstruct()) == NULL) return; - sbStrBSetAllocIncrement(pStrB, 33); + rsCStrSetAllocIncrement(pStrB, 33); pWork = pBuf; iCnt = 0; while(*p2parse && *p2parse != ':' && *p2parse != ' ' && iCnt < 32) { - sbStrBAppendChar(pStrB, *p2parse++); + rsCStrAppendChar(pStrB, *p2parse++); ++iCnt; } if(*p2parse == ':') { ++p2parse; - sbStrBAppendChar(pStrB, ':'); + rsCStrAppendChar(pStrB, ':'); } - MsgAssignTAG(pMsg, sbStrBFinish(pStrB)); + MsgAssignTAG(pMsg, rsCStrFinish(pStrB)); } else { /* we have no TAG, so we ... */ /*DO NOTHING*/; @@ -3407,7 +3407,7 @@ void doSQLEscape(char **pp, size_t *pLen, unsigned short *pbMustBeFreed) { char *p; int iLen; - sbStrBObj *pStrB; + rsCStrObj *pStrB; char *pszGenerated; assert(pp != NULL); @@ -3425,7 +3425,7 @@ void doSQLEscape(char **pp, size_t *pLen, unsigned short *pbMustBeFreed) p = *pp; iLen = *pLen; - if((pStrB = sbStrBConstruct()) == NULL) { + if((pStrB = rsCStrConstruct()) == NULL) { /* oops - no mem ... Do emergency... */ doSQLEmergencyEscape(p); return; @@ -3433,25 +3433,25 @@ void doSQLEscape(char **pp, size_t *pLen, unsigned short *pbMustBeFreed) while(*p) { if(*p == '\'') { - if(sbStrBAppendChar(pStrB, '\'') != SR_RET_OK) { + if(rsCStrAppendChar(pStrB, '\'') != SR_RET_OK) { doSQLEmergencyEscape(*pp); - if((pszGenerated = sbStrBFinish(pStrB)) + if((pszGenerated = rsCStrFinish(pStrB)) != NULL) free(pszGenerated); return; iLen++; /* reflect the extra character */ } } - if(sbStrBAppendChar(pStrB, *p) != SR_RET_OK) { + if(rsCStrAppendChar(pStrB, *p) != SR_RET_OK) { doSQLEmergencyEscape(*pp); - if((pszGenerated = sbStrBFinish(pStrB)) + if((pszGenerated = rsCStrFinish(pStrB)) != NULL) free(pszGenerated); return; } ++p; } - if((pszGenerated = sbStrBFinish(pStrB)) == NULL) { + if((pszGenerated = rsCStrFinish(pStrB)) == NULL) { doSQLEmergencyEscape(*pp); return; } @@ -3477,7 +3477,7 @@ char *iovAsString(struct filed *f) { struct iovec *v; int i; - sbStrBObj *pStrB; + rsCStrObj *pStrB; assert(f != NULL); @@ -3489,7 +3489,7 @@ char *iovAsString(struct filed *f) free(f->f_psziov); } - if((pStrB = sbStrBConstruct()) == NULL) { + if((pStrB = rsCStrConstruct()) == NULL) { /* oops - no mem, let's try to set the message we have * most probably, this will fail, too. But at least we * can try... */ @@ -3501,13 +3501,13 @@ char *iovAsString(struct filed *f) v = f->f_iov; while(i++ < f->f_iIovUsed) { if(v->iov_len > 0) { - sbStrBAppendStr(pStrB, v->iov_base); + rsCStrAppendStr(pStrB, v->iov_base); f->f_iLenpsziov += v->iov_len; } ++v; } - f->f_psziov = sbStrBFinish(pStrB); + f->f_psziov = rsCStrFinish(pStrB); return f->f_psziov; } @@ -1,2 +1,2 @@ -#define VERSION "1.0" +#define VERSION "1.10" #define PATCHLEVEL "0" |