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

📄 loader_win32.cpp

📁 这是DVD中伺服部分的核心代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:

    for ( index = 0; index < 5; index++ ) 
    {
        pKey->KeyData[index] = pbKey[index];
    }

    
    win32Result = DeviceIoControl(  data->deviceHandle,             // device handle
                                    IOCTL_DVD_SEND_KEY,             // operation
                                    &keyBuffer,                       
                                    sizeof(keyBuffer),             
                                    &keyBuffer,
                                    sizeof(keyBuffer),                              
                                    &outputByteCount,               // output byte count
                                    NULL);                          // overlapped structure for async operations

    if (win32Result == 0x00)
    {
        DbgPrint(("WIN32Loader::SendBusKey: ERROR %d\n", GetLastError() ));
        data->sessionId = 0;
        return ( WIN32LOADER_FAILURE );
    }

    return ( WIN32LOADER_SUCCESS );
}






/**
 *******************************************************************************
 *  WIN32Loader::GetDiskKey        CSS authentication
 *
 *  @return     WIN32LOADER_SUCCESS if successful, otherwise WIN32LOADER_FAILURE
 *******************************************************************************/
int WIN32Loader::GetDiskKey( unsigned char pbKey[2048] )
{
    unsigned char           keyBuffer[DVD_DISK_KEY_LENGTH];
    PDVD_COPY_PROTECT_KEY   pKey = NULL;
    BOOL                    win32Result;
    DWORD                   outputByteCount;
    int                     index;

    DbgAssert( data != NULL );

    if ( data->deviceHandle == INVALID_HANDLE_VALUE )
    {
        return ( WIN32LOADER_FAILURE );
    }

    memset(keyBuffer,0,sizeof(keyBuffer));
    pKey = (PDVD_COPY_PROTECT_KEY) &keyBuffer;
    pKey->KeyLength = DVD_DISK_KEY_LENGTH;
    pKey->SessionId = data->sessionId;
    pKey->KeyType   = DvdDiskKey;

    
    win32Result = DeviceIoControl(  data->deviceHandle,             // device handle
                                    IOCTL_DVD_READ_KEY,             // operation
                                    &keyBuffer,                       
                                    sizeof(keyBuffer),             
                                    &keyBuffer,
                                    sizeof(keyBuffer),                              
                                    &outputByteCount,               // output byte count
                                    NULL);                          // overlapped structure for async operations


    if (win32Result == 0x00)
    {
        DbgPrint(("WIN32Loader::GetDiskKey: ERROR %d\n", GetLastError() ));
        data->sessionId = 0;
        return ( WIN32LOADER_FAILURE );
    }


    for ( index = 0; index < 2048; index++ )
    {
        pbKey[index] = pKey->KeyData[index]; 
    }

    return ( WIN32LOADER_SUCCESS );
}






/**
 *******************************************************************************
 *  WIN32Loader::GetTitleKey        CSS authentication
 *
 *  @return     WIN32LOADER_SUCCESS if successful, otherwise WIN32LOADER_FAILURE
 *******************************************************************************/
int WIN32Loader::GetTitleKey( unsigned char pbKey[5], unsigned long int ulLBA )
{
    unsigned char           keyBuffer[DVD_TITLE_KEY_LENGTH];
    PDVD_COPY_PROTECT_KEY   pKey = NULL;
    BOOL                    win32Result;
    DWORD                   outputByteCount;

    DbgAssert ( data != NULL );

    if ( data->deviceHandle == INVALID_HANDLE_VALUE )
    {
        return ( WIN32LOADER_FAILURE );
    }

    memset(keyBuffer,0,sizeof(keyBuffer));
    pKey = (PDVD_COPY_PROTECT_KEY) &keyBuffer;
    pKey->KeyLength = DVD_TITLE_KEY_LENGTH;
    pKey->SessionId = data->sessionId;
    pKey->KeyType   = DvdTitleKey;
    pKey->Parameters.TitleOffset.QuadPart = (__int64)ulLBA * 2048;

    
    win32Result = DeviceIoControl(  data->deviceHandle,             // device handle
                                    IOCTL_DVD_READ_KEY,             // operation
                                    &keyBuffer,                       
                                    sizeof(keyBuffer),             
                                    &keyBuffer,
                                    sizeof(keyBuffer),                              
                                    &outputByteCount,               // output byte count
                                    NULL);                          // overlapped structure for async operations


    if (win32Result == 0x00)
    {
        DbgPrint(("WIN32Loader::GetTitleKey: ERROR %d\n", GetLastError() ));
        data->sessionId = 0;
        return ( WIN32LOADER_FAILURE );
    }

    memcpy( pbKey, pKey->KeyData, 5 );

    //DbgPrint(("TITLE KEY: %02x %02x %02x %02x %02x \n",pbKey[0],pbKey[1],pbKey[2],pbKey[3],pbKey[4] ));

    return ( WIN32LOADER_SUCCESS );
}





/**
 *******************************************************************************
 *  WIN32Loader::FileExists     Determine if file exists
 *  
 *
 *  @return     WIN32LOADER_SUCCESS if successful, otherwise WIN32LOADER_FAILURE
 *******************************************************************************/
int WIN32Loader::FileExists( const char * path, unsigned char* flag )
{
    char fullPath[MAX_PATH];

    DbgAssert ( data != NULL );
    DbgAssert ( strlen(data->driveName) + strlen(path) + 1 < MAX_PATH );
    
    strcpy(fullPath,data->driveName);
    strcat(fullPath,path);

    if  (_access( fullPath,0 )  != -1 ) 
    {
        *flag = TRUE;
    }
    else
    {
        *flag = FALSE;
    }

    return ( WIN32LOADER_SUCCESS );
}






/**
 *******************************************************************************
 *  WIN32Loader::FileOpen       Open File
 *  
 *
 *  @return     WIN32LOADER_SUCCESS if successful, otherwise WIN32LOADER_FAILURE
 *******************************************************************************/
int WIN32Loader::FileOpen( const char * path, int* handle )
{
    FILE*   fileHandle;
    char fullPath[MAX_PATH];

    DbgAssert ( data != NULL );
    DbgAssert ( strlen(data->driveName) + strlen(path) + 1 < MAX_PATH );
    
    strcpy(fullPath,data->driveName);
    strcat(fullPath,path);

    fileHandle = fopen(fullPath,"rb");
    if ( fileHandle == NULL ) 
    {
        *handle = NULL;
        return ( WIN32LOADER_FAILURE );
    }

    *handle = (int)fileHandle;

    return ( WIN32LOADER_SUCCESS );
}






/**
 *******************************************************************************
 *  WIN32Loader::FileClose       Close File
 *  
 *
 *  @return     WIN32LOADER_SUCCESS if successful, otherwise WIN32LOADER_FAILURE
 *******************************************************************************/
int WIN32Loader::FileClose( int handle )
{
    FILE*   fileHandle;

    fileHandle = (FILE*) handle;

    if ( fclose(fileHandle) != 0 )
    {
        DbgPrint(("WIN32Loader::FileClose  fclose failed, ERROR %d\n", GetLastError() ));
    }

    return ( WIN32LOADER_SUCCESS );
}






/**
 *******************************************************************************
 *  WIN32Loader::FileSeek       Move file pointer
 *  
 *
 *  @return     WIN32LOADER_SUCCESS if successful, otherwise WIN32LOADER_FAILURE
 *******************************************************************************/
int WIN32Loader::FileSeek( int handle, WIN32LOADER_SEEK_TYPE win32SeekType, unsigned __int64 offset )
{
    FILE*   fileHandle;
    int     seekType;
    DWORD   result;
    LONG    distanceLSW = (unsigned long int)(offset & 0x00000000FFFFFFFF);
    LONG    distanceMSW = (unsigned long int)((offset & 0xFFFFFFFF00000000)>>32);

    switch ( win32SeekType )
    {
        case WIN32LOADER_SEEK_TYPE_CUR: seekType = SEEK_CUR; break;
        case WIN32LOADER_SEEK_TYPE_END: seekType = SEEK_END; break;
        case WIN32LOADER_SEEK_TYPE_SET: seekType = SEEK_SET; break;
    }

    fileHandle = (FILE*) handle;

    result = _fseeki64( fileHandle, offset, seekType);
    if ( result != 0 )
    {
        DbgPrint(("WIN32Loader::FileSeek: ERROR %d\n", GetLastError() ));
        return ( WIN32LOADER_FAILURE );
    }

    return ( WIN32LOADER_SUCCESS );
}






/**
 *******************************************************************************
 *  WIN32Loader::FileTell       Get file pointer
 *  
 *
 *  @return     WIN32LOADER_SUCCESS if successful, otherwise WIN32LOADER_FAILURE
 *******************************************************************************/
int WIN32Loader::FileTell( int handle, unsigned __int64* position )
{
    FILE*   fileHandle;
    LONG    positionLSW = 0;
    LONG    positionMSW = 0;


    fileHandle = (FILE*) handle;

    positionLSW = SetFilePointer(fileHandle,positionLSW,&positionMSW,FILE_CURRENT);

    if (positionLSW == 0xFFFFFFFF && (GetLastError()) != NO_ERROR )
    {
        DbgPrint(("WIN32Loader::FileTell: ERROR %d\n", GetLastError() ));
        return ( WIN32LOADER_FAILURE );
    }

    *position = ((unsigned __int64)positionMSW << 32 ) | (unsigned __int64)positionLSW;

    return ( WIN32LOADER_SUCCESS );
}






/**
 *******************************************************************************
 *  WIN32Loader::FileSize       Get file size
 *  
 *
 *  @return     WIN32LOADER_SUCCESS if successful, otherwise WIN32LOADER_FAILURE
 *******************************************************************************/
int WIN32Loader::FileSize( int handle, unsigned __int64* size )
{
    FILE*   fileHandle;
    DWORD   sizeLSW;
    DWORD   sizeMSW;

    fileHandle = (FILE*) handle;

    sizeLSW = GetFileSize(fileHandle,&sizeMSW);
    if ( 
        (sizeLSW == 0xFFFFFFFF) && 
        (GetLastError() != NO_ERROR )
       )
    {
        DbgPrint(("WIN32Loader::FileSize: ERROR %d\n", GetLastError() ));
        return ( WIN32LOADER_FAILURE );
    }

    *size = ((unsigned __int64)sizeMSW << 32 ) | (unsigned __int64)sizeLSW;

    return ( WIN32LOADER_SUCCESS );

}






/**
 *******************************************************************************
 * WIN32Loader::FileRead        Read from a file
 *
 * @param tLoader - The loader handle.
 * @param hFileHandle - Handle to the file.
 * @param pvBuffer - Buffer to read data into.
 * @param ulSize - Size of buffer and size of data to read from file.
 * @param pulReadSize - Pointer to variable that gets set to the number of bytes read from file.
 *
 * Note: If file position is at end of file before the read, this function fails.  If position indicator reaches
 * the end of file during a read, this function returns success, and the number of bytes read is
 * stored in the variable pointed to by pulReadSize.  If EOF is not reached during a read,
 * the value pointed to by pulReadSize is equal to ulSize.  If pulReadSize is NULL, it is simply ignored.
 *
 *  @return     WIN32LOADER_SUCCESS if successful, otherwise WIN32LOADER_FAILURE
 *******************************************************************************/
int WIN32Loader::FileRead ( int handle, void* buffer, unsigned long int size, unsigned long int* numberOfBytesRead )
{
    FILE*   fileHandle;
    size_t  numberRead;

    fileHandle = (FILE*) handle;

    if ( feof(fileHandle))
    {
        return ( WIN32LOADER_FAILURE );
    }

    numberRead = fread(buffer, 1, size, fileHandle);

    if ( numberOfBytesRead != NULL ) 
    {
        *numberOfBytesRead = numberRead;
    }

    return ( WIN32LOADER_SUCCESS );
}






/**
 *******************************************************************************
 *  WIN32Loader::FileCheckEOF       check for end of file
 *  
 *
 *  @return     WIN32LOADER_SUCCESS if successful, otherwise WIN32LOADER_FAILURE
 *******************************************************************************/
int WIN32Loader::FileCheckEOF ( int handle, unsigned char* eof )
{
    FILE*   fileHandle;

    fileHandle = (FILE*) handle;

    *eof = feof(fileHandle);

    return ( WIN32LOADER_SUCCESS );
}

⌨️ 快捷键说明

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