📄 zfsvalidate.c
字号:
/*
* File : ZFSValidate.c
* Description: This file contains the implementation of support functions
* Author : Mahadev K C
* Created on : 30-APR-2003
*
* Copyright 2004 ZiLOG Inc. ALL RIGHTS RESERVED.
*
* This file contains unpublished confidential and proprietary information
* of ZiLOG, Inc.
* NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED
* IN ANY FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC.
* This is not a license and no use of any kind of this work is authorized
* in the absence of a written license granted by ZiLOG, Inc. in ZiLOG's
* sole discretion
*/
/* Revision history
* CR#6091, MKC, 05/13/2005,
* The path is not validated properly, if the directory name starts with a
* number. Found during Automation of FileSystem testcases API testing
*/
#include "glextern.h"
#include <ctype.h>
//Function: AllocateOR
//Description: This function will allocate a OPEN_REC structure to the caller. returns
// NULL in case all OPEN_REC are allocated.
PZFS_OPEN_REC_t AllocateOR( void )
{
PZFS_OPEN_REC_t por = &g_zfs_or[0];
UINT cnt = 0 ;
while( cnt < ( UINT ) g_max_or_entries )
{
if( por->status == ZFS_OR_INVALID_MAGIC_NUM )
{
memset( por, 0x00, sizeof( ZFS_OPEN_REC_t ) ) ;
por->status = ZFS_OR_MAGIC_NUM ;
return por ;
}
por++ ;
cnt++;
}
return ( PZFS_OPEN_REC_t ) NULL ;
}
//Function: ValidatePath
//Description: This function will validate the path and returns ZFS_TRUE if success, ZFS_FALSE if failure
UINT8 ValidatePath( INT8 *path )
{
UINT len = strlen( (const INT8 *)path ) ;
UINT bgn = 0 ;
UINT tkn_len = 0 ;
UINT cur_off = 0 ;
UINT i = 0 ;
// UINT8 begin_rel_path_pass = ZFS_FALSE ;
INT8 *ptr = path ;
if( path == NULL || len == 0 )
return ZFS_FALSE ;
while( len != 0 )
{
// check whether you got the token or not
// if( *ptr == '\\' ) // to support '\'
if( *ptr == '/' ) // to support '/'
{
tkn_len = cur_off - bgn ;
// you have got a token, check whether it is beginning of the token or not.
if( bgn == 0 && tkn_len == 0 )
{
// It is correct, just update the pointers and continue.
ptr++;
cur_off = 1 ;
bgn = 1 ;
len-- ;
continue ;
}
if( tkn_len == 0 )
{
// it is an error, return false
return ZFS_FALSE ;
}
// token length is > 0
// CR#6091
// check whether it contains first character as not a alphabet or an _ i.e a number
if( isdigit(*(path+bgn)) )
return ZFS_FALSE ;
// check if token_length is 1 and it is '.'
if( tkn_len == 1 )
{
// any character is valid here, do not do any thing, just update
// pointers
ptr++;
cur_off++ ;
bgn = cur_off ;
len-- ;
continue ;
}
if( tkn_len == 2 )
{
if( *(path+bgn) == '.' )
{
if( (INT8)*(path+bgn+1) != '.' )
return ZFS_FALSE ;
}
if( *(path+bgn) != '.' )
{
// if( *(path+bgn+1) == '.' )
// return ZFS_FALSE ;
}
ptr++ ;
len-- ;
cur_off++;
bgn=cur_off ;
continue ;
}
if( tkn_len > ZFS_MAX_VOL_NAME_LEN )
{
return ZFS_FALSE ;
}
// now token length is more than 2, just check whether it contains a .. and a .
// if so return an error.
// check whether it contains first character as '.'
if( *(path+bgn) == '.' )
return ZFS_FALSE ;
for(i=0; i < tkn_len ; i++ )
{
if( (INT8)*(path+bgn+i) == '.' && (INT8)(*path+bgn+i+1) == '.' )
return ZFS_FALSE ;
}
ptr ++;
cur_off ++ ;
bgn = cur_off ;
len-- ;
continue ;
}
else if( *ptr == ':' )
{
tkn_len = cur_off - bgn ;
if( bgn != 0 )
{
return ZFS_FALSE ;
}
for(i=0; i < tkn_len ; i++ )
{
if( (INT8)*(path+bgn+i) == '.' )
return ZFS_FALSE ;
}
// if( *(ptr+1) != '\\') // to support '\'
if( *(ptr+1) != '/') // to support '/'
return ZFS_FALSE ;
ptr+=2 ;
cur_off+=2;
len-=2;
bgn=cur_off ;
}
else
{
// if any other character it is invliad, only alnum, _, . is valid
if( !isalnum(*ptr) && *ptr != '.' && *ptr != '_' )
return ZFS_FALSE ;
ptr++;
cur_off++;
len--;
}
}
// check for the last stuff.
tkn_len= cur_off - bgn ;
if( tkn_len == 0 )
{
return ZFS_TRUE ;
}
// CR#6091
// check whether it contains first character as not a alphabet or an _ i.e a number
if( isdigit(*(path+bgn)) )
return ZFS_FALSE ;
if( tkn_len == 1 )
{
// any character is fine, return success,
return ZFS_TRUE ;
}
if( tkn_len == 2 )
{
if( *(path+bgn) == '.' )
{
if( (INT8)*(path+bgn+1) != '.' )
return ZFS_FALSE ;
}
if( *(path+bgn) != '.' )
{
// if( *(path+bgn+1) == '.' )
// return ZFS_FALSE ;
}
return ZFS_TRUE ;
}
if( tkn_len > ZFS_MAX_VOL_NAME_LEN )
{
return ZFS_FALSE ;
}
// now token length is more than 2, just check whether it contains a .. and a .
// if so return an error.
// check whether the name part starts with '.', it is an error
if( *(path+bgn) == '.' )
return ZFS_FALSE ;
// in file name or directory name
//a.b.c b.c abce. are correct
// .ab ab..c a.c..b is not correct
for(i=0; i < tkn_len ; i++ )
{
if( (INT8)*(path+bgn+i) == '.' && (INT8)*(path+bgn+i+1) == '.')
return ZFS_FALSE ;
}
return ZFS_TRUE ;
}
//Function: ValidateFileDirName
//Description: This function will validate the file or directory name and returns ZFS_TRUE if success, ZFS_FALSE if failure
UINT8 ValidateFileDirName( INT8 *fd_name )
{
UINT len = strlen((const INT8 *)fd_name) ;
if(len == 0 || len > ZFS_MAX_FILE_NAME_SIZE || fd_name == NULL )
return ZFS_FALSE ;
if(!isalpha(*fd_name))
{
if( *fd_name != '_' )
return ZFS_FALSE ;
}
len--;
fd_name++;
//go into the loop to find the characters.
while( len != 0 )
{
if( !isalnum(*fd_name) )
{
if( *fd_name == '.' && *(fd_name+1) == '.' )
return ZFS_FALSE ;
if( *fd_name != '.' && *fd_name != '_' )
return ZFS_FALSE ;
}
len--;
fd_name++;
}
return ZFS_TRUE ;
}
//Function: InitializeHeader
//Description: This function will fill the memory locations with 0xFFs as required by flash.
void InitializeHeader( void *addr, UINT len )
{
// fillup the header with 0xFFFFFF
memset( addr, 0xFF, len ) ;
return ;
}
INT GetTB( PZFS_VOL_INFO_t vol_info )
{
UINT num_blks = vol_info->pcfg->vol_blks ;
PZFS_BLK_INFO_t pblk_hdr = vol_info->blk_info;
INT i = 0 ;
for( i = 0 ; i < num_blks ; )
{
if( ( ( ~(pblk_hdr->blk_hdr.sec_hdr.sec_type) & ( ZFS_SEC_TYPE_TB | ZFS_SEC_TYPE_NEW_AB )) ==
ZFS_SEC_TYPE_TB ) &&
pblk_hdr->blk_hdr.zfs_magic_num == ZFS_BLOCK_MAGIC_NUM )
return i ;
i++ ;
pblk_hdr++;
}
return -1 ;
}
PZFS_BLK_INFO_t GetTBPtr( PZFS_VOL_INFO_t vol_info )
{
UINT num_blks = vol_info->pcfg->vol_blks ;
PZFS_BLK_INFO_t pblk_hdr = vol_info->blk_info;
while( num_blks > 0 )
{
if( ( ( ~(pblk_hdr->blk_hdr.sec_hdr.sec_type) & ( ZFS_SEC_TYPE_TB | ZFS_SEC_TYPE_NEW_AB ) ) == ZFS_SEC_TYPE_TB ) &&
pblk_hdr->blk_hdr.zfs_magic_num == ZFS_BLOCK_MAGIC_NUM )
return pblk_hdr ;
num_blks-- ;
pblk_hdr++;
}
return 0 ;
}
PZFS_BLK_INFO_t GetABForGC( PZFS_VOL_INFO_t vol_info )
{
UINT num_blks = vol_info->pcfg->vol_blks ;
PZFS_BLK_INFO_t pblk_hdr = vol_info->blk_info;
UINT max_dirty_secs = 0 ;
PZFS_BLK_INFO_t pblk_for_gc ;
// get the block which has highest dirty sectors
while( num_blks > 0 )
{
if( ( ( (~((UINT8)pblk_hdr->blk_hdr.sec_hdr.sec_type)) & ZFS_SEC_TYPE_TB ) == ZFS_SEC_TYPE_TB )
&& ( ( (~((UINT8)pblk_hdr->blk_hdr.sec_hdr.sec_type)) & ZFS_SEC_TYPE_NEW_AB ) != ZFS_SEC_TYPE_NEW_AB ) )
{
// do nothing
}
else
{
if( pblk_hdr->dirty_sec_count > max_dirty_secs )
{
max_dirty_secs = pblk_hdr->dirty_sec_count ;
pblk_for_gc = pblk_hdr ;
}
}
num_blks-- ;
pblk_hdr++;
}
return pblk_for_gc ;
}
UINT8 IsVolumeValid( PZFS_VOL_INFO_t pvol_info )
{
return pvol_info->is_valid ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -