converted cli_list()
[metze/samba/wip.git] / source3 / libsmb / clistr.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    client string routines
5    Copyright (C) Andrew Tridgell 2001
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 /* we will delete this variable once our client side unicode support is complete */
27 extern int cli_use_unicode;
28
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
34 flags can have:
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)
42 {
43         int len;
44
45         /* treat a pstring as "unlimited" length */
46         if (dest_len == -1) {
47                 dest_len = sizeof(pstring);
48         }
49
50         if (clistr_align(cli, PTR_DIFF(cli->outbuf, dest))) {
51                 *(char *)dest = 0;
52                 dest++;
53                 dest_len--;
54         }
55
56         if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) {
57                 /* the server doesn't want unicode */
58                 safe_strcpy(dest, src, dest_len);
59                 len = strlen(dest);
60                 if (flags & CLISTR_TERMINATE) len++;
61                 if (flags & CLISTR_CONVERT) unix_to_dos(dest,True);
62                 if (flags & CLISTR_UPPER) strupper(dest);
63                 return len;
64         }
65
66         /* the server likes unicode. give it the works */
67         if (flags & CLISTR_CONVERT) {
68                 dos_PutUniCode(dest, src, dest_len, flags & CLISTR_TERMINATE);
69         } else {
70                 ascii_to_unistr(dest, src, dest_len);
71         }
72         if (flags & CLISTR_UPPER) {
73                 strupper_w(dest);
74         }
75         len = strlen(src)*2;
76         if (flags & CLISTR_TERMINATE) len += 2;
77         return len;
78 }
79
80
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)
89 {
90         int len = strlen(src);
91         if (flags & CLISTR_TERMINATE) len++;
92         if (cli_use_unicode && (cli->capabilities & CAP_UNICODE)) len *= 2;
93
94         if (dest && clistr_align(cli, PTR_DIFF(cli->outbuf, dest))) {
95                 len++;
96         }
97
98         return len;
99 }
100
101 /****************************************************************************
102 copy a string from a unicode or ascii source (depending on
103 cli->capabilities) to a char* destination
104 flags can have:
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)
112 {
113         int len;
114
115         if (dest_len == -1) {
116                 dest_len = sizeof(pstring);
117         }
118
119         if (clistr_align(cli, PTR_DIFF(cli->inbuf, src))) {
120                 src++;
121                 if (src_len > 0) src_len--;
122         }
123
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);
128                         len = strlen(src)+1;
129                 } else {
130                         if (src_len > dest_len) src_len = dest_len;
131                         len = src_len;
132                         memcpy(dest, src, len);
133                         dest[len] = 0;
134                 }
135                 if (flags & CLISTR_CONVERT) dos_to_unix(dest,True);
136                 return len;
137         }
138
139         if (flags & CLISTR_TERMINATE) {
140                 unistr_to_ascii(dest, src, dest_len);
141                 len = strlen(dest)*2 + 2;
142         } else {
143                 int i, c;
144                 if (dest_len < src_len) src_len = dest_len;
145                 for (i=0; i < src_len; i += 2) {
146                         c = SVAL(src, i);
147                         *dest++ = c;
148                 }
149                 *dest++ = 0;
150                 len = src_len;
151         }
152         if (flags & CLISTR_CONVERT) dos_to_unix(dest,True);
153         return len;
154 }
155
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)
162 {
163         if (clistr_align(cli, PTR_DIFF(cli->inbuf, src))) {
164                 src++;
165                 if (src_len > 0) src_len--;
166         }
167
168         if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) {
169                 return strlen(src);
170         }       
171         return strlen_w(src);
172 }
173
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)
180 {
181         if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) return 0;
182         return offset & 1;
183 }