1 | /* $Id: kDbgHlpCrt.cpp 29 2009-07-01 20:30:29Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * kDbg - The Debug Info Reader, Helpers, CRT Based Implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
|
---|
8 | *
|
---|
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:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
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.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #include "kDbgHlp.h"
|
---|
35 | #include "kDbg.h"
|
---|
36 |
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <string.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #ifdef _MSC_VER
|
---|
42 | # include <io.h>
|
---|
43 | #endif
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * The stdio base implementation of KDBGHLPFILE.
|
---|
49 | */
|
---|
50 | typedef struct KDBGHLPFILE
|
---|
51 | {
|
---|
52 | /** Pointer to the stdio file stream. */
|
---|
53 | FILE *pStrm;
|
---|
54 | } KDBGHLPFILE;
|
---|
55 |
|
---|
56 | /** @def HAVE_FSEEKO
|
---|
57 | * Define HAVE_FSEEKO to indicate that fseeko and ftello should be used. */
|
---|
58 | #if !defined(_MSC_VER)
|
---|
59 | # define HAVE_FSEEKO
|
---|
60 | #endif
|
---|
61 |
|
---|
62 |
|
---|
63 | void *kDbgHlpAlloc(size_t cb)
|
---|
64 | {
|
---|
65 | return malloc(cb);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | void *kDbgHlpAllocZ(size_t cb)
|
---|
70 | {
|
---|
71 | return calloc(1, cb);
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | void *kDbgHlpAllocDup(const void *pv, size_t cb)
|
---|
76 | {
|
---|
77 | void *pvNew = malloc(cb);
|
---|
78 | if (pvNew)
|
---|
79 | memcpy(pvNew, pv, cb);
|
---|
80 | return pvNew;
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | void *kDbgHlpReAlloc(void *pv, size_t cb)
|
---|
85 | {
|
---|
86 | return realloc(pv, cb);
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | void kDbgHlpFree(void *pv)
|
---|
91 | {
|
---|
92 | free(pv);
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | int kDbgHlpCrtConvErrno(int rc)
|
---|
97 | {
|
---|
98 | switch (rc)
|
---|
99 | {
|
---|
100 | case 0: return 0;
|
---|
101 | case EINVAL: return KERR_INVALID_PARAMETER;
|
---|
102 | case ENOMEM: return KERR_NO_MEMORY;
|
---|
103 | case EISDIR:
|
---|
104 | case ENOENT: return KERR_FILE_NOT_FOUND;
|
---|
105 | default: return KERR_GENERAL_FAILURE;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | int kDbgHlpOpenRO(const char *pszFilename, PKDBGHLPFILE *ppFile)
|
---|
111 | {
|
---|
112 | PKDBGHLPFILE pFile = (PKDBGHLPFILE)kDbgHlpAlloc(sizeof(*pFile));
|
---|
113 | if (!pFile)
|
---|
114 | return KERR_NO_MEMORY;
|
---|
115 |
|
---|
116 | pFile->pStrm = fopen(pszFilename, "rb");
|
---|
117 | if (pFile->pStrm)
|
---|
118 | {
|
---|
119 | *ppFile = pFile;
|
---|
120 | return 0;
|
---|
121 | }
|
---|
122 | return kDbgHlpCrtConvErrno(errno);
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | void kDbgHlpClose(PKDBGHLPFILE pFile)
|
---|
127 | {
|
---|
128 | if (pFile)
|
---|
129 | {
|
---|
130 | fclose(pFile->pStrm);
|
---|
131 | pFile->pStrm = NULL;
|
---|
132 | kDbgHlpFree(pFile);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | uintptr_t kDbgHlpNativeFileHandle(PKDBGHLPFILE pFile)
|
---|
138 | {
|
---|
139 | int fd = fileno(pFile->pStrm);
|
---|
140 | #ifdef _MSC_VER
|
---|
141 | return _get_osfhandle(fd);
|
---|
142 | #else
|
---|
143 | return fd;
|
---|
144 | #endif
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | int64_t kDbgHlpFileSize(PKDBGHLPFILE pFile)
|
---|
149 | {
|
---|
150 | int64_t cbFile;
|
---|
151 | int64_t offCur = kDbgHlpTell(pFile);
|
---|
152 | if (offCur >= 0)
|
---|
153 | {
|
---|
154 | if (kDbgHlpSeekByEnd(pFile, 0) == 0)
|
---|
155 | cbFile = kDbgHlpTell(pFile);
|
---|
156 | else
|
---|
157 | cbFile = -1;
|
---|
158 | kDbgHlpSeek(pFile, offCur);
|
---|
159 | }
|
---|
160 | return cbFile;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | int kDbgHlpReadAt(PKDBGHLPFILE pFile, int64_t off, void *pv, size_t cb)
|
---|
165 | {
|
---|
166 | int rc = kDbgHlpSeek(pFile, off);
|
---|
167 | if (!rc)
|
---|
168 | rc = kDbgHlpRead(pFile, pv, cb);
|
---|
169 | return rc;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | int kDbgHlpRead(PKDBGHLPFILE pFile, void *pv, size_t cb)
|
---|
174 | {
|
---|
175 | if (fread(pv, cb, 1, pFile->pStrm) == 1)
|
---|
176 | return 0;
|
---|
177 | return -1;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | int kDbgHlpSeek(PKDBGHLPFILE pFile, int64_t off)
|
---|
182 | {
|
---|
183 | #ifdef HAVE_FSEEKO
|
---|
184 | if (!fseeko(pFile->pStrm, off, SEEK_SET))
|
---|
185 | return 0;
|
---|
186 | #else
|
---|
187 | long l = (long)off;
|
---|
188 | if (l != off)
|
---|
189 | return KERR_OUT_OF_RANGE;
|
---|
190 | if (!fseek(pFile->pStrm, l, SEEK_SET))
|
---|
191 | return 0;
|
---|
192 | #endif
|
---|
193 | return kDbgHlpCrtConvErrno(errno);
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | int kDbgHlpSeekByCur(PKDBGHLPFILE pFile, int64_t off)
|
---|
198 | {
|
---|
199 | #ifdef HAVE_FSEEKO
|
---|
200 | if (!fseeko(pFile->pStrm, off, SEEK_CUR))
|
---|
201 | return 0;
|
---|
202 | #else
|
---|
203 | long l = (long)off;
|
---|
204 | if (l != off)
|
---|
205 | return KERR_OUT_OF_RANGE;
|
---|
206 | if (!fseek(pFile->pStrm, l, SEEK_CUR))
|
---|
207 | return 0;
|
---|
208 | #endif
|
---|
209 | return kDbgHlpCrtConvErrno(errno);
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | int kDbgHlpSeekByEnd(PKDBGHLPFILE pFile, int64_t off)
|
---|
214 | {
|
---|
215 | #ifdef HAVE_FSEEKO
|
---|
216 | if (!fseeko(pFile->pStrm, -off, SEEK_END))
|
---|
217 | return 0;
|
---|
218 | #else
|
---|
219 | long l = (long)off;
|
---|
220 | if (l != off)
|
---|
221 | return KERR_OUT_OF_RANGE;
|
---|
222 | if (!fseek(pFile->pStrm, -l, SEEK_END))
|
---|
223 | return 0;
|
---|
224 | #endif
|
---|
225 | return kDbgHlpCrtConvErrno(errno);
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | int64_t kDbgHlpTell(PKDBGHLPFILE pFile)
|
---|
230 | {
|
---|
231 | #ifdef HAVE_FSEEKO
|
---|
232 | return ftello(pFile->pStrm);
|
---|
233 | #else
|
---|
234 | return ftell(pFile->pStrm);
|
---|
235 | #endif
|
---|
236 | }
|
---|
237 |
|
---|