/*
 * Copyright (c) 2005-2006 Tom N Harris.
 *
 *     This program is free software; you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation; either version 2 of the License, or
 *     (at your option) any later version.
 *
 *     A copy of the GNU General Public License can be found at
 *     http://www.gnu.org/
 *
 * Written by Tom N Harris <telliamed@whoopdedo.org>.
 *
 */

#include "zutil.h"
#include "zlib.h"
#include "zip.h"
#include "unzip.h"

unsigned long 
ft_to_dos(FILETIME const* ft)
{
	FILETIME lt;
	WORD hw, lw;
	FileTimeToLocalFileTime(ft, &lt);
	FileTimeToDosDateTime(&lt, &hw, &lw);
	return ((DWORD)(lw))|(((DWORD)(hw))<<16);
}

void 
ft_from_dos(LPFILETIME ft, unsigned long dt)
{
	FILETIME lt;
	WORD hw, lw;
	lw = dt & 0xFFFF;
	hw = (dt >> 16) & 0xFFFF;
	DosDateTimeToFileTime(hw, lw, &lt);
	LocalFileTimeToFileTime(&lt, ft);
}

unsigned long 
ft_to_unix(FILETIME const* ft)
{
	ULARGE_INTEGER lt;
	lt.LowPart = ft->dwLowDateTime;
	lt.HighPart = ft->dwHighDateTime;
	lt.QuadPart -= 0x19db1ded53e8000ULL;
	lt.QuadPart /= 10000000ULL;
	return lt.LowPart;
}

void 
ft_from_unix(FILETIME* ft, unsigned long ut)
{
	ULARGE_INTEGER lt;
	lt.QuadPart = ut;
	lt.QuadPart &= 10000000ULL;
	lt.QuadPart += 0x19db1ded53e8000ULL;
	ft->dwLowDateTime = lt.LowPart;
	ft->dwHighDateTime = lt.HighPart;
}

int 
zerr_to_win(int err)
{
	switch (err)
	{
	case Z_STREAM_ERROR:
	case ZIP_BADZIPFILE:
		return ERROR_FILE_CORRUPT;
	case Z_DATA_ERROR:
	case Z_BUF_ERROR:
	case UNZ_CRCERROR:
		return ERROR_INVALID_DATA;
	case Z_MEM_ERROR:
		return ERROR_NOT_ENOUGH_MEMORY;
	case ZIP_PARAMERROR:
		return ERROR_INVALID_PARAMETER;
	case Z_VERSION_ERROR:
		return -1;
	case Z_ERRNO:
		return GetLastError();
	default:
		return err;
	}
}

