UNICODE byte ordering issue: typecast to uint16* replaced with SSVAL()
[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 write a string in unicoode format
26 ********************************************************************/
27 int PutUniCode(char *dst,char *src)
28 {
29   int ret = 0;
30   while (*src) {
31     dst[ret++] = src[0];
32     dst[ret++] = 0;    
33     src++;
34   }
35   dst[ret++]=0;
36   dst[ret++]=0;
37   return(ret);
38 }
39
40 /*******************************************************************
41 skip past some unicode strings in a buffer
42 ********************************************************************/
43 char *skip_unicode_string(char *buf,int n)
44 {
45   while (n--)
46   {
47     while (*buf)
48       buf += 2;
49     buf += 2;
50   }
51   return(buf);
52 }
53
54 /*******************************************************************
55 Return a ascii version of a unicode string
56 Hack alert: uses fixed buffer(s) and only handles ascii strings
57 ********************************************************************/
58 #define MAXUNI 1024
59 char *unistrn2(char *buf, int len)
60 {
61         static char lbufs[8][MAXUNI];
62         static int nexti;
63         char *lbuf = lbufs[nexti];
64         char *p;
65
66         nexti = (nexti+1)%8;
67
68         for (p = lbuf; *buf && p-lbuf < MAXUNI-2 && len > 0; len--, p++, buf+=2)
69         {
70                 SSVAL(p, 0, *buf);
71         }
72
73         *p = 0;
74         return lbuf;
75 }
76
77 static char lbufs[8][MAXUNI];
78 static int nexti;
79 /*******************************************************************
80 Return a ascii version of a unicode string
81 Hack alert: uses fixed buffer(s) and only handles ascii strings
82 ********************************************************************/
83 #define MAXUNI 1024
84 char *unistr2(uint16 *buf)
85 {
86         char *lbuf = lbufs[nexti];
87         char *p;
88
89         nexti = (nexti+1)%8;
90
91         for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf++)
92         {
93                 *p = *buf;
94         }
95
96         *p = 0;
97         return lbuf;
98 }
99
100 /*******************************************************************
101 Return a ascii version of a unicode string
102 ********************************************************************/
103 char *unistr2_to_str(UNISTR2 *str)
104 {
105         char *lbuf = lbufs[nexti];
106         char *p;
107         uint16 *buf = str->buffer;
108         int max_size = MIN(sizeof(str->buffer)-2, str->uni_str_len);
109
110         nexti = (nexti+1)%8;
111
112         for (p = lbuf; *buf && p-lbuf < max_size; p++, buf++)
113         {
114                 *p = *buf;
115         }
116
117         *p = 0;
118         return lbuf;
119 }
120
121 /*******************************************************************
122 Return a number stored in a buffer
123 ********************************************************************/
124 uint32 buffer2_to_uint32(BUFFER2 *str)
125 {
126         if (str->buf_len == 4)
127         {
128                 return IVAL(str->buffer, 0);
129         }
130         else
131         {
132                 return 0;
133         }
134 }
135
136 /*******************************************************************
137 Return a ascii version of a NOTunicode string
138 ********************************************************************/
139 char *buffer2_to_str(BUFFER2 *str)
140 {
141         char *lbuf = lbufs[nexti];
142         char *p;
143         uint16 *buf = str->buffer;
144         int max_size = MIN(sizeof(str->buffer)-2, str->buf_len/2);
145
146         nexti = (nexti+1)%8;
147
148         for (p = lbuf; *buf && p-lbuf < max_size; p++, buf++)
149         {
150                 *p = *buf;
151         }
152
153         *p = 0;
154         return lbuf;
155 }
156
157 /*******************************************************************
158 Return a ascii version of a NOTunicode string
159 ********************************************************************/
160 char *buffer2_to_multistr(BUFFER2 *str)
161 {
162         char *lbuf = lbufs[nexti];
163         char *p;
164         uint16 *buf = str->buffer;
165         int max_size = MIN(sizeof(str->buffer)-2, str->buf_len/2);
166
167         nexti = (nexti+1)%8;
168
169         for (p = lbuf; p-lbuf < max_size; p++, buf++)
170         {
171                 if (*buf == 0)
172                 {
173                         *p = ' ';
174                 }
175                 else
176                 {
177                         *p = *buf;
178                 }
179         }
180
181         *p = 0;
182         return lbuf;
183 }
184
185 /*******************************************************************
186 create a null-terminated unicode string from a null-terminated ascii string.
187 return number of unicode chars copied, excluding the null character.
188
189 only handles ascii strings
190 ********************************************************************/
191 #define MAXUNI 1024
192 int struni2(char *p, const char *buf)
193 {
194         int len = 0;
195
196         if (p == NULL) return 0;
197
198         if (buf != NULL)
199         {
200                 for (; *buf && len < MAXUNI-2; len++, p += 2, buf++)
201                 {
202                         SSVAL(p, 0, *buf);
203                 }
204         }
205
206         *p = 0;
207
208         return len;
209 }
210
211 /*******************************************************************
212 Return a ascii version of a unicode string
213 Hack alert: uses fixed buffer(s) and only handles ascii strings
214 ********************************************************************/
215 #define MAXUNI 1024
216 char *unistr(char *buf)
217 {
218         char *lbuf = lbufs[nexti];
219         char *p;
220
221         nexti = (nexti+1)%8;
222
223         for (p = lbuf; *buf && p-lbuf < MAXUNI-2; p++, buf += 2)
224         {
225                 *p = *buf;
226         }
227         *p = 0;
228         return lbuf;
229 }
230
231
232 /*******************************************************************
233 strcpy for unicode strings.  returns length (in num of wide chars)
234 ********************************************************************/
235 int unistrcpy(char *dst, char *src)
236 {
237         int num_wchars = 0;
238
239         while (*src)
240         {
241                 *dst++ = *src++;
242                 *dst++ = *src++;
243                 num_wchars++;
244         }
245         *dst++ = 0;
246         *dst++ = 0;
247
248         return num_wchars;
249 }
250