- added rudimentary CAP_UNICODE support because i thought it was part of
[kai/samba.git] / source3 / lib / util_unistr.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Samba utility functions
5    Copyright (C) Andrew Tridgell 1992-1998
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 #include "includes.h"
23
24 /*******************************************************************
25  Put an ASCII string into a UNICODE buffer (little endian).
26  ********************************************************************/
27
28 char *ascii_to_unibuf(char *dest, const char *src, int maxlen)
29 {
30         char *destend = dest + maxlen;
31         register char c;
32
33         while (dest < destend)
34         {
35                 c = *(src++);
36                 if (c == 0)
37                 {
38                         break;
39                 }
40
41                 *(dest++) = c;
42                 *(dest++) = 0;
43         }
44
45         *dest++ = 0;
46         *dest++ = 0;
47         return dest;
48 }
49
50
51 /*******************************************************************
52  Pull an ASCII string out of a UNICODE buffer (little endian).
53  ********************************************************************/
54 const char *unibuf_to_ascii(char *dest, const char *src, int maxlen)
55 {
56         char *destend = dest + maxlen;
57         register char c;
58
59         while (dest < destend)
60         {
61                 c = *(src++);
62                 if ((c == 0) && (*src == 0))
63                 {
64                         break;
65                 }
66
67                 *dest++ = c;
68                 src++;
69         }
70
71         *dest = 0;
72
73         return src;
74 }
75
76
77 /*******************************************************************
78  Put an ASCII string into a UNICODE array (uint16's).
79  ********************************************************************/
80
81 void ascii_to_unistr(uint16 *dest, const char *src, int maxlen)
82 {
83         uint16 *destend = dest + maxlen;
84         register char c;
85
86         while (dest < destend)
87         {
88                 c = *(src++);
89                 if (c == 0)
90                 {
91                         break;
92                 }
93
94                 *(dest++) = (uint16)c;
95         }
96
97         *dest = 0;
98 }
99
100
101 /*******************************************************************
102  Pull an ASCII string out of a UNICODE array (uint16's).
103  ********************************************************************/
104
105 void unistr_to_ascii(char *dest, const uint16 *src, int len)
106 {
107         char *destend = dest + len;
108         register uint16 c;
109
110         while (dest < destend)
111         {
112                 c = *(src++);
113                 if (c == 0)
114                 {
115                         break;
116                 }
117
118                 *(dest++) = (char)c;
119         }
120
121         *dest = 0;
122 }
123
124
125 /*******************************************************************
126  Convert a UNISTR2 structure to an ASCII string
127  ********************************************************************/
128
129 void unistr2_to_ascii(char *dest, const UNISTR2 *str, int maxlen)
130 {
131         char *destend;
132         const uint16 *src;
133         int len;
134         register uint16 c;
135
136         src = str->buffer;
137         len = MIN(str->uni_str_len, maxlen);
138         destend = dest + len;
139
140         while (dest < destend)
141         {
142                 c = *(src++);
143                 if (c == 0)
144                 {
145                         break;
146                 }
147
148                 *(dest++) = (char)c;
149         }
150
151         *dest = 0;
152 }
153
154
155 /*******************************************************************
156  Skip a UNICODE string in a little endian buffer.
157  ********************************************************************/
158
159 char *skip_unibuf(char *srcbuf, int len)
160 {
161         uint16 *src = (uint16 *)srcbuf;
162         uint16 *srcend = src + len/2;
163
164         while ((src < srcend) && (*(src++) != 0))
165         {
166         }
167
168         return (char *)src;
169 }
170
171
172 /*******************************************************************
173  UNICODE strcpy between buffers.
174  ********************************************************************/
175
176 char *uni_strncpy(char *destbuf, const char *srcbuf, int len)
177 {
178         const uint16 *src = (const uint16 *)srcbuf;
179         uint16 *dest = (uint16 *)destbuf;
180         uint16 *destend = dest + len/2;
181         register uint16 c;
182
183         while (dest < destend)
184         {
185                 c = *(src++);
186                 if (c == 0)
187                 {
188                         break;
189                 }
190
191                 *(dest++) = c;
192         }
193
194         *dest++ = 0;
195         return (char *)dest;
196 }
197
198
199 /*******************************************************************
200  Return a number stored in a buffer
201  ********************************************************************/
202
203 uint32 buffer2_to_uint32(const BUFFER2 *str)
204 {
205         if (str->buf_len == 4)
206         {
207                 const char *src = str->buffer;
208                 return IVAL(src, 0);
209         }
210         else
211         {
212                 return 0;
213         }
214 }
215
216
217 /*******************************************************************
218   Convert a 'multi-string' buffer to space-separated ASCII.
219  ********************************************************************/
220
221 void buffer2_to_multistr(char *dest, const BUFFER2 *str, int maxlen)
222 {
223         char *destend;
224         const char *src;
225         int len;
226         register uint16 c;
227
228         src = str->buffer;
229         len = MIN(str->buf_len/2, maxlen);
230         destend = dest + len;
231
232         while (dest < destend)
233         {
234                 c = *(src++);
235                 *(dest++) = (c == 0) ? ' ' : (char)c;
236                 src++;
237         }
238
239         *dest = 0;
240 }