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