archive_windows.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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_BUILD
  30. #error This header is only to be used internally to libarchive.
  31. #endif
  32. /*
  33. * TODO: A lot of stuff in here isn't actually used by libarchive and
  34. * can be trimmed out. Note that this file is used by libarchive and
  35. * libarchive_test but nowhere else. (But note that it gets compiled
  36. * with many different Windows environments, including MinGW, Visual
  37. * Studio, and Cygwin. Significant changes should be tested in all three.)
  38. */
  39. /*
  40. * TODO: Don't use off_t in here. Use __int64 instead. Note that
  41. * Visual Studio and the Windows SDK define off_t as 32 bits; Win32's
  42. * more modern file handling APIs all use __int64 instead of off_t.
  43. */
  44. #ifndef LIBARCHIVE_ARCHIVE_WINDOWS_H_INCLUDED
  45. #define LIBARCHIVE_ARCHIVE_WINDOWS_H_INCLUDED
  46. /* Start of configuration for native Win32 */
  47. #include <errno.h>
  48. #define set_errno(val) ((errno)=val)
  49. #include <io.h>
  50. #include <stdlib.h> /* brings in NULL */
  51. #include <stdio.h>
  52. #include <fcntl.h>
  53. #include <sys/stat.h>
  54. #include <process.h>
  55. #include <direct.h>
  56. #define NOCRYPT
  57. #include <windows.h>
  58. #if !defined(STDIN_FILENO)
  59. #define STDIN_FILENO 0
  60. #endif
  61. #if !defined(STDOUT_FILENO)
  62. #define STDOUT_FILENO 1
  63. #endif
  64. #if !defined(STDERR_FILENO)
  65. #define STDERR_FILENO 2
  66. #endif
  67. #if defined(_MSC_VER)
  68. /* TODO: Fix the code, don't suppress the warnings. */
  69. #pragma warning(disable:4761) /* 'conversion' conversion from 'type1' to 'type2', possible loss of data */
  70. /* this one shows up on vs 6 */
  71. # if _MSC_VER < 1300
  72. # pragma warning(disable:4244) /* 'integral size mismatch in argument; conversion supplied */
  73. # endif
  74. #pragma warning(default: 4365) /* 'action':conversion from 'type_1' to 'type_2', signed/unsigned mismatch */
  75. #pragma warning(disable: 4244) /* conversion from '__int64' to 'off_t', possible loss of data */
  76. #endif
  77. #ifndef NULL
  78. #ifdef __cplusplus
  79. #define NULL 0
  80. #else
  81. #define NULL ((void *)0)
  82. #endif
  83. #endif
  84. /* Alias the Windows _function to the POSIX equivalent. */
  85. #define access _access
  86. #define chdir __la_chdir
  87. #define chmod __la_chmod
  88. #define close _close
  89. #define fcntl __la_fcntl
  90. #ifndef fileno
  91. #define fileno _fileno
  92. #endif
  93. #define fstat __la_fstat
  94. #define ftruncate __la_ftruncate
  95. #define futimes __la_futimes
  96. #define getcwd _getcwd
  97. #define link __la_link
  98. #define lseek __la_lseek
  99. #define lstat __la_stat
  100. #define mbstowcs __la_mbstowcs
  101. #define mkdir(d,m) __la_mkdir(d, m)
  102. #define mktemp _mktemp
  103. #define open __la_open
  104. #define read __la_read
  105. #define rmdir __la_rmdir
  106. #define stat(path,stref) __la_stat(path,stref)
  107. #ifndef __BORLANDC__
  108. #define strdup _strdup
  109. #endif
  110. #define tzset _tzset
  111. #ifndef __BORLANDC__
  112. #define umask _umask
  113. #endif
  114. #define unlink __la_unlink
  115. #define utimes __la_utimes
  116. #define waitpid __la_waitpid
  117. #define write __la_write
  118. #ifndef O_RDONLY
  119. #define O_RDONLY _O_RDONLY
  120. #define O_WRONLY _O_WRONLY
  121. #define O_TRUNC _O_TRUNC
  122. #define O_CREAT _O_CREAT
  123. #define O_EXCL _O_EXCL
  124. #endif
  125. #ifndef _S_IFIFO
  126. #define _S_IFIFO 0010000 /* pipe */
  127. #endif
  128. #ifndef _S_IFCHR
  129. #define _S_IFCHR 0020000 /* character special */
  130. #endif
  131. #ifndef _S_IFDIR
  132. #define _S_IFDIR 0040000 /* directory */
  133. #endif
  134. #ifndef _S_IFBLK
  135. #define _S_IFBLK 0060000 /* block special */
  136. #endif
  137. #ifndef _S_IFLNK
  138. #define _S_IFLNK 0120000 /* symbolic link */
  139. #endif
  140. #ifndef _S_IFSOCK
  141. #define _S_IFSOCK 0140000 /* socket */
  142. #endif
  143. #ifndef _S_IFREG
  144. #define _S_IFREG 0100000 /* regular */
  145. #endif
  146. #ifndef _S_IFMT
  147. #define _S_IFMT 0170000 /* file type mask */
  148. #endif
  149. #ifndef S_IFIFO
  150. #define S_IFIFO _S_IFIFO
  151. #endif
  152. #ifndef S_IFBLK
  153. #define S_IFBLK _S_IFBLK
  154. #endif
  155. #ifndef S_IFLNK
  156. #define S_IFLNK _S_IFLNK
  157. #endif
  158. #ifndef S_IFSOCK
  159. #define S_IFSOCK _S_IFSOCK
  160. #endif
  161. #ifndef S_ISBLK
  162. #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) /* block special */
  163. #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) /* fifo or socket */
  164. #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) /* char special */
  165. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) /* directory */
  166. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) /* regular file */
  167. #endif
  168. #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) /* Symbolic link */
  169. #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) /* Socket */
  170. #define _S_ISUID 0004000 /* set user id on execution */
  171. #define _S_ISGID 0002000 /* set group id on execution */
  172. #define _S_ISVTX 0001000 /* save swapped text even after use */
  173. #define S_ISUID _S_ISUID
  174. #define S_ISGID _S_ISGID
  175. #define S_ISVTX _S_ISVTX
  176. #define _S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
  177. #define _S_IXUSR _S_IEXEC /* read permission, user */
  178. #define _S_IWUSR _S_IWRITE /* write permission, user */
  179. #define _S_IRUSR _S_IREAD /* execute/search permission, user */
  180. #define _S_IRWXG (_S_IRWXU >> 3)
  181. #define _S_IXGRP (_S_IXUSR >> 3) /* read permission, group */
  182. #define _S_IWGRP (_S_IWUSR >> 3) /* write permission, group */
  183. #define _S_IRGRP (_S_IRUSR >> 3) /* execute/search permission, group */
  184. #define _S_IRWXO (_S_IRWXG >> 3)
  185. #define _S_IXOTH (_S_IXGRP >> 3) /* read permission, other */
  186. #define _S_IWOTH (_S_IWGRP >> 3) /* write permission, other */
  187. #define _S_IROTH (_S_IRGRP >> 3) /* execute/search permission, other */
  188. #ifndef S_IRWXU
  189. #define S_IRWXU _S_IRWXU
  190. #define S_IXUSR _S_IXUSR
  191. #define S_IWUSR _S_IWUSR
  192. #define S_IRUSR _S_IRUSR
  193. #endif
  194. #define S_IRWXG _S_IRWXG
  195. #define S_IXGRP _S_IXGRP
  196. #define S_IWGRP _S_IWGRP
  197. #define S_IRGRP _S_IRGRP
  198. #define S_IRWXO _S_IRWXO
  199. #define S_IXOTH _S_IXOTH
  200. #define S_IWOTH _S_IWOTH
  201. #define S_IROTH _S_IROTH
  202. #define F_DUPFD 0 /* Duplicate file descriptor. */
  203. #define F_GETFD 1 /* Get file descriptor flags. */
  204. #define F_SETFD 2 /* Set file descriptor flags. */
  205. #define F_GETFL 3 /* Get file status flags. */
  206. #define F_SETFL 4 /* Set file status flags. */
  207. #define F_GETOWN 5 /* Get owner (receiver of SIGIO). */
  208. #define F_SETOWN 6 /* Set owner (receiver of SIGIO). */
  209. #define F_GETLK 7 /* Get record locking info. */
  210. #define F_SETLK 8 /* Set record locking info (non-blocking). */
  211. #define F_SETLKW 9 /* Set record locking info (blocking). */
  212. /* XXX missing */
  213. #define F_GETLK64 7 /* Get record locking info. */
  214. #define F_SETLK64 8 /* Set record locking info (non-blocking). */
  215. #define F_SETLKW64 9 /* Set record locking info (blocking). */
  216. /* File descriptor flags used with F_GETFD and F_SETFD. */
  217. #define FD_CLOEXEC 1 /* Close on exec. */
  218. /*NOT SURE IF O_NONBLOCK is OK here but at least the 0x0004 flag is not used by anything else... */
  219. #define O_NONBLOCK 0x0004 /* Non-blocking I/O. */
  220. /*#define O_NDELAY O_NONBLOCK */
  221. /* Symbolic constants for the access() function */
  222. #if !defined(F_OK)
  223. #define R_OK 4 /* Test for read permission */
  224. #define W_OK 2 /* Test for write permission */
  225. #define X_OK 1 /* Test for execute permission */
  226. #define F_OK 0 /* Test for existence of file */
  227. #endif
  228. #ifdef _LARGEFILE_SOURCE
  229. # define __USE_LARGEFILE 1 /* declare fseeko and ftello */
  230. #endif
  231. #if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS == 64
  232. # define __USE_FILE_OFFSET64 1 /* replace 32-bit functions by 64-bit ones */
  233. #endif
  234. #if __USE_LARGEFILE && __USE_FILE_OFFSET64
  235. /* replace stat and seek by their large-file equivalents */
  236. #undef stat
  237. #define stat _stati64
  238. #undef lseek
  239. #define lseek _lseeki64
  240. #define lseek64 _lseeki64
  241. #define tell _telli64
  242. #define tell64 _telli64
  243. #ifdef __MINGW32__
  244. # define fseek fseeko64
  245. # define fseeko fseeko64
  246. # define ftell ftello64
  247. # define ftello ftello64
  248. # define ftell64 ftello64
  249. #endif /* __MINGW32__ */
  250. #endif /* LARGE_FILES */
  251. #ifdef USE_WINSOCK_TIMEVAL
  252. /* Winsock timeval has long size tv_sec. */
  253. #define __timeval timeval
  254. #else
  255. struct _timeval64i32 {
  256. time_t tv_sec;
  257. long tv_usec;
  258. };
  259. #define __timeval _timeval64i32
  260. #endif
  261. #if defined(_MSC_VER) || defined(__BORLANDC__)
  262. typedef int pid_t;
  263. #endif /* _MSC_VER __BORLANDC__ */
  264. /* Message digest define */
  265. #if !defined(HAVE_OPENSSL_MD5_H) && !defined(HAVE_OPENSSL_SHA_H)
  266. # if defined(_MSC_VER) && _MSC_VER < 1300
  267. # define _WIN32_WINNT 0x0400
  268. # endif
  269. #include <wincrypt.h>
  270. typedef struct {
  271. int valid;
  272. HCRYPTPROV cryptProv;
  273. HCRYPTHASH hash;
  274. } Digest_CTX;
  275. #endif
  276. #if !defined(HAVE_OPENSSL_MD5_H) && defined(CALG_MD5)
  277. #define MD5_DIGEST_LENGTH 16
  278. #define HAVE_MD5 1
  279. #define MD5_CTX Digest_CTX
  280. #endif
  281. #ifndef HAVE_OPENSSL_SHA_H
  282. #ifdef CALG_SHA1
  283. #define SHA1_DIGEST_LENGTH 20
  284. #define HAVE_SHA1 1
  285. #define SHA1_CTX Digest_CTX
  286. #endif
  287. #ifdef CALG_SHA256
  288. #define SHA256_DIGEST_LENGTH 32
  289. #define HAVE_SHA256 1
  290. #define SHA256_CTX Digest_CTX
  291. #endif
  292. #ifdef CALG_SHA384
  293. #define SHA384_DIGEST_LENGTH 48
  294. #define HAVE_SHA384 1
  295. #define SHA384_CTX Digest_CTX
  296. #endif
  297. #ifdef CALG_SHA512
  298. #define SHA512_DIGEST_LENGTH 64
  299. #define HAVE_SHA512 1
  300. #define SHA512_CTX Digest_CTX
  301. #endif
  302. #endif /* HAVE_OPENSSL_SHA_H */
  303. /* End of Win32 definitions. */
  304. /* Tell libarchive code that we have simulations for these. */
  305. #ifndef HAVE_FTRUNCATE
  306. #define HAVE_FTRUNCATE 1
  307. #endif
  308. #ifndef HAVE_FUTIMES
  309. #define HAVE_FUTIMES 1
  310. #endif
  311. #ifndef HAVE_UTIMES
  312. #define HAVE_UTIMES 1
  313. #endif
  314. #ifndef HAVE_LINK
  315. #define HAVE_LINK 1
  316. #endif
  317. /* Replacement POSIX function */
  318. extern int __la_chdir(const char *path);
  319. extern int __la_chmod(const char *path, mode_t mode);
  320. extern int __la_fcntl(int fd, int cmd, int val);
  321. extern int __la_fstat(int fd, struct stat *st);
  322. extern int __la_ftruncate(int fd, off_t length);
  323. extern int __la_futimes(int fd, const struct __timeval *times);
  324. extern int __la_link(const char *src, const char *dst);
  325. extern __int64 __la_lseek(int fd, __int64 offset, int whence);
  326. extern size_t __la_mbstowcs(wchar_t *wcstr, const char *mbstr, size_t nwchars);
  327. extern int __la_mkdir(const char *path, mode_t mode);
  328. extern int __la_open(const char *path, int flags, ...);
  329. extern ssize_t __la_read(int fd, void *buf, size_t nbytes);
  330. extern int __la_rmdir(const char *path);
  331. extern int __la_stat(const char *path, struct stat *st);
  332. extern int __la_unlink(const char *path);
  333. extern int __la_utimes(const char *name, const struct __timeval *times);
  334. extern pid_t __la_waitpid(pid_t wpid, int *status, int option);
  335. extern ssize_t __la_write(int fd, const void *buf, size_t nbytes);
  336. #define _stat64i32(path, st) __la_stat(path, st)
  337. #define _stat64(path, st) __la_stat(path, st)
  338. /* for status returned by la_waitpid */
  339. #define WIFSIGNALED(sts) 0
  340. #define WTERMSIG(sts) 0
  341. #define WIFEXITED(sts) ((sts & 0x100) == 0)
  342. #define WEXITSTATUS(sts) (sts & 0x0FF)
  343. /* Message digest function */
  344. #if !defined(HAVE_OPENSSL_MD5_H) && !defined(HAVE_OPENSSL_SHA_H)
  345. #ifdef MD5_DIGEST_LENGTH
  346. extern void MD5_Init(Digest_CTX *ctx);
  347. extern void MD5_Update(Digest_CTX *ctx, const unsigned char *buf,
  348. size_t len);
  349. extern void MD5_Final(unsigned char buf[MD5_DIGEST_LENGTH],
  350. Digest_CTX *ctx);
  351. #endif
  352. #ifdef SHA1_DIGEST_LENGTH
  353. extern void SHA1_Init(Digest_CTX *ctx);
  354. extern void SHA1_Update(Digest_CTX *ctx, const unsigned char *buf,
  355. size_t len);
  356. extern void SHA1_Final(unsigned char buf[SHA1_DIGEST_LENGTH],
  357. Digest_CTX *ctx);
  358. #endif
  359. #ifdef SHA256_DIGEST_LENGTH
  360. extern void SHA256_Init(Digest_CTX *ctx);
  361. extern void SHA256_Update(Digest_CTX *ctx, const unsigned char *buf,
  362. size_t len);
  363. extern void SHA256_Final(unsigned char buf[SHA256_DIGEST_LENGTH],
  364. Digest_CTX *ctx);
  365. #endif
  366. #ifdef SHA384_DIGEST_LENGTH
  367. extern void SHA384_Init(Digest_CTX *ctx);
  368. extern void SHA384_Update(Digest_CTX *ctx, const unsigned char *buf,
  369. size_t len);
  370. extern void SHA384_Final(unsigned char buf[SHA384_DIGEST_LENGTH],
  371. Digest_CTX *ctx);
  372. #endif
  373. #ifdef SHA512_DIGEST_LENGTH
  374. extern void SHA512_Init(Digest_CTX *ctx);
  375. extern void SHA512_Update(Digest_CTX *ctx, const unsigned char *buf,
  376. size_t len);
  377. extern void SHA512_Final(unsigned char buf[SHA512_DIGEST_LENGTH],
  378. Digest_CTX *ctx);
  379. #endif
  380. #endif
  381. #endif /* LIBARCHIVE_ARCHIVE_WINDOWS_H_INCLUDED */