You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

272 lines
7.6 KiB

  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, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifndef REPLACEMENTS_H
  27. #define REPLACEMENTS_H
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include "types.h"
  32. #if BUILD_ECOSBOARD
  33. #include <pkgconf/system.h>
  34. #include <stdlib.h>
  35. #include <sys/select.h>
  36. #endif
  37. /* include necessary headers for socket functionality */
  38. #ifdef _WIN32
  39. #include <winsock2.h>
  40. #else
  41. #include <sys/socket.h>
  42. #include <sys/poll.h>
  43. #include <netinet/in.h>
  44. #include <unistd.h>
  45. #include <fcntl.h>
  46. #endif
  47. #ifdef HAVE_SYS_PARAM_H
  48. #include <sys/param.h> /* for MIN/MAX macros */
  49. #endif
  50. /* MIN,MAX macros */
  51. #ifndef MIN
  52. #define MIN(a,b) (((a)<(b))?(a):(b))
  53. #endif
  54. #ifndef MAX
  55. #define MAX(a,b) (((a)>(b))?(a):(b))
  56. #endif
  57. /* for systems that do not support ENOTSUP
  58. * win32 being one of them */
  59. #ifndef ENOTSUP
  60. #define ENOTSUP 134 /* Not supported */
  61. #endif
  62. #ifndef HAVE_SYS_TIME_H
  63. #ifndef _TIMEVAL_DEFINED
  64. #define _TIMEVAL_DEFINED
  65. struct timeval {
  66. long tv_sec;
  67. long tv_usec;
  68. };
  69. #endif /* _TIMEVAL_DEFINED */
  70. #endif
  71. /* gettimeofday() */
  72. #ifndef HAVE_GETTIMEOFDAY
  73. #ifdef _WIN32
  74. struct timezone {
  75. int tz_minuteswest;
  76. int tz_dsttime;
  77. };
  78. #endif
  79. struct timezone;
  80. extern int gettimeofday(struct timeval *tv, struct timezone *tz);
  81. #endif
  82. /**** clear_malloc & fill_malloc ****/
  83. void *clear_malloc(size_t size);
  84. void *fill_malloc(size_t size);
  85. /*
  86. * Now you have 3 ways for the malloc function:
  87. *
  88. * 1. Do not change anything, use the original malloc
  89. *
  90. * 2. Use the clear_malloc function instead of the original malloc.
  91. * In this case you must use the following define:
  92. * #define malloc((_a)) clear_malloc((_a))
  93. *
  94. * 3. Use the fill_malloc function instead of the original malloc.
  95. * In this case you must use the following define:
  96. * #define malloc((_a)) fill_malloc((_a))
  97. *
  98. * We have figured out that there could exist some malloc problems
  99. * where variables are using without to be initialise. To find this
  100. * places, use the fill_malloc function. With this function we want
  101. * to initialize memory to some known bad state. This is quite easily
  102. * spotted in the debugger and will trap to an invalid address.
  103. *
  104. * clear_malloc can be used if you want to set not initialise
  105. * variable to 0.
  106. *
  107. * If you do not want to change the malloc function, to not use one of
  108. * the following macros. Which is the default way.
  109. */
  110. /* #define malloc(_a) clear_malloc(_a) */
  111. /* #define malloc(_a) fill_malloc(_a) */
  112. /* GNU extensions to the C library that may be missing on some systems */
  113. #ifndef HAVE_STRNDUP
  114. extern char* strndup(const char *s, size_t n);
  115. #endif /* HAVE_STRNDUP */
  116. #ifndef HAVE_STRNLEN
  117. extern size_t strnlen(const char *s, size_t maxlen);
  118. #endif /* HAVE_STRNLEN */
  119. #ifndef HAVE_USLEEP
  120. #ifdef _WIN32
  121. static __inline unsigned usleep(unsigned int usecs)
  122. {
  123. Sleep((usecs/1000));
  124. return 0;
  125. }
  126. #else
  127. #if BUILD_ECOSBOARD
  128. void usleep(int us);
  129. #else
  130. #error no usleep defined for your platform
  131. #endif
  132. #endif
  133. #endif /* HAVE_USLEEP */
  134. /* Windows specific */
  135. #ifdef _WIN32
  136. #define WIN32_LEAN_AND_MEAN
  137. #include <windows.h>
  138. #include <time.h>
  139. /* win32 systems do not support ETIMEDOUT */
  140. #ifndef ETIMEDOUT
  141. #define ETIMEDOUT WSAETIMEDOUT
  142. #endif
  143. #if IS_MINGW == 1
  144. static __inline unsigned char inb(unsigned short int port)
  145. {
  146. unsigned char _v;
  147. __asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (port));
  148. return _v;
  149. }
  150. static __inline void outb(unsigned char value, unsigned short int port)
  151. {
  152. __asm__ __volatile__ ("outb %b0,%w1": :"a" (value), "Nd" (port));
  153. }
  154. #endif /* IS_MINGW */
  155. #endif /* _WIN32 */
  156. /* generic socket functions for Windows and Posix */
  157. static __inline int write_socket( int handle, const void *buffer, unsigned int count )
  158. {
  159. #ifdef _WIN32
  160. return send(handle, buffer, count, 0);
  161. #else
  162. return write(handle, buffer, count);
  163. #endif
  164. }
  165. static __inline int read_socket( int handle, void *buffer, unsigned int count )
  166. {
  167. #ifdef _WIN32
  168. return recv(handle, buffer, count, 0);
  169. #else
  170. return read(handle, buffer, count);
  171. #endif
  172. }
  173. static __inline int close_socket(int sock)
  174. {
  175. #ifdef _WIN32
  176. return closesocket(sock);
  177. #else
  178. return close(sock);
  179. #endif
  180. }
  181. static __inline void socket_nonblock(int fd)
  182. {
  183. #ifdef _WIN32
  184. unsigned long nonblock = 1;
  185. ioctlsocket(fd, FIONBIO, &nonblock );
  186. #else
  187. int oldopts = fcntl(fd, F_GETFL, 0);
  188. fcntl(fd, F_SETFL, oldopts | O_NONBLOCK);
  189. #endif
  190. }
  191. #ifndef HAVE_ELF_H
  192. typedef struct
  193. {
  194. unsigned char e_ident[16]; /* Magic number and other info */
  195. u16 e_type; /* Object file type */
  196. u16 e_machine; /* Architecture */
  197. u32 e_version; /* Object file version */
  198. u32 e_entry; /* Entry point virtual address */
  199. u32 e_phoff; /* Program header table file offset */
  200. u32 e_shoff; /* Section header table file offset */
  201. u32 e_flags; /* Processor-specific flags */
  202. u16 e_ehsize; /* ELF header size in bytes */
  203. u16 e_phentsize; /* Program header table entry size */
  204. u16 e_phnum; /* Program header table entry count */
  205. u16 e_shentsize; /* Section header table entry size */
  206. u16 e_shnum; /* Section header table entry count */
  207. u16 e_shstrndx; /* Section header string table index */
  208. } Elf32_Ehdr;
  209. #define ELFMAG "\177ELF"
  210. #define SELFMAG 4
  211. #define EI_CLASS 4 /* File class byte index */
  212. #define ELFCLASS32 1 /* 32-bit objects */
  213. #define ELFCLASS64 2 /* 64-bit objects */
  214. #define EI_DATA 5 /* Data encoding byte index */
  215. #define ELFDATA2LSB 1 /* 2's complement, little endian */
  216. #define ELFDATA2MSB 2 /* 2's complement, big endian */
  217. typedef struct
  218. {
  219. u32 p_type; /* Segment type */
  220. u32 p_offset; /* Segment file offset */
  221. u32 p_vaddr; /* Segment virtual address */
  222. u32 p_paddr; /* Segment physical address */
  223. u32 p_filesz; /* Segment size in file */
  224. u32 p_memsz; /* Segment size in memory */
  225. u32 p_flags; /* Segment flags */
  226. u32 p_align; /* Segment alignment */
  227. } Elf32_Phdr;
  228. #define PT_LOAD 1 /* Loadable program segment */
  229. #endif /* HAVE_ELF_H */
  230. #endif /* REPLACEMENTS_H */