1 | /* $Id: kDbgHlpCrt.cpp 77 2016-06-22 17:03:55Z 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 | else
|
---|
161 | cbFile = -1;
|
---|
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 |
|
---|