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