/* $Id: kLdrRdr.c 2826 2006-10-22 15:58:55Z bird $ */ /** @file * * kLdr - The Dynamic Loader, file abstraction. * * Copyright (c) 2006 knut st. osmundsen * * * This file is part of kLdr. * * kLdr is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * kLdr is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with kLdr; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /******************************************************************************* * Header Files * *******************************************************************************/ #include #include "kLdrInternal.h" /******************************************************************************* * Global Variables * *******************************************************************************/ /** The list of file providers. */ static PCKLDRRDROPS g_pRdrHead = &g_kLdrRdrFileOps; /** * Adds a new file provider. * * @param pAdd The new file provider. */ void kLdrRdrAddProvider(PKLDRRDROPS pAdd) { pAdd->pNext = g_pRdrHead; g_pRdrHead = pAdd; } /** * Tries to opens a file. * * @returns 0 on success, OS status code on failure. * @param ppRdr Where to store the file provider instance. * @param pszFilename The filename. */ int kLdrRdrOpen(PPKLDRRDR ppRdr, const char *pszFilename) { int rc; PCKLDRRDROPS pCur; for (pCur = g_pRdrHead; pCur; pCur = pCur->pNext) { rc = pCur->pfnCreate(ppRdr, pszFilename); if (!rc) return 0; } return rc; } /** * Closes the file. * * @returns 0 on success, OS specific error code on failure. * On failure, the file provider instance will be in an indeterminate state - don't touch it! * @param pRdr The file provider instance. */ int kLdrRdrClose(PKLDRRDR pRdr) { return pRdr->pOps->pfnDestroy(pRdr); } /** Read bits from the file. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pvBuf Where to put the bits. * @param cb The number of bytes to read. * @param off Where to start reading. */ int kLdrRdrRead(PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off) { return pRdr->pOps->pfnRead(pRdr, pvBuf, cb, off); } /** Map all the file bits into memory (read only). * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param ppvBits Where to store the address of the mapping. * The size can be obtained using pfnSize. */ int kLdrRdrAllMap(PKLDRRDR pRdr, const void **ppvBits) { return pRdr->pOps->pfnAllMap(pRdr, ppvBits); } /** Unmap a file bits mapping obtained by KLDRRDROPS::pfnAllMap. * * @returns 0 on success, OS specific error code on failure. * @param pRdr The file provider instance. * @param pvBits The mapping address. */ int kLdrRdrAllUnmap(PKLDRRDR pRdr, const void *pvBits) { return pRdr->pOps->pfnAllUnmap(pRdr, pvBits); } /** Get the file size. * * @returns The file size. Returns -1 on failure. * @param pRdr The file provider instance. */ off_t kLdrRdrSize(PKLDRRDR pRdr) { return pRdr->pOps->pfnSize(pRdr); } /** Get the file pointer offset. * * @returns The file pointer offset. Returns -1 on failure. * @param pRdr The file provider instance. */ off_t kLdrRdrTell(PKLDRRDR pRdr) { return pRdr->pOps->pfnTell(pRdr); } /** Get the file name. * * @returns The file size. Returns -1 on failure. * @param pRdr The file provider instance. */ const char *kLdrRdrName(PKLDRRDR pRdr) { return pRdr->pOps->pfnName(pRdr); }