⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unicode_str.c

📁 隐藏目录
💻 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;    
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -