source: trunk/kStuff/kRdr/kRdr.cpp

Last change on this file was 3611, checked in by bird, 18 years ago

license update.

  • Property svn:keywords set to Id
File size: 8.8 KB
RevLine 
[2826]1/* $Id: kRdr.cpp 3611 2007-10-29 03:37:27Z bird $ */
[2825]2/** @file
[3543]3 * kRdr - The File Provider.
4 */
5
6/*
[3611]7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
[2825]8 *
[3543]9 * This file is part of kStuff.
[2825]10 *
[3611]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.
[2825]15 *
[3611]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 *
[3543]22 * kStuff is distributed in the hope that it will be useful,
[2825]23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
[3611]24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
[2825]26 *
[3611]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
[2825]31 */
32
33
34/*******************************************************************************
35* Header Files *
36*******************************************************************************/
[3543]37#include "kRdrInternal.h"
[2825]38
39
40/*******************************************************************************
41* Global Variables *
42*******************************************************************************/
43/** The list of file providers. */
[3543]44static PCKRDROPS g_pRdrHead = &g_kRdrFileOps;
[2825]45
46
47/**
48 * Adds a new file provider.
49 *
50 * @param pAdd The new file provider.
51 */
[3578]52KRDR_DECL(void) kRdrAddProvider(PKRDROPS pAdd)
[2825]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 */
[3578]66KRDR_DECL(int) kRdrOpen(PPKRDR ppRdr, const char *pszFilename)
[2825]67{
[2893]68 int rc = -1;
[3543]69 PCKRDROPS pCur;
[2825]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 */
[3578]87KRDR_DECL(int) kRdrClose(PKRDR pRdr)
[2825]88{
[3543]89 KRDR_VALIDATE(pRdr);
[2825]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 */
[3578]102KRDR_DECL(int) kRdrRead(PKRDR pRdr, void *pvBuf, KSIZE cb, KFOFF off)
[2825]103{
[3543]104 KRDR_VALIDATE(pRdr);
[2825]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 */
[3578]116KRDR_DECL(int) kRdrAllMap(PKRDR pRdr, const void **ppvBits)
[2825]117{
[3543]118 KRDR_VALIDATE(pRdr);
[2825]119 return pRdr->pOps->pfnAllMap(pRdr, ppvBits);
120}
121
122
[3543]123/** Unmap a file bits mapping obtained by KRDROPS::pfnAllMap.
[2825]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 */
[3578]129KRDR_DECL(int) kRdrAllUnmap(PKRDR pRdr, const void *pvBits)
[2825]130{
[3543]131 KRDR_VALIDATE(pRdr);
[2825]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 */
[3578]141KRDR_DECL(KFOFF) kRdrSize(PKRDR pRdr)
[2825]142{
[3543]143 KRDR_VALIDATE(pRdr);
[2825]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 */
[3578]153KRDR_DECL(KFOFF) kRdrTell(PKRDR pRdr)
[2825]154{
[3543]155 KRDR_VALIDATE(pRdr);
[2825]156 return pRdr->pOps->pfnTell(pRdr);
157}
158
159
160/** Get the file name.
161 *
[2856]162 * @returns The file name. Returns NULL on failure.
[2825]163 * @param pRdr The file provider instance.
164 */
[3578]165KRDR_DECL(const char *) kRdrName(PKRDR pRdr)
[2825]166{
[3543]167 KRDR_VALIDATE_EX(pRdr, NULL);
[2825]168 return pRdr->pOps->pfnName(pRdr);
169}
170
171
[3543]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 */
[3578]177KRDR_DECL(KIPTR) kRdrNativeFH(PKRDR pRdr)
[3543]178{
179 KRDR_VALIDATE_EX(pRdr, -1);
180 return pRdr->pOps->pfnNativeFH(pRdr);
181}
182
183
[2856]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 */
[3578]190KRDR_DECL(KSIZE) kRdrPageSize(PKRDR pRdr)
[2856]191{
[3543]192 KRDR_VALIDATE_EX(pRdr, 0x10000);
[2856]193 return pRdr->pOps->pfnPageSize(pRdr);
194}
195
196
197/**
[2861]198 * Maps the segments of a image into memory.
[2856]199 *
[2861]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.
[2856]202 *
203 * @returns 0 on success, OS specific error code on failure.
204 * @param pRdr The file provider instance.
[2861]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.
[2856]210 */
[3578]211KRDR_DECL(int) kRdrMap(PKRDR pRdr, void **ppvBase, KU32 cSegments, PCKLDRSEG paSegments, KBOOL fFixed)
[2856]212{
[3543]213 KRDR_VALIDATE(pRdr);
[2861]214 return pRdr->pOps->pfnMap(pRdr, ppvBase, cSegments, paSegments, fFixed);
[2856]215}
216
217
218/**
[2861]219 * Reloads dirty pages in mapped image.
[2856]220 *
221 * @returns 0 on success, OS specific error code on failure.
222 * @param pRdr The file provider instance.
[2861]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.
[2856]226 */
[3578]227KRDR_DECL(int) kRdrRefresh(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments)
[2856]228{
[3543]229 KRDR_VALIDATE(pRdr);
[2861]230 return pRdr->pOps->pfnRefresh(pRdr, pvBase, cSegments, paSegments);
[2856]231}
232
233
234/**
[2861]235 * Protects or unprotects an image mapping.
[2856]236 *
[2861]237 * This is typically used for getting write access to read or execute only
238 * pages while applying fixups.
[2856]239 *
240 * @returns 0 on success, OS specific error code on failure.
241 * @param pRdr The file provider instance.
[2861]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.
[2856]247 */
[3578]248KRDR_DECL(int) kRdrProtect(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments, KBOOL fUnprotectOrProtect)
[2856]249{
[3543]250 KRDR_VALIDATE(pRdr);
[2861]251 return pRdr->pOps->pfnProtect(pRdr, pvBase, cSegments, paSegments, fUnprotectOrProtect);
[2856]252}
253
254
255/**
[2861]256 * Unmaps a image mapping.
[2856]257 *
258 * @returns 0 on success, OS specific error code on failure.
259 * @param pRdr The file provider instance.
[2861]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.
[2856]263 */
[3578]264KRDR_DECL(int) kRdrUnmap(PKRDR pRdr, void *pvBase, KU32 cSegments, PCKLDRSEG paSegments)
[2856]265{
[3543]266 KRDR_VALIDATE(pRdr);
[2861]267 return pRdr->pOps->pfnUnmap(pRdr, pvBase, cSegments, paSegments);
[2856]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 */
[3578]279KRDR_DECL(void) kRdrDone(PKRDR pRdr)
[2856]280{
[3543]281 KRDR_VALIDATE_VOID(pRdr);
[2856]282 pRdr->pOps->pfnDone(pRdr);
283}
284
Note: See TracBrowser for help on using the repository browser.