Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:07:28

0001 /* 7zStream.c -- 7z Stream functions

0002 2010-03-11 : Igor Pavlov : Public domain */
0003 
0004 #include <cstring>
0005 
0006 #include "Types.h"
0007 
0008 SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType) {
0009   while (size != 0) {
0010     size_t processed = size;
0011     RINOK(stream->Read(stream, buf, &processed));
0012     if (processed == 0)
0013       return errorType;
0014     buf = (void *)((Byte *)buf + processed);
0015     size -= processed;
0016   }
0017   return SZ_OK;
0018 }
0019 
0020 SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size) {
0021   return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
0022 }
0023 
0024 SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf) {
0025   size_t processed = 1;
0026   RINOK(stream->Read(stream, buf, &processed));
0027   return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
0028 }
0029 
0030 SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset) {
0031   Int64 t = offset;
0032   return stream->Seek(stream, &t, SZ_SEEK_SET);
0033 }
0034 
0035 SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size) {
0036   const void *lookBuf;
0037   if (*size == 0)
0038     return SZ_OK;
0039   RINOK(stream->Look(stream, &lookBuf, size));
0040   memcpy(buf, lookBuf, *size);
0041   return stream->Skip(stream, *size);
0042 }
0043 
0044 SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType) {
0045   while (size != 0) {
0046     size_t processed = size;
0047     RINOK(stream->Read(stream, buf, &processed));
0048     if (processed == 0)
0049       return errorType;
0050     buf = (void *)((Byte *)buf + processed);
0051     size -= processed;
0052   }
0053   return SZ_OK;
0054 }
0055 
0056 SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size) {
0057   return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
0058 }
0059 
0060 static SRes LookToRead_Look_Lookahead(void *pp, const void **buf, size_t *size) {
0061   SRes res = SZ_OK;
0062   CLookToRead *p = (CLookToRead *)pp;
0063   size_t size2 = p->size - p->pos;
0064   if (size2 == 0 && *size > 0) {
0065     p->pos = 0;
0066     size2 = LookToRead_BUF_SIZE;
0067     res = p->realStream->Read(p->realStream, p->buf, &size2);
0068     p->size = size2;
0069   }
0070   if (size2 < *size)
0071     *size = size2;
0072   *buf = p->buf + p->pos;
0073   return res;
0074 }
0075 
0076 static SRes LookToRead_Look_Exact(void *pp, const void **buf, size_t *size) {
0077   SRes res = SZ_OK;
0078   CLookToRead *p = (CLookToRead *)pp;
0079   size_t size2 = p->size - p->pos;
0080   if (size2 == 0 && *size > 0) {
0081     p->pos = 0;
0082     if (*size > LookToRead_BUF_SIZE)
0083       *size = LookToRead_BUF_SIZE;
0084     res = p->realStream->Read(p->realStream, p->buf, size);
0085     size2 = p->size = *size;
0086   }
0087   if (size2 < *size)
0088     *size = size2;
0089   *buf = p->buf + p->pos;
0090   return res;
0091 }
0092 
0093 static SRes LookToRead_Skip(void *pp, size_t offset) {
0094   CLookToRead *p = (CLookToRead *)pp;
0095   p->pos += offset;
0096   return SZ_OK;
0097 }
0098 
0099 static SRes LookToRead_Read(void *pp, void *buf, size_t *size) {
0100   CLookToRead *p = (CLookToRead *)pp;
0101   size_t rem = p->size - p->pos;
0102   if (rem == 0)
0103     return p->realStream->Read(p->realStream, buf, size);
0104   if (rem > *size)
0105     rem = *size;
0106   memcpy(buf, p->buf + p->pos, rem);
0107   p->pos += rem;
0108   *size = rem;
0109   return SZ_OK;
0110 }
0111 
0112 static SRes LookToRead_Seek(void *pp, Int64 *pos, ESzSeek origin) {
0113   CLookToRead *p = (CLookToRead *)pp;
0114   p->pos = p->size = 0;
0115   return p->realStream->Seek(p->realStream, pos, origin);
0116 }
0117 
0118 void LookToRead_CreateVTable(CLookToRead *p, int lookahead) {
0119   p->s.Look = lookahead ? LookToRead_Look_Lookahead : LookToRead_Look_Exact;
0120   p->s.Skip = LookToRead_Skip;
0121   p->s.Read = LookToRead_Read;
0122   p->s.Seek = LookToRead_Seek;
0123 }
0124 
0125 void LookToRead_Init(CLookToRead *p) { p->pos = p->size = 0; }
0126 
0127 static SRes SecToLook_Read(void *pp, void *buf, size_t *size) {
0128   CSecToLook *p = (CSecToLook *)pp;
0129   return LookInStream_LookRead(p->realStream, buf, size);
0130 }
0131 
0132 void SecToLook_CreateVTable(CSecToLook *p) { p->s.Read = SecToLook_Read; }
0133 
0134 static SRes SecToRead_Read(void *pp, void *buf, size_t *size) {
0135   CSecToRead *p = (CSecToRead *)pp;
0136   return p->realStream->Read(p->realStream, buf, size);
0137 }
0138 
0139 void SecToRead_CreateVTable(CSecToRead *p) { p->s.Read = SecToRead_Read; }