libndr: Avoid assigning duplicate versions to symbols
[amitay/samba.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 #include "lib/replace/replace.h" /* for config symbols */
27
28 #define strlcpy_base(dest, src, base, size) \
29 do { \
30         const char *_strlcpy_base_src = (const char *)src; \
31         strlcpy((dest), _strlcpy_base_src? _strlcpy_base_src : "", (size)-PTR_DIFF((dest),(base))); \
32 } while (0)
33
34 /* String copy functions - macro hell below adds 'type checking' (limited,
35    but the best we can do in C) */
36
37 #define fstrcpy(d,s) \
38 do { \
39         const char *_fstrcpy_src = (const char *)(s); \
40         strlcpy((d),_fstrcpy_src ? _fstrcpy_src : "",sizeof(fstring)); \
41 } while (0)
42
43 #define fstrcat(d,s) \
44 do { \
45         const char *_fstrcat_src = (const char *)(s); \
46         strlcat((d),_fstrcat_src ? _fstrcat_src : "",sizeof(fstring)); \
47 } while (0)
48 #define unstrcpy(d,s) \
49 do { \
50         const char *_unstrcpy_src = (const char *)(s); \
51         strlcpy((d),_unstrcpy_src ? _unstrcpy_src : "",sizeof(unstring)); \
52 } while (0)
53
54 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
55
56 /* We need a number of different prototypes for our
57    non-existent fuctions */
58 char * __unsafe_string_function_usage_here__(void);
59
60 size_t __unsafe_string_function_usage_here_size_t__(void);
61
62 NTSTATUS __unsafe_string_function_usage_here_NTSTATUS__(void);
63
64 #define CHECK_STRING_SIZE(d, len) (sizeof(d) != (len) && sizeof(d) != sizeof(char *))
65
66 /* if the compiler will optimize out function calls, then use this to tell if we are
67    have the correct types (this works only where sizeof() returns the size of the buffer, not
68    the size of the pointer). */
69
70 #define push_string_check(dest, src, dest_len, flags) \
71     (CHECK_STRING_SIZE(dest, dest_len) \
72     ? __unsafe_string_function_usage_here_size_t__() \
73     : push_string_check_fn(dest, src, dest_len, flags))
74
75 #define srvstr_push(base_ptr, smb_flags2, dest, src, dest_len, flags, ret_len) \
76     (CHECK_STRING_SIZE(dest, dest_len) \
77     ? __unsafe_string_function_usage_here_NTSTATUS__() \
78     : srvstr_push_fn(base_ptr, smb_flags2, dest, src, dest_len, flags, ret_len))
79
80 /* This allows the developer to choose to check the arguments to
81    strlcpy.  if the compiler will optimize out function calls, then
82    use this to tell if we are have the correct size buffer (this works only
83    where sizeof() returns the size of the buffer, not the size of the
84    pointer), so stack and static variables only */
85
86 #define checked_strlcpy(dest, src, size) \
87     (sizeof(dest) != (size) \
88     ? __unsafe_string_function_usage_here_size_t__() \
89      : strlcpy(dest, src, size))
90
91 #else
92
93 #define push_string_check push_string_check_fn
94 #define srvstr_push srvstr_push_fn
95 #define checked_strlcpy strlcpy
96
97 #endif
98
99 #endif