added support for a CLISTR_ASCII flag so we can use a uniform
[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   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)
43 {
44         int len=0;
45
46         /* treat a pstring as "unlimited" length */
47         if (dest_len == -1) {
48                 dest_len = sizeof(pstring);
49         }
50
51         if (!(flags & CLISTR_ASCII) && clistr_align(cli, PTR_DIFF(dest, cli->outbuf))) {
52                 *(char *)dest = 0;
53                 dest++;
54                 dest_len--;
55                 len++;
56         }
57
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);
61                 len = strlen(dest);
62                 if (flags & CLISTR_TERMINATE) len++;
63                 if (flags & CLISTR_CONVERT) unix_to_dos(dest,True);
64                 if (flags & CLISTR_UPPER) strupper(dest);
65                 return len;
66         }
67
68         /* the server likes unicode. give it the works */
69         if (flags & CLISTR_CONVERT) {
70                 dos_PutUniCode(dest, src, dest_len, flags & CLISTR_TERMINATE);
71         } else {
72                 ascii_to_unistr(dest, src, dest_len);
73         }
74         if (flags & CLISTR_UPPER) {
75                 strupper_w(dest);
76         }
77         len += strlen(src)*2;
78         if (flags & CLISTR_TERMINATE) len += 2;
79         return len;
80 }
81
82
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)
91 {
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;
95
96         if (!(flags & CLISTR_ASCII) && dest && clistr_align(cli, PTR_DIFF(cli->outbuf, dest))) {
97                 len++;
98         }
99
100         return len;
101 }
102
103 /****************************************************************************
104 copy a string from a unicode or ascii source (depending on
105 cli->capabilities) to a char* destination
106 flags can have:
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)
114 {
115         int len;
116
117         if (dest_len == -1) {
118                 dest_len = sizeof(pstring);
119         }
120
121         if (clistr_align(cli, PTR_DIFF(cli->inbuf, src))) {
122                 src++;
123                 if (src_len > 0) src_len--;
124         }
125
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);
130                         len = strlen(src)+1;
131                 } else {
132                         if (src_len > dest_len) src_len = dest_len;
133                         len = src_len;
134                         memcpy(dest, src, len);
135                         dest[len] = 0;
136                 }
137                 if (flags & CLISTR_CONVERT) dos_to_unix(dest,True);
138                 return len;
139         }
140
141         if (flags & CLISTR_TERMINATE) {
142                 unistr_to_ascii(dest, src, dest_len);
143                 len = strlen(dest)*2 + 2;
144         } else {
145                 int i, c;
146                 if (dest_len < src_len) src_len = dest_len;
147                 for (i=0; i < src_len; i += 2) {
148                         c = SVAL(src, i);
149                         *dest++ = c;
150                 }
151                 *dest++ = 0;
152                 len = src_len;
153         }
154         if (flags & CLISTR_CONVERT) dos_to_unix(dest,True);
155         return len;
156 }
157
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)
164 {
165         if (clistr_align(cli, PTR_DIFF(cli->inbuf, src))) {
166                 src++;
167                 if (src_len > 0) src_len--;
168         }
169
170         if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) {
171                 return strlen(src);
172         }       
173         return strlen_w(src);
174 }
175
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)
182 {
183         if (!cli_use_unicode || !(cli->capabilities & CAP_UNICODE)) return 0;
184         return offset & 1;
185 }