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.
 
 
 
 
 
 

205 lines
5.5 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. /* include necessary headers for socket functionality */
  27. #ifdef _WIN32
  28. #include <winsock2.h>
  29. #else
  30. #include <sys/socket.h>
  31. #include <sys/poll.h>
  32. #include <netinet/in.h>
  33. #include <unistd.h>
  34. #include <fcntl.h>
  35. #endif
  36. #ifdef HAVE_SYS_PARAM_H
  37. #include <sys/param.h> /* for MIN/MAX macros */
  38. #endif
  39. /* MIN,MAX macros */
  40. #ifndef MIN
  41. #define MIN(a,b) (((a)<(b))?(a):(b))
  42. #endif
  43. #ifndef MAX
  44. #define MAX(a,b) (((a)>(b))?(a):(b))
  45. #endif
  46. /* gettimeofday() */
  47. #ifndef HAVE_GETTIMEOFDAY
  48. #ifndef _TIMEVAL_DEFINED
  49. #define _TIMEVAL_DEFINED
  50. struct timeval {
  51. long tv_sec;
  52. long tv_usec;
  53. };
  54. #endif /* _TIMEVAL_DEFINED */
  55. struct timezone {
  56. int tz_minuteswest;
  57. int tz_dsttime;
  58. };
  59. extern int gettimeofday(struct timeval *tv, struct timezone *tz);
  60. #endif
  61. /* GNU extensions to the C library that may be missing on some systems */
  62. #ifndef HAVE_STRNDUP
  63. extern char* strndup(const char *s, size_t n);
  64. #endif /* HAVE_STRNDUP */
  65. #ifndef HAVE_STRNLEN
  66. extern size_t strnlen(const char *s, size_t maxlen);
  67. #endif /* HAVE_STRNLEN */
  68. #ifndef HAVE_USLEEP
  69. static __inline unsigned usleep(unsigned int usecs)
  70. {
  71. #ifdef _WIN32
  72. Sleep((usecs/1000));
  73. return 0;
  74. #else
  75. #error no usleep defined for your platform
  76. #endif
  77. }
  78. #endif /* HAVE_USLEEP */
  79. /* Windows specific */
  80. #ifdef _WIN32
  81. #define WIN32_LEAN_AND_MEAN
  82. #include <windows.h>
  83. #include <time.h>
  84. #undef ERROR
  85. #if IS_MINGW == 1
  86. static __inline unsigned char inb(unsigned short int port)
  87. {
  88. unsigned char _v;
  89. __asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (port));
  90. return _v;
  91. }
  92. static __inline void outb(unsigned char value, unsigned short int port)
  93. {
  94. __asm__ __volatile__ ("outb %b0,%w1": :"a" (value), "Nd" (port));
  95. }
  96. #endif /* IS_MINGW */
  97. #endif /* _WIN32 */
  98. /* generic socket functions for Windows and Posix */
  99. static __inline int write_socket( int handle, const void *buffer, unsigned int count )
  100. {
  101. #ifdef _WIN32
  102. return send(handle, buffer, count, 0);
  103. #else
  104. return write(handle, buffer, count);
  105. #endif
  106. }
  107. static __inline int read_socket( int handle, void *buffer, unsigned int count )
  108. {
  109. #ifdef _WIN32
  110. return recv(handle, buffer, count, 0);
  111. #else
  112. return read(handle, buffer, count);
  113. #endif
  114. }
  115. static __inline int close_socket(int sock)
  116. {
  117. #ifdef _WIN32
  118. return closesocket(sock);
  119. #else
  120. return close(sock);
  121. #endif
  122. }
  123. static __inline void socket_nonblock(int fd)
  124. {
  125. #ifdef _WIN32
  126. long nonblock = 1;
  127. ioctlsocket(fd, FIONBIO, &nonblock );
  128. #else
  129. int oldopts = fcntl(fd, F_GETFL, 0);
  130. fcntl(fd, F_SETFL, oldopts | O_NONBLOCK);
  131. #endif
  132. }
  133. #ifndef HAVE_ELF_H
  134. typedef struct
  135. {
  136. unsigned char e_ident[16]; /* Magic number and other info */
  137. u16 e_type; /* Object file type */
  138. u16 e_machine; /* Architecture */
  139. u32 e_version; /* Object file version */
  140. u32 e_entry; /* Entry point virtual address */
  141. u32 e_phoff; /* Program header table file offset */
  142. u32 e_shoff; /* Section header table file offset */
  143. u32 e_flags; /* Processor-specific flags */
  144. u16 e_ehsize; /* ELF header size in bytes */
  145. u16 e_phentsize; /* Program header table entry size */
  146. u16 e_phnum; /* Program header table entry count */
  147. u16 e_shentsize; /* Section header table entry size */
  148. u16 e_shnum; /* Section header table entry count */
  149. u16 e_shstrndx; /* Section header string table index */
  150. } Elf32_Ehdr;
  151. #define ELFMAG "\177ELF"
  152. #define SELFMAG 4
  153. #define EI_CLASS 4 /* File class byte index */
  154. #define ELFCLASS32 1 /* 32-bit objects */
  155. #define ELFCLASS64 2 /* 64-bit objects */
  156. #define EI_DATA 5 /* Data encoding byte index */
  157. #define ELFDATA2LSB 1 /* 2's complement, little endian */
  158. #define ELFDATA2MSB 2 /* 2's complement, big endian */
  159. typedef struct
  160. {
  161. u32 p_type; /* Segment type */
  162. u32 p_offset; /* Segment file offset */
  163. u32 p_vaddr; /* Segment virtual address */
  164. u32 p_paddr; /* Segment physical address */
  165. u32 p_filesz; /* Segment size in file */
  166. u32 p_memsz; /* Segment size in memory */
  167. u32 p_flags; /* Segment flags */
  168. u32 p_align; /* Segment alignment */
  169. } Elf32_Phdr;
  170. #define PT_LOAD 1 /* Loadable program segment */
  171. #endif /* HAVE_ELF_H */
  172. #endif /* REPLACEMENTS_H */