1370b324cSopenharmony_ci/* 7zStream.c -- 7z Stream functions 2370b324cSopenharmony_ci2023-04-02 : Igor Pavlov : Public domain */ 3370b324cSopenharmony_ci 4370b324cSopenharmony_ci#include "Precomp.h" 5370b324cSopenharmony_ci 6370b324cSopenharmony_ci#include <string.h> 7370b324cSopenharmony_ci 8370b324cSopenharmony_ci#include "7zTypes.h" 9370b324cSopenharmony_ci 10370b324cSopenharmony_ci 11370b324cSopenharmony_ciSRes SeqInStream_ReadMax(ISeqInStreamPtr stream, void *buf, size_t *processedSize) 12370b324cSopenharmony_ci{ 13370b324cSopenharmony_ci size_t size = *processedSize; 14370b324cSopenharmony_ci *processedSize = 0; 15370b324cSopenharmony_ci while (size != 0) 16370b324cSopenharmony_ci { 17370b324cSopenharmony_ci size_t cur = size; 18370b324cSopenharmony_ci const SRes res = ISeqInStream_Read(stream, buf, &cur); 19370b324cSopenharmony_ci *processedSize += cur; 20370b324cSopenharmony_ci buf = (void *)((Byte *)buf + cur); 21370b324cSopenharmony_ci size -= cur; 22370b324cSopenharmony_ci if (res != SZ_OK) 23370b324cSopenharmony_ci return res; 24370b324cSopenharmony_ci if (cur == 0) 25370b324cSopenharmony_ci return SZ_OK; 26370b324cSopenharmony_ci } 27370b324cSopenharmony_ci return SZ_OK; 28370b324cSopenharmony_ci} 29370b324cSopenharmony_ci 30370b324cSopenharmony_ci/* 31370b324cSopenharmony_ciSRes SeqInStream_Read2(ISeqInStreamPtr stream, void *buf, size_t size, SRes errorType) 32370b324cSopenharmony_ci{ 33370b324cSopenharmony_ci while (size != 0) 34370b324cSopenharmony_ci { 35370b324cSopenharmony_ci size_t processed = size; 36370b324cSopenharmony_ci RINOK(ISeqInStream_Read(stream, buf, &processed)) 37370b324cSopenharmony_ci if (processed == 0) 38370b324cSopenharmony_ci return errorType; 39370b324cSopenharmony_ci buf = (void *)((Byte *)buf + processed); 40370b324cSopenharmony_ci size -= processed; 41370b324cSopenharmony_ci } 42370b324cSopenharmony_ci return SZ_OK; 43370b324cSopenharmony_ci} 44370b324cSopenharmony_ci 45370b324cSopenharmony_ciSRes SeqInStream_Read(ISeqInStreamPtr stream, void *buf, size_t size) 46370b324cSopenharmony_ci{ 47370b324cSopenharmony_ci return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF); 48370b324cSopenharmony_ci} 49370b324cSopenharmony_ci*/ 50370b324cSopenharmony_ci 51370b324cSopenharmony_ci 52370b324cSopenharmony_ciSRes SeqInStream_ReadByte(ISeqInStreamPtr stream, Byte *buf) 53370b324cSopenharmony_ci{ 54370b324cSopenharmony_ci size_t processed = 1; 55370b324cSopenharmony_ci RINOK(ISeqInStream_Read(stream, buf, &processed)) 56370b324cSopenharmony_ci return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF; 57370b324cSopenharmony_ci} 58370b324cSopenharmony_ci 59370b324cSopenharmony_ci 60370b324cSopenharmony_ci 61370b324cSopenharmony_ciSRes LookInStream_SeekTo(ILookInStreamPtr stream, UInt64 offset) 62370b324cSopenharmony_ci{ 63370b324cSopenharmony_ci Int64 t = (Int64)offset; 64370b324cSopenharmony_ci return ILookInStream_Seek(stream, &t, SZ_SEEK_SET); 65370b324cSopenharmony_ci} 66370b324cSopenharmony_ci 67370b324cSopenharmony_ciSRes LookInStream_LookRead(ILookInStreamPtr stream, void *buf, size_t *size) 68370b324cSopenharmony_ci{ 69370b324cSopenharmony_ci const void *lookBuf; 70370b324cSopenharmony_ci if (*size == 0) 71370b324cSopenharmony_ci return SZ_OK; 72370b324cSopenharmony_ci RINOK(ILookInStream_Look(stream, &lookBuf, size)) 73370b324cSopenharmony_ci memcpy(buf, lookBuf, *size); 74370b324cSopenharmony_ci return ILookInStream_Skip(stream, *size); 75370b324cSopenharmony_ci} 76370b324cSopenharmony_ci 77370b324cSopenharmony_ciSRes LookInStream_Read2(ILookInStreamPtr stream, void *buf, size_t size, SRes errorType) 78370b324cSopenharmony_ci{ 79370b324cSopenharmony_ci while (size != 0) 80370b324cSopenharmony_ci { 81370b324cSopenharmony_ci size_t processed = size; 82370b324cSopenharmony_ci RINOK(ILookInStream_Read(stream, buf, &processed)) 83370b324cSopenharmony_ci if (processed == 0) 84370b324cSopenharmony_ci return errorType; 85370b324cSopenharmony_ci buf = (void *)((Byte *)buf + processed); 86370b324cSopenharmony_ci size -= processed; 87370b324cSopenharmony_ci } 88370b324cSopenharmony_ci return SZ_OK; 89370b324cSopenharmony_ci} 90370b324cSopenharmony_ci 91370b324cSopenharmony_ciSRes LookInStream_Read(ILookInStreamPtr stream, void *buf, size_t size) 92370b324cSopenharmony_ci{ 93370b324cSopenharmony_ci return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF); 94370b324cSopenharmony_ci} 95370b324cSopenharmony_ci 96370b324cSopenharmony_ci 97370b324cSopenharmony_ci 98370b324cSopenharmony_ci#define GET_LookToRead2 Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CLookToRead2) 99370b324cSopenharmony_ci 100370b324cSopenharmony_cistatic SRes LookToRead2_Look_Lookahead(ILookInStreamPtr pp, const void **buf, size_t *size) 101370b324cSopenharmony_ci{ 102370b324cSopenharmony_ci SRes res = SZ_OK; 103370b324cSopenharmony_ci GET_LookToRead2 104370b324cSopenharmony_ci size_t size2 = p->size - p->pos; 105370b324cSopenharmony_ci if (size2 == 0 && *size != 0) 106370b324cSopenharmony_ci { 107370b324cSopenharmony_ci p->pos = 0; 108370b324cSopenharmony_ci p->size = 0; 109370b324cSopenharmony_ci size2 = p->bufSize; 110370b324cSopenharmony_ci res = ISeekInStream_Read(p->realStream, p->buf, &size2); 111370b324cSopenharmony_ci p->size = size2; 112370b324cSopenharmony_ci } 113370b324cSopenharmony_ci if (*size > size2) 114370b324cSopenharmony_ci *size = size2; 115370b324cSopenharmony_ci *buf = p->buf + p->pos; 116370b324cSopenharmony_ci return res; 117370b324cSopenharmony_ci} 118370b324cSopenharmony_ci 119370b324cSopenharmony_cistatic SRes LookToRead2_Look_Exact(ILookInStreamPtr pp, const void **buf, size_t *size) 120370b324cSopenharmony_ci{ 121370b324cSopenharmony_ci SRes res = SZ_OK; 122370b324cSopenharmony_ci GET_LookToRead2 123370b324cSopenharmony_ci size_t size2 = p->size - p->pos; 124370b324cSopenharmony_ci if (size2 == 0 && *size != 0) 125370b324cSopenharmony_ci { 126370b324cSopenharmony_ci p->pos = 0; 127370b324cSopenharmony_ci p->size = 0; 128370b324cSopenharmony_ci if (*size > p->bufSize) 129370b324cSopenharmony_ci *size = p->bufSize; 130370b324cSopenharmony_ci res = ISeekInStream_Read(p->realStream, p->buf, size); 131370b324cSopenharmony_ci size2 = p->size = *size; 132370b324cSopenharmony_ci } 133370b324cSopenharmony_ci if (*size > size2) 134370b324cSopenharmony_ci *size = size2; 135370b324cSopenharmony_ci *buf = p->buf + p->pos; 136370b324cSopenharmony_ci return res; 137370b324cSopenharmony_ci} 138370b324cSopenharmony_ci 139370b324cSopenharmony_cistatic SRes LookToRead2_Skip(ILookInStreamPtr pp, size_t offset) 140370b324cSopenharmony_ci{ 141370b324cSopenharmony_ci GET_LookToRead2 142370b324cSopenharmony_ci p->pos += offset; 143370b324cSopenharmony_ci return SZ_OK; 144370b324cSopenharmony_ci} 145370b324cSopenharmony_ci 146370b324cSopenharmony_cistatic SRes LookToRead2_Read(ILookInStreamPtr pp, void *buf, size_t *size) 147370b324cSopenharmony_ci{ 148370b324cSopenharmony_ci GET_LookToRead2 149370b324cSopenharmony_ci size_t rem = p->size - p->pos; 150370b324cSopenharmony_ci if (rem == 0) 151370b324cSopenharmony_ci return ISeekInStream_Read(p->realStream, buf, size); 152370b324cSopenharmony_ci if (rem > *size) 153370b324cSopenharmony_ci rem = *size; 154370b324cSopenharmony_ci memcpy(buf, p->buf + p->pos, rem); 155370b324cSopenharmony_ci p->pos += rem; 156370b324cSopenharmony_ci *size = rem; 157370b324cSopenharmony_ci return SZ_OK; 158370b324cSopenharmony_ci} 159370b324cSopenharmony_ci 160370b324cSopenharmony_cistatic SRes LookToRead2_Seek(ILookInStreamPtr pp, Int64 *pos, ESzSeek origin) 161370b324cSopenharmony_ci{ 162370b324cSopenharmony_ci GET_LookToRead2 163370b324cSopenharmony_ci p->pos = p->size = 0; 164370b324cSopenharmony_ci return ISeekInStream_Seek(p->realStream, pos, origin); 165370b324cSopenharmony_ci} 166370b324cSopenharmony_ci 167370b324cSopenharmony_civoid LookToRead2_CreateVTable(CLookToRead2 *p, int lookahead) 168370b324cSopenharmony_ci{ 169370b324cSopenharmony_ci p->vt.Look = lookahead ? 170370b324cSopenharmony_ci LookToRead2_Look_Lookahead : 171370b324cSopenharmony_ci LookToRead2_Look_Exact; 172370b324cSopenharmony_ci p->vt.Skip = LookToRead2_Skip; 173370b324cSopenharmony_ci p->vt.Read = LookToRead2_Read; 174370b324cSopenharmony_ci p->vt.Seek = LookToRead2_Seek; 175370b324cSopenharmony_ci} 176370b324cSopenharmony_ci 177370b324cSopenharmony_ci 178370b324cSopenharmony_ci 179370b324cSopenharmony_cistatic SRes SecToLook_Read(ISeqInStreamPtr pp, void *buf, size_t *size) 180370b324cSopenharmony_ci{ 181370b324cSopenharmony_ci Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSecToLook) 182370b324cSopenharmony_ci return LookInStream_LookRead(p->realStream, buf, size); 183370b324cSopenharmony_ci} 184370b324cSopenharmony_ci 185370b324cSopenharmony_civoid SecToLook_CreateVTable(CSecToLook *p) 186370b324cSopenharmony_ci{ 187370b324cSopenharmony_ci p->vt.Read = SecToLook_Read; 188370b324cSopenharmony_ci} 189370b324cSopenharmony_ci 190370b324cSopenharmony_cistatic SRes SecToRead_Read(ISeqInStreamPtr pp, void *buf, size_t *size) 191370b324cSopenharmony_ci{ 192370b324cSopenharmony_ci Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CSecToRead) 193370b324cSopenharmony_ci return ILookInStream_Read(p->realStream, buf, size); 194370b324cSopenharmony_ci} 195370b324cSopenharmony_ci 196370b324cSopenharmony_civoid SecToRead_CreateVTable(CSecToRead *p) 197370b324cSopenharmony_ci{ 198370b324cSopenharmony_ci p->vt.Read = SecToRead_Read; 199370b324cSopenharmony_ci} 200