archive_windows.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*-
  2. * Copyright (c) 2009 Michihiro NAKAJIMA
  3. * Copyright (c) 2003-2006 Tim Kientzle
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer
  11. * in this position and unchanged.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * $FreeBSD$
  28. */
  29. #ifndef LIBARCHIVE_NONPOSIX_H_INCLUDED
  30. #define LIBARCHIVE_NONPOSIX_H_INCLUDED
  31. /* Start of configuration for native Win32 */
  32. #include <errno.h>
  33. #define set_errno(val) ((errno)=val)
  34. #include <io.h>
  35. #include <stdlib.h> //brings in NULL
  36. #include <stdio.h>
  37. #include <fcntl.h>
  38. #include <sys/stat.h>
  39. #include <process.h>
  40. #include <direct.h>
  41. #include <windows.h>
  42. //#define EFTYPE 7
  43. #if !defined(STDIN_FILENO)
  44. #define STDIN_FILENO 0
  45. #endif
  46. #if !defined(STDOUT_FILENO)
  47. #define STDOUT_FILENO 1
  48. #endif
  49. #if !defined(STDERR_FILENO)
  50. #define STDERR_FILENO 2
  51. #endif
  52. #if defined(_MSC_VER)
  53. /* TODO: Fix the code, don't suppress the warnings. */
  54. #pragma warning(disable:4244) /* 'conversion' conversion from 'type1' to 'type2', possible loss of data */
  55. #pragma warning(disable:4146) /* unary minus operator applied to unsigned type, result still unsigned */
  56. //#pragma warning(disable:4267) /* Conversion, possible loss of data */
  57. #pragma warning(default: 4061) /* The enumerate has no associated handler in a switch statement */
  58. #pragma warning(default: 4296) /* An unsigned variable was used in a comparison operation with zero */
  59. #pragma warning(default: 4365) /* 'action':conversion from 'type_1' to 'type_2', signed/unsigned mismatch */
  60. #endif
  61. #ifndef NULL
  62. #ifdef __cplusplus
  63. #define NULL 0
  64. #else
  65. #define NULL ((void *)0)
  66. #endif
  67. #endif
  68. /* Replacement for major/minor/makedev. */
  69. #define major(x) ((int)(0x00ff & ((x) >> 8)))
  70. #define minor(x) ((int)(0xffff00ff & (x)))
  71. #define makedev(maj,min) ((0xff00 & ((maj)<<8))|(0xffff00ff & (min)))
  72. /* Alias the Windows _function to the POSIX equivalent. */
  73. #define access _access
  74. #define chdir la_chdir
  75. #define chmod la_chmod
  76. #define close _close
  77. #define fcntl la_fcntl
  78. #define fileno _fileno
  79. #define fstat la_fstat
  80. #define getcwd _getcwd
  81. #define lseek la_lseek
  82. #define lstat la_stat
  83. #define open la_open
  84. #define stat _stat
  85. #define mbstowcs la_mbstowcs
  86. #define mkdir(d,m) la_mkdir(d, m)
  87. #define mktemp _mktemp
  88. #define read la_read
  89. #define rmdir la_rmdir
  90. #define strdup _strdup
  91. #define tzset _tzset
  92. #define umask _umask
  93. #define unlink la_unlink
  94. #define waitpid la_waitpid
  95. #define write la_write
  96. #define O_RDONLY _O_RDONLY
  97. #define O_WRONLY _O_WRONLY
  98. #define O_TRUNC _O_TRUNC
  99. #define O_CREAT _O_CREAT
  100. #define O_EXCL _O_EXCL
  101. #ifndef _S_IFIFO
  102. #define _S_IFIFO 0010000 /* pipe */
  103. #endif
  104. #ifndef _S_IFCHR
  105. #define _S_IFCHR 0020000 /* character special */
  106. #endif
  107. #ifndef _S_IFDIR
  108. #define _S_IFDIR 0040000 /* directory */
  109. #endif
  110. #ifndef _S_IFBLK
  111. #define _S_IFBLK 0060000 /* block special */
  112. #endif
  113. #ifndef _S_IFLNK
  114. #define _S_IFLNK 0120000 /* symbolic link */
  115. #endif
  116. #ifndef _S_IFSOCK
  117. #define _S_IFSOCK 0140000 /* socket */
  118. #endif
  119. #ifndef _S_IFREG
  120. #define _S_IFREG 0100000 /* regular */
  121. #endif
  122. #ifndef _S_IFMT
  123. #define _S_IFMT 0170000 /* file type mask */
  124. #endif
  125. #define S_IFIFO _S_IFIFO
  126. //#define S_IFCHR _S_IFCHR
  127. //#define S_IFDIR _S_IFDIR
  128. #define S_IFBLK _S_IFBLK
  129. #define S_IFLNK _S_IFLNK
  130. #define S_IFSOCK _S_IFSOCK
  131. //#define S_IFREG _S_IFREG
  132. //#define S_IFMT _S_IFMT
  133. #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) /* block special */
  134. #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) /* fifo or socket */
  135. #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) /* char special */
  136. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) /* directory */
  137. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) /* regular file */
  138. #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) /* Symbolic link */
  139. #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) /* Socket */
  140. #define _S_ISUID 0004000 /* set user id on execution */
  141. #define _S_ISGID 0002000 /* set group id on execution */
  142. #define _S_ISVTX 0001000 /* save swapped text even after use */
  143. #define S_ISUID _S_ISUID
  144. #define S_ISGID _S_ISGID
  145. #define S_ISVTX _S_ISVTX
  146. #define _S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
  147. #define _S_IXUSR _S_IEXEC /* read permission, user */
  148. #define _S_IWUSR _S_IWRITE /* write permission, user */
  149. #define _S_IRUSR _S_IREAD /* execute/search permission, user */
  150. #define _S_IRWXG (_S_IRWXU >> 3)
  151. #define _S_IXGRP (_S_IXUSR >> 3) /* read permission, group */
  152. #define _S_IWGRP (_S_IWUSR >> 3) /* write permission, group */
  153. #define _S_IRGRP (_S_IRUSR >> 3) /* execute/search permission, group */
  154. #define _S_IRWXO (_S_IRWXG >> 3)
  155. #define _S_IXOTH (_S_IXGRP >> 3) /* read permission, other */
  156. #define _S_IWOTH (_S_IWGRP >> 3) /* write permission, other */
  157. #define _S_IROTH (_S_IRGRP >> 3) /* execute/search permission, other */
  158. #define S_IRWXU _S_IRWXU
  159. #define S_IXUSR _S_IXUSR
  160. #define S_IWUSR _S_IWUSR
  161. #define S_IRUSR _S_IRUSR
  162. #define S_IRWXG _S_IRWXG
  163. #define S_IXGRP _S_IXGRP
  164. #define S_IWGRP _S_IWGRP
  165. #define S_IRGRP _S_IRGRP
  166. #define S_IRWXO _S_IRWXO
  167. #define S_IXOTH _S_IXOTH
  168. #define S_IWOTH _S_IWOTH
  169. #define S_IROTH _S_IROTH
  170. #define F_DUPFD 0 /* Duplicate file descriptor. */
  171. #define F_GETFD 1 /* Get file descriptor flags. */
  172. #define F_SETFD 2 /* Set file descriptor flags. */
  173. #define F_GETFL 3 /* Get file status flags. */
  174. #define F_SETFL 4 /* Set file status flags. */
  175. #define F_GETOWN 5 /* Get owner (receiver of SIGIO). */
  176. #define F_SETOWN 6 /* Set owner (receiver of SIGIO). */
  177. #define F_GETLK 7 /* Get record locking info. */
  178. #define F_SETLK 8 /* Set record locking info (non-blocking). */
  179. #define F_SETLKW 9 /* Set record locking info (blocking). */
  180. /* XXX missing */
  181. #define F_GETLK64 7 /* Get record locking info. */
  182. #define F_SETLK64 8 /* Set record locking info (non-blocking). */
  183. #define F_SETLKW64 9 /* Set record locking info (blocking). */
  184. /* File descriptor flags used with F_GETFD and F_SETFD. */
  185. #define FD_CLOEXEC 1 /* Close on exec. */
  186. //NOT SURE IF O_NONBLOCK is OK here but at least the 0x0004 flag is not used by anything else...
  187. #define O_NONBLOCK 0x0004 /* Non-blocking I/O. */
  188. //#define O_NDELAY O_NONBLOCK
  189. /* Symbolic constants for the access() function */
  190. #if !defined(F_OK)
  191. #define R_OK 4 /* Test for read permission */
  192. #define W_OK 2 /* Test for write permission */
  193. #define X_OK 1 /* Test for execute permission */
  194. #define F_OK 0 /* Test for existence of file */
  195. #endif
  196. #ifdef _LARGEFILE_SOURCE
  197. # define __USE_LARGEFILE 1 /* declare fseeko and ftello */
  198. #endif
  199. #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64
  200. # define __USE_FILE_OFFSET64 1 /* replace 32-bit functions by 64-bit ones */
  201. #endif
  202. #if __USE_LARGEFILE && __USE_FILE_OFFSET64
  203. /* replace stat and seek by their large-file equivalents */
  204. #undef stat
  205. #define stat _stati64
  206. #undef lseek
  207. #define lseek _lseeki64
  208. #define lseek64 _lseeki64
  209. #define tell _telli64
  210. #define tell64 _telli64
  211. #ifdef __MINGW32__
  212. # define fseek fseeko64
  213. # define fseeko fseeko64
  214. # define ftell ftello64
  215. # define ftello ftello64
  216. # define ftell64 ftello64
  217. #endif /* __MINGW32__ */
  218. #endif /* LARGE_FILES */
  219. #ifdef USE_WINSOCK_TIMEVAL
  220. /* Winsock timeval has long size tv_sec. */
  221. #define __timeval timeval
  222. #else
  223. struct _timeval64i32 {
  224. time_t tv_sec;
  225. long tv_usec;
  226. };
  227. #define __timeval _timeval64i32
  228. #endif
  229. typedef int pid_t;
  230. /* Message digest define */
  231. #if !defined(HAVE_OPENSSL_MD5_H) && !defined(HAVE_OPENSSL_SHA_H)
  232. typedef struct {
  233. int valid;
  234. HCRYPTPROV cryptProv;
  235. HCRYPTHASH hash;
  236. } Digest_CTX;
  237. #endif
  238. #if !defined(HAVE_OPENSSL_MD5_H) && defined(CALG_MD5)
  239. #define MD5_DIGEST_LENGTH 16
  240. #define HAVE_MD5 1
  241. #define MD5_CTX Digest_CTX
  242. #endif
  243. #ifndef HAVE_OPENSSL_SHA_H
  244. #ifdef CALG_SHA1
  245. #define SHA1_DIGEST_LENGTH 20
  246. #define HAVE_SHA1 1
  247. #define SHA1_CTX Digest_CTX
  248. #endif
  249. #ifdef CALG_SHA256
  250. #define SHA256_DIGEST_LENGTH 32
  251. #define HAVE_SHA256 1
  252. #define SHA256_CTX Digest_CTX
  253. #endif
  254. #ifdef CALG_SHA384
  255. #define SHA384_DIGEST_LENGTH 48
  256. #define HAVE_SHA384 1
  257. #define SHA384_CTX Digest_CTX
  258. #endif
  259. #ifdef CALG_SHA512
  260. #define SHA512_DIGEST_LENGTH 64
  261. #define HAVE_SHA512 1
  262. #define SHA512_CTX Digest_CTX
  263. #endif
  264. #endif /* HAVE_OPENSSL_SHA_H */
  265. /* End of Win32 definitions. */
  266. #ifdef __cplusplus
  267. extern "C" {
  268. #endif
  269. /* Implementation POSIX function */
  270. extern int link (const char *from, const char *to);
  271. extern int symlink (const char *from, const char *to);
  272. extern int ftruncate(int fd, off_t length);
  273. extern int futimes(int fd, const struct __timeval *times);
  274. extern int utimes(const char *name, const struct __timeval *times);
  275. /* Replacement POSIX function */
  276. extern int la_chdir(const char *path);
  277. extern int la_chmod(const char *path, mode_t mode);
  278. extern int la_fcntl(int fd, int cmd, int val);
  279. extern int la_fstat(int fd, struct stat *st);
  280. extern __int64 la_lseek(int fd, __int64 offset, int whence);
  281. extern int la_mkdir(const char *path, mode_t mode);
  282. extern size_t la_mbstowcs(wchar_t *wcstr, const char *mbstr, size_t nwchars);
  283. extern int la_open(const char *path, int flags, ...);
  284. extern ssize_t la_read(int fd, void *buf, size_t nbytes);
  285. extern int la_rmdir(const char *path);
  286. extern int la_stat(const char *path, struct stat *st);
  287. extern int la_unlink(const char *path);
  288. extern pid_t la_waitpid(pid_t wpid, int *status, int option);
  289. extern ssize_t la_write(int fd, const void *buf, size_t nbytes);
  290. #define _stat64i32(path, st) la_stat(path, st)
  291. #define _stat64(path, st) la_stat(path, st)
  292. /* for status returned by la_waitpid */
  293. #define WIFSIGNALED(sts) 0
  294. #define WTERMSIG(sts) 0
  295. #define WIFEXITED(sts) ((sts & 0x100) == 0)
  296. #define WEXITSTATUS(sts) (sts & 0x0FF)
  297. /* Convert a Win32 API error code */
  298. extern void _dosmaperr(unsigned long);
  299. /* Message digest function */
  300. #if !defined(HAVE_OPENSSL_MD5_H) && !defined(HAVE_OPENSSL_SHA_H)
  301. #ifdef MD5_DIGEST_LENGTH
  302. extern void MD5_Init(Digest_CTX *ctx);
  303. extern void MD5_Update(Digest_CTX *ctx, const unsigned char *buf,
  304. size_t len);
  305. extern void MD5_Final(unsigned char buf[MD5_DIGEST_LENGTH],
  306. Digest_CTX *ctx);
  307. #endif
  308. #ifdef SHA1_DIGEST_LENGTH
  309. extern void SHA1_Init(Digest_CTX *ctx);
  310. extern void SHA1_Update(Digest_CTX *ctx, const unsigned char *buf,
  311. size_t len);
  312. extern void SHA1_Final(unsigned char buf[SHA1_DIGEST_LENGTH],
  313. Digest_CTX *ctx);
  314. #endif
  315. #ifdef SHA256_DIGEST_LENGTH
  316. extern void SHA256_Init(Digest_CTX *ctx);
  317. extern void SHA256_Update(Digest_CTX *ctx, const unsigned char *buf,
  318. size_t len);
  319. extern void SHA256_Final(unsigned char buf[SHA256_DIGEST_LENGTH],
  320. Digest_CTX *ctx);
  321. #endif
  322. #ifdef SHA384_DIGEST_LENGTH
  323. extern void SHA384_Init(Digest_CTX *ctx);
  324. extern void SHA384_Update(Digest_CTX *ctx, const unsigned char *buf,
  325. size_t len);
  326. extern void SHA384_Final(unsigned char buf[SHA384_DIGEST_LENGTH],
  327. Digest_CTX *ctx);
  328. #endif
  329. #ifdef SHA512_DIGEST_LENGTH
  330. extern void SHA512_Init(Digest_CTX *ctx);
  331. extern void SHA512_Update(Digest_CTX *ctx, const unsigned char *buf,
  332. size_t len);
  333. extern void SHA512_Final(unsigned char buf[SHA512_DIGEST_LENGTH],
  334. Digest_CTX *ctx);
  335. #endif
  336. #endif
  337. #ifdef __cplusplus
  338. }
  339. #endif
  340. #endif /* LIBARCHIVE_NONPOSIX_H_INCLUDED */