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

📄 storage.cpp

📁 基于EP7312的MP3播放器源代码,包括MCU和PC端代码.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        hr = E_FAIL;
    }

    //
    // If there is a progress object, then indicate that the delete has
    // completed.
    //
    if(pProgress)
    {
        //
        // Set the progress to the end.
        //
        pProgress->Progress(100);

        //
        // Indicate that the delete is complete.
        //
        pProgress->End();
    }

    //
    // Return the result.
    //
    return(hr);
}

//****************************************************************************
//
// The IMDSPObject::Move method, which moves this storage object to another
// storage object.
//
//****************************************************************************
STDMETHODIMP
CMDSPStorage::Move(UINT fuMode, IWMDMProgress *pProgress,
                   IMDSPStorage *pTarget)
{
    //
    // If there is a progress object, then handle it now.
    //
    if(pProgress)
    {
        //
        // Indicate that the move is beginning.
        //
        pProgress->Begin(100);

        //
        // Set the progress to the end.
        //
        pProgress->Progress(100);

        //
        // Indicate that the move is complete.
        //
        pProgress->End();
    }

    //
    // If we do not have a secure channel server object, then return a failure.
    //
    if(!g_pAppSCServer)
    {
        return(S_FALSE);
    }

    //
    // If we are not authenticated, then return a failure.
    //
    if(!(g_pAppSCServer->fIsAuthenticated()))
    {
        return(WMDM_E_NOTCERTIFIED);
    }

    //
    // If this storage object has been deleted, then return a failure.
    //
    if(!m_pwcName[0])
    {
        return(WMDM_E_INTERFACEDEAD);
    }

    //
    // We do not support moving storage objects.
    //
    return(WMDM_E_NOTSUPPORTED);
}

//****************************************************************************
//
// The IMDSPObject::Open method, which opens this storage object and prepares
// it for other operations.
//
//****************************************************************************
STDMETHODIMP
CMDSPStorage::Open(UINT fuMode)
{
    unsigned long ulFlags;

    //
    // If we do not have a secure channel server object, then return a failure.
    //
    if(!g_pAppSCServer)
    {
        return(S_FALSE);
    }

    //
    // If we are not authenticated, then return a failure.
    //
    if(!(g_pAppSCServer->fIsAuthenticated()))
    {
        return(WMDM_E_NOTCERTIFIED);
    }

    //
    // If this storage object has been deleted, then return a failure.
    //
    if(!m_pwcName[0])
    {
        return(WMDM_E_INTERFACEDEAD);
    }

    //
    // If this storage object is the root directory, then we can not open it.
    //
    if(m_bIsRoot)
    {
        return(WMDM_E_NOTSUPPORTED);
    }

    //
    // If this storage object is already open, then return a failure.
    //
    if(m_bIsOpen)
    {
        return(WMDM_E_BUSY);
    }

    //
    // Determine the flags for opening this file.
    //
    ulFlags = 0;
    if(fuMode & MDSP_READ)
    {
        ulFlags |= 1;
    }
    if(fuMode & MDSP_WRITE)
    {
        ulFlags |= 2;
    }

    //
    // Attempt to open this file.
    //
    if(Maverick_Open(m_ulDrive, m_pwcName, ulFlags) == 0)
    {
        //
        // Attempt to create this file.
        //
        if(Maverick_Open(m_ulDrive, m_pwcName, ulFlags | 4) == 0)
        {
            //
            // We could not open or create this file, so return a failure.
            //
            return(E_FAIL);
        }
    }

    //
    // Indicate that this file is opened.
    //
    m_bIsOpen = TRUE;

    //
    // Success.
    //
    return(S_OK);
}

//****************************************************************************
//
// The IMDSPObject::Read method, which reads data from this storage object.
//
//****************************************************************************
STDMETHODIMP
CMDSPStorage::Read(BYTE *pData, DWORD *pdwSize, BYTE abMac[WMDM_MAC_LENGTH])
{
    void *pTmpData;
    HRESULT hr;
    HMAC hMAC;

    //
    // If we do not have a secure channel server object, then return a failure.
    //
    if(!g_pAppSCServer)
    {
        return(S_FALSE);
    }

    //
    // If we are not authenticated, then return a failure.
    //
    if(!(g_pAppSCServer->fIsAuthenticated()))
    {
        return(WMDM_E_NOTCERTIFIED);
    }

    //
    // If we were passed a NULL pointer, then return a failure.
    //
    if(!pData || !pdwSize)
    {
        return(E_INVALIDARG);
    }

    //
    // If this storage object has been deleted, then return a failure.
    //
    if(!m_pwcName[0])
    {
        return(WMDM_E_INTERFACEDEAD);
    }

    //
    // If this storage object is not opened, then return a failure.
    //
    if(!m_bIsOpen)
    {
        return(E_FAIL);
    }

    //
    // Allocate memory to contain the data read from the device.
    //
    pTmpData = new BYTE [*pdwSize];
    if(!pTmpData)
    {
        return(E_FAIL);
    }

    //
    // Read data from the file.
    //
    *pdwSize = Maverick_Read(pTmpData, *pdwSize);

    //
    // See if we actually read any data.
    //
    if(*pdwSize)
    {
        //
        // Acquire a MAC channel.
        //
        hr = g_pAppSCServer->MACInit(&hMAC);
        if(FAILED(hr))
        {
            delete [] pTmpData;
            return(hr);
        }

        //
        // Update the MAC value with the data buffer contents.
        //
        hr = g_pAppSCServer->MACUpdate(hMAC, (BYTE *)pTmpData, *pdwSize);
        if(FAILED(hr))
        {
            delete [] pTmpData;
            return(hr);
        }

        //
        // Update the MAC value with the length of the data buffer.
        //
        hr = g_pAppSCServer->MACUpdate(hMAC, (BYTE *)pdwSize, sizeof(DWORD));
        if(FAILED(hr))
        {
            delete [] pTmpData;
            return(hr);
        }

        //
        // Release the MAC channel and retrieve the final MAC value.
        //
        hr = g_pAppSCServer->MACFinal(hMAC, abMac);
        if(FAILED(hr))
        {
            delete [] pTmpData;
            return(hr);
        }

        //
        // Encrypt the data buffer with the current session key.
        //
        hr = g_pAppSCServer->EncryptParam((BYTE *)pTmpData, *pdwSize);
        if(FAILED(hr))
        {
            delete [] pTmpData;
            return(hr);
        }

        //
        // Copy the encrypted data from the temporary buffer into the user
        // supplied buffer.
        //
        memcpy(pData, pTmpData, *pdwSize);
    }

    //
    // Delete the temporary memory buffer.
    //
    delete [] pTmpData;

    //
    // Success.
    //
    return((*pdwSize == 0) ? E_FAIL : S_OK);
}

//****************************************************************************
//
// The IMDSPObject::Rename method, which renames this storage object.
//
//****************************************************************************
STDMETHODIMP
CMDSPStorage::Rename(LPWSTR pwszNewName, IWMDMProgress *pProgress)
{
    //
    // If there is a progress object, then handle it now.
    //
    if(pProgress)
    {
        //
        // Indicate that the rename is beginning.
        //
        pProgress->Begin(100);

        //
        // Set the progress to the end.
        //
        pProgress->Progress(100);

        //
        // Indicate that the rename is complete.
        //
        pProgress->End();
    }

    //
    // If we do not have a secure channel server object, then return a failure.
    //
    if(!g_pAppSCServer)
    {
        return(S_FALSE);
    }

    //
    // If we are not authenticated, then return a failure.
    //
    if(!(g_pAppSCServer->fIsAuthenticated()))
    {
        return(WMDM_E_NOTCERTIFIED);
    }

    //
    // If this storage object has been deleted, then return a failure.
    //
    if(!m_pwcName[0])
    {
        return(WMDM_E_INTERFACEDEAD);
    }

    //
    // We do not support renaming storage objects.
    //
    return(WMDM_E_NOTSUPPORTED);
}

//****************************************************************************
//
// The IMDSPObject::Seek method, which sets the current position within this
// storage object.
//
//****************************************************************************
STDMETHODIMP
CMDSPStorage::Seek(UINT fuFlags, DWORD dwOffset)
{
    //
    // If we do not have a secure channel server object, then return a failure.
    //
    if(!g_pAppSCServer)
    {
        return(S_FALSE);
    }

    //
    // If we are not authenticated, then return a failure.
    //
    if(!(g_pAppSCServer->fIsAuthenticated()))
    {
        return(WMDM_E_NOTCERTIFIED);
    }

    //
    // If this storage object has been deleted, then return a failure.
    //
    if(!m_pwcName[0])
    {
        return(WMDM_E_INTERFACEDEAD);
    }

    //
    // If this storage object is not opened, then return a failure.
    //
    if(!m_bIsOpen)
    {
        return(E_FAIL);
    }

    //
    // Determine the seek method requested.
    //
    switch(fuFlags)
    {
        //
        // We should seek to the given offset from the beginning of the file.
        //
        case MDSP_SEEK_BOF:
        {
            //
            // The seek offset is simply the supplied offset, so there is
            // nothing to be done.
            //
            break;
        }

        //
        // We should seek to the given offset from the current position.
        //
        case MDSP_SEEK_CUR:
        {
            //
            // Add the current file position to the seek position.
            //
            dwOffset += Maverick_Tell();

            //
            // We now have a seek position relative to the beginning of the
            // file.
            //
            break;
        }

        //
        // We should seek to the given offset from the end of the file.
        //
        case MDSP_SEEK_EOF:
        {
            //
            // Add the file length to the seek position.
            //
            dwOffset += Maverick_Length();

            //
            // We now have a seek position relative to the beginning of the
            // file.
            //
            break;
        }

        //
        // An un-recognized seek method was specified, so return an error.
        //
        default:
        {
            return(E_INVALIDARG);
        }
    }

    //
    // Perform the seek.
    //
    if(Maverick_Seek(dwOffset) == 0)
    {
        return(E_FAIL);
    }

    //
    // Success.
    //
    return(S_OK);
}

//****************************************************************************
//
// The IMDSPObject::Write method, which writes data to this storage object.
//
//****************************************************************************
STDMETHODIMP
CMDSPStorage::Write(BYTE *pData, DWORD *pdwSize, BYTE abMac[WMDM_MAC_LENGTH])
{
    BYTE pSelfMac[WMDM_MAC_LENGTH];
    void *pTmpData;
    HRESULT hr;
    HMAC hMAC;

    //
    // If we do not have a secure channel server object, then return a failure.
    //
    if(!g_pAppSCServer)
    {
        return(S_FALSE);
    }

    //
    // If we are not authenticated, then return a failure.
    //
    if(!(g_pAppSCServer->fIsAuthenticated()))
    {
        return(WMDM_E_NOTCERTIFIED);
    }

    //
    // If we were passed a NULL pointer, then return a failure.
    //
    if(!pData || !pdwSize)
    {
        return(E_INVALIDARG);
    }

    //
    // If this storage object has been deleted, then return a failure.
    //
    if(!m_pwcName[0])
    {
        return(WMDM_E_INTERFACEDEAD);
    }

    //
    // If this storage object is not opened, then return a failure.
    //
    if(!m_bIsOpen)
    {
        return(E_FAIL);
    }

    //
    // Allocate memory to contain the data to be written to the device.
    //
    pTmpData = new BYTE [*pdwSize];
    if(!pTmpData)
    {
        return(E_FAIL);
    }

    //
    // Copy the data from the user supplied buffer to our temporary buffer.
    //
    memcpy(pTmpData, pData, *pdwSize);

    //
    // Decrypt the data buffer with the current session key.
    //
    hr = g_pAppSCServer->DecryptParam((BYTE *)pTmpData, *pdwSize);
    if(FAILED(hr))
    {
        delete [] pTmpData;
        return(hr);
    }

    //
    // Acquire a MAC channel.
    //
    hr = g_pAppSCServer->MACInit(&hMAC);
    if(FAILED(hr))
    {
        delete [] pTmpData;
        return(hr);
    }

    //
    // Update the MAC value with the data buffer contents.
    //
    hr = g_pAppSCServer->MACUpdate(hMAC, (BYTE *)pTmpData, *pdwSize);
    if(FAILED(hr))
    {
        delete [] pTmpData;
        return(hr);
    }

    //
    // Update the MAC value with the length of the data buffer.
    //
    hr = g_pAppSCServer->MACUpdate(hMAC, (BYTE *)pdwSize, sizeof(DWORD));
    if(FAILED(hr))
    {
        delete [] pTmpData;
        return(hr);
    }

    //
    // Release the MAC channel and retrieve the final MAC value.
    //
    hr = g_pAppSCServer->MACFinal(hMAC, pSelfMac);

    //
    // Compare the computed MAC value with the supplied MAC value.
    //
    if(memcmp(abMac, pSelfMac, WMDM_MAC_LENGTH) != 0)
    {
        //
        // The MAC did not match, so do not perform the write (are we being
        // hacked?).
        //
        delete [] pTmpData;
        return(WMDM_E_MAC_CHECK_FAILED);
    }

    //
    // Write the data to the file.
    //
    *pdwSize = Maverick_Write(pTmpData, *pdwSize);

    //
    // Delete the temporary memory buffer.
    //
    delete [] pTmpData;

    //
    // Update our copy of the length of this file since it might change as a
    // result of a write.
    //
    m_ulSize = Maverick_Length();

    //
    // Success.
    //
    return((*pdwSize == 0) ? E_FAIL : S_OK);
}

⌨️ 快捷键说明

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