[2] | 1 | /* $Id: kRdr.cpp 29 2009-07-01 20:30:29Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * kRdr - The File Provider.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[29] | 7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
|
---|
[2] | 8 | *
|
---|
[29] | 9 | * Permission is hereby granted, free of charge, to any person
|
---|
| 10 | * obtaining a copy of this software and associated documentation
|
---|
| 11 | * files (the "Software"), to deal in the Software without
|
---|
| 12 | * restriction, including without limitation the rights to use,
|
---|
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
| 14 | * copies of the Software, and to permit persons to whom the
|
---|
| 15 | * Software is furnished to do so, subject to the following
|
---|
| 16 | * conditions:
|
---|
[2] | 17 | *
|
---|
[29] | 18 | * The above copyright notice and this permission notice shall be
|
---|
| 19 | * included in all copies or substantial portions of the Software.
|
---|
[2] | 20 | *
|
---|
[29] | 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
| 28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
[2] | 29 | */
|
---|
| 30 |
|
---|
| 31 | /*******************************************************************************
|
---|
| 32 | * Header Files *
|
---|
| 33 | *******************************************************************************/
|
---|
| 34 | #include "kRdrInternal.h"
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | /*******************************************************************************
|
---|
| 38 | * Global Variables *
|
---|
| 39 | *******************************************************************************/
|
---|
| 40 | /** The list of file providers. */
|
---|
| 41 | static PCKRDROPS g_pRdrHead = &g_kRdrFileOps;
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Adds a new file provider.
|
---|
| 46 | *
|
---|
| 47 | * @param pAdd The new file provider.
|
---|
| 48 | */
|
---|
| 49 | KRDR_DECL(void) kRdrAddProvider(PKRDROPS pAdd)
|
---|
| 50 | {
|
---|
| 51 | pAdd->pNext = g_pRdrHead;
|
---|
| 52 | g_pRdrHead = pAdd;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
| 57 | * Tries to opens a file.
|
---|
| 58 | *
|
---|
| 59 | * @returns 0 on success, OS status code on failure.
|
---|
| 60 | * @param ppRdr Where to store the file provider instance.
|
---|
| 61 | * @param pszFilename The filename.
|
---|
| 62 | */
|
---|
| 63 | KRDR_DECL(int) kRdrOpen(PPKRDR ppRdr, const char *pszFilename)
|
---|
| 64 | {
|
---|
| 65 | int rc = -1;
|
---|
| 66 | PCKRDROPS pCur;
|
---|
| 67 | for (pCur = g_pRdrHead; pCur; pCur = pCur->pNext)
|
---|
| 68 | {
|
---|
| 69 | rc = pCur->pfnCreate(ppRdr, pszFilename);
|
---|
| 70 | if (!rc)
|
---|
| 71 | return 0;
|
---|
| 72 | }
|
---|
| 73 | return rc;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Closes the file.
|
---|
| 79 | *
|
---|
| 80 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 81 | * On failure, the file provider instance will be in an indeterminate state - don't touch it!
|
---|
| 82 | * @param pRdr The file provider instance.
|
---|
| 83 | */
|
---|
| 84 | KRDR_DECL(int) kRdrClose(PKRDR pRdr)
|
---|
| 85 | {
|
---|
| 86 | KRDR_VALIDATE(pRdr);
|
---|
| 87 | return pRdr->pOps->pfnDestroy(pRdr);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | /** Read bits from the file.
|
---|
| 92 | *
|
---|
| 93 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 94 | * @param pRdr The file provider instance.
|
---|
| 95 | * @param pvBuf Where to put the bits.
|
---|
| 96 | * @param cb The number of bytes to read.
|
---|
| 97 | * @param off Where to start reading.
|
---|
| 98 | */
|
---|
| 99 | KRDR_DECL(int) kRdrRead(PKRDR pRdr, void *pvBuf, KSIZE cb, KFOFF off)
|
---|
| 100 | {
|
---|
| 101 | KRDR_VALIDATE(pRdr);
|
---|
| 102 | return pRdr->pOps->pfnRead(pRdr, pvBuf, cb, off);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | /** Map all the file bits into memory (read only).
|
---|
| 107 | *
|
---|
| 108 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 109 | * @param pRdr The file provider instance.
|
---|
| 110 | * @param ppvBits Where to store the address of the mapping.
|
---|
| 111 | * The size can be obtained using pfnSize.
|
---|
| 112 | */
|
---|
| 113 | KRDR_DECL(int) kRdrAllMap(PKRDR pRdr, const void **ppvBits)
|
---|
| 114 | {
|
---|
| 115 | KRDR_VALIDATE(pRdr);
|
---|
| 116 | return pRdr->pOps->pfnAllMap(pRdr, ppvBits);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 | /** Unmap a file bits mapping obtained by KRDROPS::pfnAllMap.
|
---|
| 121 | *
|
---|
| 122 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 123 | * @param pRdr The file provider instance.
|
---|
| 124 | * @param pvBits The mapping address.
|
---|
| 125 | */
|
---|
| 126 | KRDR_DECL(int) kRdrAllUnmap(PKRDR pRdr, const void *pvBits)
|
---|
| 127 | {
|
---|
| 128 | KRDR_VALIDATE(pRdr);
|
---|
| 129 | return pRdr->pOps->pfnAllUnmap(pRdr, pvBits);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | /** Get the file size.
|
---|
| 134 | *
|
---|
| 135 | * @returns The file size. Returns -1 on failure.
|
---|
| 136 | * @param pRdr The file provider instance.
|
---|
| 137 | */
|
---|
| 138 | KRDR_DECL(KFOFF) kRdrSize(PKRDR pRdr)
|
---|
| 139 | {
|
---|
| 140 | KRDR_VALIDATE(pRdr);
|
---|
| 141 | return pRdr->pOps->pfnSize(pRdr);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 | /** Get the file pointer offset.
|
---|
| 146 | *
|
---|
| 147 | * @returns The file pointer offset. Returns -1 on failure.
|
---|
| 148 | * @param pRdr The file provider instance.
|
---|
| 149 | */
|
---|
| 150 | KRDR_DECL(KFOFF) kRdrTell(PKRDR pRdr)
|
---|
| 151 | {
|
---|
| 152 | KRDR_VALIDATE(pRdr);
|
---|
| 153 | return pRdr->pOps->pfnTell(pRdr);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /** Get the file name.
|
---|
| 158 | *
|
---|
| 159 | * @returns The file name. Returns NULL on failure.
|
---|
| 160 | * @param pRdr The file provider instance.
|
---|
| 161 | */
|
---|
| 162 | KRDR_DECL(const char *) kRdrName(PKRDR pRdr)
|
---|
| 163 | {
|
---|
| 164 | KRDR_VALIDATE_EX(pRdr, NULL);
|
---|
| 165 | return pRdr->pOps->pfnName(pRdr);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | /** Get the native file handle if possible.
|
---|
| 170 | *
|
---|
| 171 | * @returns The native file handle. Returns -1 if not available.
|
---|
| 172 | * @param pRdr The file provider instance.
|
---|
| 173 | */
|
---|
| 174 | KRDR_DECL(KIPTR) kRdrNativeFH(PKRDR pRdr)
|
---|
| 175 | {
|
---|
| 176 | KRDR_VALIDATE_EX(pRdr, -1);
|
---|
| 177 | return pRdr->pOps->pfnNativeFH(pRdr);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
| 181 | /**
|
---|
| 182 | * Gets the page size used when mapping sections of the file.
|
---|
| 183 | *
|
---|
| 184 | * @returns The page size.
|
---|
| 185 | * @param pRdr The file provider instance.
|
---|
| 186 | */
|
---|
| 187 | KRDR_DECL(KSIZE) kRdrPageSize(PKRDR pRdr)
|
---|
| 188 | {
|
---|
| 189 | KRDR_VALIDATE_EX(pRdr, 0x10000);
|
---|
| 190 | return pRdr->pOps->pfnPageSize(pRdr);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | /**
|
---|
| 195 | * Maps the segments of a image into memory.
|
---|
| 196 | *
|
---|
| 197 | * The file reader will be using the RVA member of each segment to figure out where
|
---|
| 198 | * it goes relative to the image base address.
|
---|
| 199 | *
|
---|
| 200 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 201 | * @param pRdr The file provider instance.
|
---|
| 202 | * @param ppvBase On input when fFixed is set, this contains the base address of the mapping.
|
---|
| 203 | * On output this contains the base of the image mapping.
|
---|
| 204 | * @param cSegments The number of segments in the array pointed to by paSegments.
|
---|
| 205 | * @param paSegments The segments thats going to be mapped.
|
---|
| 206 | * @param fFixed If set, the address at *ppvBase should be the base address of the mapping.
|
---|
| 207 | */
|
---|
| 208 | KRDR_DECL(int) kRdrMap(PKRDR pRdr, void **ppvBase, KU32 cSegments, PCKLDRSEG paSegments, KBOOL fFixed)
|
---|
| 209 | {
|
---|
| 210 | KRDR_VALIDATE(pRdr);
|
---|
| 211 | return pRdr->pOps->pfnMap(pRdr, ppvBase, cSegments, paSegments, fFixed);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | /**
|
---|
| 216 | * Reloads dirty pages in mapped image.
|
---|
| 217 | *
|
---|
| 218 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 219 | * @param pRdr The file provider instance.
|
---|
| 220 | * @param pvBase The base address of the image mapping.
|
---|
| 221 | * @param cSegments The number of segments in the array pointed to by paSegments.
|
---|
| 222 | * @param paSegments The segments thats going to be mapped.
|
---|
| 223 | */
|
---|
| 224 | KRDR_DECL(int) kRdrRefresh(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments)
|
---|
| 225 | {
|
---|
| 226 | KRDR_VALIDATE(pRdr);
|
---|
| 227 | return pRdr->pOps->pfnRefresh(pRdr, pvBase, cSegments, paSegments);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
| 232 | * Protects or unprotects an image mapping.
|
---|
| 233 | *
|
---|
| 234 | * This is typically used for getting write access to read or execute only
|
---|
| 235 | * pages while applying fixups.
|
---|
| 236 | *
|
---|
| 237 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 238 | * @param pRdr The file provider instance.
|
---|
| 239 | * @param pvBase The base address of the image mapping.
|
---|
| 240 | * @param cSegments The number of segments in the array pointed to by paSegments.
|
---|
| 241 | * @param paSegments The segments thats going to be mapped.
|
---|
| 242 | * @param fUnprotectOrProtect When set the all mapped segments are made writable.
|
---|
| 243 | * When clean the segment protection is restored.
|
---|
| 244 | */
|
---|
| 245 | KRDR_DECL(int) kRdrProtect(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments, KBOOL fUnprotectOrProtect)
|
---|
| 246 | {
|
---|
| 247 | KRDR_VALIDATE(pRdr);
|
---|
| 248 | return pRdr->pOps->pfnProtect(pRdr, pvBase, cSegments, paSegments, fUnprotectOrProtect);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 |
|
---|
| 252 | /**
|
---|
| 253 | * Unmaps a image mapping.
|
---|
| 254 | *
|
---|
| 255 | * @returns 0 on success, OS specific error code on failure.
|
---|
| 256 | * @param pRdr The file provider instance.
|
---|
| 257 | * @param pvBase The base address of the image mapping.
|
---|
| 258 | * @param cSegments The number of segments in the array pointed to by paSegments.
|
---|
| 259 | * @param paSegments The segments thats going to be mapped.
|
---|
| 260 | */
|
---|
| 261 | KRDR_DECL(int) kRdrUnmap(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments)
|
---|
| 262 | {
|
---|
| 263 | KRDR_VALIDATE(pRdr);
|
---|
| 264 | return pRdr->pOps->pfnUnmap(pRdr, pvBase, cSegments, paSegments);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 |
|
---|
| 268 | /**
|
---|
| 269 | * We're done reading from the file but would like to keep file mappings.
|
---|
| 270 | *
|
---|
| 271 | * If the OS support closing the file handle while the file is mapped,
|
---|
| 272 | * the reader should do so.
|
---|
| 273 | *
|
---|
| 274 | * @param pRdr The file provider instance.
|
---|
| 275 | */
|
---|
| 276 | KRDR_DECL(void) kRdrDone(PKRDR pRdr)
|
---|
| 277 | {
|
---|
| 278 | KRDR_VALIDATE_VOID(pRdr);
|
---|
| 279 | pRdr->pOps->pfnDone(pRdr);
|
---|
| 280 | }
|
---|
| 281 |
|
---|