1 /* 7zStream.c -- 7z Stream functions
2 2010-03-11 : Igor Pavlov : Public domain */
8 SRes
SeqInStream_Read2(ISeqInStream
*stream
, void *buf
, size_t size
, SRes errorType
)
12 size_t processed
= size
;
13 RINOK(stream
->Read(stream
, buf
, &processed
));
16 buf
= (void *)((Byte
*)buf
+ processed
);
22 SRes
SeqInStream_Read(ISeqInStream
*stream
, void *buf
, size_t size
)
24 return SeqInStream_Read2(stream
, buf
, size
, SZ_ERROR_INPUT_EOF
);
27 SRes
SeqInStream_ReadByte(ISeqInStream
*stream
, Byte
*buf
)
30 RINOK(stream
->Read(stream
, buf
, &processed
));
31 return (processed
== 1) ? SZ_OK
: SZ_ERROR_INPUT_EOF
;
34 SRes
LookInStream_SeekTo(ILookInStream
*stream
, UInt64 offset
)
37 return stream
->Seek(stream
, &t
, SZ_SEEK_SET
);
40 SRes
LookInStream_LookRead(ILookInStream
*stream
, void *buf
, size_t *size
)
45 RINOK(stream
->Look(stream
, &lookBuf
, size
));
46 memcpy(buf
, lookBuf
, *size
);
47 return stream
->Skip(stream
, *size
);
50 SRes
LookInStream_Read2(ILookInStream
*stream
, void *buf
, size_t size
, SRes errorType
)
54 size_t processed
= size
;
55 RINOK(stream
->Read(stream
, buf
, &processed
));
58 buf
= (void *)((Byte
*)buf
+ processed
);
64 SRes
LookInStream_Read(ILookInStream
*stream
, void *buf
, size_t size
)
66 return LookInStream_Read2(stream
, buf
, size
, SZ_ERROR_INPUT_EOF
);
69 static SRes
LookToRead_Look_Lookahead(void *pp
, const void **buf
, size_t *size
)
72 CLookToRead
*p
= (CLookToRead
*)pp
;
73 size_t size2
= p
->size
- p
->pos
;
74 if (size2
== 0 && *size
> 0)
77 size2
= LookToRead_BUF_SIZE
;
78 res
= p
->realStream
->Read(p
->realStream
, p
->buf
, &size2
);
83 *buf
= p
->buf
+ p
->pos
;
87 static SRes
LookToRead_Look_Exact(void *pp
, const void **buf
, size_t *size
)
90 CLookToRead
*p
= (CLookToRead
*)pp
;
91 size_t size2
= p
->size
- p
->pos
;
92 if (size2
== 0 && *size
> 0)
95 if (*size
> LookToRead_BUF_SIZE
)
96 *size
= LookToRead_BUF_SIZE
;
97 res
= p
->realStream
->Read(p
->realStream
, p
->buf
, size
);
98 size2
= p
->size
= *size
;
102 *buf
= p
->buf
+ p
->pos
;
106 static SRes
LookToRead_Skip(void *pp
, size_t offset
)
108 CLookToRead
*p
= (CLookToRead
*)pp
;
113 static SRes
LookToRead_Read(void *pp
, void *buf
, size_t *size
)
115 CLookToRead
*p
= (CLookToRead
*)pp
;
116 size_t rem
= p
->size
- p
->pos
;
118 return p
->realStream
->Read(p
->realStream
, buf
, size
);
121 memcpy(buf
, p
->buf
+ p
->pos
, rem
);
127 static SRes
LookToRead_Seek(void *pp
, Int64
*pos
, ESzSeek origin
)
129 CLookToRead
*p
= (CLookToRead
*)pp
;
130 p
->pos
= p
->size
= 0;
131 return p
->realStream
->Seek(p
->realStream
, pos
, origin
);
134 void LookToRead_CreateVTable(CLookToRead
*p
, int lookahead
)
136 p
->s
.Look
= lookahead
?
137 LookToRead_Look_Lookahead
:
138 LookToRead_Look_Exact
;
139 p
->s
.Skip
= LookToRead_Skip
;
140 p
->s
.Read
= LookToRead_Read
;
141 p
->s
.Seek
= LookToRead_Seek
;
144 void LookToRead_Init(CLookToRead
*p
)
146 p
->pos
= p
->size
= 0;
149 static SRes
SecToLook_Read(void *pp
, void *buf
, size_t *size
)
151 CSecToLook
*p
= (CSecToLook
*)pp
;
152 return LookInStream_LookRead(p
->realStream
, buf
, size
);
155 void SecToLook_CreateVTable(CSecToLook
*p
)
157 p
->s
.Read
= SecToLook_Read
;
160 static SRes
SecToRead_Read(void *pp
, void *buf
, size_t *size
)
162 CSecToRead
*p
= (CSecToRead
*)pp
;
163 return p
->realStream
->Read(p
->realStream
, buf
, size
);
166 void SecToRead_CreateVTable(CSecToRead
*p
)
168 p
->s
.Read
= SecToRead_Read
;