文件過濾驅動--一個Unicode操作的Lib

來自: bbs.driverdevelop.com  作者:楚狂人 Mail: [email protected]

先介紹一下,一個Unicode String 操作的Lib

// unicode.c

#include "unicode_strdef.h"
#include "unicode_str.h"
#include <string.h>
#include <assert.h>
#include <stdio.h>

//
// CopyRight: Wanfustudio (C) 2007
// Distribute: free use and copy ,modify
//

 

ULONG
GetWideStrLen(
  const WCHAR* str 
  )
//==============================
// Get a NULL terminated unicode
// string's length
//============================== 
 
{
    ULONG len = 0;
   
    if(NULL == str)
    {   
        return 0;
    }
   
    while(*(str++) != L'/0')
    {   
        ++len;
    }
   
    return len;
}

 

VOID
CopyWideStr(
  WCHAR* dst,
  const WCHAR* src
  )
//==============================
// Copy a NULL terminated unicode
// string to another one
//============================== 
{
    if(NULL == dst || NULL == src)
    {       
        return;   
    }
   
    while( ( *(dst++)= *(src++) ) != L'/0' )
    {   
        ; // do nothing   
    }
}

 

PVOID
AllocStrWithWideStr(
  const WCHAR* str
  )
//==============================
// Allocate a String
//
//============================== 
{
     USTR_BLOCK *block =
         (USTR_BLOCK *)ExAllocatePool(NonPagedPool, sizeof(USTR_BLOCK));
     WCHAR* buf = NULL;
     WCHAR buflen = Max(wcslen(str) + WIDE_STR_APD_LEN, WIDE_STR_BUF_LEN);
    
     if(NULL == block)
     {        
         return NULL;
     }
    
     buf = ExAllocatePool(NonPagedPool, buflen * sizeof(WCHAR) );
    
     if( NULL == buf)
     {    
         ExFreePool(block);
         return NULL;
     }
    
     wcscpy(buf, str);
     RtlInitUnicodeString(&block->ustr, buf);
     block->buf = buf;
     block->buflen = buflen;
    
     return (PVOID)block;
}

 

 

PVOID
AllocStrWithUniStr(
  const PUNICODE_STRING ustr 
  )
//==============================
// Allocate a string with
// unicode string
//==============================  
{
     USTR_BLOCK *block =
         (USTR_BLOCK *)ExAllocatePool(NonPagedPool, sizeof(USTR_BLOCK));
     WCHAR* buf = NULL;
     WCHAR buflen = Max(ustr->Length + WIDE_STR_APD_LEN, WIDE_STR_BUF_LEN);
    
     if(NULL == block)
     {        
         return NULL;
     }
    
     buf = ExAllocatePool(NonPagedPool, buflen * sizeof( WCHAR) );
    
     if( NULL == buf)
     {    
         ExFreePool(block);
         return NULL;
     }
    
     block->buf = buf;
     block->buflen = buflen;
     RtlInitEmptyUnicodeString(&block->ustr, block->buf, buflen * sizeof(WCHAR));
    
     RtlCopyUnicodeString(&block->ustr, ustr);
     block->buf[ustr->Length] = L'/0';
    
     return (PVOID)block;    
}

 


VOID
FreeStr(
  PVOID str
  )
//==============================
// Free String 's Buffer
//
//==============================  
{
    USTR_BLOCK *block = (USTR_BLOCK *)str;
   
    if( NULL == str)
    {   
        return;
    }
   
    if(block->buf)
    {   
        ExFreePool(block->buf);
    }
   
    ExFreePool(block);   
}

 

BOOLEAN
SetStrWithWideStr(
  PVOID str,
  const WCHAR* content
  )
//==============================
// Set String With WideByte String
// USTR_BLOCK TYPE
//==============================  
{
    USTR_BLOCK *block = (USTR_BLOCK *)str;
    WCHAR *buf = NULL;
    ULONG buflen = GetWideStrLen(content) + WIDE_STR_APD_LEN;
   
    if(block->buflen < buflen)
    {   
        buf = ExAllocatePool(NonPagedPool, buflen * sizeof(WCHAR) );
       
        if(NULL == buf)
        {           
            return FALSE;
        }
       
        if(block->buf)
        {       
            ExFreePool(block->buf);
            block->buf = NULL;
        }
       
        block->buf = buf;
        block->buflen = buflen;
    }
   
    RtlZeroMemory(&block->ustr, sizeof(block->ustr) );
    wcscpy(block->buf, content);
   
    RtlInitUnicodeString(&block->ustr, block->buf);
   
    return TRUE;
}

 


BOOLEAN
SetStrWithUniStr(
  PVOID str,
  const PUNICODE_STRING content
  )
//==============================
// Set String With Unicode String
// USTR_BLOCK TYPE
//==============================   
{
    USTR_BLOCK *block = (USTR_BLOCK*)str;
    WCHAR* buf = NULL;
    ULONG buflen = content->Length + WIDE_STR_APD_LEN;
   
    if(block->buflen < buflen ){
   
        buf = ExAllocatePool(NonPagedPool, buflen * sizeof(WCHAR) );
       
        if( NULL == buf)
        {           
            return FALSE;
        }
       
        if(block->buf)
        {       
            ExFreePool(block->buf);
            block->buf = NULL;
        }
       
        block->buf = buf;
        block->buflen = buflen ;
    }
   
    RtlZeroMemory(&block->ustr, sizeof(block->ustr) );
    RtlInitEmptyUnicodeString(&block->ustr, block->buf, buflen * sizeof(WCHAR));
   
    RtlCopyUnicodeString(&block->ustr, content);
   
    return TRUE;
}

 


VOID
CopyStr(
  PVOID dst,
  const PVOID src 
  )
//==============================
// Copy A String To Dst. String
// USTR_BLOCK TYPE
//==============================   
{
    USTR_BLOCK *block_dst = (USTR_BLOCK*)dst;
    USTR_BLOCK *block_src = (USTR_BLOCK*)src;
   
    SetStrWithUniStr(block_dst, &block_src->ustr);
}

 

VOID
AppendStrWithInt(
  int num
  )
//==============================
// Append Int-Type  to String
// USTR_BLOCK TYPE
//==============================   
{
    assert(0);
    //implement later
}

 

BOOLEAN
AppendStrWithWideStr(
  PVOID dst,
  const WCHAR* str
  )
//==============================
// Append WideBytes String to String
// USTR_BLOCK TYPE
//==============================   
{
    USTR_BLOCK *block_dst = (USTR_BLOCK*)dst;
    WCHAR *buf = NULL;
    ULONG buflen = wcslen(str) + block_dst->ustr.Length + WIDE_STR_APD_LEN;
   
    if(block_dst->buflen < buflen)
    {   
        buf = ExAllocatePool(NonPagedPool, buflen * sizeof(WCHAR) );
       
        if( NULL == buf )
        {           
            return FALSE;
        }
       
        RtlZeroMemory(buf, buflen * sizeof(WCHAR) );
       
        if(block_dst->buf)
        {           
            wcscpy(buf, block_dst->buf);
            ExFreePool(block_dst->buf);
           
            block_dst->buf = buf;
            block_dst->buflen = buflen;
        }       
    }
   
    wcscat( block_dst->buf, str);
   
    RtlZeroMemory(&block_dst->ustr, sizeof(block_dst->ustr) );
    RtlInitUnicodeString(&block_dst->ustr, block_dst->buf);
   
    return TRUE;
}

 


BOOLEAN
AppendStrWithUniStr(
  PVOID dst,
  PUNICODE_STRING str
  )
//==============================
// Append Uniocde String to String
// USTR_BLOCK TYPE
//============================== 
{
    USTR_BLOCK * block_dst = (USTR_BLOCK*)dst;
    WCHAR *buf = NULL;
    ULONG buflen = wcslen(block_dst->buf) + str->Length + WIDE_STR_APD_LEN;
    ULONG oldstrlen = wcslen(block_dst->buf);
   
    if(str->Length == 0)
    {       
        return FALSE;
    }
   
    if(block_dst->buflen < buflen)
    {   
        buf = ExAllocatePool(NonPagedPool, buflen * sizeof(WCHAR) );
      
        if(NULL == buf)
        {       
            return FALSE;
        }
       
        RtlZeroMemory(buf, buflen * sizeof(WCHAR) );
       
        if(block_dst->buf)
        {           
            wcscpy(buf, block_dst->buf);
            ExFreePool(block_dst->buf);
           
            block_dst->buf = buf;
            block_dst->buflen = buflen;
        }       
    }
   
    wcscat(block_dst->buf, str->Buffer);
   
    RtlZeroMemory( &block_dst->ustr, sizeof(block_dst->ustr) );
    RtlInitUnicodeString(&block_dst->ustr, block_dst->buf);
   
    return TRUE;
}

 


BOOLEAN
AppendStrWithStr(
  PVOID dst,
  PVOID str
  )
//==============================
// Append A String to String
// USTR_BLOCK TYPE
//==============================   
{
    USTR_BLOCK *block_str = (USTR_BLOCK*)str;
    return AppendStrWithUniStr(dst, &block_str->ustr);
}

 


WCHAR*
GetStrBuf(
  PVOID str
  )
//==============================
// Get a String's buffer
// USTR_BLOCK TYPE
//============================== 
{
    USTR_BLOCK* block = (USTR_BLOCK*)str;
    return block->buf;
}

 


PUNICODE_STRING
GetStrUniStr(
  PVOID str
  )
//==============================
// Get a String's Unicode string
// USTR_BLOCK TYPE
//============================== 
{
    USTR_BLOCK* block = (USTR_BLOCK*)str;
    return &block->ustr;
}

 


ULONG
GetStrBufLen(
  PVOID str
  )
//==============================
// Get a String's buffer length
// USTR_BLOCK TYPE
//==============================   
{
    USTR_BLOCK* block = (USTR_BLOCK*)str;
    return block->ustr.Length;   
}

 

 

WCHAR*
GetUniStrBuf(
  IN const PUNICODE_STRING str
  )
//==============================
// Get a Unicode String Buffer
// UNICODE_STRING
//============================== 
{
    return str->Buffer;
}

 


USHORT
GetUniStrLen(
  IN const PUNICODE_STRING str
  )
//==============================
// Get a Unicode String Length
// UNICODE_STRING
//============================== 
{
    return str->Length;
}

 

//unicode.h

#ifndef _MY_UNICODE_STR_LIB_WRITE_BY_LWF_07_07_24_INC_H
#define _MY_UNICODE_STR_LIB_WRITE_BY_LWF_07_07_24_INC_H

//
// CopyRight: Wanfustudio (C) 2007
// Distribute: free use and copy ,modify
//
 
ULONG
GetWideStrLen(
  const WCHAR* str 
  );

VOID
CopyWideStr(
  WCHAR* dst,
  const WCHAR* src
  );

PVOID
AllocStrWithWideStr(
  const WCHAR* str
  );
   
PVOID
AllocStrWithUniStr(
  const PUNICODE_STRING ustr 
  );
 
VOID
FreeStr(
  PVOID str
  );

BOOLEAN
SetStrWithWideStr(
  PVOID str,
  const WCHAR* content
  );   

BOOLEAN
SetStrWithUniStr(
  PVOID str,
  const PUNICODE_STRING content
  );

VOID
CopyStr(
  PVOID dst,
  const PVOID src 
  );

VOID
AppendStrWithInt(
  int num
  );

BOOLEAN
AppendStrWithWideStr(
  PVOID dst,
  const WCHAR* str
  );

BOOLEAN
AppendStrWithUniStr(
  PVOID dst,
  PUNICODE_STRING str
  );
 
BOOLEAN
AppendStrWithStr(
  PVOID dst,
  PVOID str
  );
 
WCHAR*
GetStrBuf(
  PVOID str
  );
 
PUNICODE_STRING
GetStrUniStr(
  PVOID str
  );
 
ULONG
GetStrBufLen(
  PVOID str
  );  
 
WCHAR*
GetUniStrBuf(
  IN const PUNICODE_STRING str
  ); 
 
USHORT
GetUniStrLen(
  IN const PUNICODE_STRING str
  );
            
#endif

 

 

 

//unicode_def.h

#ifndef _MY_UNICODE_STR_LIB_WRITE_BY_LWF_07_07_24_DEF_H
#define _MY_UNICODE_STR_LIB_WRITE_BY_LWF_07_07_24_DEF_H

#include <ntifs.h>

#define WIDE_STR_BUF_LEN 64
#define WIDE_STR_APD_LEN 8

#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))


typedef struct UNISTR_BLOCK
{
    UNICODE_STRING ustr;
    WCHAR* buf;
    ULONG buflen;
}UNICODE_STRING_BLOCK,USTR_BLOCK;


#endif

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章