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