1 /* 7zStream.c -- 7z Stream functions
2 2017-04-03 : Igor Pavlov : Public domain */
10 SRes
SeqInStream_Read2(const ISeqInStream
*stream
, void *buf
, size_t size
, SRes errorType
)
14 size_t processed
= size
;
15 RINOK(ISeqInStream_Read(stream
, buf
, &processed
));
18 buf
= (void *)((Byte
*)buf
+ processed
);
24 SRes
SeqInStream_Read(const ISeqInStream
*stream
, void *buf
, size_t size
)
26 return SeqInStream_Read2(stream
, buf
, size
, SZ_ERROR_INPUT_EOF
);
29 SRes
SeqInStream_ReadByte(const ISeqInStream
*stream
, Byte
*buf
)
32 RINOK(ISeqInStream_Read(stream
, buf
, &processed
));
33 return (processed
== 1) ? SZ_OK
: SZ_ERROR_INPUT_EOF
;
38 SRes
LookInStream_SeekTo(const ILookInStream
*stream
, UInt64 offset
)
41 return ILookInStream_Seek(stream
, &t
, SZ_SEEK_SET
);
44 SRes
LookInStream_LookRead(const ILookInStream
*stream
, void *buf
, size_t *size
)
49 RINOK(ILookInStream_Look(stream
, &lookBuf
, size
));
50 memcpy(buf
, lookBuf
, *size
);
51 return ILookInStream_Skip(stream
, *size
);
54 SRes
LookInStream_Read2(const ILookInStream
*stream
, void *buf
, size_t size
, SRes errorType
)
58 size_t processed
= size
;
59 RINOK(ILookInStream_Read(stream
, buf
, &processed
));
62 buf
= (void *)((Byte
*)buf
+ processed
);
68 SRes
LookInStream_Read(const ILookInStream
*stream
, void *buf
, size_t size
)
70 return LookInStream_Read2(stream
, buf
, size
, SZ_ERROR_INPUT_EOF
);
75 #define GET_LookToRead2 CLookToRead2 *p = CONTAINER_FROM_VTBL(pp, CLookToRead2, vt);
77 static SRes
LookToRead2_Look_Lookahead(const ILookInStream
*pp
, const void **buf
, size_t *size
)
81 size_t size2
= p
->size
- p
->pos
;
82 if (size2
== 0 && *size
!= 0)
87 res
= ISeekInStream_Read(p
->realStream
, p
->buf
, &size2
);
92 *buf
= p
->buf
+ p
->pos
;
96 static SRes
LookToRead2_Look_Exact(const ILookInStream
*pp
, const void **buf
, size_t *size
)
100 size_t size2
= p
->size
- p
->pos
;
101 if (size2
== 0 && *size
!= 0)
105 if (*size
> p
->bufSize
)
107 res
= ISeekInStream_Read(p
->realStream
, p
->buf
, size
);
108 size2
= p
->size
= *size
;
112 *buf
= p
->buf
+ p
->pos
;
116 static SRes
LookToRead2_Skip(const ILookInStream
*pp
, size_t offset
)
123 static SRes
LookToRead2_Read(const ILookInStream
*pp
, void *buf
, size_t *size
)
126 size_t rem
= p
->size
- p
->pos
;
128 return ISeekInStream_Read(p
->realStream
, buf
, size
);
131 memcpy(buf
, p
->buf
+ p
->pos
, rem
);
137 static SRes
LookToRead2_Seek(const ILookInStream
*pp
, Int64
*pos
, ESzSeek origin
)
140 p
->pos
= p
->size
= 0;
141 return ISeekInStream_Seek(p
->realStream
, pos
, origin
);
144 void LookToRead2_CreateVTable(CLookToRead2
*p
, int lookahead
)
146 p
->vt
.Look
= lookahead
?
147 LookToRead2_Look_Lookahead
:
148 LookToRead2_Look_Exact
;
149 p
->vt
.Skip
= LookToRead2_Skip
;
150 p
->vt
.Read
= LookToRead2_Read
;
151 p
->vt
.Seek
= LookToRead2_Seek
;
156 static SRes
SecToLook_Read(const ISeqInStream
*pp
, void *buf
, size_t *size
)
158 CSecToLook
*p
= CONTAINER_FROM_VTBL(pp
, CSecToLook
, vt
);
159 return LookInStream_LookRead(p
->realStream
, buf
, size
);
162 void SecToLook_CreateVTable(CSecToLook
*p
)
164 p
->vt
.Read
= SecToLook_Read
;
167 static SRes
SecToRead_Read(const ISeqInStream
*pp
, void *buf
, size_t *size
)
169 CSecToRead
*p
= CONTAINER_FROM_VTBL(pp
, CSecToRead
, vt
);
170 return ILookInStream_Read(p
->realStream
, buf
, size
);
173 void SecToRead_CreateVTable(CSecToRead
*p
)
175 p
->vt
.Read
= SecToRead_Read
;