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

📄 provisionsetting.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#include "ProvisionSetting.hpp"
#include "StoreBase.hpp"
#include "CommonFunctions.hpp"
#include <wincrypt.h>
#include <msxml2.h>

HRESULT
EncryptData(
    BYTE*   pInputBuffer, 
    DWORD   InputBufferSize, 
    BYTE**  ppOutputBuffer, 
    DWORD*  pOutputBufferSize
    )
{
    if (pInputBuffer == NULL ||
        InputBufferSize <= 0 ||
        ppOutputBuffer == NULL ||
        pOutputBufferSize == NULL)
    {
        return E_INVALIDARG; 
    }

    *ppOutputBuffer     = NULL; 
    *pOutputBufferSize  = 0; 
    
    DATA_BLOB   DataIn  = {InputBufferSize, pInputBuffer}; 
    DATA_BLOB   DataOut = {0, NULL};  

    if (!CryptProtectData(
            &DataIn, 
            NULL, NULL, NULL, NULL, 
            CRYPTPROTECT_SYSTEM, 
            &DataOut))
    {
        if (DataOut.pbData != NULL)
        {
            LocalFree(DataOut.pbData); 
        }

        return E_FAIL; ///TODO: get error code from win32
    }

    *ppOutputBuffer     = reinterpret_cast<BYTE*>(DataOut.pbData); 
    *pOutputBufferSize  = DataOut.cbData; 

    return S_OK; 

}


HRESULT
DecryptData(
    BYTE*   pInputBuffer, 
    DWORD   InputBufferSize, 
    BYTE**  ppOutputBuffer, 
    DWORD*  pOutputBufferSize
    )
{
    if (pInputBuffer == NULL ||
        InputBufferSize <= 0 ||
        ppOutputBuffer == NULL ||
        pOutputBufferSize == NULL)
    {
        return E_INVALIDARG; 
    }

    *ppOutputBuffer     = NULL; 
    *pOutputBufferSize  = 0; 
    
    DATA_BLOB   DataIn  = {InputBufferSize, pInputBuffer}; 
    DATA_BLOB   DataOut = {0, NULL};  

    if (!CryptUnprotectData(
            &DataIn, 
            NULL, NULL, NULL, NULL, 
            CRYPTPROTECT_SYSTEM, 
            &DataOut))
    {
        if (DataOut.pbData != NULL)
        {
            LocalFree(DataOut.pbData); 
        }

        return E_FAIL; ///TODO: get error code from win32
    }

    *ppOutputBuffer     = reinterpret_cast<BYTE*>(DataOut.pbData); 
    *pOutputBufferSize  = DataOut.cbData; 

    return S_OK; 

}



ProvisionSetting_t::ProvisionSetting_t(
    const WCHAR*    c_pSettingName, 
    DWORD           SettingFlag, 
    Store_t*        pStore, 
    bool            ShouldEncrypted
    )
{
    if (c_pSettingName == NULL || 
        c_pSettingName[0] == NULL ||
        SettingFlag == 0 ||
        pStore == NULL)
    {
        ASSERT(FALSE); 
    }

    m_bstrSettingName = SysAllocString(c_pSettingName); 
    if (m_bstrSettingName == NULL)
    {
        ASSERT(FALSE); 
    }

    m_SettingFlag       = SettingFlag; 
    m_pStore            = pStore; 
    m_ShouldEncrypted   = ShouldEncrypted; 

    FlushCachedValue(); 
    
}

ProvisionSetting_t::~ProvisionSetting_t(
    void
    )
{
    if (m_pStore)
    {
        delete m_pStore; 
    }

    FlushCachedValue(); 

}

HRESULT
ProvisionSetting_t::Set(
    SettingCategory_e   OperationCategory, 
    const WCHAR*        pValue, 
    int                 ValueSize
    )
{
    if (pValue == NULL ||
        pValue[0] == NULL ||
        ValueSize <= 0)
    {
        return E_INVALIDARG; 
    }
    
    if (m_pStore == NULL)
    {
        return E_UNEXPECTED; 
    }

    HRESULT hr = S_OK; 
    BYTE* pBuffer     = reinterpret_cast<BYTE*>(const_cast<WCHAR*>(pValue)); 
    DWORD BufferSize  = sizeof(WCHAR) * (ValueSize + 1);    //include the null terminator

    if (m_ShouldEncrypted)
    {
        hr = EncryptData(
                        pBuffer, 
                        BufferSize, 
                        &pBuffer, 
                        &BufferSize
                        ); 
        if (FAILED(hr))
        {
            return hr; 
        }
    }

    hr = m_pStore->SetValue(
                        OperationCategory, 
                        pBuffer, 
                        BufferSize
                        ); 
    
    //de-acclocate the memory
    if (m_ShouldEncrypted && pBuffer != NULL)
    {
        LocalFree(pBuffer); 
    }

    return hr; 

}


HRESULT
ProvisionSetting_t::Query(
    SettingCategory_e   OperationCategory
    )
{
    if (m_pStore == NULL)
    {
        ASSERT(FALSE); 
        return E_UNEXPECTED; 
    }

    FlushCachedValue(); 
    
    BYTE*   pBuffer = NULL; 
    DWORD   BufferSize; 
    
    HRESULT hr = m_pStore->GetValue(
                            OperationCategory, 
                            &pBuffer, 
                            &BufferSize
                            ); 
    if (FAILED(hr))
    {
        return hr; 
    }

    if (m_ShouldEncrypted && 
        pBuffer != NULL && 
        BufferSize >= 0)
    {
        hr = DecryptData(
                pBuffer, 
                BufferSize, 
                &pBuffer, 
                &BufferSize
                ); 
        if (FAILED(hr))
        {
            return hr; 
        }
    }

    if (pBuffer == NULL)
    {
        m_bstrCachedValue = SysAllocString(L""); 
    }
    else
    {
        m_bstrCachedValue = SysAllocString((WCHAR*)pBuffer); 
    }

    if (pBuffer != NULL)
    {
        LocalFree(pBuffer); 
    }

    return S_OK; 
}


HRESULT
ProvisionSetting_t::Delete(
    SettingCategory_e   OperationCategory, 
    bool*               pNotExist
    )
{
    if (m_pStore == NULL)
    {
        ASSERT(FALSE); 
        return E_UNEXPECTED; 
    }

    return m_pStore->Delete(
                        OperationCategory, 
                        pNotExist
                        ); 
}


HRESULT
ProvisionSetting_t::CheckOldValueExists(
    SettingCategory_e   CheckCategory, 
    bool*               pNotExist
    )
{
    if (m_pStore == NULL)
    {
        ASSERT(FALSE); 
        return E_UNEXPECTED; 
    }

    return m_pStore->CheckOldValueExists(
                        CheckCategory, 
                        pNotExist
                        ); 
}


bool
ProvisionSetting_t::MatchesName(
    const WCHAR*    c_pName, 
    int             NameLength
    )
{
    if (c_pName == NULL ||
        c_pName[0] == NULL ||
        NameLength <= 0)
    {
        return false; 
    }

    if ((NameLength == SysStringLen(m_bstrSettingName)) &&
        (0 == PhoneAppUtilities_t::InvStrCmpNI(
                    m_bstrSettingName,
                    c_pName,
                    NameLength
                    )))
    {
        return true; 
    }

    
    return false; 
}
    
HRESULT
ProvisionSetting_t::CopyValue(
    SettingCategory_e FromCategory, 
    SettingCategory_e ToCategory
    )
{
    HRESULT hr = Query(FromCategory); 
    if (FAILED(hr))
    {
        return hr; 
    }

    if (SysStringLen(m_bstrCachedValue) == 0)
    {
        //value doesn't exist in 'from category', bail out
        return S_OK; 
    }

    return Set(
            ToCategory, 
            m_bstrCachedValue, 
            SysStringLen(m_bstrCachedValue)
            ); 

}

HRESULT
ProvisionSetting_t::WriteCachedValue(
    IMXAttributes*  pMXAttributes
    )
{
    if (pMXAttributes == NULL)
    {
        return E_INVALIDARG; 
    }

    ce::auto_bstr bstrEmpty; 
    ce::auto_bstr bstrAttributeName; 

    bstrEmpty = SysAllocString(L""); 
    if (bstrEmpty == NULL)
    {
        return E_OUTOFMEMORY; 
    }

    struct XMLAttribute
    {
        const WCHAR*    pAttributeName; 
        BSTR            bstrAttributeValue;             
    }; 

    XMLAttribute XMLAttributes[] = 
    {
        {L"name",   m_bstrSettingName}, 
        {L"value",  m_bstrCachedValue}, 
    }; 

    for (int Index = 0; Index < ARRAYSIZE(XMLAttributes); Index++)
    {
        bstrAttributeName = SysAllocString(XMLAttributes[Index].pAttributeName); 
        if (bstrAttributeName == NULL)
        {
            return E_OUTOFMEMORY; 
        }

        HRESULT hr = pMXAttributes->addAttribute(
                                        bstrEmpty, 
                                        bstrAttributeName, 
                                        bstrAttributeName, 
                                        bstrEmpty, 
                                        XMLAttributes[Index].bstrAttributeValue
                                        ); 
        if (FAILED(hr))
        {
            return hr; 
        }
        
    }
    
    return S_OK;     
}

void
ProvisionSetting_t::FlushCachedValue(
    void
    )
{
    if (m_bstrCachedValue != NULL)
    {
        m_bstrCachedValue.release(); 
    }

    return; 
}


⌨️ 快捷键说明

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