Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

bitop.h

Go to the documentation of this file.
00001 /*
00002 ** $Id$
00003 ** 
00004 ** bitopt.c
00005 **
00006 ** Copyright (C) 2002 Sourcefire,Inc
00007 ** Dan Roelker <droelker@sourcefire.com>
00008 ** Marc Norton <mnorton@sourcefire.com>
00009 **
00010 ** This program is free software; you can redistribute it and/or modify
00011 ** it under the terms of the GNU General Public License as published by
00012 ** the Free Software Foundation; either version 2 of the License, or
00013 ** (at your option) any later version.
00014 **
00015 ** This program is distributed in the hope that it will be useful,
00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 ** GNU General Public License for more details.
00019 **
00020 ** You should have received a copy of the GNU General Public License
00021 ** along with this program; if not, write to the Free Software
00022 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023 **
00024 ** NOTES
00025 **   5.15.02 - Initial Source Code. Norton/Roelker
00026 **   5.23.02 - Moved bitop functions to bitop.h to inline. Norton/Roelker
00027 **   1.21.04 - Added static initialization. Roelker
00028 ** 
00029 */
00030 
00031 #ifndef _BITOP_H
00032 #define _BITOP_H
00033 
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include <sys/types.h>
00037 
00038 #ifdef HAVE_CONFIG_H
00039 #include "config.h"
00040 #endif
00041 
00042 #ifdef WIN32
00043 #ifndef INLINE
00044 #define INLINE __inline
00045 #endif
00046 
00047 #else
00048 
00049 #ifndef INLINE
00050 #define INLINE inline
00051 #endif
00052 #endif
00053 
00054 typedef struct _BITOP {
00055     unsigned char *pucBitBuffer;
00056     unsigned int  uiBitBufferSize;
00057     unsigned int  uiMaxBits;
00058 } BITOP;
00059 
00060 /*
00061 **  NAME
00062 **    boInitStaticBITOP::
00063 */
00064 /**
00065 **  This function is for use if you handle the bitop buffer allocation
00066 **  yourself.  Just pass in the char array and the number of bytes the array
00067 **  is and this function sets up the structure for you.
00068 **
00069 **  @retval int
00070 **
00071 **  @return  0  successful
00072 **  @return !0  failed
00073 */
00074 static INLINE int boInitStaticBITOP(BITOP *BitOp,int iBytes,unsigned char *buf)
00075 {
00076     if(iBytes < 1 || !buf || !BitOp)
00077         return 1;
00078 
00079     BitOp->pucBitBuffer   = buf;
00080     BitOp->uiBitBufferSize = (unsigned int)iBytes;
00081     BitOp->uiMaxBits       = (unsigned int)(iBytes << 3);
00082 
00083     memset(buf, 0x00, iBytes);
00084 
00085     return 0;
00086 }
00087 
00088 /*
00089 **
00090 **  NAME
00091 **    boInitBITOP
00092 **
00093 **  DESCRIPTION
00094 **    Initializes the BITOP structure for use.
00095 **
00096 **    NOTE:
00097 **    BITOP structure must be zeroed to avoid misinterpretation
00098 **    of initialization.
00099 **
00100 **  FORMAL INPUTS
00101 **    BITOP * - the structure to initialize
00102 **    int     - the number of bit positions to hold.
00103 **
00104 **  FORMAL OUTPUTS
00105 **    int - 0 if successful, 1 if failed.
00106 **
00107 */
00108 static INLINE int boInitBITOP(BITOP *BitOp, int iSize)
00109 {
00110     int iBytes;
00111 
00112     /*
00113     **  Sanity check for size
00114     */
00115     if(iSize < 1)
00116     {
00117         return 1;
00118     }
00119 
00120     /*
00121     **  Check for already initialized buffer, and
00122     **  if it is already initialized then we return that it
00123     **  is initialized.
00124     */
00125     if(BitOp->pucBitBuffer)
00126     {
00127         return 0;
00128     }
00129 
00130     iBytes = iSize >> 3;
00131     if(iSize & 7) 
00132     {
00133         iBytes++;
00134     }
00135 
00136     BitOp->pucBitBuffer = (unsigned char  *)calloc(1, iBytes);
00137     if(BitOp->pucBitBuffer == NULL)
00138     {
00139         return 1;
00140     }
00141 
00142     BitOp->uiBitBufferSize = (unsigned int)iBytes;
00143     BitOp->uiMaxBits       = (unsigned int)iSize;
00144 
00145     return 0;
00146 }
00147 
00148 /*
00149 **
00150 **  NAME
00151 **    boResetBITOP
00152 **
00153 **  DESCRIPTION
00154 **    This resets the bit buffer so that it can be used again.
00155 **
00156 **  FORMAL INPUTS
00157 **    BITOP * - structure to reset
00158 **
00159 **  FORMAL OUTPUT
00160 **    int - 0 if successful, 1 if failed.
00161 **
00162 */
00163 static INLINE int boResetBITOP(BITOP *BitOp)
00164 {
00165     memset(BitOp->pucBitBuffer, 0x00, BitOp->uiBitBufferSize);
00166     return 0;
00167 }
00168 
00169 /*
00170 **
00171 **  NAME
00172 **    boSetBit
00173 **
00174 **  DESCRIPTION
00175 **    Set the bit in the specified position within the bit buffer.
00176 **
00177 **  FORMAL INPUTS
00178 **    BITOP * - the structure with the bit buffer
00179 **    int     - the position to set within the bit buffer
00180 **
00181 **  FORMAL OUTPUTS
00182 **    int - 0 if the bit was set, 1 if there was an error.
00183 **
00184 */
00185 static INLINE int boSetBit(BITOP *BitOp, unsigned int uiPos)
00186 {
00187     unsigned char  mask;
00188 
00189     /*
00190     **  Sanity Check while setting bits
00191     */
00192     if(BitOp->uiMaxBits <= uiPos)
00193     {
00194         return 1;
00195     }
00196 
00197     mask = 0x80 >> (uiPos & 7);
00198 
00199     BitOp->pucBitBuffer[uiPos >> 3] |= mask;
00200 
00201     return 0;
00202 }
00203 /*
00204 **
00205 **  NAME
00206 **    boIsBitSet
00207 **
00208 **  DESCRIPTION
00209 **    Checks for the bit set in iPos of bit buffer.
00210 **
00211 **  FORMAL INPUTS
00212 **    BITOP * - structure that holds the bit buffer
00213 **    int     - the position number in the bit buffer
00214 **
00215 **  FORMAL OUTPUTS
00216 **    int - 0 if bit not set, 1 if bit is set.
00217 **
00218 */
00219 static INLINE int boIsBitSet(BITOP *BitOp, unsigned int uiPos)
00220 {
00221     unsigned char  mask;
00222 
00223     /*
00224     **  Sanity Check while setting bits
00225     */
00226     if(BitOp->uiMaxBits <= uiPos)
00227     {
00228         return 0;
00229     }
00230 
00231     mask = 0x80 >> (uiPos & 7);
00232 
00233     return (mask & BitOp->pucBitBuffer[uiPos >> 3]);
00234 }
00235 
00236 /*
00237 **
00238 **  NAME
00239 **    boClearBit
00240 **
00241 **  DESCRIPTION
00242 **    Clear the bit in the specified position within the bit buffer.
00243 **
00244 **  FORMAL INPUTS
00245 **    BITOP * - the structure with the bit buffer
00246 **    int     - the position to clear within the bit buffer
00247 **
00248 **  FORMAL OUTPUTS
00249 **    int - 0 if the bit was cleared, 1 if there was an error.
00250 **
00251 */
00252 static INLINE void boClearBit(BITOP *BitOp, unsigned int uiPos)
00253 {
00254     unsigned char  mask;
00255 
00256     /*
00257     **  Sanity Check while clearing bits
00258     */
00259     if(BitOp->uiMaxBits <= uiPos)
00260     {
00261         return ;
00262     }
00263 
00264     mask = 0x80 >> (uiPos & 7);
00265 
00266     BitOp->pucBitBuffer[uiPos >> 3] &= ~mask;
00267 }
00268 
00269 /*
00270 **
00271 **  NAME
00272 **    boClearByte
00273 **
00274 **  DESCRIPTION
00275 **    Clear the byte in the specified position within the bit buffer.
00276 **
00277 **  FORMAL INPUTS
00278 **    BITOP * - the structure with the bit buffer
00279 **    int     - the position to clear within the bit buffer
00280 **
00281 **  FORMAL OUTPUTS
00282 **    int - 0 if the byte was cleared, 1 if there was an error.
00283 **
00284 */
00285 static INLINE void boClearByte(BITOP *BitOp, unsigned int uiPos)
00286 {
00287     /*
00288     **  Sanity Check while clearing bytes
00289     */
00290     if(BitOp->uiMaxBits <= uiPos)
00291     {
00292         return ;
00293     }
00294 
00295     BitOp->pucBitBuffer[uiPos >> 3] = 0;
00296 }
00297 
00298 #endif

Generated on Sun May 14 14:51:18 2006 by  doxygen 1.4.2