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