summaryrefslogtreecommitdiff
path: root/source3/lib/util_unistr.c
blob: 4a1117d8d07fae514731190394c2b6ba5c077752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* 
   Unix SMB/Netbios implementation.
   Version 3.0
   Samba utility functions
   Copyright (C) Andrew Tridgell 1992-2001
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

extern int DEBUGLEVEL;

#ifndef MAXUNI
#define MAXUNI 1024
#endif

/*******************************************************************
 Write a string in (little-endian) unicode format. src is in
 the current DOS codepage. len is the length in bytes of the
 string pointed to by dst.

 if null_terminate is True then null terminate the packet (adds 2 bytes)

 the return value is the length in bytes consumed by the string, including the
 null termination if applied
********************************************************************/

size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate)
{
	return push_ucs2(NULL, dst, src, len, 
			 STR_UNICODE|STR_NOALIGN | (null_terminate?STR_TERMINATE:0));
}


/*******************************************************************
 Skip past a unicode string, but not more than len. Always move
 past a terminating zero if found.
********************************************************************/

char *skip_unibuf(char *src, size_t len)
{
    char *srcend = src + len;

    while (src < srcend && SVAL(src,0))
        src += 2;

    if(!SVAL(src,0))
        src += 2;

    return src;
}

/* Copy a string from little-endian or big-endian unicode source (depending
 * on flags) to internal samba format destination
 */ 
int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
{
	if(dest_len==-1) dest_len=MAXUNI-3;
	return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
}

/* Converts a string from internal samba format to unicode
 */ 
int rpcstr_push(void* dest, const char *src, int dest_len, int flags)
{
	return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
}

/*******************************************************************
 Return a DOS codepage version of a little-endian unicode string.
 len is the filename length (ignoring any terminating zero) in uin16
 units. Always null terminates.
 Hack alert: uses fixed buffer(s).
********************************************************************/
char *dos_unistrn2(const uint16 *src, int len)
{
	static char lbufs[8][MAXUNI];
	static int nexti;
	char *lbuf = lbufs[nexti];
	nexti = (nexti+1)%8;
	pull_ucs2(NULL, lbuf, src, MAXUNI-3, len*2, STR_NOALIGN);
	return lbuf;
}

/*******************************************************************
 Convert a (little-endian) UNISTR2 structure to an ASCII string
********************************************************************/
void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
{
	if (str == NULL) {
		*dest='\0';
		return;
	}
	pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
}


/*******************************************************************
Return a number stored in a buffer
********************************************************************/

uint32 buffer2_to_uint32(BUFFER2 *str)
{
	if (str->buf_len == 4)
		return IVAL(str->buffer, 0);
	else
		return 0;
}

/*******************************************************************
 Mapping tables for UNICODE character. Allows toupper/tolower and
 isXXX functions to work.

 tridge: split into 2 pieces. This saves us 5/6 of the memory
 with a small speed penalty
 The magic constants are the lower/upper range of the tables two
 parts
********************************************************************/

typedef struct {
	smb_ucs2_t lower;
	smb_ucs2_t upper;
	unsigned char flags;
} smb_unicode_table_t;

#define TABLE1_BOUNDARY 9450
#define TABLE2_BOUNDARY 64256

static smb_unicode_table_t map_table1[] = {
#include "unicode_map_table1.h"
};

static smb_unicode_table_t map_table2[] = {
#include "unicode_map_table2.h"
};

static unsigned char map_table_flags(smb_ucs2_t v)
{
	if (v < TABLE1_BOUNDARY) return map_table1[v].flags;
	if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].flags;
	return 0;
}

static smb_ucs2_t map_table_lower(smb_ucs2_t v)
{
	if (v < TABLE1_BOUNDARY) return map_table1[v].lower;
	if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].lower;
	return v;
}

static smb_ucs2_t map_table_upper(smb_ucs2_t v)
{
	if (v < TABLE1_BOUNDARY) return map_table1[v].upper;
	if (v >= TABLE2_BOUNDARY) return map_table2[v - TABLE2_BOUNDARY].upper;
	return v;
}

/*******************************************************************
 Is an upper case wchar.
********************************************************************/

int isupper_w( smb_ucs2_t val)
{
	return (map_table_flags(val) & UNI_UPPER);
}

/*******************************************************************
 Is a lower case wchar.
********************************************************************/

int islower_w( smb_ucs2_t val)
{
	return (map_table_flags(val) & UNI_LOWER);
}

/*******************************************************************
 Convert a wchar to upper case.
********************************************************************/

smb_ucs2_t toupper_w( smb_ucs2_t val )
{
	return map_table_upper(val);
}

/*******************************************************************
 Convert a wchar to lower case.
********************************************************************/

smb_ucs2_t tolower_w( smb_ucs2_t val )
{
	return map_table_lower(val);
}

/*******************************************************************
 Count the number of characters in a smb_ucs2_t string.
********************************************************************/
size_t strlen_w(const smb_ucs2_t *src)
{
	size_t len;

	for(len = 0; *src++; len++) ;

	return len;
}

/*******************************************************************
wide strchr()
********************************************************************/
smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
{
	while (*s != 0) {
		if (c == *s) return (smb_ucs2_t *)s;
		s++;
	}
	return NULL;
}


/*******************************************************************
 Convert a string to lower case.
********************************************************************/
void strlower_w(smb_ucs2_t *s)
{
	while (*s) {
		if (isupper_w(*s))
			*s = tolower_w(*s);
		s++;
	}
}

/*******************************************************************
 Convert a string to upper case.
********************************************************************/
void strupper_w(smb_ucs2_t *s)
{
	while (*s) {
		if (islower_w(*s))
			*s = toupper_w(*s);
		s++;
	}
}

/*******************************************************************
case insensitive string comparison
********************************************************************/
int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
{
	while (*b && tolower_w(*a) == tolower_w(*b)) { a++; b++; }
	return (tolower_w(*a) - tolower_w(*b));
}


/*
  The *_wa() functions take a combination of 7 bit ascii
  and wide characters They are used so that you can use string
  functions combining C string constants with ucs2 strings

  The char* arguments must NOT be multibyte - to be completely sure
  of this only pass string constants */


void pstrcpy_wa(smb_ucs2_t *dest, const char *src)
{
	int i;
	for (i=0;i<PSTRING_LEN;i++) {
		dest[i] = UCS2_CHAR(src[i]);
		if (src[i] == 0) return;
	}
}

int strcmp_wa(const smb_ucs2_t *a, const char *b)
{
	while (*b && *a == UCS2_CHAR(*b)) { a++; b++; }
	return (*a - UCS2_CHAR(*b));
}

smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
{
	while (*s != 0) {
		if (UCS2_CHAR(c) == *s) return (smb_ucs2_t *)s;
		s++;
	}
	return NULL;
}

smb_ucs2_t *strrchr_wa(const smb_ucs2_t *s, char c)
{
	const smb_ucs2_t *p = s;
	int len = strlen_w(s);
	if (len == 0) return NULL;
	p += (len-1);
	do {
		if (UCS2_CHAR(c) == *p) return (smb_ucs2_t *)p;
	} while (p-- != s);
	return NULL;
}

smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
{
	while (*s != 0) {
		int i;
		for (i=0; p[i] && *s != UCS2_CHAR(p[i]); i++) 
			;
		if (p[i]) return (smb_ucs2_t *)s;
		s++;
	}
	return NULL;
}