sdugeneral.c

来自「文件驱动加密,功能强大,可产生加密分区,支持AES,MD2,MD4,MD5MD2」· C语言 代码 · 共 990 行 · 第 1/2 页

C
990
字号

#include "SDUGeneral.h"
#include <Wingdi.h>
#include <commctrl.h>  // Only used for LVS_TYPEMASK
#include <stdio.h>
#include <wchar.h>

// =========================================================================
void SDUParsePath(
    const WCHAR* fullPath, 
    WCHAR** uncMachine,
    WCHAR** drive,
    WCHAR** path,
    WCHAR** filename,
    WCHAR** fileExtension
)
{
    WCHAR* pos;
    int i;
    int tmpLen;

    *uncMachine    = NULL;
    *drive         = NULL;
    *path          = NULL;
    *filename      = NULL;
    *fileExtension = NULL;


    // Identify UNC path/drive letter and path
    if (wcslen(fullPath) >= 3)
        {
        // Check if starts with "\\"; UNC path
        // e.g. \\<machine>\path\...
        if (wcsncmp(fullPath, TEXT("\\\\"), 2) == 0)
            {
            *uncMachine = (WCHAR*)&(fullPath[2]);

            // Seek following path...
            pos = &((*uncMachine)[1]); // Skip "\\"
            while (wcsncmp(pos, TEXT("\0"), 1) != 0)
                {
                if (
                    (wcsncmp(pos, TEXT("\\"), 1) == 0) ||
                    (wcsncmp(pos, TEXT("/"), 1) == 0)
                   )
                    {
                    *path = pos;
                    break;
                    }

                pos++;
                }
            }
        // Check if second char is ":"; DOS drive letter
        // e.g. C:\path\...
        else if (wcsncmp(&(fullPath[1]), TEXT(":"), 1) == 0)
            {
            *drive = (WCHAR*)fullPath;
            *path = (WCHAR*)&(fullPath[2]); // Move past drive letter and ":"
            }
        else 
        // Assume that the input is *just* a path
            {
            *path = (WCHAR*)fullPath;
            }
        }
    else if (wcslen(fullPath) >= 1)
    // Assume that the input is *just* a path
        {
        *path = (WCHAR*)fullPath;
        }


    // Process filename...
    if (*path != NULL)
        {
        // Seek backwards to pick up the filename extension and filename
        tmpLen = wcslen(*path);
        for (i = (tmpLen - 1); i >= 0; i--)
            {
            if (
                (wcsncmp(&((*path)[i]), TEXT("\\"), 1) == 0) ||
                (wcsncmp(&((*path)[i]), TEXT("/"), 1) == 0)
                )
                {
                // If the character we found wasn't the last character, we
                // do have a filename we can use
                if (i != (tmpLen - 1))
                    {
                    *filename = &((*path)[i+1]);
                    }

                break;
                }
            }
        }


    // Process filename extension...
    if (*filename != NULL)
        {
        // Seek backwards to pick up the filename extension and filename
        tmpLen = wcslen(*filename);
        for (i = (tmpLen - 1); i >= 0; i--)
            {
            if (wcsncmp(&((*filename)[i]), TEXT("."), 1) == 0)                 
                {
                // If the character we found wasn't the last character, we
                // do have a filename we can use
                if (i != (tmpLen - 1))
                    {
                    *fileExtension = &((*filename)[i+1]);
                    }

                break;
                }
            }
        }

}


// =========================================================================
void* SDUUnpack_LARGE_INTEGER(const void* ptr, LARGE_INTEGER* val)
{
    void *tmpPtr;
    int i;
    unsigned char x;

    tmpPtr = (void*)ptr;
    val->QuadPart = 0;
    for (i = 0; i < 8; i++)
        {
        tmpPtr = SDUUnpack_char(tmpPtr, (char*)&x);
        val->QuadPart = (val->QuadPart << 8) + x;
        }

    return tmpPtr;
}


// =========================================================================
void* SDUUnpack_DWORD(const void* ptr, DWORD* val)
{
    void *tmpPtr;
    int i;
    unsigned char x;

    tmpPtr = (void*)ptr;
    *val = 0;
    for (i = 0; i < 4; i++)
        {
        tmpPtr = SDUUnpack_char(tmpPtr, (char*)&x);
        *val = (*val << 8) + x;
        }

    return tmpPtr;
}


// =========================================================================
void* SDUUnpack_char(const void* ptr, char* val)
{
    *val = (*((char*)ptr));
    return ((char*)ptr + 1);
}


// =========================================================================
void* SDUPack_LARGE_INTEGER(const void* ptr, LARGE_INTEGER val)
{
    void *tmpPtr;

    tmpPtr = (void *)ptr;

    tmpPtr = SDUPack_DWORD(tmpPtr, val.HighPart);
    tmpPtr = SDUPack_DWORD(tmpPtr, val.LowPart);

    return tmpPtr;
}


// =========================================================================
void* SDUPack_DWORD(const void* ptr, DWORD val)
{
    void *tmpPtr;
    unsigned char x;

    tmpPtr = (void *)ptr;

    x = (unsigned char)((val >> 24) & 0xFF);
    tmpPtr = SDUPack_char(tmpPtr, (char)x);
    x = (unsigned char)((val >> 16) & 0xFF);
    tmpPtr = SDUPack_char(tmpPtr, (char)x);
    x = (unsigned char)((val >> 8) & 0xFF);
    tmpPtr = SDUPack_char(tmpPtr, (char)x);
    x = (unsigned char)(val & 0xFF);
    tmpPtr = SDUPack_char(tmpPtr, (char)x);

    return tmpPtr;
}


// =========================================================================
void* SDUPack_char(const void* ptr, char val)
{
    *((char*)ptr) = val;
    return ((char*)ptr + 1);
}


// =========================================================================
// Set filename to NULL to get version info on the current process
// Note: Caller must free(lpBuffer) after use
BOOL SDUGetVersionInfoShortFmtAlloc(
					   LPWSTR filename,
					   LPWSTR* lpBuffer
					  )
{
    BOOL retval = FALSE;
    const int VERSION_STR_SIZE = 256;

    *lpBuffer = (LPWSTR)malloc(VERSION_STR_SIZE * sizeof(*lpBuffer));
    if (SDUGetVersionInfoShortFmt(
                             filename,
                             *lpBuffer,
                             VERSION_STR_SIZE
                             ))
        {
        retval = TRUE;
        }
    else
        {
        free(*lpBuffer);
        *lpBuffer = NULL;
        }

    return retval;
}


// =========================================================================
// Set filename to NULL to get version info on the current process
// Note: nSize must be the max number of *characters*
BOOL SDUGetVersionInfoShortFmt(
					   LPWSTR filename,
					   LPWSTR lpBuffer,
					   DWORD nSize
					  )
{
    BOOL retval = FALSE;
    DWORD Version;

    if (SDUGetVersionInfoShort(filename, &Version))
        {
        if (_snwprintf(
                       lpBuffer, 
                       nSize,
                       TEXT("v%d.%0.2d.%0.4d"), 
                       (Version & 0xFF000000) / 0x00FF0000,
                       (Version & 0x00FF0000) / 0x0000FF00,
                       (Version & 0x0000FFFF)
                      ) >= 0)
/*
        if (_snwprintf_(
                       lpBuffer, 
                       nSize,
                       nSize,
                       TEXT("v%d.%0.2d.%0.4d"), 
                       (Version & 0xFF000000) / 0x00FF0000,
                       (Version & 0x00FF0000) / 0x0000FF00,
                       (Version & 0x0000FFFF)
                      ) >= 0)
*/
            {
            retval = TRUE;
            }
        }

    return retval;   
}


// =========================================================================
// Set filename to NULL to get version info on the current process
// Note: Caller must free(lpBuffer)  after use
BOOL SDUGetVersionInfoFmtAlloc(
					   LPWSTR filename,
					   LPWSTR* lpBuffer
					  )
{
    BOOL retval = FALSE;
    const int VERSION_STR_SIZE = 256;

    *lpBuffer = (LPWSTR)malloc(VERSION_STR_SIZE);
    if (SDUGetVersionInfoFmt(
                             filename,
                             *lpBuffer,
                             VERSION_STR_SIZE
                             ))
        {
        retval = TRUE;
        }
    else
        {
        free(*lpBuffer);
        *lpBuffer = NULL;
        }

    return retval;
}


// =========================================================================
// Set filename to NULL to get version info on the current process
BOOL SDUGetVersionInfoFmt(
					   LPWSTR filename,
					   LPWSTR lpBuffer,
					   DWORD nSize
					  )
{
    BOOL retval = FALSE;
    int majorVersion;
    int minorVersion;
    int revisionVersion;
    int buildVersion;

    if (SDUGetVersionInfo(
                          filename,
                          &majorVersion,
                          &minorVersion, 
                          &revisionVersion, 
                          &buildVersion
                         ))
        {
        if (_snwprintf(
                       lpBuffer, 
                       nSize,
                       TEXT("v%d.%0.2d.%0.2d.%0.4d"), 
                       majorVersion, 
                       minorVersion, 
                       revisionVersion, 
                       buildVersion
                      ) >= 0)
/*
        if (_snwprintf_s(
                       lpBuffer, 
                       nSize,
                       nSize,
                       TEXT("v%d.%0.2d.%0.2d.%0.4d"), 
                       majorVersion, 
                       minorVersion, 
                       revisionVersion, 
                       buildVersion
                      ) >= 0)
*/
            {
            retval = TRUE;
            }
        }

    return retval;   
}


// =========================================================================
// Set filename to NULL to get version info on the current process
// This sets Version to the version ID in the form: 0xAABBCCCC
// Where: 
//   AA - Major version
//   BB - Minor version
//   CC - Revision version
BOOL SDUGetVersionInfoShort(
					   LPWSTR Filename,
					   DWORD* Version
					  )
{
    BOOL retval;
    int majorVersion;
    int minorVersion;
    int revisionVersion;
    int buildVersion;

    retval = SDUGetVersionInfo(
                               Filename,
                               &majorVersion,
                               &minorVersion,
                               &revisionVersion,
                               &buildVersion
                              );
    if (retval)
        {
        *Version = (
                    (majorVersion << 24) +
                    (minorVersion << 16) +
                    revisionVersion
                   );
        }

    return retval;
}


// =========================================================================
// Set filename to NULL to get version info on the current process
BOOL SDUGetVersionInfo(
					   LPWSTR filename,
					   int* majorVersion,
					   int* minorVersion, 
					   int* revisionVersion, 
					   int* buildVersion
					  )
{
    BOOL retval = FALSE;
    int vsize;
    UINT puLen;
    DWORD dwHandle;
    void* pBlock;
    void* pVPointer = NULL;
    VS_FIXEDFILEINFO* tvs;
    // Twice MAX_PATH should be sufficient for most purposes...
    WCHAR bufFilename[sizeof(WCHAR) * (2 * MAX_PATH)];

    // Autodetermine filename if not passed in
    if (filename == NULL)
        {
        if (GetModuleFileName( 
                              NULL,
                              bufFilename, 
                              sizeof(bufFilename)
                             ) != 0)
            {
            filename = bufFilename;
            }
        }

    if (filename != NULL)
        {
        vsize = GetFileVersionInfoSize(filename, &dwHandle);
        if (vsize > 0) 
            {
            pBlock = malloc(vsize);

            if (GetFileVersionInfo(filename, dwHandle, vsize, pBlock))
                {
                if (VerQueryValue(pBlock, _T("\\"), &pVPointer, &puLen))
                    {
                    if (puLen > 0)
                        {
                        tvs = (VS_FIXEDFILEINFO*)pVPointer;
                        *majorVersion    = tvs->dwFileVersionMS >> 16;
                        *minorVersion    = tvs->dwFileVersionMS & 0xFFFF;
                        *revisionVersion = tvs->dwFileVersionLS >> 16;
                        *buildVersion    = tvs->dwFileVersionLS & 0xFFFF;
                        retval = TRUE;
                        }
                    }
                }

            free(pBlock);
            }
        }

    return retval;
}


// =========================================================================
void SDUTextSetBold(
    HWND hWnd,
    int ctrlID,
    BOOL UseBold
)
{
    HFONT hFont;
    LOGFONT lFont;

    // Set welcome title to bold
    hFont = (HFONT)SendMessage(
                GetDlgItem(hWnd, ctrlID),
                WM_GETFONT,
                0,
                0
               );
    if (hFont != NULL)
        {
        if (GetObject(hFont, sizeof(lFont), &lFont) != 0)
            {
            lFont.lfWeight = FW_BOLD;
            hFont = CreateFontIndirect(&lFont);
            SendMessage(
                        GetDlgItem(hWnd, ctrlID),
                        WM_SETFONT,
                        (WPARAM)hFont,
                        TRUE

⌨️ 快捷键说明

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