lib: move the string wrappers from source3/include to common lib/util
[ira/wip.git] / lib / util / string_wrappers.h
1 /*
2    Unix SMB/CIFS implementation.
3
4    string wrappers, for checking string sizes
5
6    Copyright (C) Andrew Tridgell 1994-2011
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #ifndef _STRING_WRAPPERS_H
24 #define _STRING_WRAPPERS_H
25
26 /* We need a number of different prototypes for our
27    non-existant fuctions */
28 char * __unsafe_string_function_usage_here__(void);
29
30 size_t __unsafe_string_function_usage_here_size_t__(void);
31
32 size_t __unsafe_string_function_usage_here_char__(void);
33
34 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
35
36 /* if the compiler will optimize out function calls, then use this to tell if we are
37    have the correct types (this works only where sizeof() returns the size of the buffer, not
38    the size of the pointer). */
39
40 #define CHECK_STRING_SIZE(d, len) (sizeof(d) != (len) && sizeof(d) != sizeof(char *))
41
42 #else /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
43
44 #endif /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
45
46 #define safe_strcpy_base(dest, src, base, size) \
47     safe_strcpy(dest, src, size-PTR_DIFF(dest,base)-1)
48
49 /* String copy functions - macro hell below adds 'type checking' (limited,
50    but the best we can do in C) */
51
52 #define fstrcpy(d,s) safe_strcpy((d),(s),sizeof(fstring)-1)
53 #define fstrcat(d,s) safe_strcat((d),(s),sizeof(fstring)-1)
54 #define nstrcpy(d,s) safe_strcpy((d), (s),sizeof(nstring)-1)
55 #define unstrcpy(d,s) safe_strcpy((d), (s),sizeof(unstring)-1)
56
57 /* the addition of the DEVELOPER checks in safe_strcpy means we must
58  * update a lot of code. To make this a little easier here are some
59  * functions that provide the lengths with less pain */
60
61 /* overmalloc_safe_strcpy: DEPRECATED!  Used when you know the
62  * destination buffer is longer than maxlength, but you don't know how
63  * long.  This is not a good situation, because we can't do the normal
64  * sanity checks. Don't use in new code! */
65
66 #define overmalloc_safe_strcpy(dest,src,maxlength) \
67         safe_strcpy_fn(dest,src,maxlength)
68
69 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
70
71 /* if the compiler will optimize out function calls, then use this to tell if we are
72    have the correct types (this works only where sizeof() returns the size of the buffer, not
73    the size of the pointer). */
74
75 #define safe_strcpy(d, s, max_len) \
76     (CHECK_STRING_SIZE(d, max_len+1) \
77     ? __unsafe_string_function_usage_here__() \
78     : safe_strcpy_fn((d), (s), (max_len)))
79
80 #define safe_strcat(d, s, max_len) \
81     (CHECK_STRING_SIZE(d, max_len+1) \
82     ? __unsafe_string_function_usage_here__() \
83     : safe_strcat_fn((d), (s), (max_len)))
84
85 #define push_string_check(dest, src, dest_len, flags) \
86     (CHECK_STRING_SIZE(dest, dest_len) \
87     ? __unsafe_string_function_usage_here_size_t__() \
88     : push_string_check_fn(dest, src, dest_len, flags))
89
90 #define pull_string_talloc(ctx, base_ptr, smb_flags2, dest, src, src_len, flags) \
91     pull_string_talloc_fn(ctx, base_ptr, smb_flags2, dest, src, src_len, flags)
92
93 #define clistr_push(cli, dest, src, dest_len, flags) \
94     (CHECK_STRING_SIZE(dest, dest_len) \
95     ? __unsafe_string_function_usage_here_size_t__() \
96     : clistr_push_fn(cli, dest, src, dest_len, flags))
97
98 #define clistr_pull(inbuf, dest, src, dest_len, srclen, flags) \
99     (CHECK_STRING_SIZE(dest, dest_len) \
100     ? __unsafe_string_function_usage_here_size_t__() \
101     : clistr_pull_fn(inbuf, dest, src, dest_len, srclen, flags))
102
103 #define srvstr_push(base_ptr, smb_flags2, dest, src, dest_len, flags) \
104     (CHECK_STRING_SIZE(dest, dest_len) \
105     ? __unsafe_string_function_usage_here_size_t__() \
106     : srvstr_push_fn(base_ptr, smb_flags2, dest, src, dest_len, flags))
107
108 /* This allows the developer to choose to check the arguments to
109    strlcpy.  if the compiler will optimize out function calls, then
110    use this to tell if we are have the correct size buffer (this works only
111    where sizeof() returns the size of the buffer, not the size of the
112    pointer), so stack and static variables only */
113
114 #define checked_strlcpy(dest, src, size) \
115     (sizeof(dest) != (size) \
116     ? __unsafe_string_function_usage_here_size_t__() \
117      : strlcpy(dest, src, size))
118
119 #else
120
121 #define safe_strcpy safe_strcpy_fn
122 #define safe_strcat safe_strcat_fn
123 #define push_string_check push_string_check_fn
124 #define pull_string_talloc pull_string_talloc_fn
125 #define clistr_push clistr_push_fn
126 #define clistr_pull clistr_pull_fn
127 #define srvstr_push srvstr_push_fn
128 #define checked_strlcpy strlcpy
129
130 #endif
131
132 #endif