1998-11-05 19:48:35 +03:00
/*
Unix SMB / Netbios implementation .
Version 1.9 .
Samba utility functions
Copyright ( C ) Andrew Tridgell 1992 - 1998
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 0213 9 , USA .
*/
# include "includes.h"
/*******************************************************************
1999-02-12 03:16:09 +03:00
Put an ASCII string into a UNICODE buffer ( little endian ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-02-11 01:30:47 +03:00
1999-02-12 03:16:09 +03:00
char * ascii_to_unibuf ( char * dest , const char * src , int maxlen )
1998-11-05 19:48:35 +03:00
{
1999-02-15 08:31:52 +03:00
char * destend = dest + maxlen ;
1999-02-12 03:16:09 +03:00
register char c ;
1999-02-12 18:35:27 +03:00
while ( dest < destend )
{
1999-02-15 08:31:52 +03:00
c = * ( src + + ) ;
1999-02-12 18:35:27 +03:00
if ( c = = 0 )
{
1999-02-12 03:16:09 +03:00
break ;
}
1999-02-15 08:31:52 +03:00
* ( dest + + ) = c ;
* ( dest + + ) = 0 ;
1998-11-05 19:48:35 +03:00
}
1999-02-15 08:31:52 +03:00
* dest + + = 0 ;
* dest + + = 0 ;
1999-02-12 03:16:09 +03:00
return dest ;
1998-11-05 19:48:35 +03:00
}
1999-02-11 01:30:47 +03:00
1998-11-05 19:48:35 +03:00
/*******************************************************************
1999-02-12 03:16:09 +03:00
Pull an ASCII string out of a UNICODE buffer ( little endian ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-02-11 01:30:47 +03:00
1999-02-12 03:16:09 +03:00
void unibuf_to_ascii ( char * dest , const char * src , int maxlen )
1998-11-05 19:48:35 +03:00
{
1999-02-12 03:16:09 +03:00
char * destend = dest + maxlen ;
register char c ;
1998-11-05 19:48:35 +03:00
1999-02-12 18:35:27 +03:00
while ( dest < destend )
{
1999-02-15 08:31:52 +03:00
c = * ( src + + ) ;
1999-02-12 18:35:27 +03:00
if ( ( c = = 0 ) & & ( * src = = 0 ) )
{
1999-02-12 03:16:09 +03:00
break ;
}
1999-02-15 08:31:52 +03:00
* dest + + = c ;
1999-02-12 03:16:09 +03:00
src + + ;
1998-11-05 19:48:35 +03:00
}
1999-02-15 08:31:52 +03:00
* dest = 0 ;
1998-11-05 19:48:35 +03:00
}
1999-02-12 03:16:09 +03:00
1998-11-05 19:48:35 +03:00
/*******************************************************************
1999-02-12 03:16:09 +03:00
Put an ASCII string into a UNICODE array ( uint16 ' s ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-02-11 01:30:47 +03:00
1999-02-12 03:16:09 +03:00
void ascii_to_unistr ( uint16 * dest , const char * src , int maxlen )
1998-11-05 19:48:35 +03:00
{
1999-02-12 03:16:09 +03:00
uint16 * destend = dest + maxlen ;
register char c ;
1998-11-05 19:48:35 +03:00
1999-02-12 18:35:27 +03:00
while ( dest < destend )
{
1999-02-12 03:16:09 +03:00
c = * ( src + + ) ;
1999-02-12 18:35:27 +03:00
if ( c = = 0 )
{
1999-02-12 03:16:09 +03:00
break ;
}
1999-02-15 08:31:52 +03:00
* ( dest + + ) = ( uint16 ) c ;
1998-11-05 19:48:35 +03:00
}
1999-02-15 08:31:52 +03:00
* dest = 0 ;
1998-11-05 19:48:35 +03:00
}
1998-11-10 22:05:00 +03:00
/*******************************************************************
1999-02-12 03:16:09 +03:00
Pull an ASCII string out of a UNICODE array ( uint16 ' s ) .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-02-11 01:30:47 +03:00
1999-02-12 03:16:09 +03:00
void unistr_to_ascii ( char * dest , const uint16 * src , int len )
1998-11-10 22:05:00 +03:00
{
1999-02-12 03:16:09 +03:00
char * destend = dest + len ;
register uint16 c ;
1998-11-10 22:05:00 +03:00
1999-02-12 18:35:27 +03:00
while ( dest < destend )
{
1999-02-12 03:16:09 +03:00
c = * ( src + + ) ;
1999-02-12 18:35:27 +03:00
if ( c = = 0 )
{
1999-02-12 03:16:09 +03:00
break ;
}
1999-02-15 08:31:52 +03:00
* ( dest + + ) = ( char ) c ;
1998-11-10 22:05:00 +03:00
}
1999-02-15 08:31:52 +03:00
* dest = 0 ;
1998-11-10 22:05:00 +03:00
}
1999-02-12 03:16:09 +03:00
1998-11-10 22:05:00 +03:00
/*******************************************************************
1999-02-12 03:16:09 +03:00
Convert a UNISTR2 structure to an ASCII string
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-02-11 01:30:47 +03:00
1999-02-15 08:31:52 +03:00
void unistr2_to_ascii ( char * dest , const UNISTR2 * str , int maxlen )
1998-11-10 22:05:00 +03:00
{
1999-02-12 03:16:09 +03:00
char * destend ;
const uint16 * src ;
int len ;
register uint16 c ;
1998-11-10 22:05:00 +03:00
1999-02-12 03:16:09 +03:00
src = str - > buffer ;
1999-02-15 08:31:52 +03:00
len = MIN ( str - > uni_str_len , maxlen ) ;
1999-02-12 03:16:09 +03:00
destend = dest + len ;
1998-11-10 22:05:00 +03:00
1999-02-12 18:35:27 +03:00
while ( dest < destend )
{
1999-02-12 03:16:09 +03:00
c = * ( src + + ) ;
1999-02-12 18:35:27 +03:00
if ( c = = 0 )
{
1999-02-12 03:16:09 +03:00
break ;
1998-11-10 22:05:00 +03:00
}
1999-02-15 08:31:52 +03:00
* ( dest + + ) = ( char ) c ;
1998-11-10 22:05:00 +03:00
}
1999-02-15 08:31:52 +03:00
* dest = 0 ;
1998-11-10 22:05:00 +03:00
}
1999-02-11 21:50:13 +03:00
1999-02-12 03:16:09 +03:00
/*******************************************************************
Skip a UNICODE string in a little endian buffer .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-02-11 21:50:13 +03:00
1999-02-12 03:16:09 +03:00
char * skip_unibuf ( char * srcbuf , int len )
{
uint16 * src = ( uint16 * ) srcbuf ;
uint16 * srcend = src + len / 2 ;
1999-02-11 21:50:13 +03:00
1999-02-12 03:16:09 +03:00
while ( ( src < srcend ) & & ( * ( src + + ) ! = 0 ) )
1999-02-12 18:35:27 +03:00
{
}
1999-02-11 21:50:13 +03:00
1999-02-12 03:16:09 +03:00
return ( char * ) src ;
1999-02-11 21:50:13 +03:00
}
1999-02-12 03:16:09 +03:00
1998-11-05 19:48:35 +03:00
/*******************************************************************
1999-02-12 03:16:09 +03:00
UNICODE strcpy between buffers .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-02-11 01:30:47 +03:00
1999-02-12 03:16:09 +03:00
char * uni_strncpy ( char * destbuf , const char * srcbuf , int len )
1998-11-05 19:48:35 +03:00
{
1999-02-12 18:35:27 +03:00
const uint16 * src = ( const uint16 * ) srcbuf ;
1999-02-12 03:16:09 +03:00
uint16 * dest = ( uint16 * ) destbuf ;
uint16 * destend = dest + len / 2 ;
register uint16 c ;
1999-02-12 18:35:27 +03:00
while ( dest < destend )
{
1999-02-15 08:31:52 +03:00
c = * ( src + + ) ;
1999-02-12 18:35:27 +03:00
if ( c = = 0 )
{
1999-02-12 03:16:09 +03:00
break ;
1998-11-05 19:48:35 +03:00
}
1999-02-15 08:31:52 +03:00
* ( dest + + ) = c ;
1998-11-05 19:48:35 +03:00
}
1999-02-15 08:31:52 +03:00
* dest + + = 0 ;
1999-02-12 03:16:09 +03:00
return ( char * ) dest ;
1998-11-05 19:48:35 +03:00
}
1999-02-12 03:16:09 +03:00
1998-11-05 19:48:35 +03:00
/*******************************************************************
1999-02-12 03:16:09 +03:00
Return a number stored in a buffer
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-02-11 01:30:47 +03:00
1999-02-12 03:16:09 +03:00
uint32 buffer2_to_uint32 ( const BUFFER2 * str )
1998-11-05 19:48:35 +03:00
{
1999-02-12 03:16:09 +03:00
if ( str - > buf_len = = 4 )
{
1999-04-08 09:34:11 +04:00
const char * src = str - > buffer ;
1999-02-12 18:35:27 +03:00
return IVAL ( src , 0 ) ;
1999-02-12 03:16:09 +03:00
}
else
1998-11-05 19:48:35 +03:00
{
1999-02-12 03:16:09 +03:00
return 0 ;
1998-11-05 19:48:35 +03:00
}
}
/*******************************************************************
1999-02-12 03:16:09 +03:00
Convert a ' multi - string ' buffer to space - separated ASCII .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1999-02-11 01:30:47 +03:00
1999-02-15 08:31:52 +03:00
void buffer2_to_multistr ( char * dest , const BUFFER2 * str , int maxlen )
1998-11-05 19:48:35 +03:00
{
1999-02-12 03:16:09 +03:00
char * destend ;
1999-04-08 09:34:11 +04:00
const char * src ;
1999-02-12 03:16:09 +03:00
int len ;
register uint16 c ;
src = str - > buffer ;
1999-02-15 08:31:52 +03:00
len = MIN ( str - > buf_len / 2 , maxlen ) ;
destend = dest + len ;
1999-02-12 03:16:09 +03:00
1999-02-12 18:35:27 +03:00
while ( dest < destend )
{
1999-02-12 03:16:09 +03:00
c = * ( src + + ) ;
* ( dest + + ) = ( c = = 0 ) ? ' ' : ( char ) c ;
1999-04-08 09:34:11 +04:00
src + + ;
1998-11-05 19:48:35 +03:00
}
1999-02-12 03:16:09 +03:00
* dest = 0 ;
1998-11-05 19:48:35 +03:00
}