2 Unix SMB/Netbios implementation.
4 server specific string routines
5 Copyright (C) Andrew Tridgell 2001
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #define UNICODE_FLAG(buf) (SVAL(buf, smb_flg2) & FLAGS2_UNICODE_STRINGS)
28 /****************************************************************************
29 copy a string from a char* src to a unicode or ascii
30 dos code page destination choosing unicode or ascii based on the
31 FLAGS2_UNICODE_STRINGS bit in inbuf
32 return the number of bytes occupied by the string in the destination
34 STR_TERMINATE means include the null termination
35 STR_CONVERT means convert from unix to dos codepage
36 STR_UPPER means uppercase in the destination
37 STR_ASCII use ascii even with unicode servers
38 dest_len is the maximum length allowed in the destination. If dest_len
39 is -1 then no maxiumum is used
40 ****************************************************************************/
41 int srvstr_push(void *outbuf, void *dest, const char *src, int dest_len, int flags)
45 /* treat a pstring as "unlimited" length */
47 dest_len = sizeof(pstring);
50 if (!(flags & STR_ASCII) && srvstr_align(outbuf, PTR_DIFF(dest, outbuf))) {
52 dest = (void *)((char *)dest + 1);
57 if ((flags & STR_ASCII) || !UNICODE_FLAG(outbuf)) {
58 /* the client doesn't want unicode */
59 safe_strcpy(dest, src, dest_len);
61 if (flags & STR_TERMINATE) len++;
62 if (flags & STR_CONVERT) unix_to_dos(dest,True);
63 if (flags & STR_UPPER) strupper(dest);
67 /* the server likes unicode. give it the works */
68 if (flags & STR_CONVERT) {
69 dos_PutUniCode(dest, src, dest_len, flags & STR_TERMINATE);
71 ascii_to_unistr(dest, src, dest_len);
73 if (flags & STR_UPPER) {
77 if (flags & STR_TERMINATE) len += 2;
81 /****************************************************************************
82 copy a string from a unicode or ascii source (depending on flg2)
83 to a char* destination
85 STR_CONVERT means convert from dos to unix codepage
86 STR_TERMINATE means the string in src is null terminated
87 STR_UNICODE means to force as unicode
88 if STR_TERMINATE is set then src_len is ignored
89 src_len is the length of the source area in bytes
90 return the number of bytes occupied by the string in src
91 ****************************************************************************/
92 int srvstr_pull(void *inbuf, char *dest, const void *src, int dest_len, int src_len, int flags)
97 dest_len = sizeof(pstring);
100 if (!(flags & STR_ASCII) && srvstr_align(inbuf, PTR_DIFF(src, inbuf))) {
101 src = (void *)((char *)src + 1);
102 if (src_len > 0) src_len--;
105 if ((flags & STR_ASCII) || (!(flags & STR_UNICODE) && !UNICODE_FLAG(inbuf))) {
106 /* the server doesn't want unicode */
107 if (flags & STR_TERMINATE) {
108 safe_strcpy(dest, src, dest_len);
111 if (src_len > dest_len) src_len = dest_len;
113 memcpy(dest, src, len);
116 if (flags & STR_CONVERT) dos_to_unix(dest,True);
120 if (flags & STR_TERMINATE) {
121 unistr_to_ascii(dest, src, dest_len);
122 len = strlen(dest)*2 + 2;
125 if (dest_len*2 < src_len) src_len = 2*dest_len;
126 for (i=0; i < src_len; i += 2) {
133 if (flags & STR_CONVERT) dos_to_unix(dest,True);
137 /****************************************************************************
138 return an alignment of either 0 or 1
139 if unicode is not negotiated then return 0
140 otherwise return 1 if offset is off
141 ****************************************************************************/
142 int srvstr_align(void *inbuf, int offset)
144 if (!UNICODE_FLAG(inbuf)) return 0;
149 /****************************************************************************
150 these are useful for replacing all those StrnCpy() ops for copying data
152 ****************************************************************************/
154 int srvstr_push_ascii(void *dest, const char *src, int dest_len)
156 return srvstr_push(NULL, dest, src, dest_len,
157 STR_ASCII|STR_CONVERT|STR_TERMINATE);
160 int srvstr_pull_ascii(char *dest, const void *src, int dest_len)
162 return srvstr_pull(NULL, dest, src, dest_len, -1,
163 STR_ASCII|STR_CONVERT|STR_TERMINATE);