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.
 
 
 
 
 
 

248 lines
6.9 KiB

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