68032b7fd5654a4ed47ea6360b05a6d35f60ff06
[fw/openocd] / src / helper / replacements.c
1 /***************************************************************************
2  *   Copyright (C) 2006 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   Copyright (C) 2008 by Spencer Oliver                                  *
9  *   spen@spen-soft.co.uk                                                  *
10  *                                                                         *
11  *   This program 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  *   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. *
23  ***************************************************************************/
24 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
25
26 #include <stdlib.h>
27 #include <string.h>
28 /*
29  * clear_malloc
30  *
31  * will alloc memory and clear it
32  */
33 void *clear_malloc(size_t size)
34 {
35         void *t = malloc(size);
36         if (t)
37                 memset(t, 0x00, size);
38         return t;
39 }
40
41 void *fill_malloc(size_t size)
42 {
43         void *t = malloc(size);
44         if (t) {
45                 /* We want to initialize memory to some known bad state.
46                  * 0 and 0xff yields 0 and -1 as integers, which often
47                  * have meaningful values. 0x5555... is not often a valid
48                  * integer and is quite easily spotted in the debugger
49                  * also it is almost certainly an invalid address */
50                 memset(t, 0x55, size);
51         }
52         return t;
53 }
54
55 #define IN_REPLACEMENTS_C
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59 #ifdef HAVE_STRINGS_H
60 #include <strings.h>
61 #endif
62
63 #ifdef _WIN32
64 #include <io.h>
65 #include <winsock2.h>
66 #endif
67
68 /* replacements for gettimeofday */
69 #ifndef HAVE_GETTIMEOFDAY
70
71 /* Windows */
72 #ifdef _WIN32
73
74 #ifndef __GNUC__
75 #define EPOCHFILETIME (116444736000000000i64)
76 #else
77 #define EPOCHFILETIME (116444736000000000LL)
78 #endif
79
80 int gettimeofday(struct timeval *tv, struct timezone *tz)
81 {
82         FILETIME ft;
83         LARGE_INTEGER li;
84         __int64 t;
85         static int tzflag;
86
87         if (tv) {
88                 GetSystemTimeAsFileTime(&ft);
89                 li.LowPart  = ft.dwLowDateTime;
90                 li.HighPart = ft.dwHighDateTime;
91                 t  = li.QuadPart;                                       /* In 100-nanosecond intervals */
92                 t -= EPOCHFILETIME;                                     /* Offset to the Epoch time */
93                 t /= 10;                                                        /* In microseconds */
94                 tv->tv_sec  = (long)(t / 1000000);
95                 tv->tv_usec = (long)(t % 1000000);
96         }
97
98         if (tz) {
99                 if (!tzflag) {
100                         _tzset();
101                         tzflag++;
102                 }
103                 tz->tz_minuteswest = _timezone / 60;
104                 tz->tz_dsttime = _daylight;
105         }
106
107         return 0;
108 }
109 #endif  /* _WIN32 */
110
111 #endif  /* HAVE_GETTIMEOFDAY */
112
113 #ifndef HAVE_STRNLEN
114 size_t strnlen(const char *s, size_t maxlen)
115 {
116         const char *end = (const char *)memchr(s, '\0', maxlen);
117         return end ? (size_t) (end - s) : maxlen;
118 }
119 #endif
120
121 #ifndef HAVE_STRNDUP
122 char *strndup(const char *s, size_t n)
123 {
124         size_t len = strnlen(s, n);
125         char *new = malloc(len + 1);
126
127         if (!new)
128                 return NULL;
129
130         new[len] = '\0';
131         return (char *) memcpy(new, s, len);
132 }
133 #endif
134
135 #ifdef _WIN32
136 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
137 {
138         DWORD ms_total, limit;
139         HANDLE handles[MAXIMUM_WAIT_OBJECTS];
140         int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
141         int n_handles = 0, i;
142         fd_set sock_read, sock_write, sock_except;
143         fd_set aread, awrite, aexcept;
144         int sock_max_fd = -1;
145         struct timeval tvslice;
146         int retcode;
147
148 #define SAFE_FD_ISSET(fd, set)  (set != NULL && FD_ISSET(fd, set))
149
150         /* calculate how long we need to wait in milliseconds */
151         if (!tv)
152                 ms_total = INFINITE;
153         else {
154                 ms_total = tv->tv_sec * 1000;
155                 ms_total += tv->tv_usec / 1000;
156         }
157
158         FD_ZERO(&sock_read);
159         FD_ZERO(&sock_write);
160         FD_ZERO(&sock_except);
161
162         /* build an array of handles for non-sockets */
163         for (i = 0; i < max_fd; i++) {
164                 if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
165                         intptr_t handle = (intptr_t) _get_osfhandle(i);
166                         handles[n_handles] = (HANDLE)handle;
167                         if (handles[n_handles] == INVALID_HANDLE_VALUE) {
168                                 /* socket */
169                                 if (SAFE_FD_ISSET(i, rfds))
170                                         FD_SET(i, &sock_read);
171                                 if (SAFE_FD_ISSET(i, wfds))
172                                         FD_SET(i, &sock_write);
173                                 if (SAFE_FD_ISSET(i, efds))
174                                         FD_SET(i, &sock_except);
175                                 if (i > sock_max_fd)
176                                         sock_max_fd = i;
177                         } else {
178                                 handle_slot_to_fd[n_handles] = i;
179                                 n_handles++;
180                         }
181                 }
182         }
183
184         if (n_handles == 0) {
185                 /* plain sockets only - let winsock handle the whole thing */
186                 return select(max_fd, rfds, wfds, efds, tv);
187         }
188
189         /* mixture of handles and sockets; lets multiplex between
190          * winsock and waiting on the handles */
191
192         FD_ZERO(&aread);
193         FD_ZERO(&awrite);
194         FD_ZERO(&aexcept);
195
196         limit = GetTickCount() + ms_total;
197         do {
198                 retcode = 0;
199
200                 if (sock_max_fd >= 0) {
201                         /* overwrite the zero'd sets here; the select call
202                          * will clear those that are not active */
203                         aread = sock_read;
204                         awrite = sock_write;
205                         aexcept = sock_except;
206
207                         tvslice.tv_sec = 0;
208                         tvslice.tv_usec = 1000;
209
210                         retcode = select(sock_max_fd + 1, &aread, &awrite, &aexcept, &tvslice);
211                 }
212
213                 if (n_handles > 0) {
214                         /* check handles */
215                         DWORD wret;
216
217                         wret = MsgWaitForMultipleObjects(n_handles,
218                                         handles,
219                                         FALSE,
220                                         retcode > 0 ? 0 : 1,
221                                         QS_ALLEVENTS);
222
223                         if (wret == WAIT_TIMEOUT) {
224                                 /* set retcode to 0; this is the default.
225                                  * select() may have set it to something else,
226                                  * in which case we leave it alone, so this branch
227                                  * does nothing */
228                                 ;
229                         } else if (wret == WAIT_FAILED) {
230                                 if (retcode == 0)
231                                         retcode = -1;
232                         } else {
233                                 if (retcode < 0)
234                                         retcode = 0;
235                                 for (i = 0; i < n_handles; i++) {
236                                         if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
237                                                 if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
238                                                         DWORD bytes;
239                                                         intptr_t handle = (intptr_t) _get_osfhandle(
240                                                                         handle_slot_to_fd[i]);
241
242                                                         if (PeekNamedPipe((HANDLE)handle, NULL, 0,
243                                                                     NULL, &bytes, NULL)) {
244                                                                 /* check to see if gdb pipe has data available */
245                                                                 if (bytes) {
246                                                                         FD_SET(handle_slot_to_fd[i], &aread);
247                                                                         retcode++;
248                                                                 }
249                                                         } else {
250                                                                 FD_SET(handle_slot_to_fd[i], &aread);
251                                                                 retcode++;
252                                                         }
253                                                 }
254                                                 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
255                                                         FD_SET(handle_slot_to_fd[i], &awrite);
256                                                         retcode++;
257                                                 }
258                                                 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
259                                                         FD_SET(handle_slot_to_fd[i], &aexcept);
260                                                         retcode++;
261                                                 }
262                                         }
263                                 }
264                         }
265                 }
266         } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
267
268         if (rfds)
269                 *rfds = aread;
270         if (wfds)
271                 *wfds = awrite;
272         if (efds)
273                 *efds = aexcept;
274
275         return retcode;
276 }
277 #endif
278
279 #if defined HAVE_LIBUSB1 && !defined HAVE_LIBUSB_ERROR_NAME
280 #include <libusb.h>
281 /* Verbatim from git://git.libusb.org/libusb.git tag 1.0.9
282  * The libusb_error enum is compatible down to v0.9.1
283  */
284 const char *libusb_error_name(int error_code)
285 {
286         enum libusb_error error = error_code;
287         switch (error) {
288         case LIBUSB_SUCCESS:
289                 return "LIBUSB_SUCCESS";
290         case LIBUSB_ERROR_IO:
291                 return "LIBUSB_ERROR_IO";
292         case LIBUSB_ERROR_INVALID_PARAM:
293                 return "LIBUSB_ERROR_INVALID_PARAM";
294         case LIBUSB_ERROR_ACCESS:
295                 return "LIBUSB_ERROR_ACCESS";
296         case LIBUSB_ERROR_NO_DEVICE:
297                 return "LIBUSB_ERROR_NO_DEVICE";
298         case LIBUSB_ERROR_NOT_FOUND:
299                 return "LIBUSB_ERROR_NOT_FOUND";
300         case LIBUSB_ERROR_BUSY:
301                 return "LIBUSB_ERROR_BUSY";
302         case LIBUSB_ERROR_TIMEOUT:
303                 return "LIBUSB_ERROR_TIMEOUT";
304         case LIBUSB_ERROR_OVERFLOW:
305                 return "LIBUSB_ERROR_OVERFLOW";
306         case LIBUSB_ERROR_PIPE:
307                 return "LIBUSB_ERROR_PIPE";
308         case LIBUSB_ERROR_INTERRUPTED:
309                 return "LIBUSB_ERROR_INTERRUPTED";
310         case LIBUSB_ERROR_NO_MEM:
311                 return "LIBUSB_ERROR_NO_MEM";
312         case LIBUSB_ERROR_NOT_SUPPORTED:
313                 return "LIBUSB_ERROR_NOT_SUPPORTED";
314         case LIBUSB_ERROR_OTHER:
315                 return "LIBUSB_ERROR_OTHER";
316         }
317         return "**UNKNOWN**";
318 }
319 #endif