Add support for Microsoft Universal Windows Platform.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9a28f08..1b1b9f7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -133,6 +133,11 @@
 ADD_DEFINITIONS("-D_CRT_NONSTDC_NO_DEPRECATE")
 ENDIF(MSVC)
 
+if(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore)
+  ADD_DEFINITIONS(-DMS_UWP)
+  SET (OPTIONAL_LIBRARY "${OPTIONAL_LIBRARY}" bcrypt)
+endif(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore)
+
 ADD_DEFINITIONS("-DHAVE_CONFIG_H")
 
 # rpath handling: use rpath in installed binaries
diff --git a/THANKS b/THANKS
index dadd1bd..5b270d1 100644
--- a/THANKS
+++ b/THANKS
@@ -40,6 +40,7 @@
 Martin Buchholz <martinrb@google.com>
 Martin Szulecki <m.szulecki@libimobiledevice.org>
 Michael Beck <mm.beck@gmx.net>
+Michał Janiszewski
 Michal Vyskocil <mvyskocil@suse.cz>
 Mikhail Gusarov <dottedmag@dottedmag.net>.
 Oliver Kaiser <under.northern.sky@googlemail.com>
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 14803dc..855ccf5 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -165,12 +165,20 @@
 
 IF(WIN32)
   SET(LIBZIP_OPSYS_FILES
-    zip_random_win32.c
-    zip_source_win32a.c
     zip_source_win32handle.c
     zip_source_win32utf8.c
     zip_source_win32w.c
   )
+  IF(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore)
+    SET(LIBZIP_OPSYS_FILES "${LIBZIP_OPSYS_FILES}"
+      zip_random_uwp.c
+    )
+  ELSE()
+    SET(LIBZIP_OPSYS_FILES "${LIBZIP_OPSYS_FILES}"
+      zip_random_win32.c
+      zip_source_win32a.c
+    )
+  ENDIF()
 ELSE(WIN32)
   SET(LIBZIP_OPSYS_FILES
     zip_random_unix.c
diff --git a/lib/zip_random_uwp.c b/lib/zip_random_uwp.c
new file mode 100644
index 0000000..aeead0f
--- /dev/null
+++ b/lib/zip_random_uwp.c
@@ -0,0 +1,55 @@
+/*
+  zip_random_uwp.c -- fill the user's buffer with random stuff (UWP version)
+  Copyright (C) 2017 Dieter Baron and Thomas Klausner
+
+  This file is part of libzip, a library to manipulate ZIP archives.
+  The authors can be contacted at <libzip@nih.at>
+
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions
+  are met:
+  1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+  2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in
+     the documentation and/or other materials provided with the
+     distribution.
+  3. The names of the authors may not be used to endorse or promote
+     products derived from this software without specific prior
+     written permission.
+
+  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
+  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
+  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#include <windows.h>
+#include <ntstatus.h>
+#include <bcrypt.h>
+
+#include "zipint.h"
+#include "zipwin32.h"
+
+bool
+zip_random(zip_uint8_t *buffer, zip_uint16_t length)
+{
+    BCRYPT_ALG_HANDLE hAlg = NULL;
+    NTSTATUS hr = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RNG_ALGORITHM, MS_PRIMITIVE_PROVIDER, 0);
+    if (hr != STATUS_SUCCESS || hAlg == NULL) {
+	return false;
+    }
+    hr = BCryptGenRandom(&hAlg, buffer, length, 0);
+    BCryptCloseAlgorithmProvider(&hAlg, 0);
+    if (hr != STATUS_SUCCESS) {
+	return false;
+    }
+    return true;
+}
diff --git a/lib/zip_source_win32handle.c b/lib/zip_source_win32handle.c
index 7fe003d..543593b 100644
--- a/lib/zip_source_win32handle.c
+++ b/lib/zip_source_win32handle.c
@@ -35,6 +35,7 @@
 #include <wchar.h>
 #include <stdlib.h>
 #include <string.h>
+#include <aclapi.h>
 
 #include "zipint.h"
 #include "zipwin32.h"
@@ -420,12 +421,12 @@
     int i;
     HANDLE th = INVALID_HANDLE_VALUE;
     void *temp = NULL;
-    SECURITY_INFORMATION si;
-    SECURITY_ATTRIBUTES sa;
     PSECURITY_DESCRIPTOR psd = NULL;
     PSECURITY_ATTRIBUTES psa = NULL;
-    DWORD len;
-    BOOL success;
+    SECURITY_ATTRIBUTES sa;
+    SECURITY_INFORMATION si;
+    DWORD success;
+    PACL dacl = NULL;
 
     /*
     Read the DACL from the original file, so we can copy it to the temp file.
@@ -434,16 +435,8 @@
     */
     if (ctx->h != INVALID_HANDLE_VALUE && GetFileType(ctx->h) == FILE_TYPE_DISK) {
 	si = DACL_SECURITY_INFORMATION | UNPROTECTED_DACL_SECURITY_INFORMATION;
-	len = 0;
-	success = GetUserObjectSecurity(ctx->h, &si, NULL, len, &len);
-	if (!success && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
-	    if ((psd = (PSECURITY_DESCRIPTOR)malloc(len)) == NULL) {
-		zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0);
-		return -1;
-	    }
-	    success = GetUserObjectSecurity(ctx->h, &si, psd, len, &len);
-	}
-	if (success) {
+	success = GetSecurityInfo(ctx->h, SE_FILE_OBJECT, si, NULL, NULL, &dacl, NULL, &psd);
+	if (success == ERROR_SUCCESS) {
 	    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
 	    sa.bInheritHandle = FALSE;
 	    sa.lpSecurityDescriptor = psd;
@@ -451,7 +444,13 @@
 	}
     }
 
+
+#ifndef MS_UWP
     value = GetTickCount();
+#else
+    value = (zip_uint32_t)GetTickCount64();
+#endif
+
     for (i = 0; i < 1024 && th == INVALID_HANDLE_VALUE; i++) {
 	th = ctx->ops->op_create_temp(ctx, &temp, value + i, psa);
 	if (th == INVALID_HANDLE_VALUE && GetLastError() != ERROR_FILE_EXISTS)
@@ -460,12 +459,12 @@
 
     if (th == INVALID_HANDLE_VALUE) {
 	free(temp);
-	free(psd);
+	LocalFree(psd);
 	zip_error_set(&ctx->error, ZIP_ER_TMPOPEN, _zip_win32_error_to_errno(GetLastError()));
 	return -1;
     }
 
-    free(psd);
+    LocalFree(psd);
     ctx->hout = th;
     ctx->tmpname = temp;
 
diff --git a/lib/zip_source_win32w.c b/lib/zip_source_win32w.c
index 21fd3bd..685b57f 100644
--- a/lib/zip_source_win32w.c
+++ b/lib/zip_source_win32w.c
@@ -1,34 +1,34 @@
 /*
-zip_source_win32w.c -- create data source from Windows file (UTF-16)
-Copyright (C) 1999-2017 Dieter Baron and Thomas Klausner
+  zip_source_win32w.c -- create data source from Windows file (UTF-16)
+  Copyright (C) 1999-2017 Dieter Baron and Thomas Klausner
 
-This file is part of libzip, a library to manipulate ZIP archives.
-The authors can be contacted at <libzip@nih.at>
+  This file is part of libzip, a library to manipulate ZIP archives.
+  The authors can be contacted at <libzip@nih.at>
 
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-1. Redistributions of source code must retain the above copyright
-notice, this list of conditions and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright
-notice, this list of conditions and the following disclaimer in
-the documentation and/or other materials provided with the
-distribution.
-3. The names of the authors may not be used to endorse or promote
-products derived from this software without specific prior
-written permission.
+  Redistribution and use in source and binary forms, with or without
+  modification, are permitted provided that the following conditions
+  are met:
+  1. Redistributions of source code must retain the above copyright
+     notice, this list of conditions and the following disclaimer.
+  2. Redistributions in binary form must reproduce the above copyright
+     notice, this list of conditions and the following disclaimer in
+     the documentation and/or other materials provided with the
+     distribution.
+  3. The names of the authors may not be used to endorse or promote
+     products derived from this software without specific prior
+     written permission.
 
-THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
-OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
-DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
-GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
-IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
-IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
+  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
+  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 
@@ -83,7 +83,19 @@
 static HANDLE
 _win32_open_w(_zip_source_win32_read_file_t *ctx)
 {
+#ifdef MS_UWP
+    CREATEFILE2_EXTENDED_PARAMETERS extParams = { 0 };
+    extParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
+    extParams.dwFileFlags = FILE_FLAG_RANDOM_ACCESS;
+    extParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
+    extParams.dwSize = sizeof(extParams);
+    extParams.hTemplateFile = NULL;
+    extParams.lpSecurityAttributes = NULL;
+
+    return CreateFile2(ctx->fname, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, OPEN_EXISTING, &extParams);
+#else
     return CreateFileW(ctx->fname, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+#endif
 }
 
 
@@ -103,7 +115,19 @@
 	return INVALID_HANDLE_VALUE;
     }
 
+#ifdef MS_UWP
+    CREATEFILE2_EXTENDED_PARAMETERS extParams = { 0 };
+    extParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY;
+    extParams.dwFileFlags = FILE_FLAG_RANDOM_ACCESS;
+    extParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
+    extParams.dwSize = sizeof(extParams);
+    extParams.hTemplateFile = NULL;
+    extParams.lpSecurityAttributes = NULL;
+
+    return CreateFile2((const wchar_t *)*temp, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, CREATE_NEW, &extParams);
+#else
     return CreateFileW((const wchar_t *)*temp, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, sa, CREATE_NEW, FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_TEMPORARY, NULL);
+#endif
 }
 
 
diff --git a/lib/zipwin32.h b/lib/zipwin32.h
index 60f8d0c..db549e2 100644
--- a/lib/zipwin32.h
+++ b/lib/zipwin32.h
@@ -35,7 +35,10 @@
 */
 
 /* 0x0501 => Windows XP; needs to be at least this value because of GetFileSizeEx */
+#ifndef MS_UWP
 #define _WIN32_WINNT 0x0501
+#endif
+
 #include <windows.h>
 
 /* context for Win32 source */