r24000: Add message_push_blob() and message_push_string().
[samba.git] / source / smbd / srvstr.c
1 /* 
2    Unix SMB/CIFS implementation.
3    server specific string routines
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 extern int max_send;
23
24 /* Make sure we can't write a string past the end of the buffer */
25
26 size_t srvstr_push_fn(const char *function, unsigned int line, 
27                       const char *base_ptr, void *dest, 
28                       const char *src, int dest_len, int flags)
29 {
30         size_t buf_used = PTR_DIFF(dest, base_ptr);
31         if (dest_len == -1) {
32                 if (((ptrdiff_t)dest < (ptrdiff_t)base_ptr) || (buf_used > (size_t)max_send)) {
33 #if 0
34                         DEBUG(0, ("Pushing string of 'unlimited' length into non-SMB buffer!\n"));
35 #endif
36                         return push_string_fn(function, line, base_ptr, dest, src, -1, flags);
37                 }
38                 return push_string_fn(function, line, base_ptr, dest, src, max_send - buf_used, flags);
39         }
40         
41         /* 'normal' push into size-specified buffer */
42         return push_string_fn(function, line, base_ptr, dest, src, dest_len, flags);
43 }
44
45 /*******************************************************************
46  Add a string to the end of a smb_buf, adjusting bcc and smb_len.
47  Return the bytes added
48 ********************************************************************/
49
50 ssize_t message_push_string(uint8 **outbuf, const char *str, int flags)
51 {
52         size_t buf_size = smb_len(*outbuf) + 4;
53         size_t grow_size;
54         size_t result;
55         uint8 *tmp;
56
57         /*
58          * We need to over-allocate, now knowing what srvstr_push will
59          * actually use. This is very generous by incorporating potential
60          * padding, the terminating 0 and at most 4 chars per UTF-16 code
61          * point.
62          */
63         grow_size = (strlen(str) + 2) * 4;
64
65         if (!(tmp = TALLOC_REALLOC_ARRAY(NULL, *outbuf, uint8,
66                                          buf_size + grow_size))) {
67                 DEBUG(0, ("talloc failed\n"));
68                 return -1;
69         }
70
71         result = srvstr_push((char *)tmp, tmp + buf_size, str, grow_size,
72                              flags);
73
74         if (result == (size_t)-1) {
75                 DEBUG(0, ("srvstr_push failed\n"));
76                 return -1;
77         }
78         set_message_bcc(NULL, (char *)tmp, smb_buflen(tmp) + result);
79
80         *outbuf = tmp;
81
82         return result;
83 }