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 dest_len is the maximum length allowed in the destination. If dest_len
39 is -1 then no maxiumum is used
40 ****************************************************************************/
41 int clistr_push(struct cli_state *cli, void *dest, char *src, int dest_len, int flags)
45 /* treat a pstring as "unlimited" length */
47 dest_len = sizeof(pstring);
50 if (clistr_align(cli, PTR_DIFF(cli->outbuf, dest))) {
56 if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) {
57 /* the server doesn't want unicode */
58 safe_strcpy(dest, src, dest_len);
60 if (flags & CLISTR_TERMINATE) len++;
61 if (flags & CLISTR_CONVERT) unix_to_dos(dest,True);
62 if (flags & CLISTR_UPPER) strupper(dest);
66 /* the server likes unicode. give it the works */
67 if (flags & CLISTR_CONVERT) {
68 dos_PutUniCode(dest, src, dest_len, flags & CLISTR_TERMINATE);
70 ascii_to_unistr(dest, src, dest_len);
72 if (flags & CLISTR_UPPER) {
76 if (flags & CLISTR_TERMINATE) len += 2;
81 /****************************************************************************
82 return the length that a string would occupy when copied with clistr_push()
83 CLISTR_TERMINATE means include the null termination
84 CLISTR_CONVERT means convert from unix to dos codepage
85 CLISTR_UPPER means uppercase in the destination
86 note that dest is only used for alignment purposes. No data is written.
87 ****************************************************************************/
88 int clistr_push_size(struct cli_state *cli, void *dest, char *src, int dest_len, int flags)
90 int len = strlen(src);
91 if (flags & CLISTR_TERMINATE) len++;
92 if (cli_use_unicode && (cli->capabilities & CAP_UNICODE)) len *= 2;
94 if (dest && clistr_align(cli, PTR_DIFF(cli->outbuf, dest))) {
101 /****************************************************************************
102 copy a string from a unicode or ascii source (depending on
103 cli->capabilities) to a char* destination
105 CLISTR_CONVERT means convert from dos to unix codepage
106 CLISTR_TERMINATE means the string in src is null terminated
107 if CLISTR_TERMINATE is set then src_len is ignored
108 src_len is the length of the source area in bytes
109 return the number of bytes occupied by the string in src
110 ****************************************************************************/
111 int clistr_pull(struct cli_state *cli, char *dest, void *src, int dest_len, int src_len, int flags)
115 if (dest_len == -1) {
116 dest_len = sizeof(pstring);
119 if (clistr_align(cli, PTR_DIFF(cli->inbuf, src))) {
121 if (src_len > 0) src_len--;
124 if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) {
125 /* the server doesn't want unicode */
126 if (flags & CLISTR_TERMINATE) {
127 safe_strcpy(dest, src, dest_len);
130 if (src_len > dest_len) src_len = dest_len;
132 memcpy(dest, src, len);
135 if (flags & CLISTR_CONVERT) dos_to_unix(dest,True);
139 if (flags & CLISTR_TERMINATE) {
140 unistr_to_ascii(dest, src, dest_len);
141 len = strlen(dest)*2 + 2;
144 if (dest_len < src_len) src_len = dest_len;
145 for (i=0; i < src_len; i += 2) {
152 if (flags & CLISTR_CONVERT) dos_to_unix(dest,True);
156 /****************************************************************************
157 return the length that a string would occupy (not including the null)
158 when copied with clistr_pull()
159 if src_len is -1 then assume the source is null terminated
160 ****************************************************************************/
161 int clistr_pull_size(struct cli_state *cli, void *src, int src_len)
163 if (clistr_align(cli, PTR_DIFF(cli->inbuf, src))) {
165 if (src_len > 0) src_len--;
168 if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) {
171 return strlen_w(src);
174 /****************************************************************************
175 return an alignment of either 0 or 1
176 if unicode is not negotiated then return 0
177 otherwise return 1 if offset is off
178 ****************************************************************************/
179 int clistr_align(struct cli_state *cli, int offset)
181 if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) return 0;