📄 buffer.c
字号:
/* * Copyright 2000-2005 Wind River Systems, Inc. * All rights reserved. Provided under license only. * Distribution or other use of this software is only * permitted pursuant to the terms of a license agreement * from Wind River Systems (and is otherwise prohibited). * Refer to that license agreement for terms of use. *//* * Copyright 1988-1997 Epilogue Technology Corporation. * Copyright 1998 Integrated Systems, Inc. * All rights reserved. *//* * $Log: buffer.c,v $ * Revision 1.2 2001/11/06 21:20:06 josh * revised new path hacking * * Revision 1.1.1.1 2001/11/05 17:47:42 tneale * Tornado shuffle * * Revision 9.2 2001/01/19 22:22:19 paul * Update copyright. * * Revision 9.1 2000/03/17 00:19:00 meister * Update copyright message * * Revision 9.0 1998/10/16 22:11:08 sar * Update version stamp to match release * * Revision 8.7 1998/06/22 03:34:26 sar * Cast some things to unsigned ints to try and keep compilers happy * * Revision 8.6 1998/06/19 20:13:49 sar * make sure all files include asn1conf.h and snmp.h to pick up all of * the common code * * Revision 8.5 1998/06/18 04:32:32 sar * Update the type info for bflags in an ebuffer and make * everybody match it. * * Revision 8.4 1998/06/16 05:18:32 sar * tidy up an unreachable return * * Revision 8.3 1998/06/05 18:53:10 sra * "#include <foo.h>" => "#include <envoy/h/foo.h>". * * Revision 8.2 1998/05/24 04:40:33 sar * Added EBufferAllocateLoad also updated types * * Revision 8.1 1998/02/25 04:51:29 sra * Update copyrights. * * Revision 8.0 1997/11/18 00:56:44 sar * Updated revision to 8.0 * * Revision 7.4 1997/10/16 00:39:40 sar * Cleaned up some possible problems in the seek routine, mostly * dealing with attempts to go beyond the buffer * * Revision 7.3 1997/03/20 06:48:44 sra * DFARS-safe copyright text. Zap! * * Revision 7.2 1997/02/25 10:49:26 sra * Update copyright notice, dust under the bed. * * Revision 7.1 1996/10/07 23:55:58 sar * Added some parans in ebufferreplace to make sure the compare * is for the right objects. * * Revision 7.0 1996/03/18 20:01:11 sar * Updated revision to 7.0 and copyright to 96 * * Revision 6.1 1995/10/20 22:59:09 sar * removed no_pp stuff & casts of 0 * modified memcpy to MEMCPY * * Revision 6.0 1995/05/31 21:47:15 sra * Release 6.0. * * Revision 5.1 1995/05/08 20:32:04 sar * Did some casting between pointer arithmetic, ints and ALENGTH_Ts to * keep compilers happy. * * Revision 5.0 1994/05/16 15:42:42 sar * Updated revision to 5.0 and copyright to include 1994 * * Revision 4.1 1993/09/30 20:37:35 sar * Modified BufferAdjust to not allow extension of a static buffer that * would require freeing the current space. * * Revision 4.0 1993/06/24 15:45:46 sar * Updated revision to 4.0 and copyright to 93 * * Revision 3.6 1993/04/29 15:27:42 sar * Removed a check that could never fail if (ALENGTH_T < 0) * * Revision 3.5 1993/03/20 00:25:43 sar * Updated some types for version two party type stuff (indicies, local * storage, status, mtu and privileges). * Also removed some unused break statements. * * Revision 3.4 1993/03/06 22:54:50 sar * Added test in EbufferAppend and EbufferAdd to avoid doing a memcpy of * zero length. * * Revision 3.3 1993/03/05 03:58:02 dab * fixed up types * * Revision 3.2 1993/03/02 19:06:46 sar * added Ebuffer_add which extends the string in a buffer by allocating a * new one copying the old one and then deleteing it. * * Revision 3.1 1993/02/17 21:01:07 sar * Removed #define then and use of it. * Added EBufferSeek which is like seek in unix, it moves the pointers to * new position in the buffer. * * Revision 3.0 92/04/03 19:52:37 dab * Release 3.0 * * Revision 2.103 91/09/18 12:32:03 dab * Updated to use new macros from <asn1conf.h> and <snmpconf.h>. * * Revision 2.102 91/08/15 12:30:47 dab * Removed <libfuncs.h>. * * Revision 2.101 91/08/12 12:47:35 dab * Cast arguments to memcpy() and memset() to PTR_T. * * Revision 2.100 91/08/09 14:08:17 dab * Update version. * * Revision 1.1 91/07/30 02:23:28 romkey * Initial revision * * * Rev 2.0 31 Mar 1990 15:06:40 * Release 2.00 * * Rev 1.4 27 Apr 1989 15:56:30 * Removed unused variables * * Rev 1.3 17 Mar 1989 23:22:00 * An attempt to clone a zero length buffer now results in a static buffer * with a length of zero at address zero. * * Rev 1.2 17 Mar 1989 21:41:42 * Calls to memcpy/memset protected against zero lengths * * Rev 1.1 14 Sep 1988 17:57:18 * Moved includes of system include files into libfuncs.h. * * Rev 1.0 12 Sep 1988 10:46:52 * Initial revision.*//* [clearcase]modification history-------------------01b,18apr05,job update copyright notices01a,24nov03,job update copyright information*/#include <wrn/wm/snmp/engine/asn1conf.h>#include <wrn/wm/snmp/engine/snmp.h>#include <wrn/wm/snmp/engine/buffer.h>/****************************************************************************NAME: EBufferClonePURPOSE: Clone an extended bufferPARAMETERS: EBUFFER_T * Source buffer structure EBUFFER_T * Destination buffer structureRETURNS: 0 if sucessful, -1 if not****************************************************************************/int EBufferClone(EBUFFER_T * srcp, EBUFFER_T * dstp){ALENGTH_T need;need = EBufferUsed(srcp);if (need != 0) { bits8_t *newbuffp; if ((newbuffp = (bits8_t *)SNMP_memory_alloc(need)) == 0) { /* Allocation failed */ return -1; } (void) MEMCPY(newbuffp, srcp->start_bp, need); EBufferPreLoad(BFL_IS_DYNAMIC, dstp, newbuffp, need); }else { /* Length is zero */ EBufferPreLoad(BFL_IS_STATIC, dstp, 0, 0); }return 0;}/****************************************************************************NAME: EBufferCleanPURPOSE: Release the buffer memory if possiblePARAMETERS: EBUFFER_T * Buffer descriptorRETURNS: NothingRESTRICTIONS: BUGS: ****************************************************************************/void EBufferClean(EBUFFER_T * ebuffp){if ((ebuffp->bflags & BFL_IS_DYNAMIC) && (ebuffp->start_bp != 0)) SNMP_memory_free((char *)(ebuffp->start_bp));EBufferInitialize(ebuffp);}/****************************************************************************NAME: EBufferAppendPURPOSE: Append the contents of one buffer onto another.PARAMETERS: EBUFFER_T * First (and destination) buffer descriptor EBUFFER_T * Second buffer descriptorRETURNS: NothingRESTRICTIONS: BUGS: ****************************************************************************/void EBufferAppend(EBUFFER_T * b1p, EBUFFER_T * b2p){ALENGTH_T used;if ((used = min(EBufferUsed(b2p), b1p->remaining)) != 0) (void)MEMCPY(b1p->next_bp, b2p->start_bp, used);b1p->next_bp += used;b1p->remaining -= used;}/****************************************************************************NAME: EBufferSeekPURPOSE: Move the pointers to a new position in the buffer.PARAMETERS: EBUFFER_T * Source buffer structure ALENGTH_T Length to move the pointers. int 0 to set pointer to offset bytes from the start 1 to move pointer by offset bytes 2 to set pointer to offset bytes from the endRETURNS: 0 if sucessful, -1 if not****************************************************************************/int EBufferSeek(EBUFFER_T *ebuffp, ALENGTH_T offset, int whence){switch (whence) { case 0: if (offset > ((ALENGTH_T)(ebuffp->next_bp - ebuffp->start_bp) + ebuffp->remaining)) return(-1); ebuffp->remaining = ((ALENGTH_T)(ebuffp->next_bp - ebuffp->start_bp)) + ebuffp->remaining - offset; ebuffp->next_bp = ebuffp->start_bp + offset; break; case 1: if (offset > ebuffp->remaining) return(-1); ebuffp->next_bp += offset; ebuffp->remaining -= offset; break; case 2: if (offset > ((ALENGTH_T)(ebuffp->next_bp - ebuffp->start_bp) + ebuffp->remaining)) return(-1); ebuffp->next_bp = ebuffp->next_bp + ebuffp->remaining - offset; ebuffp->remaining = offset; break; default: return (-1); }return(0);}/****************************************************************************NAME: EBufferAdjustPURPOSE: Make the buffer at least the given length long. If necessary allocate more space. NOTE: you can not use this routine to extend a static buffer it will return a -1 failure.PARAMETERS: EBUFFER_T * Buffer descriptor ALENGTH_T Minimum length for the bufferRETURNS: int 0 on successRESTRICTIONS: BUGS: ****************************************************************************/int EBufferAdjust(EBUFFER_T * ebuffp, ALENGTH_T length){ALENGTH_T used;bits8_t *buf;used = EBufferUsed(ebuffp);if (used + ebuffp->remaining >= length) return(0);if ((ebuffp->bflags != BFL_IS_DYNAMIC) || ((buf = (bits8_t *)SNMP_memory_alloc(length)) == 0)) return(-1);if (used != 0) MEMCPY(buf, ebuffp->start_bp, used);if ((ebuffp->bflags & BFL_IS_DYNAMIC) && (ebuffp->start_bp != 0)) SNMP_memory_free((char *)(ebuffp->start_bp));ebuffp->start_bp = buf;ebuffp->next_bp = buf + used;ebuffp->remaining = length - used;return(0);}/****************************************************************************NAME: EBufferReplacePURPOSE: Replace the string currently in the ebuffer with a copy of the given buffer. If necessary allocate more space to grow the buffer.PARAMETERS: EBUFFER_T * EBuffer descriptor bits8_t * buffer pointer ALENGTH_T length for the bufferRETURNS: int 0 on successRESTRICTIONS: BUGS: ****************************************************************************/int EBufferReplace(EBUFFER_T * ebuffp, bits8_t * buffer, ALENGTH_T length){bits8_t *newbuf;if (length != 0) { if (length > (EBufferUsed(ebuffp) + ebuffp->remaining)) { if ((newbuf = (bits8_t *)SNMP_memory_alloc(length)) == 0) return(-1); if ((ebuffp->bflags & BFL_IS_DYNAMIC) && (ebuffp->start_bp != 0)) SNMP_memory_free((char *)(ebuffp->start_bp)); EBufferSetup(BFL_IS_DYNAMIC, ebuffp, newbuf, length); } MEMCPY(ebuffp->start_bp, buffer, (int)length); }EBufferSeek(ebuffp, length, 0);return(0);}/****************************************************************************NAME: EBufferAllocateLoadPURPOSE: Insert the given string into the buffer. For dynamic and static strings we clean the buffer and then insert the new string. For allocate strings we check to see if we already have enough space, in which case we copy the string. Otherwise we attempt to allocate a new buffer to replace the old buffer. If the allocation is successful we free the old buffer, copy the string and insert the new buffer into the ebuffer.PARAMETERS: unsigned short int flags, see buffer.h EBUFFER_T * EBuffer descriptor bits8_t * buffer pointer ALENGTH_T length for the bufferRETURNS: int 0 on success****************************************************************************/int EBufferAllocateLoad(unsigned int bflags, EBUFFER_T * ebuffp, bits8_t * buffer, ALENGTH_T length){bits8_t *newbuf = 0;switch (bflags) { case BFL_IS_ALLOC: if (length) { if (length > (EBufferUsed(ebuffp) + ebuffp->remaining)) { newbuf = (bits8_t *)SNMP_memory_alloc(length); if (newbuf == 0) return(-1); if ((ebuffp->bflags & BFL_IS_DYNAMIC) && (ebuffp->start_bp != 0)) SNMP_memory_free((char *)(ebuffp->start_bp)); EBufferSetup(BFL_IS_DYNAMIC, ebuffp, newbuf, length); } MEMCPY(ebuffp->start_bp, buffer, (int)length); } EBufferSeek(ebuffp, length, 0); return(0); case BFL_IS_DYNAMIC: case BFL_IS_STATIC: EBufferClean(ebuffp); EBufferPreLoad(bflags, ebuffp, buffer, length); return(0); default: break; }return(-1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -