Fix for endianness problem reported by Edan Idzerda <edan@mtu.edu>. A
[sfrench/samba-autobuild/.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
55 void unibuf_to_ascii(char *dest, const char *src, int maxlen)
56 {
57         char *destend = dest + maxlen;
58         register char c;
59
60         while (dest < destend)
61         {
62                 c = *(src++);
63                 if ((c == 0) && (*src == 0))
64                 {
65                         break;
66                 }
67
68                 *dest++ = c;
69                 src++;
70         }
71
72         *dest = 0;
73 }
74
75
76 /*******************************************************************
77  Put an ASCII string into a UNICODE array (uint16's).
78  ********************************************************************/
79
80 void ascii_to_unistr(uint16 *dest, const char *src, int maxlen)
81 {
82         uint16 *destend = dest + maxlen;
83         register char c;
84
85         while (dest < destend)
86         {
87                 c = *(src++);
88                 if (c == 0)
89                 {
90                         break;
91                 }
92
93                 *(dest++) = (uint16)c;
94         }
95
96         *dest = 0;
97 }
98
99
100 /*******************************************************************
101  Pull an ASCII string out of a UNICODE array (uint16's).
102  ********************************************************************/
103
104 void unistr_to_ascii(char *dest, const uint16 *src, int len)
105 {
106         char *destend = dest + len;
107         register uint16 c;
108
109         while (dest < destend)
110         {
111                 c = *(src++);
112                 if (c == 0)
113                 {
114                         break;
115                 }
116
117                 *(dest++) = (char)c;
118         }
119
120         *dest = 0;
121 }
122
123
124 /*******************************************************************
125  Convert a UNISTR2 structure to an ASCII string
126  ********************************************************************/
127
128 void unistr2_to_ascii(char *dest, const UNISTR2 *str, int maxlen)
129 {
130         char *destend;
131         const uint16 *src;
132         int len;
133         register uint16 c;
134
135         src = str->buffer;
136         len = MIN(str->uni_str_len, maxlen);
137         destend = dest + len;
138
139         while (dest < destend)
140         {
141                 c = *(src++);
142                 if (c == 0)
143                 {
144                         break;
145                 }
146
147                 *(dest++) = (char)c;
148         }
149
150         *dest = 0;
151 }
152
153
154 /*******************************************************************
155  Skip a UNICODE string in a little endian buffer.
156  ********************************************************************/
157
158 char *skip_unibuf(char *srcbuf, int len)
159 {
160         uint16 *src = (uint16 *)srcbuf;
161         uint16 *srcend = src + len/2;
162
163         while ((src < srcend) && (*(src++) != 0))
164         {
165         }
166
167         return (char *)src;
168 }
169
170
171 /*******************************************************************
172  UNICODE strcpy between buffers.
173  ********************************************************************/
174
175 char *uni_strncpy(char *destbuf, const char *srcbuf, int len)
176 {
177         const uint16 *src = (const uint16 *)srcbuf;
178         uint16 *dest = (uint16 *)destbuf;
179         uint16 *destend = dest + len/2;
180         register uint16 c;
181
182         while (dest < destend)
183         {
184                 c = *(src++);
185                 if (c == 0)
186                 {
187                         break;
188                 }
189
190                 *(dest++) = c;
191         }
192
193         *dest++ = 0;
194         return (char *)dest;
195 }
196
197
198 /*******************************************************************
199  Return a number stored in a buffer
200  ********************************************************************/
201
202 uint32 buffer2_to_uint32(const BUFFER2 *str)
203 {
204         if (str->buf_len == 4)
205         {
206                 const char *src = str->buffer;
207                 return IVAL(src, 0);
208         }
209         else
210         {
211                 return 0;
212         }
213 }
214
215
216 /*******************************************************************
217   Convert a 'multi-string' buffer to space-separated ASCII.
218  ********************************************************************/
219
220 void buffer2_to_multistr(char *dest, const BUFFER2 *str, int maxlen)
221 {
222         char *destend;
223         const char *src;
224         int len;
225         register uint16 c;
226
227         src = str->buffer;
228         len = MIN(str->buf_len/2, maxlen);
229         destend = dest + len;
230
231         while (dest < destend)
232         {
233                 c = *(src++);
234                 *(dest++) = (c == 0) ? ' ' : (char)c;
235                 src++;
236         }
237
238         *dest = 0;
239 }