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

📄 loader_app.c

📁 这是DVD中伺服部分的核心代码
💻 C
📖 第 1 页 / 共 5 页
字号:
            break;
        case LOADER_CD_SECTOR_MODE2_FORM2:
            if(dataSelect == LOADER_CDDS_ALLHDR_USER_ECC)
            {
                ulSectorSize = CD_ALLHDR_USER_ECC_SIZE;
            }
            else
            {
                DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderCDRead() -- Unimplemented case\n"));
                return (LOADER_NOT_IMPLEMENTED);
            }
            break;
        default:
            DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderCDRead() -- Unimplemented case\n"));
            return (LOADER_NOT_IMPLEMENTED);
            break;
    }

    if (ulSectorSize*ulNumBlocks  > ulBufSize)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderCDRead() -- Buffer doesn't have enough space\n"));
        return (LOADER_INVALID_PARAMETER);
    }


    memset(&cgc,   0, sizeof(cgc));
    memset(&sense, 0, sizeof(sense));

    cgc.sense          = &sense;
    cgc.buffer         = pbData;
    cgc.buflen         = ulSectorSize*ulNumBlocks;
    cgc.data_direction = CGC_DATA_READ;
    cgc.timeout        = HZ*5;
    cgc.stat           = 0;
    cgc.quiet          = 0;

    cgc.cmd[ 0] = GPCMD_READ_CD;
    cgc.cmd[ 1] = sectorType << 2;
    cgc.cmd[ 2] = (BYTE)((ulLBA >> 24) & 0xff);
    cgc.cmd[ 3] = (BYTE)((ulLBA >> 16) & 0xff);
    cgc.cmd[ 4] = (BYTE)((ulLBA >>  8) & 0xff);
    cgc.cmd[ 5] = (BYTE)((ulLBA)       & 0xff);
    cgc.cmd[ 7] = (BYTE)((ulNumBlocks >> 8) & 0xff);
    cgc.cmd[ 8] = (BYTE)((ulNumBlocks)      & 0xff);
    cgc.cmd[ 9] = dataSelect;

    if (ioctl(((LOADERHANDLE*)tLoader)->lFileHandle, CDROM_SEND_PACKET, &cgc) < 0)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderCDRead() -- FAILED on LBA=0x%x (%s)\n", ulLBA, strerror( errno )));
        loaderSetError( tLoader, errno );
        ulRet = LOADER_FAILURE;
    }
    else
    {
        /* return success */
        ulRet = LOADER_SUCCESS;
    }

    return (ulRet);
}

/**
 * Determine accessibility of a file or directory path on a disc.
 *
 * @param tLoader - The loader handle.
 * @param pcPath - Path name or file name
 * @param tAccessType - Permissions to check for.  This can be a bitwise inclusive or of access permissions
 *                      READ, WRITE, EXECUTE or it can be an existence test.
 * @param pbPermit - Pointer to flag that gets set if the access is permitted.
 */
LOADER_ERR LoaderFileAccess(LOADER_HANDLE tLoader, const char *pcPath, LOADER_FILE_ACCESS_TYPE tAccessType, BYTE *pbPermit)
{
    ULONG ulPermissions = 0;

    if ( (tLoader == NULL) || (pbPermit == NULL) || (pcPath == NULL) )
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileAccess() -- NULL pointer!\n"));
        return (LOADER_NULL_PTR);
    }

    /*
     * Set permissions to be checked.
     */
    if (tAccessType == (ULONG)LOADER_FILE_ACCESS_EXIST)
    {
        /* existence test */
        ulPermissions = F_OK;
    }
    else
    {
        if ( (tAccessType & (ULONG)LOADER_FILE_ACCESS_READ) != 0)
        {
            /* test for read permission */
            ulPermissions |= R_OK;
        }

        if ( (tAccessType & (ULONG)LOADER_FILE_ACCESS_WRITE) != 0)
        {
            /* test for write permission */
            ulPermissions |= W_OK;
        }

        if ( (tAccessType & (ULONG)LOADER_FILE_ACCESS_EXECUTE) != 0)
        {
            /* test for execute permission */
            ulPermissions |= X_OK;
        }
    }

    /* Determine accessibility of path or file name */
    if (access(pcPath, (int)ulPermissions) != 0)
    {
        *pbPermit = 0;
    }
    else
    {
        *pbPermit = 1;
    }

    return (LOADER_SUCCESS);
}

/**
 * Open a file from the disc.
 *
 * @param tLoader - The loader handle.
 * @param pcFileName - File name.
 * @param hFileHandle - Pointer to handle to the file that is opened.
 * @param fDirectIO - Flag to enable/disable direct IO.
 */
LOADER_ERR loaderFileOpen(LOADER_HANDLE tLoader, const char *pcFileName, LOADER_FILE_HANDLE *hFileHandle, BOOLEAN fDirectIO)
{
    LOADER_ERR err = LOADER_SUCCESS;
    int        fd;
    int        flags;

    if ( (tLoader == NULL) || (pcFileName == NULL) || (hFileHandle == NULL) )
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileOpen() -- NULL pointer!\n"));
        return (LOADER_NULL_PTR);
    }

    flags = O_RDONLY;

#if defined(LOADER_USE_DIRECT_IO)
    if ( (fDirectIO == LOADER_DIRECT_IO_ON) && (PLAY_FROM_HD == 0) )
    {
        flags |= O_DIRECT;
        DBGPRINT(DBG_ON(DBG_VERBOSE), ("LoaderFileOpen() -- DIRECT_IO  %s fd %d\n", pcFileName, *hFileHandle));
    }
#endif

    /* open file in read mode */
    fd = open(pcFileName, flags);
    if (fd < 0)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileOpen() -- Error opening file! %d, %s\n", *hFileHandle, pcFileName));
        *hFileHandle = 0;
        err = LOADER_FILE_ERROR;
    }
    else
    {
        *hFileHandle = (LOADER_FILE_HANDLE)fd;
        err = LOADER_SUCCESS;
    }

    DBGPRINT(DBG_ON(DBG_VERBOSE), ("LoaderFileOpen() -- fd %d\n", *hFileHandle));

    return (err);
}

/**
 * Open a file from the disc.
 *
 * @param tLoader - The loader handle.
 * @param pcFileName - File name.
 * @param hFileHandle - Pointer to handle to the file that is opened.
 */
LOADER_ERR LoaderFileOpenAV(LOADER_HANDLE tLoader, const char *pcFileName, LOADER_FILE_HANDLE *hFileHandle)
{
    return ( loaderFileOpen(tLoader, pcFileName, hFileHandle, LOADER_DIRECT_IO_ON) );
}

/**
 * Open a file from the disc.
 *
 * @param tLoader - The loader handle.
 * @param pcFileName - File name.
 * @param hFileHandle - Pointer to handle to the file that is opened.
 */
LOADER_ERR LoaderFileOpen(LOADER_HANDLE tLoader, const char *pcFileName, LOADER_FILE_HANDLE *hFileHandle)
{
    return ( loaderFileOpen(tLoader, pcFileName, hFileHandle, LOADER_DIRECT_IO_OFF) );
}



/**
 * Close a file.
 *
 * @param tLoader - The loader handle.
 * @param hFileHandle - Handle to the file to close.
 */
LOADER_ERR LoaderFileClose(LOADER_HANDLE tLoader, LOADER_FILE_HANDLE hFileHandle)
{
    LOADER_ERR err = LOADER_SUCCESS;

    if ( (tLoader == NULL) || (hFileHandle < 0) )
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileClose() -- NULL pointer!\n"));
        return (LOADER_NULL_PTR);
    }

    /* close the file */
    if (close(hFileHandle) != 0)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileClose() -- Error closing file!\n"));
        err = LOADER_FILE_ERROR;
    }

    return (err);
}



/**
 * Seek to a specified position in a file.
 *
 * @param tLoader - The loader handle.
 * @param hFileHandle - Handle to the file.
 * @param tSeek - Seek type; from beginning of file, from current position in file, or from end of file.
 * @param ulOffset - Offset relative to seek type.
 */
LOADER_ERR LoaderFileSeek(LOADER_HANDLE tLoader, LOADER_FILE_HANDLE hFileHandle, LOADER_SEEK_TYPE tSeek, LONGLONG llOffset)
{
    LOADER_ERR  err         = LOADER_SUCCESS;
    ULONG       ulSeekType  = 0;

    if ( (tLoader == NULL) || (hFileHandle < 0) )
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileSeek() -- NULL pointer!\n"));
        return (LOADER_NULL_PTR);
    }

    /*
     * Determine the seek type.
     */
    if (tSeek == LOADER_SEEK_SET)
    {
        /* seek from beginning of file */
        ulSeekType = SEEK_SET;
    }
    else if (tSeek == LOADER_SEEK_CURRENT)
    {
        /* seek from current position in file */
        ulSeekType = SEEK_CUR;
    }
    else if (tSeek == LOADER_SEEK_END)
    {
        /* seek from end of file */
        ulSeekType = SEEK_END;
    }
    else
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileSeek() -- Invalid seek type!\n"));
        return (LOADER_INVALID_PARAMETER);
    }

    /* Seek to specified position in file */
    /* @todo The return value from lseek, if positive, will give the absolute location of the current
     * position in the file.  Could this be useful as to calling functions?
     */
    if (lseek(hFileHandle, llOffset, (int)ulSeekType) < 0)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileSeek() -- Error seeking in file!\n"));
        err = LOADER_FILE_ERROR;
    }

    return (err);
}



/**
 * Report the current position into the file.
 *
 * @param tLoader - the loader handle.
 * @param hFileHandle - Handle to the file.
 * @param ullPosition - the current position.
 */
LOADER_ERR LoaderFileTell(LOADER_HANDLE tLoader, LOADER_FILE_HANDLE hFileHandle, ULONGLONG *ullPosition)
{
    LOADER_ERR err = LOADER_SUCCESS;

    if ( (tLoader == NULL) || (hFileHandle == 0) )
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileTell() -- NULL pointer!\n"));
        return (LOADER_NULL_PTR);
    }

    long pos = lseek(hFileHandle, 0, SEEK_CUR);

    if (pos == -1)
        err = LOADER_FAILURE;
    else
        *ullPosition = pos;

    return (err);
}


/**
 * Determine a file's total size.
 *
 * @param tLoader - The loader handle.
 * @param hFileHandle - Handle to the file.
 * @param ullSize - Size of the given file.
 */
LOADER_ERR LoaderFileSize(LOADER_HANDLE tLoader, char * fileName, ULONGLONG *ullSize)
{
    LOADER_ERR  err = LOADER_SUCCESS;
    struct stat buf;

    if ( (tLoader == NULL) || (fileName == NULL) )
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileSize() -- NULL pointer!\n"));
        return (LOADER_NULL_PTR);
    }

    /* Get stats from file */
    if (stat(fileName, &buf) != 0)
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileSize() -- Error getting file stats!\n"));
        err = LOADER_FILE_ERROR;
    }

    /* return the file size */
    *ullSize = buf.st_size;

    return (err);
}


/**
 * 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.
 */
LOADER_ERR LoaderFileRead(LOADER_HANDLE tLoader, LOADER_FILE_HANDLE hFileHandle, PVOID pvBuffer, ULONG ulSize, ULONG *pulReadSize)
{
    LOADER_ERR  err = LOADER_SUCCESS;
    LONG lSz;

    if ( (tLoader == NULL) || (hFileHandle < 0) || (pvBuffer == NULL) || (ulSize == 0))
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileRead() -- CALLED WITH REQUIRED VALUE == 0!\n"));
        return (LOADER_NULL_PTR);
    }

    /* Read data from the file */
    lSz = read(hFileHandle, pvBuffer,(size_t)ulSize);

    if (pulReadSize != NULL)
    {
        /* set number of bytes read */
        *pulReadSize = (ULONG)lSz;
    }

    if (lSz > 0)
    {
        if (lSz != (LONG)ulSize)
        {
            DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileRead() -- ERROR: File Handle 0x%08x: READ REQUEST SIZE %d, %d BYTES RETURNED!!!\n",
                hFileHandle, ulSize, lSz));

            err = LOADER_FILE_ERROR;
        }
    }
    else if (lSz == 0)
    {
        DBGPRINT(DBG_ON(DBG_TRACE), ("LoaderFileRead() -- READ REQUESTED AT END OF FILE!\n"));
        err = LOADER_FILE_EOF;
    }
    else
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileRead() -- READ ERROR: File Handle 0x%08x: READ REQUEST SIZE %d, %d BYTES RETURNED!!!\n",
                    hFileHandle, ulSize, lSz));

        err = LOADER_FILE_ERROR;
    }

    return (err);
}


/**
 * Determine if a file marker is at the end of a file.
 *
 * @param tLoader - The loader handle.
 * @param hFileHandle - Handle to the file.
 * @param pbEOF - Pointer to flag that gets set if file marker is at end of file.
 */
LOADER_ERR LoaderFileCheckEOF(LOADER_HANDLE tLoader, LOADER_FILE_HANDLE hFileHandle, BYTE *pbEOF)
{
    if ( (tLoader == NULL) || (hFileHandle == 0) || (pbEOF == NULL) )
    {
        DBGPRINT(DBG_ON(DBG_ERROR), ("LoaderFileCheckEOF() -- NULL pointer!\n"));
        return (LOADER_NULL_PTR);
    }

    /* Chec

⌨️ 快捷键说明

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