1 | /* $Id: $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * kLdr - The Dynamic Loader, file abstraction.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of kLdr.
|
---|
10 | *
|
---|
11 | * kLdr is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kLdr 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 General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kLdr; 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 <kLdr.h>
|
---|
32 | #include "kLdrInternal.h"
|
---|
33 |
|
---|
34 |
|
---|
35 | /*******************************************************************************
|
---|
36 | * Global Variables *
|
---|
37 | *******************************************************************************/
|
---|
38 | /** The list of file providers. */
|
---|
39 | static PCKLDRRDROPS g_pRdrHead = &g_kLdrRdrFileOps;
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Adds a new file provider.
|
---|
44 | *
|
---|
45 | * @param pAdd The new file provider.
|
---|
46 | */
|
---|
47 | void kLdrRdrAddProvider(PKLDRRDROPS pAdd)
|
---|
48 | {
|
---|
49 | pAdd->pNext = g_pRdrHead;
|
---|
50 | g_pRdrHead = pAdd;
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Tries to opens a file.
|
---|
56 | *
|
---|
57 | * @returns 0 on success, OS status code on failure.
|
---|
58 | * @param ppRdr Where to store the file provider instance.
|
---|
59 | * @param pszFilename The filename.
|
---|
60 | */
|
---|
61 | int kLdrRdrOpen(PPKLDRRDR ppRdr, const char *pszFilename)
|
---|
62 | {
|
---|
63 | int rc;
|
---|
64 | PCKLDRRDROPS pCur;
|
---|
65 | for (pCur = g_pRdrHead; pCur; pCur = pCur->pNext)
|
---|
66 | {
|
---|
67 | rc = pCur->pfnCreate(ppRdr, pszFilename);
|
---|
68 | if (!rc)
|
---|
69 | return 0;
|
---|
70 | }
|
---|
71 | return rc;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Closes the file.
|
---|
77 | *
|
---|
78 | * @returns 0 on success, OS specific error code on failure.
|
---|
79 | * On failure, the file provider instance will be in an indeterminate state - don't touch it!
|
---|
80 | * @param pRdr The file provider instance.
|
---|
81 | */
|
---|
82 | int kLdrRdrClose(PKLDRRDR pRdr)
|
---|
83 | {
|
---|
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 kLdrRdrRead(PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off)
|
---|
97 | {
|
---|
98 | return pRdr->pOps->pfnRead(pRdr, pvBuf, cb, off);
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | /** Map all the file bits into memory (read only).
|
---|
103 | *
|
---|
104 | * @returns 0 on success, OS specific error code on failure.
|
---|
105 | * @param pRdr The file provider instance.
|
---|
106 | * @param ppvBits Where to store the address of the mapping.
|
---|
107 | * The size can be obtained using pfnSize.
|
---|
108 | */
|
---|
109 | int kLdrRdrAllMap(PKLDRRDR pRdr, const void **ppvBits)
|
---|
110 | {
|
---|
111 | return pRdr->pOps->pfnAllMap(pRdr, ppvBits);
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /** Unmap a file bits mapping obtained by KLDRRDROPS::pfnAllMap.
|
---|
116 | *
|
---|
117 | * @returns 0 on success, OS specific error code on failure.
|
---|
118 | * @param pRdr The file provider instance.
|
---|
119 | * @param pvBits The mapping address.
|
---|
120 | */
|
---|
121 | int kLdrRdrAllUnmap(PKLDRRDR pRdr, const void *pvBits)
|
---|
122 | {
|
---|
123 | return pRdr->pOps->pfnAllUnmap(pRdr, pvBits);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | /** Get the file size.
|
---|
128 | *
|
---|
129 | * @returns The file size. Returns -1 on failure.
|
---|
130 | * @param pRdr The file provider instance.
|
---|
131 | */
|
---|
132 | off_t kLdrRdrSize(PKLDRRDR pRdr)
|
---|
133 | {
|
---|
134 | return pRdr->pOps->pfnSize(pRdr);
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /** Get the file pointer offset.
|
---|
139 | *
|
---|
140 | * @returns The file pointer offset. Returns -1 on failure.
|
---|
141 | * @param pRdr The file provider instance.
|
---|
142 | */
|
---|
143 | off_t kLdrRdrTell(PKLDRRDR pRdr)
|
---|
144 | {
|
---|
145 | return pRdr->pOps->pfnTell(pRdr);
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /** Get the file name.
|
---|
150 | *
|
---|
151 | * @returns The file size. Returns -1 on failure.
|
---|
152 | * @param pRdr The file provider instance.
|
---|
153 | */
|
---|
154 | const char *kLdrRdrName(PKLDRRDR pRdr)
|
---|
155 | {
|
---|
156 | return pRdr->pOps->pfnName(pRdr);
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|