source: trunk/kStuff/kRdr/kRdr.cpp@ 3546

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

use .cpp. some string fixes.

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