2017-10-01 22:42:35 -07:00
|
|
|
/* mzstrm.c -- Stream interface
|
2014-01-12 14:04:54 -07:00
|
|
|
part of the MiniZip project
|
2012-02-25 23:28:28 -07:00
|
|
|
|
2017-09-16 13:25:02 +08:00
|
|
|
Copyright (C) 2012-2017 Nathan Moinvaziri
|
|
|
|
https://github.com/nmoinvaz/minizip
|
2012-07-14 16:31:22 -07:00
|
|
|
Modifications for Zip64 support
|
2014-01-12 14:04:54 -07:00
|
|
|
Copyright (C) 2009-2010 Mathias Svensson
|
|
|
|
http://result42.com
|
2017-09-16 13:25:02 +08:00
|
|
|
Copyright (C) 1998-2010 Gilles Vollant
|
|
|
|
http://www.winimage.com/zLibDll/minizip.html
|
2014-01-12 14:04:54 -07:00
|
|
|
|
|
|
|
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-02-25 23:28:28 -07:00
|
|
|
*/
|
|
|
|
|
2012-10-21 15:20:13 -07:00
|
|
|
#include <stdlib.h>
|
2017-10-01 22:42:35 -07:00
|
|
|
#include <stdint.h>
|
2017-03-04 10:01:10 -08:00
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
#include "mzstrm.h"
|
2012-10-21 15:20:13 -07:00
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_open(void *stream, const char *path, int mode)
|
2012-02-25 23:28:28 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream *strm = (mz_stream *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (strm == NULL || strm->open == NULL)
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
2017-10-01 22:42:35 -07:00
|
|
|
return strm->open(strm, path, mode);
|
2012-02-25 23:28:28 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_is_open(void *stream)
|
2012-02-25 23:28:28 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream *strm = (mz_stream *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (strm == NULL || strm->is_open == NULL)
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
2017-09-29 21:02:09 -07:00
|
|
|
return strm->is_open(strm);
|
2012-02-25 23:28:28 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_read(void *stream, void* buf, uint32_t size)
|
2012-02-25 23:28:28 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream *strm = (mz_stream *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (strm == NULL || strm->read == NULL)
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
2017-09-29 21:02:09 -07:00
|
|
|
return strm->read(strm, buf, size);
|
2012-02-25 23:28:28 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_write(void *stream, const void *buf, uint32_t size)
|
2012-02-25 23:28:28 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream *strm = (mz_stream *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (strm == NULL || strm->write == NULL)
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
2017-09-29 21:02:09 -07:00
|
|
|
return strm->write(strm, buf, size);
|
2012-02-25 23:28:28 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int64_t mz_stream_tell(void *stream)
|
2012-02-25 23:28:28 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream *strm = (mz_stream *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (strm == NULL || strm->tell == NULL)
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
2017-09-29 21:02:09 -07:00
|
|
|
return strm->tell(strm);
|
2012-02-25 23:28:28 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_seek(void *stream, uint64_t offset, int origin)
|
2012-02-25 23:28:28 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream *strm = (mz_stream *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (strm == NULL || strm->seek == NULL)
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
2017-09-29 21:02:09 -07:00
|
|
|
return strm->seek(strm, offset, origin);
|
|
|
|
}
|
2012-02-25 23:28:28 -07:00
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_close(void *stream)
|
2012-02-25 23:28:28 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream *strm = (mz_stream *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (strm == NULL || strm->close == NULL)
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
2017-09-29 21:02:09 -07:00
|
|
|
return strm->close(strm);
|
2012-02-25 23:28:28 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_error(void *stream)
|
2012-02-25 23:28:28 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream *strm = (mz_stream *)stream;
|
2017-09-29 21:02:09 -07:00
|
|
|
if (strm == NULL || strm->error == NULL)
|
2017-10-01 21:43:24 -07:00
|
|
|
return MZ_STREAM_ERR;
|
2017-09-29 21:02:09 -07:00
|
|
|
return strm->error(strm);
|
2012-02-25 23:28:28 -07:00
|
|
|
}
|
|
|
|
|
2017-10-01 22:42:35 -07:00
|
|
|
int32_t mz_stream_set_base(void *stream, void *base)
|
2012-02-25 23:28:28 -07:00
|
|
|
{
|
2017-10-01 21:43:24 -07:00
|
|
|
mz_stream *strm = (mz_stream *)stream;
|
|
|
|
strm->base = (mz_stream *)base;
|
|
|
|
return MZ_STREAM_OK;
|
2012-02-25 23:28:28 -07:00
|
|
|
}
|
2017-10-01 22:42:35 -07:00
|
|
|
|
|
|
|
int32_t mz_os_file_exists(const char *path)
|
|
|
|
{
|
|
|
|
void *stream = NULL;
|
|
|
|
int opened = 0;
|
|
|
|
|
|
|
|
mz_stream_os_create(&stream);
|
|
|
|
|
|
|
|
if (mz_stream_os_open(stream, path, MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
|
|
|
|
{
|
|
|
|
mz_stream_os_close(stream);
|
|
|
|
opened = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mz_stream_os_delete(&stream);
|
|
|
|
|
|
|
|
return opened;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t mz_os_file_is_large(const char *path)
|
|
|
|
{
|
|
|
|
void *stream = NULL;
|
|
|
|
int64_t size = 0;
|
|
|
|
|
|
|
|
mz_stream_os_create(&stream);
|
|
|
|
|
|
|
|
if (mz_stream_os_open(stream, path, MZ_STREAM_MODE_READ) == MZ_STREAM_OK)
|
|
|
|
{
|
|
|
|
mz_stream_os_seek(stream, 0, MZ_STREAM_SEEK_END);
|
|
|
|
size = mz_stream_os_tell(stream);
|
|
|
|
mz_stream_os_close(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
mz_stream_os_delete(&stream);
|
|
|
|
|
|
|
|
return (size >= UINT32_MAX);
|
|
|
|
}
|