2 Unix SMB/Netbios implementation.
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 /* we will delete this variable once our client side unicode support is complete */
27 extern int cli_use_unicode;
29 /****************************************************************************
30 copy a string from a char* src to a unicode or ascii
31 dos code page destination choosing unicode or ascii based on the
32 cli->capabilities flag
33 return the number of bytes occupied by the string in the destination
35 CLISTR_TERMINATE means include the null termination
36 CLISTR_CONVERT means convert from unix to dos codepage
37 CLISTR_UPPER means uppercase in the destination
38 CLISTR_ASCII use ascii even with unicode servers
39 dest_len is the maximum length allowed in the destination. If dest_len
40 is -1 then no maxiumum is used
41 ****************************************************************************/
42 int clistr_push(struct cli_state *cli, void *dest, char *src, int dest_len, int flags)
46 /* treat a pstring as "unlimited" length */
48 dest_len = sizeof(pstring);
51 if (!(flags & CLISTR_ASCII) && clistr_align(cli, PTR_DIFF(dest, cli->outbuf))) {
58 if ((flags & CLISTR_ASCII) || !cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) {
59 /* the server doesn't want unicode */
60 safe_strcpy(dest, src, dest_len);
62 if (flags & CLISTR_TERMINATE) len++;
63 if (flags & CLISTR_CONVERT) unix_to_dos(dest,True);
64 if (flags & CLISTR_UPPER) strupper(dest);
68 /* the server likes unicode. give it the works */
69 if (flags & CLISTR_CONVERT) {
70 dos_PutUniCode(dest, src, dest_len, flags & CLISTR_TERMINATE);
72 ascii_to_unistr(dest, src, dest_len);
74 if (flags & CLISTR_UPPER) {
78 if (flags & CLISTR_TERMINATE) len += 2;
83 /****************************************************************************
84 return the length that a string would occupy when copied with clistr_push()
85 CLISTR_TERMINATE means include the null termination
86 CLISTR_CONVERT means convert from unix to dos codepage
87 CLISTR_UPPER means uppercase in the destination
88 note that dest is only used for alignment purposes. No data is written.
89 ****************************************************************************/
90 int clistr_push_size(struct cli_state *cli, void *dest, char *src, int dest_len, int flags)
92 int len = strlen(src);
93 if (flags & CLISTR_TERMINATE) len++;
94 if (!(flags & CLISTR_ASCII) && cli_use_unicode && (cli->capabilities & CAP_UNICODE)) len *= 2;
96 if (!(flags & CLISTR_ASCII) && dest && clistr_align(cli, PTR_DIFF(cli->outbuf, dest))) {
103 /****************************************************************************
104 copy a string from a unicode or ascii source (depending on
105 cli->capabilities) to a char* destination
107 CLISTR_CONVERT means convert from dos to unix codepage
108 CLISTR_TERMINATE means the string in src is null terminated
109 if CLISTR_TERMINATE is set then src_len is ignored
110 src_len is the length of the source area in bytes
111 return the number of bytes occupied by the string in src
112 ****************************************************************************/
113 int clistr_pull(struct cli_state *cli, char *dest, void *src, int dest_len, int src_len, int flags)
117 if (dest_len == -1) {
118 dest_len = sizeof(pstring);
121 if (clistr_align(cli, PTR_DIFF(cli->inbuf, src))) {
123 if (src_len > 0) src_len--;
126 if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) {
127 /* the server doesn't want unicode */
128 if (flags & CLISTR_TERMINATE) {
129 safe_strcpy(dest, src, dest_len);
132 if (src_len > dest_len) src_len = dest_len;
134 memcpy(dest, src, len);
137 if (flags & CLISTR_CONVERT) dos_to_unix(dest,True);
141 if (flags & CLISTR_TERMINATE) {
142 unistr_to_ascii(dest, src, dest_len);
143 len = strlen(dest)*2 + 2;
146 if (dest_len < src_len) src_len = dest_len;
147 for (i=0; i < src_len; i += 2) {
154 if (flags & CLISTR_CONVERT) dos_to_unix(dest,True);
158 /****************************************************************************
159 return the length that a string would occupy (not including the null)
160 when copied with clistr_pull()
161 if src_len is -1 then assume the source is null terminated
162 ****************************************************************************/
163 int clistr_pull_size(struct cli_state *cli, void *src, int src_len)
165 if (clistr_align(cli, PTR_DIFF(cli->inbuf, src))) {
167 if (src_len > 0) src_len--;
170 if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) {
173 return strlen_w(src);
176 /****************************************************************************
177 return an alignment of either 0 or 1
178 if unicode is not negotiated then return 0
179 otherwise return 1 if offset is off
180 ****************************************************************************/
181 int clistr_align(struct cli_state *cli, int offset)
183 if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) return 0;