[2] | 1 | /* $Id: kRdr.cpp 2 2007-11-16 16:07:14Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * kRdr - The File Provider.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
|
---|
| 8 | *
|
---|
| 9 | * This file is part of kStuff.
|
---|
| 10 | *
|
---|
| 11 | * kStuff is free software; you can redistribute it and/or
|
---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
---|
| 13 | * License as published by the Free Software Foundation; either
|
---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * In addition to the permissions in the GNU Lesser General Public
|
---|
| 17 | * License, you are granted unlimited permission to link the compiled
|
---|
| 18 | * version of this file into combinations with other programs, and to
|
---|
| 19 | * distribute those combinations without any restriction coming from
|
---|
| 20 | * the use of this file.
|
---|
| 21 | *
|
---|
| 22 | * kStuff is distributed in the hope that it will be useful,
|
---|
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 25 | * Lesser General Public License for more details.
|
---|
| 26 | *
|
---|
| 27 | * You should have received a copy of the GNU Lesser General Public
|
---|
| 28 | * License along with kStuff; if not, write to the Free Software
|
---|
| 29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
| 30 | * 02110-1301, USA
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | /*******************************************************************************
|
---|
| 35 | * Header Files *
|
---|
| 36 | *******************************************************************************/
|
---|
| 37 | #include "kRdrInternal.h"
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | /*******************************************************************************
|
---|
| 41 | * Global Variables *
|
---|
| 42 | *******************************************************************************/
|
---|
| 43 | /** The list of file providers. */
|
---|
| 44 | static PCKRDROPS g_pRdrHead = &g_kRdrFileOps;
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Adds a new file provider.
|
---|
| 49 | *
|
---|
| 50 | * @param pAdd The new file provider.
|
---|
| 51 | */
|
---|
| 52 | KRDR_DECL(void) kRdrAddProvider(PKRDROPS pAdd)
|
---|
| 53 | {
|
---|
| 54 | pAdd->pNext = g_pRdrHead;
|
---|
| 55 | g_pRdrHead = pAdd;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Tries to opens a file.
|
---|
| 61 | *
|
---|
| 62 | * @returns 0 on success, OS status code on failure.
|
---|
| 63 | * @param ppRdr Where to store the file provider instance.
|
---|
| 64 | * @param pszFilename The filename.
|
---|
| 65 | */
|
---|
| 66 | KRDR_DECL(int) kRdrOpen(PPKRDR ppRdr, const char *pszFilename)
|
---|
| 67 | {
|
---|
| 68 | int rc = -1;
|
---|
| 69 | PCKRDROPS pCur;
|
---|
| 70 | for (pCur = g_pRdrHead; pCur; pCur = pCur->pNext)
|
---|
| 71 | {
|
---|
| 72 | rc = pCur->pfnCreate(ppRdr, pszFilename);
|
---|
| 73 | if (!rc)
|
---|
| 74 | return 0;
|
---|
| 75 | }
|
---|
| 76 | return rc;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * Closes the file.
|
---|
| 82 | *
|
---|
| 83 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 84 | * On failure, the file provider instance will be in an indeterminate state - don't touch it!
|
---|
| 85 | * @param pRdr The file provider instance.
|
---|
| 86 | */
|
---|
| 87 | KRDR_DECL(int) kRdrClose(PKRDR pRdr)
|
---|
| 88 | {
|
---|
| 89 | KRDR_VALIDATE(pRdr);
|
---|
| 90 | return pRdr->pOps->pfnDestroy(pRdr);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | /** Read bits from the file.
|
---|
| 95 | *
|
---|
| 96 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 97 | * @param pRdr The file provider instance.
|
---|
| 98 | * @param pvBuf Where to put the bits.
|
---|
| 99 | * @param cb The number of bytes to read.
|
---|
| 100 | * @param off Where to start reading.
|
---|
| 101 | */
|
---|
| 102 | KRDR_DECL(int) kRdrRead(PKRDR pRdr, void *pvBuf, KSIZE cb, KFOFF off)
|
---|
| 103 | {
|
---|
| 104 | KRDR_VALIDATE(pRdr);
|
---|
| 105 | return pRdr->pOps->pfnRead(pRdr, pvBuf, cb, off);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | /** Map all the file bits into memory (read only).
|
---|
| 110 | *
|
---|
| 111 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 112 | * @param pRdr The file provider instance.
|
---|
| 113 | * @param ppvBits Where to store the address of the mapping.
|
---|
| 114 | * The size can be obtained using pfnSize.
|
---|
| 115 | */
|
---|
| 116 | KRDR_DECL(int) kRdrAllMap(PKRDR pRdr, const void **ppvBits)
|
---|
| 117 | {
|
---|
| 118 | KRDR_VALIDATE(pRdr);
|
---|
| 119 | return pRdr->pOps->pfnAllMap(pRdr, ppvBits);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | /** Unmap a file bits mapping obtained by KRDROPS::pfnAllMap.
|
---|
| 124 | *
|
---|
| 125 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 126 | * @param pRdr The file provider instance.
|
---|
| 127 | * @param pvBits The mapping address.
|
---|
| 128 | */
|
---|
| 129 | KRDR_DECL(int) kRdrAllUnmap(PKRDR pRdr, const void *pvBits)
|
---|
| 130 | {
|
---|
| 131 | KRDR_VALIDATE(pRdr);
|
---|
| 132 | return pRdr->pOps->pfnAllUnmap(pRdr, pvBits);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | /** Get the file size.
|
---|
| 137 | *
|
---|
| 138 | * @returns The file size. Returns -1 on failure.
|
---|
| 139 | * @param pRdr The file provider instance.
|
---|
| 140 | */
|
---|
| 141 | KRDR_DECL(KFOFF) kRdrSize(PKRDR pRdr)
|
---|
| 142 | {
|
---|
| 143 | KRDR_VALIDATE(pRdr);
|
---|
| 144 | return pRdr->pOps->pfnSize(pRdr);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | /** Get the file pointer offset.
|
---|
| 149 | *
|
---|
| 150 | * @returns The file pointer offset. Returns -1 on failure.
|
---|
| 151 | * @param pRdr The file provider instance.
|
---|
| 152 | */
|
---|
| 153 | KRDR_DECL(KFOFF) kRdrTell(PKRDR pRdr)
|
---|
| 154 | {
|
---|
| 155 | KRDR_VALIDATE(pRdr);
|
---|
| 156 | return pRdr->pOps->pfnTell(pRdr);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | /** Get the file name.
|
---|
| 161 | *
|
---|
| 162 | * @returns The file name. Returns NULL on failure.
|
---|
| 163 | * @param pRdr The file provider instance.
|
---|
| 164 | */
|
---|
| 165 | KRDR_DECL(const char *) kRdrName(PKRDR pRdr)
|
---|
| 166 | {
|
---|
| 167 | KRDR_VALIDATE_EX(pRdr, NULL);
|
---|
| 168 | return pRdr->pOps->pfnName(pRdr);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 |
|
---|
| 172 | /** Get the native file handle if possible.
|
---|
| 173 | *
|
---|
| 174 | * @returns The native file handle. Returns -1 if not available.
|
---|
| 175 | * @param pRdr The file provider instance.
|
---|
| 176 | */
|
---|
| 177 | KRDR_DECL(KIPTR) kRdrNativeFH(PKRDR pRdr)
|
---|
| 178 | {
|
---|
| 179 | KRDR_VALIDATE_EX(pRdr, -1);
|
---|
| 180 | return pRdr->pOps->pfnNativeFH(pRdr);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * Gets the page size used when mapping sections of the file.
|
---|
| 186 | *
|
---|
| 187 | * @returns The page size.
|
---|
| 188 | * @param pRdr The file provider instance.
|
---|
| 189 | */
|
---|
| 190 | KRDR_DECL(KSIZE) kRdrPageSize(PKRDR pRdr)
|
---|
| 191 | {
|
---|
| 192 | KRDR_VALIDATE_EX(pRdr, 0x10000);
|
---|
| 193 | return pRdr->pOps->pfnPageSize(pRdr);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
| 198 | * Maps the segments of a image into memory.
|
---|
| 199 | *
|
---|
| 200 | * The file reader will be using the RVA member of each segment to figure out where
|
---|
| 201 | * it goes relative to the image base address.
|
---|
| 202 | *
|
---|
| 203 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 204 | * @param pRdr The file provider instance.
|
---|
| 205 | * @param ppvBase On input when fFixed is set, this contains the base address of the mapping.
|
---|
| 206 | * On output this contains the base of the image mapping.
|
---|
| 207 | * @param cSegments The number of segments in the array pointed to by paSegments.
|
---|
| 208 | * @param paSegments The segments thats going to be mapped.
|
---|
| 209 | * @param fFixed If set, the address at *ppvBase should be the base address of the mapping.
|
---|
| 210 | */
|
---|
| 211 | KRDR_DECL(int) kRdrMap(PKRDR pRdr, void **ppvBase, KU32 cSegments, PCKLDRSEG paSegments, KBOOL fFixed)
|
---|
| 212 | {
|
---|
| 213 | KRDR_VALIDATE(pRdr);
|
---|
| 214 | return pRdr->pOps->pfnMap(pRdr, ppvBase, cSegments, paSegments, fFixed);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
| 219 | * Reloads dirty pages in mapped image.
|
---|
| 220 | *
|
---|
| 221 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 222 | * @param pRdr The file provider instance.
|
---|
| 223 | * @param pvBase The base address of the image mapping.
|
---|
| 224 | * @param cSegments The number of segments in the array pointed to by paSegments.
|
---|
| 225 | * @param paSegments The segments thats going to be mapped.
|
---|
| 226 | */
|
---|
| 227 | KRDR_DECL(int) kRdrRefresh(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments)
|
---|
| 228 | {
|
---|
| 229 | KRDR_VALIDATE(pRdr);
|
---|
| 230 | return pRdr->pOps->pfnRefresh(pRdr, pvBase, cSegments, paSegments);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * Protects or unprotects an image mapping.
|
---|
| 236 | *
|
---|
| 237 | * This is typically used for getting write access to read or execute only
|
---|
| 238 | * pages while applying fixups.
|
---|
| 239 | *
|
---|
| 240 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 241 | * @param pRdr The file provider instance.
|
---|
| 242 | * @param pvBase The base address of the image mapping.
|
---|
| 243 | * @param cSegments The number of segments in the array pointed to by paSegments.
|
---|
| 244 | * @param paSegments The segments thats going to be mapped.
|
---|
| 245 | * @param fUnprotectOrProtect When set the all mapped segments are made writable.
|
---|
| 246 | * When clean the segment protection is restored.
|
---|
| 247 | */
|
---|
| 248 | KRDR_DECL(int) kRdrProtect(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments, KBOOL fUnprotectOrProtect)
|
---|
| 249 | {
|
---|
| 250 | KRDR_VALIDATE(pRdr);
|
---|
| 251 | return pRdr->pOps->pfnProtect(pRdr, pvBase, cSegments, paSegments, fUnprotectOrProtect);
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 |
|
---|
| 255 | /**
|
---|
| 256 | * Unmaps a image mapping.
|
---|
| 257 | *
|
---|
| 258 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 259 | * @param pRdr The file provider instance.
|
---|
| 260 | * @param pvBase The base address of the image mapping.
|
---|
| 261 | * @param cSegments The number of segments in the array pointed to by paSegments.
|
---|
| 262 | * @param paSegments The segments thats going to be mapped.
|
---|
| 263 | */
|
---|
| 264 | KRDR_DECL(int) kRdrUnmap(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments)
|
---|
| 265 | {
|
---|
| 266 | KRDR_VALIDATE(pRdr);
|
---|
| 267 | return pRdr->pOps->pfnUnmap(pRdr, pvBase, cSegments, paSegments);
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 |
|
---|
| 271 | /**
|
---|
| 272 | * We're done reading from the file but would like to keep file mappings.
|
---|
| 273 | *
|
---|
| 274 | * If the OS support closing the file handle while the file is mapped,
|
---|
| 275 | * the reader should do so.
|
---|
| 276 | *
|
---|
| 277 | * @param pRdr The file provider instance.
|
---|
| 278 | */
|
---|
| 279 | KRDR_DECL(void) kRdrDone(PKRDR pRdr)
|
---|
| 280 | {
|
---|
| 281 | KRDR_VALIDATE_VOID(pRdr);
|
---|
| 282 | pRdr->pOps->pfnDone(pRdr);
|
---|
| 283 | }
|
---|
| 284 |
|
---|