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

bounds.h

Go to the documentation of this file.
00001 #ifndef _BOUNDS_H
00002 #define _BOUNDS_H
00003 /*
00004 ** Copyright (C) 2003, Sourcefire, Inc.
00005 **               Chris Green <cmg@sourcefire.com>
00006 **
00007 ** This program is free software; you can redistribute it and/or modify
00008 ** it under the terms of the GNU General Public License as published by
00009 ** the Free Software Foundation; either version 2 of the License, or
00010 ** (at your option) any later version.
00011 **
00012 ** This program is distributed in the hope that it will be useful,
00013 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 ** GNU General Public License for more details.
00016 **
00017 ** You should have received a copy of the GNU General Public License
00018 ** along with this program; if not, write to the Free Software
00019 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 */
00022 
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include "config.h"
00026 #endif
00027 
00028 #ifdef OSF1
00029 #include <sys/bitypes.h>
00030 #endif
00031 
00032 #include <string.h>
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <sys/types.h>
00036 #include <assert.h>
00037 #include <unistd.h>
00038 /* This INLINE is conflicting with the INLINE defined in bitop.h.
00039  * So, let's just add a little sanity check here.
00040  */
00041 #ifndef DEBUG
00042     #ifndef INLINE
00043         #define INLINE inline
00044     #endif
00045     #define ERRORRET return 0;
00046 #else
00047     #ifdef INLINE
00048         #undef INLINE
00049     #endif
00050     #define INLINE   
00051     #define ERRORRET assert(0==1)
00052 #endif /* DEBUG */
00053 
00054 /*
00055  * Check to make sure that p is less than or equal to the ptr range
00056  * pointers
00057  *
00058  * 1 means it's in bounds, 0 means it's not
00059  */
00060 static INLINE int inBounds(u_int8_t *start, u_int8_t *end, u_int8_t *p)
00061 {
00062     if(p >= start && p < end)
00063     {
00064         return 1;
00065     }
00066     
00067     return 0;
00068 }
00069 
00070 /** 
00071  * A Safer Memcpy
00072  * 
00073  * @param dst where to copy to
00074  * @param src where to copy from
00075  * @param n number of bytes to copy
00076  * @param start start of the dest buffer
00077  * @param end end of the dst buffer
00078  * 
00079  * @return 0 on failure, 1 on success
00080  */
00081 static INLINE int SafeMemcpy(void *dst, void *src, size_t n, void *start, void *end)
00082 {
00083      if(n < 1)
00084      {
00085          ERRORRET;
00086      }
00087 
00088      if(!inBounds(start,end, dst) || !inBounds(start,end,((u_int8_t*)dst)+n))
00089      {
00090          ERRORRET;
00091      }
00092 
00093      memcpy(dst, src, n);
00094      return 1;
00095 }
00096 
00097 /** 
00098  * A Safer *a = *b
00099  * 
00100  * @param start start of the dst buffer
00101  * @param end end of the dst buffer
00102  * @param dst the location to write to
00103  * @param src the source to read from
00104  * 
00105  * @return 0 on failure, 1 on success
00106  */
00107 static INLINE int SafeWrite(u_int8_t *start, u_int8_t *end, u_int8_t *dst, u_int8_t *src)
00108 {
00109     if(!inBounds(start, end, dst))
00110     {
00111         ERRORRET;
00112     }
00113      
00114     *dst = *src;        
00115     return 1;
00116 }
00117 
00118 static inline int SafeRead(u_int8_t *start, u_int8_t *end, u_int8_t *src, u_int8_t *read)
00119 {
00120     if(!inBounds(start,end, src))
00121     {
00122         ERRORRET;
00123     }
00124     
00125     *read = *start;
00126     return 1;
00127 }
00128 
00129 #endif /* _BOUNDS_H */

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