gzdoom/libraries/lzma/C/7zStream.c

177 lines
4.1 KiB
C
Raw Normal View History

2016-03-01 15:47:10 +00:00
/* 7zStream.c -- 7z Stream functions
2021-02-09 : Igor Pavlov : Public domain */
2016-03-01 15:47:10 +00:00
#include "Precomp.h"
#include <string.h>
#include "7zTypes.h"
2018-01-08 12:00:01 +00:00
SRes SeqInStream_Read2(const ISeqInStream *stream, void *buf, size_t size, SRes errorType)
2016-03-01 15:47:10 +00:00
{
while (size != 0)
{
size_t processed = size;
2018-01-08 12:00:01 +00:00
RINOK(ISeqInStream_Read(stream, buf, &processed));
2016-03-01 15:47:10 +00:00
if (processed == 0)
return errorType;
buf = (void *)((Byte *)buf + processed);
size -= processed;
}
return SZ_OK;
}
2018-01-08 12:00:01 +00:00
SRes SeqInStream_Read(const ISeqInStream *stream, void *buf, size_t size)
2016-03-01 15:47:10 +00:00
{
return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
}
2018-01-08 12:00:01 +00:00
SRes SeqInStream_ReadByte(const ISeqInStream *stream, Byte *buf)
2016-03-01 15:47:10 +00:00
{
size_t processed = 1;
2018-01-08 12:00:01 +00:00
RINOK(ISeqInStream_Read(stream, buf, &processed));
2016-03-01 15:47:10 +00:00
return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
}
2018-01-08 12:00:01 +00:00
SRes LookInStream_SeekTo(const ILookInStream *stream, UInt64 offset)
2016-03-01 15:47:10 +00:00
{
Int64 t = (Int64)offset;
2018-01-08 12:00:01 +00:00
return ILookInStream_Seek(stream, &t, SZ_SEEK_SET);
2016-03-01 15:47:10 +00:00
}
2018-01-08 12:00:01 +00:00
SRes LookInStream_LookRead(const ILookInStream *stream, void *buf, size_t *size)
2016-03-01 15:47:10 +00:00
{
const void *lookBuf;
if (*size == 0)
return SZ_OK;
2018-01-08 12:00:01 +00:00
RINOK(ILookInStream_Look(stream, &lookBuf, size));
2016-03-01 15:47:10 +00:00
memcpy(buf, lookBuf, *size);
2018-01-08 12:00:01 +00:00
return ILookInStream_Skip(stream, *size);
2016-03-01 15:47:10 +00:00
}
2018-01-08 12:00:01 +00:00
SRes LookInStream_Read2(const ILookInStream *stream, void *buf, size_t size, SRes errorType)
2016-03-01 15:47:10 +00:00
{
while (size != 0)
{
size_t processed = size;
2018-01-08 12:00:01 +00:00
RINOK(ILookInStream_Read(stream, buf, &processed));
2016-03-01 15:47:10 +00:00
if (processed == 0)
return errorType;
buf = (void *)((Byte *)buf + processed);
size -= processed;
}
return SZ_OK;
}
2018-01-08 12:00:01 +00:00
SRes LookInStream_Read(const ILookInStream *stream, void *buf, size_t size)
2016-03-01 15:47:10 +00:00
{
return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
}
2018-01-08 12:00:01 +00:00
#define GET_LookToRead2 CLookToRead2 *p = CONTAINER_FROM_VTBL(pp, CLookToRead2, vt);
static SRes LookToRead2_Look_Lookahead(const ILookInStream *pp, const void **buf, size_t *size)
2016-03-01 15:47:10 +00:00
{
SRes res = SZ_OK;
2018-01-08 12:00:01 +00:00
GET_LookToRead2
2016-03-01 15:47:10 +00:00
size_t size2 = p->size - p->pos;
2018-01-08 12:00:01 +00:00
if (size2 == 0 && *size != 0)
2016-03-01 15:47:10 +00:00
{
p->pos = 0;
2018-01-08 12:00:01 +00:00
p->size = 0;
size2 = p->bufSize;
res = ISeekInStream_Read(p->realStream, p->buf, &size2);
2016-03-01 15:47:10 +00:00
p->size = size2;
}
2018-01-08 12:00:01 +00:00
if (*size > size2)
2016-03-01 15:47:10 +00:00
*size = size2;
*buf = p->buf + p->pos;
return res;
}
2018-01-08 12:00:01 +00:00
static SRes LookToRead2_Look_Exact(const ILookInStream *pp, const void **buf, size_t *size)
2016-03-01 15:47:10 +00:00
{
SRes res = SZ_OK;
2018-01-08 12:00:01 +00:00
GET_LookToRead2
2016-03-01 15:47:10 +00:00
size_t size2 = p->size - p->pos;
2018-01-08 12:00:01 +00:00
if (size2 == 0 && *size != 0)
2016-03-01 15:47:10 +00:00
{
p->pos = 0;
2018-01-08 12:00:01 +00:00
p->size = 0;
if (*size > p->bufSize)
*size = p->bufSize;
res = ISeekInStream_Read(p->realStream, p->buf, size);
2016-03-01 15:47:10 +00:00
size2 = p->size = *size;
}
2018-01-08 12:00:01 +00:00
if (*size > size2)
2016-03-01 15:47:10 +00:00
*size = size2;
*buf = p->buf + p->pos;
return res;
}
2018-01-08 12:00:01 +00:00
static SRes LookToRead2_Skip(const ILookInStream *pp, size_t offset)
2016-03-01 15:47:10 +00:00
{
2018-01-08 12:00:01 +00:00
GET_LookToRead2
2016-03-01 15:47:10 +00:00
p->pos += offset;
return SZ_OK;
}
2018-01-08 12:00:01 +00:00
static SRes LookToRead2_Read(const ILookInStream *pp, void *buf, size_t *size)
2016-03-01 15:47:10 +00:00
{
2018-01-08 12:00:01 +00:00
GET_LookToRead2
2016-03-01 15:47:10 +00:00
size_t rem = p->size - p->pos;
if (rem == 0)
2018-01-08 12:00:01 +00:00
return ISeekInStream_Read(p->realStream, buf, size);
2016-03-01 15:47:10 +00:00
if (rem > *size)
rem = *size;
memcpy(buf, p->buf + p->pos, rem);
p->pos += rem;
*size = rem;
return SZ_OK;
}
2018-01-08 12:00:01 +00:00
static SRes LookToRead2_Seek(const ILookInStream *pp, Int64 *pos, ESzSeek origin)
2016-03-01 15:47:10 +00:00
{
2018-01-08 12:00:01 +00:00
GET_LookToRead2
2016-03-01 15:47:10 +00:00
p->pos = p->size = 0;
2018-01-08 12:00:01 +00:00
return ISeekInStream_Seek(p->realStream, pos, origin);
2016-03-01 15:47:10 +00:00
}
2018-01-08 12:00:01 +00:00
void LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead)
2016-03-01 15:47:10 +00:00
{
2018-01-08 12:00:01 +00:00
p->vt.Look = lookahead ?
LookToRead2_Look_Lookahead :
LookToRead2_Look_Exact;
p->vt.Skip = LookToRead2_Skip;
p->vt.Read = LookToRead2_Read;
p->vt.Seek = LookToRead2_Seek;
2016-03-01 15:47:10 +00:00
}
2018-01-08 12:00:01 +00:00
static SRes SecToLook_Read(const ISeqInStream *pp, void *buf, size_t *size)
2016-03-01 15:47:10 +00:00
{
2018-01-08 12:00:01 +00:00
CSecToLook *p = CONTAINER_FROM_VTBL(pp, CSecToLook, vt);
2016-03-01 15:47:10 +00:00
return LookInStream_LookRead(p->realStream, buf, size);
}
void SecToLook_CreateVTable(CSecToLook *p)
{
2018-01-08 12:00:01 +00:00
p->vt.Read = SecToLook_Read;
2016-03-01 15:47:10 +00:00
}
2018-01-08 12:00:01 +00:00
static SRes SecToRead_Read(const ISeqInStream *pp, void *buf, size_t *size)
2016-03-01 15:47:10 +00:00
{
2018-01-08 12:00:01 +00:00
CSecToRead *p = CONTAINER_FROM_VTBL(pp, CSecToRead, vt);
return ILookInStream_Read(p->realStream, buf, size);
2016-03-01 15:47:10 +00:00
}
void SecToRead_CreateVTable(CSecToRead *p)
{
2018-01-08 12:00:01 +00:00
p->vt.Read = SecToRead_Read;
2016-03-01 15:47:10 +00:00
}