minizip-ng/iowin32.c

284 lines
7.6 KiB
C
Raw Normal View History

2012-01-21 14:53:44 -07:00
/* iowin32.c -- IO base function header for compress/uncompress .zip
2017-09-16 17:35:23 +08:00
Version 1.2.0, September 16th, 2017
part of the MiniZip project
2012-01-21 14:53:44 -07:00
2017-09-16 17:35:23 +08:00
Copyright (C) 2012-2017 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
http://result42.com
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
2012-01-21 14:53:44 -07:00
*/
#include <stdlib.h>
2012-10-21 15:20:13 -07:00
#include <tchar.h>
2012-01-21 14:53:44 -07:00
#include "zlib.h"
#include "ioapi.h"
#include "iowin32.h"
#ifndef INVALID_HANDLE_VALUE
2014-01-07 19:13:14 -07:00
# define INVALID_HANDLE_VALUE (0xFFFFFFFF)
2012-01-21 14:53:44 -07:00
#endif
#ifndef INVALID_SET_FILE_POINTER
2014-01-07 19:13:14 -07:00
# define INVALID_SET_FILE_POINTER ((DWORD)-1)
2012-01-21 14:53:44 -07:00
#endif
#if defined(WINAPI_FAMILY_PARTITION) && (!(defined(IOWIN32_USING_WINRT_API)))
# if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
# define IOWIN32_USING_WINRT_API 1
# endif
#endif
2014-01-04 22:23:51 -07:00
2017-09-29 21:02:09 -07:00
typedef struct mzstream_win32_s
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream stream;
HANDLE handle;
int error;
char *filename;
int filename_size;
} mzstream_win32;
int32_t ZCALLBACK mzstream_win32_open(voidpf stream, const char *filename, int mode)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream_win32 *win32 = (mzstream_win32 *)stream;
uint32_t desired_access = 0;
uint32_t creation_disposition = 0;
uint32_t share_mode = 0;
uint32_t flags_attribs = FILE_ATTRIBUTE_NORMAL;
wchar_t *filename_wide = NULL;
uint32_t filename_wide_size = 0;
HANDLE handle = NULL;
if (filename == NULL)
return MZSTREAM_ERR;
if ((mode & MZSTREAM_MODE_READWRITEFILTER) == MZSTREAM_MODE_READ)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
desired_access = GENERIC_READ;
creation_disposition = OPEN_EXISTING;
share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
else if (mode & MZSTREAM_MODE_EXISTING)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
desired_access = GENERIC_WRITE | GENERIC_READ;
creation_disposition = OPEN_EXISTING;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
else if (mode & MZSTREAM_MODE_CREATE)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
desired_access = GENERIC_WRITE | GENERIC_READ;
creation_disposition = CREATE_ALWAYS;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
else
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
return MZSTREAM_ERR;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
filename_wide_size = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
filename_wide = (wchar_t *)malloc((filename_wide_size + 1) * sizeof(wchar_t));
memset(filename_wide, 0, sizeof(wchar_t) * (filename_wide_size + 1));
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
MultiByteToWideChar(CP_UTF8, 0, filename, -1, filename_wide, filename_wide_size);
2015-11-25 01:35:03 -07:00
#ifdef IOWIN32_USING_WINRT_API
2017-09-29 21:02:09 -07:00
win32->handle = CreateFile2W(filename_wide, desired_access, share_mode, creation_disposition, NULL);
2014-01-04 22:23:51 -07:00
#else
2017-09-29 21:02:09 -07:00
win32->handle = CreateFileW(filename_wide, desired_access, share_mode, NULL, creation_disposition, flags_attribs, NULL);
2014-01-04 22:23:51 -07:00
#endif
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
free(filename_wide);
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
if (mzstream_win32_is_open(stream) == MZSTREAM_ERR)
return MZSTREAM_ERR;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
win32->filename_size = strlen(filename) + 1;
win32->filename = (char *)malloc(win32->filename_size);
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
strncpy(win32->filename, filename, win32->filename_size);
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
return MZSTREAM_OK;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
int32_t ZCALLBACK mzstream_win32_is_open(voidpf stream)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream_win32 *win32 = (mzstream_win32 *)stream;
if (win32->handle == NULL || win32->handle == INVALID_HANDLE_VALUE)
return MZSTREAM_ERR;
return MZSTREAM_OK;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
int32_t ZCALLBACK mzstream_win32_read(voidpf stream, void* buf, uint32_t size)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream_win32 *win32 = (mzstream_win32 *)stream;
uint32_t read = 0;
HANDLE handle = NULL;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
if (mzstream_win32_is_open(stream) == MZSTREAM_ERR)
return MZSTREAM_ERR;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
if (!ReadFile(win32->handle, buf, size, &read, NULL))
2014-01-04 22:23:51 -07:00
{
2017-09-29 21:02:09 -07:00
win32->error = GetLastError();
if (win32->error == ERROR_HANDLE_EOF)
win32->error = 0;
2014-01-04 22:23:51 -07:00
}
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
return read;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
int32_t ZCALLBACK mzstream_win32_write(voidpf stream, const void *buf, uint32_t size)
2012-01-21 15:10:18 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream_win32 *win32 = (mzstream_win32 *)stream;
uint32_t written = 0;
uint32_t error = 0;
HANDLE handle = NULL;
2012-01-21 15:10:18 -07:00
2017-09-29 21:02:09 -07:00
if (mzstream_win32_is_open(stream) == MZSTREAM_ERR)
return MZSTREAM_ERR;
2012-01-21 15:10:18 -07:00
2017-09-29 21:02:09 -07:00
if (!WriteFile(win32->handle, buf, size, &written, NULL))
2012-01-21 15:10:18 -07:00
{
2017-09-29 21:02:09 -07:00
win32->error = GetLastError();
if (win32->error == ERROR_HANDLE_EOF)
win32->error = 0;
2012-01-21 15:10:18 -07:00
}
2017-09-29 21:02:09 -07:00
return written;
2012-01-21 15:10:18 -07:00
}
2017-09-29 21:02:09 -07:00
static int32_t mzstream_win32_seekinternal(HANDLE handle, LARGE_INTEGER large_pos, LARGE_INTEGER *new_pos, uint32_t move_method)
2012-01-21 15:10:18 -07:00
{
2017-09-29 21:02:09 -07:00
#ifdef IOWIN32_USING_WINRT_API
return SetFilePointerEx(handle, pos, newPos, dwMoveMethod);
#else
LONG high_part = large_pos.HighPart;
uint32_t pos = SetFilePointer(handle, large_pos.LowPart, &high_part, move_method);
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
if ((pos == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
return MZSTREAM_ERR;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
if (new_pos != NULL)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
new_pos->LowPart = pos;
new_pos->HighPart = high_part;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
return MZSTREAM_OK;
#endif
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
int64_t ZCALLBACK mzstream_win32_tell(voidpf stream)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream_win32 *win32 = (mzstream_win32 *)stream;
uint32_t written = 0;
uint32_t error = 0;
HANDLE handle = NULL;
LARGE_INTEGER large_pos;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
if (mzstream_win32_is_open(stream) == MZSTREAM_ERR)
return MZSTREAM_ERR;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
large_pos.QuadPart = 0;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
if (mzstream_win32_seekinternal(win32->handle, large_pos, &large_pos, FILE_CURRENT) == MZSTREAM_ERR)
2015-11-25 01:35:03 -07:00
{
2017-09-29 21:02:09 -07:00
error = GetLastError();
win32->error = error;
2015-11-25 01:35:03 -07:00
}
2014-01-04 22:23:51 -07:00
2017-09-29 21:02:09 -07:00
return large_pos.LowPart;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
int32_t ZCALLBACK mzstream_win32_seek(voidpf stream, uint64_t offset, int origin)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream_win32 *win32 = (mzstream_win32 *)stream;
uint32_t move_method = 0xFFFFFFFF;
uint32_t error = 0;
HANDLE handle = NULL;
int64_t position = -1;
LARGE_INTEGER large_pos;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
if (mzstream_win32_is_open(stream) == MZSTREAM_ERR)
return MZSTREAM_ERR;
2013-12-20 13:59:33 -07:00
2012-01-21 14:53:44 -07:00
switch (origin)
{
2017-09-29 21:02:09 -07:00
case MZSTREAM_SEEK_CUR:
move_method = FILE_CURRENT;
2013-12-20 13:59:33 -07:00
break;
2017-09-29 21:02:09 -07:00
case MZSTREAM_SEEK_END:
move_method = FILE_END;
2013-12-20 13:59:33 -07:00
break;
2017-09-29 21:02:09 -07:00
case MZSTREAM_SEEK_SET:
move_method = FILE_BEGIN;
2013-12-20 13:59:33 -07:00
break;
default:
2017-09-29 21:02:09 -07:00
return MZSTREAM_ERR;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
large_pos.QuadPart = offset;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
if (mzstream_win32_seekinternal(win32->handle, large_pos, NULL, move_method) == MZSTREAM_ERR)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
error = GetLastError();
win32->error = error;
return MZSTREAM_ERR;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
return MZSTREAM_OK;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
int ZCALLBACK mzstream_win32_close(voidpf stream)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream_win32 *win32 = (mzstream_win32 *)stream;
if (win32->filename != NULL)
free(win32->filename);
if (win32->handle != NULL)
CloseHandle(win32->handle);
win32->handle = NULL;
return MZSTREAM_OK;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
int ZCALLBACK mzstream_win32_error(voidpf stream)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream_win32 *win32 = (mzstream_win32 *)stream;
return win32->error;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
voidpf mzstream_win32_alloc(void)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream_win32 *win32 = NULL;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
win32 = (mzstream_win32 *)malloc(sizeof(mzstream_win32));
if (win32 == NULL)
return NULL;
2012-01-21 14:53:44 -07:00
2017-09-29 21:02:09 -07:00
memset(win32, 0, sizeof(mzstream_win32));
win32->stream.open = mzstream_win32_open;
win32->stream.is_open = mzstream_win32_is_open;
win32->stream.read = mzstream_win32_read;
win32->stream.write = mzstream_win32_write;
win32->stream.tell = mzstream_win32_tell;
win32->stream.seek = mzstream_win32_seek;
win32->stream.close = mzstream_win32_close;
win32->stream.error = mzstream_win32_error;
win32->stream.alloc = mzstream_win32_alloc;
win32->stream.free = mzstream_win32_free;
return (voidpf)win32;
2012-01-21 14:53:44 -07:00
}
2017-09-29 21:02:09 -07:00
void mzstream_win32_free(voidpf stream)
2012-01-21 14:53:44 -07:00
{
2017-09-29 21:02:09 -07:00
mzstream_win32 *win32 = (mzstream_win32 *)stream;
if (win32 != NULL)
free(win32);
}