Home | History | Annotate | Line # | Download | only in ssp
string.h revision 1.1
      1 /*	$NetBSD: string.h,v 1.1 2007/05/30 01:17:36 tls Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 #ifndef _SSP_STRING_H_
     39 #define _SSP_STRING_H_
     40 
     41 #include <ssp.h>
     42 #include_next <string.h>
     43 
     44 #if __SSP_FORTIFY_LEVEL > 0
     45 
     46 #define __ssp_bos_check3(fun, dst, src, len) \
     47     ((__ssp_bos0(dst) != (size_t)-1) ? \
     48     __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)) : \
     49     __ ## fun ## _ichk(dst, src, len))
     50 
     51 #define __ssp_bos_check2(fun, dst, src) \
     52     ((__ssp_bos0(dst) != (size_t)-1) ? \
     53     __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)) : \
     54     __ ## fun ## _ichk(dst, src))
     55 
     56 #define __ssp_bos_icheck3_restrict(fun, type1, type2) \
     57 static __inline type1 __ ## fun ## _ichk(type1 __restrict, type2 __restrict, size_t); \
     58 static __inline __attribute__((__always_inline__)) type1 \
     59 __ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src, size_t len) { \
     60 	return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \
     61 }
     62 
     63 #define __ssp_bos_icheck3(fun, type1, type2) \
     64 static __inline type1 __ ## fun ## _ichk(type1, type2, size_t); \
     65 static __inline __attribute__((__always_inline__)) type1 \
     66 __ ## fun ## _ichk(type1 dst, type2 src, size_t len) { \
     67 	return __builtin___ ## fun ## _chk(dst, src, len, __ssp_bos0(dst)); \
     68 }
     69 
     70 #define __ssp_bos_icheck2_restrict(fun, type1, type2) \
     71 static __inline type1 __ ## fun ## _ichk(type1, type2); \
     72 static __inline __attribute__((__always_inline__)) type1 \
     73 __ ## fun ## _ichk(type1 __restrict dst, type2 __restrict src) { \
     74 	return __builtin___ ## fun ## _chk(dst, src, __ssp_bos0(dst)); \
     75 }
     76 
     77 __BEGIN_DECLS
     78 __ssp_bos_icheck3_restrict(memcpy, void *, const void *)
     79 __ssp_bos_icheck3(memmove, void *, const void *)
     80 __ssp_bos_icheck3(memset, void *, int)
     81 __ssp_bos_icheck2_restrict(strcpy, char *, const char *)
     82 __ssp_bos_icheck2_restrict(strcat, char *, const char *)
     83 __ssp_bos_icheck3_restrict(strncpy, char *, const char *)
     84 __ssp_bos_icheck3_restrict(strncat, char *, const char *)
     85 __END_DECLS
     86 
     87 #define memcpy(dst, src, len) __ssp_bos_check3(memcpy, dst, src, len)
     88 #define memmove(dst, src, len) __ssp_bos_check3(memmove, dst, src, len)
     89 #define memset(dst, val, len) __ssp_bos_check3(memset, dst, val, len)
     90 #define strcpy(dst, src) __ssp_bos_check2(strcpy, dst, src)
     91 #define strcat(dst, src) __ssp_bos_check2(strcat, dst, src)
     92 #define strncpy(dst, src, len) __ssp_bos_check3(strncpy, dst, src, len)
     93 #define strncat(dst, src, len) __ssp_bos_check3(strncat, dst, src, len)
     94 
     95 #endif /* __SSP_FORTIFY_LEVEL > 0 */
     96 #endif /* _SSP_STRING_H_ */
     97