2007-09-15 02:03:41 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
2000-04-30 15:25:09 +04:00
filename matching routine
2004-10-02 13:14:43 +04:00
Copyright ( C ) Andrew Tridgell 1992 - 2004
2000-04-30 08:45:16 +04:00
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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2000-04-30 08:45:16 +04:00
( at your option ) any later version .
2007-09-15 02:03:41 +04:00
2000-04-30 08:45:16 +04:00
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 .
2007-09-15 02:03:41 +04:00
2000-04-30 08:45:16 +04:00
You should have received a copy of the GNU General Public License
2007-09-15 02:03:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2004-10-02 13:14:43 +04:00
*/
2000-04-30 08:45:16 +04:00
2000-04-30 15:25:09 +04:00
/*
This module was originally based on fnmatch . c copyright by the Free
2004-10-02 13:14:43 +04:00
Software Foundation . It bears little ( if any ) resemblence to that
code now
2007-09-15 02:03:41 +04:00
*/
2000-04-30 15:25:09 +04:00
2000-04-30 08:45:16 +04:00
# include "includes.h"
2004-10-02 13:14:43 +04:00
static int null_match ( const smb_ucs2_t * p )
2001-04-17 09:41:07 +04:00
{
2004-10-02 13:14:43 +04:00
for ( ; * p ; p + + ) {
if ( * p ! = UCS2_CHAR ( ' * ' ) & &
* p ! = UCS2_CHAR ( ' < ' ) & &
* p ! = UCS2_CHAR ( ' " ' ) & &
* p ! = UCS2_CHAR ( ' > ' ) ) return - 1 ;
}
return 0 ;
}
/*
the max_n structure is purely for efficiency , it doesn ' t contribute
to the matching algorithm except by ensuring that the algorithm does
not grow exponentially
*/
struct max_n {
const smb_ucs2_t * predot ;
const smb_ucs2_t * postdot ;
} ;
2001-04-17 09:41:07 +04:00
2004-10-02 13:14:43 +04:00
/*
p and n are the pattern and string being matched . The max_n array is
an optimisation only . The ldot pointer is NULL if the string does
not contain a ' . ' , otherwise it points at the last dot in ' n ' .
*/
2007-09-15 02:03:41 +04:00
static int ms_fnmatch_core ( const smb_ucs2_t * p , const smb_ucs2_t * n ,
2004-10-05 07:26:02 +04:00
struct max_n * max_n , const smb_ucs2_t * ldot ,
2007-10-19 04:40:25 +04:00
bool is_case_sensitive )
2004-10-02 13:14:43 +04:00
{
smb_ucs2_t c ;
int i ;
2001-04-17 09:41:07 +04:00
while ( ( c = * p + + ) ) {
switch ( c ) {
2004-10-02 13:14:43 +04:00
/* a '*' matches zero or more characters of any type */
case UCS2_CHAR ( ' * ' ) :
if ( max_n - > predot & & max_n - > predot < = n ) {
return null_match ( p ) ;
}
for ( i = 0 ; n [ i ] ; i + + ) {
2004-10-05 07:26:02 +04:00
if ( ms_fnmatch_core ( p , n + i , max_n + 1 , ldot , is_case_sensitive ) = = 0 ) {
2004-10-02 13:14:43 +04:00
return 0 ;
}
}
if ( ! max_n - > predot | | max_n - > predot > n ) max_n - > predot = n ;
return null_match ( p ) ;
/* a '<' matches zero or more characters of
any type , but stops matching at the last
' . ' in the string . */
case UCS2_CHAR ( ' < ' ) :
if ( max_n - > predot & & max_n - > predot < = n ) {
return null_match ( p ) ;
}
if ( max_n - > postdot & & max_n - > postdot < = n & & n < = ldot ) {
return - 1 ;
}
for ( i = 0 ; n [ i ] ; i + + ) {
2004-10-05 07:26:02 +04:00
if ( ms_fnmatch_core ( p , n + i , max_n + 1 , ldot , is_case_sensitive ) = = 0 ) return 0 ;
2004-10-02 13:14:43 +04:00
if ( n + i = = ldot ) {
2004-10-05 07:26:02 +04:00
if ( ms_fnmatch_core ( p , n + i + 1 , max_n + 1 , ldot , is_case_sensitive ) = = 0 ) return 0 ;
2004-10-02 13:14:43 +04:00
if ( ! max_n - > postdot | | max_n - > postdot > n ) max_n - > postdot = n ;
return - 1 ;
}
}
if ( ! max_n - > predot | | max_n - > predot > n ) max_n - > predot = n ;
return null_match ( p ) ;
2001-04-17 09:41:07 +04:00
2004-10-02 13:14:43 +04:00
/* a '?' matches any single character */
2001-07-04 11:15:53 +04:00
case UCS2_CHAR ( ' ? ' ) :
2004-10-02 13:14:43 +04:00
if ( ! * n ) {
return - 1 ;
}
2001-04-17 09:41:07 +04:00
n + + ;
break ;
2004-10-02 13:14:43 +04:00
/* a '?' matches any single character */
2001-07-04 11:15:53 +04:00
case UCS2_CHAR ( ' > ' ) :
if ( n [ 0 ] = = UCS2_CHAR ( ' . ' ) ) {
2004-10-02 13:14:43 +04:00
if ( ! n [ 1 ] & & null_match ( p ) = = 0 ) {
return 0 ;
2001-04-17 09:41:07 +04:00
}
2004-10-02 13:14:43 +04:00
break ;
2001-04-17 09:41:07 +04:00
}
2004-10-02 13:14:43 +04:00
if ( ! * n ) return null_match ( p ) ;
n + + ;
2001-04-17 09:41:07 +04:00
break ;
2001-07-04 11:15:53 +04:00
case UCS2_CHAR ( ' " ' ) :
2004-10-02 13:14:43 +04:00
if ( * n = = 0 & & null_match ( p ) = = 0 ) {
return 0 ;
}
if ( * n ! = UCS2_CHAR ( ' . ' ) ) return - 1 ;
2001-04-17 09:41:07 +04:00
n + + ;
break ;
default :
2004-10-05 07:26:02 +04:00
if ( c ! = * n ) {
if ( is_case_sensitive ) {
return - 1 ;
}
2011-07-20 00:35:45 +04:00
if ( toupper_w ( c ) ! = toupper_w ( * n ) ) {
2004-10-05 07:26:02 +04:00
return - 1 ;
}
2003-08-12 08:25:26 +04:00
}
2001-04-17 09:41:07 +04:00
n + + ;
2004-10-02 13:14:43 +04:00
break ;
2001-04-17 09:41:07 +04:00
}
}
2007-09-15 02:03:41 +04:00
2004-10-02 13:14:43 +04:00
if ( ! * n ) {
return 0 ;
}
2007-09-15 02:03:41 +04:00
2001-04-17 09:41:07 +04:00
return - 1 ;
}
2007-10-19 04:40:25 +04:00
int ms_fnmatch ( const char * pattern , const char * string , bool translate_pattern ,
bool is_case_sensitive )
2001-04-17 09:41:07 +04:00
{
2007-09-15 02:03:41 +04:00
smb_ucs2_t * p = NULL ;
smb_ucs2_t * s = NULL ;
2004-10-02 13:14:43 +04:00
int ret , count , i ;
struct max_n * max_n = NULL ;
2008-01-09 01:18:03 +03:00
struct max_n * max_n_free = NULL ;
struct max_n one_max_n ;
2008-04-30 01:36:24 +04:00
size_t converted_size ;
2001-04-17 09:41:07 +04:00
2007-09-15 02:03:41 +04:00
if ( ISDOTDOT ( string ) ) {
2004-10-02 13:14:43 +04:00
string = " . " ;
2001-04-17 09:41:07 +04:00
}
2004-10-02 13:14:43 +04:00
if ( strpbrk ( pattern , " <>*? \" " ) = = NULL ) {
/* this is not just an optmisation - it is essential
for LANMAN1 correctness */
2004-10-05 07:26:02 +04:00
if ( is_case_sensitive ) {
return strcmp ( pattern , string ) ;
} else {
2011-05-13 22:21:30 +04:00
return strcasecmp_m ( pattern , string ) ;
2004-10-05 07:26:02 +04:00
}
2001-04-17 09:41:07 +04:00
}
2000-04-30 08:45:16 +04:00
2009-03-18 10:05:17 +03:00
if ( ! push_ucs2_talloc ( talloc_tos ( ) , & p , pattern , & converted_size ) ) {
2004-12-02 03:55:47 +03:00
return - 1 ;
}
2009-03-18 10:05:17 +03:00
if ( ! push_ucs2_talloc ( talloc_tos ( ) , & s , string , & converted_size ) ) {
TALLOC_FREE ( p ) ;
2004-12-02 03:55:47 +03:00
return - 1 ;
}
2000-04-30 08:45:16 +04:00
2005-03-24 22:33:02 +03:00
if ( translate_pattern ) {
2004-10-02 13:14:43 +04:00
/*
for older negotiated protocols it is possible to
translate the pattern to produce a " new style "
pattern that exactly matches w2k behaviour
*/
for ( i = 0 ; p [ i ] ; i + + ) {
if ( p [ i ] = = UCS2_CHAR ( ' ? ' ) ) {
p [ i ] = UCS2_CHAR ( ' > ' ) ;
2007-09-15 02:03:41 +04:00
} else if ( p [ i ] = = UCS2_CHAR ( ' . ' ) & &
( p [ i + 1 ] = = UCS2_CHAR ( ' ? ' ) | |
2004-10-02 13:14:43 +04:00
p [ i + 1 ] = = UCS2_CHAR ( ' * ' ) | |
p [ i + 1 ] = = 0 ) ) {
p [ i ] = UCS2_CHAR ( ' " ' ) ;
} else if ( p [ i ] = = UCS2_CHAR ( ' * ' ) & & p [ i + 1 ] = = UCS2_CHAR ( ' . ' ) ) {
p [ i ] = UCS2_CHAR ( ' < ' ) ;
2003-08-12 08:25:26 +04:00
}
2000-04-30 08:45:16 +04:00
}
}
2004-10-02 13:14:43 +04:00
for ( count = i = 0 ; p [ i ] ; i + + ) {
if ( p [ i ] = = UCS2_CHAR ( ' * ' ) | | p [ i ] = = UCS2_CHAR ( ' < ' ) ) count + + ;
2003-08-12 08:25:26 +04:00
}
2004-10-02 13:14:43 +04:00
if ( count ! = 0 ) {
2008-01-09 01:18:03 +03:00
if ( count = = 1 ) {
/*
* We ' re doing this a LOT , so save the effort to allocate
*/
ZERO_STRUCT ( one_max_n ) ;
max_n = & one_max_n ;
}
else {
max_n = SMB_CALLOC_ARRAY ( struct max_n , count ) ;
if ( ! max_n ) {
2009-03-18 10:05:17 +03:00
TALLOC_FREE ( p ) ;
TALLOC_FREE ( s ) ;
2008-01-09 01:18:03 +03:00
return - 1 ;
}
max_n_free = max_n ;
2004-10-02 13:14:43 +04:00
}
2003-08-12 08:25:26 +04:00
}
2000-04-30 08:45:16 +04:00
2004-10-05 07:26:02 +04:00
ret = ms_fnmatch_core ( p , s , max_n , strrchr_w ( s , UCS2_CHAR ( ' . ' ) ) , is_case_sensitive ) ;
2004-10-02 13:14:43 +04:00
2008-01-09 01:18:03 +03:00
SAFE_FREE ( max_n_free ) ;
2009-03-18 10:05:17 +03:00
TALLOC_FREE ( p ) ;
TALLOC_FREE ( s ) ;
2001-08-20 09:15:26 +04:00
return ret ;
}