CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/GeneratorInterface/ReggeGribovPartonMCInterface/src/7zStream.cc

Go to the documentation of this file.
00001 /* 7zStream.c -- 7z Stream functions
00002 2010-03-11 : Igor Pavlov : Public domain */
00003 
00004 #include <string.h>
00005 
00006 #include "Types.h"
00007 
00008 SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType)
00009 {
00010   while (size != 0)
00011   {
00012     size_t processed = size;
00013     RINOK(stream->Read(stream, buf, &processed));
00014     if (processed == 0)
00015       return errorType;
00016     buf = (void *)((Byte *)buf + processed);
00017     size -= processed;
00018   }
00019   return SZ_OK;
00020 }
00021 
00022 SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size)
00023 {
00024   return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
00025 }
00026 
00027 SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf)
00028 {
00029   size_t processed = 1;
00030   RINOK(stream->Read(stream, buf, &processed));
00031   return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
00032 }
00033 
00034 SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset)
00035 {
00036   Int64 t = offset;
00037   return stream->Seek(stream, &t, SZ_SEEK_SET);
00038 }
00039 
00040 SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size)
00041 {
00042   const void *lookBuf;
00043   if (*size == 0)
00044     return SZ_OK;
00045   RINOK(stream->Look(stream, &lookBuf, size));
00046   memcpy(buf, lookBuf, *size);
00047   return stream->Skip(stream, *size);
00048 }
00049 
00050 SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType)
00051 {
00052   while (size != 0)
00053   {
00054     size_t processed = size;
00055     RINOK(stream->Read(stream, buf, &processed));
00056     if (processed == 0)
00057       return errorType;
00058     buf = (void *)((Byte *)buf + processed);
00059     size -= processed;
00060   }
00061   return SZ_OK;
00062 }
00063 
00064 SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size)
00065 {
00066   return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
00067 }
00068 
00069 static SRes LookToRead_Look_Lookahead(void *pp, const void **buf, size_t *size)
00070 {
00071   SRes res = SZ_OK;
00072   CLookToRead *p = (CLookToRead *)pp;
00073   size_t size2 = p->size - p->pos;
00074   if (size2 == 0 && *size > 0)
00075   {
00076     p->pos = 0;
00077     size2 = LookToRead_BUF_SIZE;
00078     res = p->realStream->Read(p->realStream, p->buf, &size2);
00079     p->size = size2;
00080   }
00081   if (size2 < *size)
00082     *size = size2;
00083   *buf = p->buf + p->pos;
00084   return res;
00085 }
00086 
00087 static SRes LookToRead_Look_Exact(void *pp, const void **buf, size_t *size)
00088 {
00089   SRes res = SZ_OK;
00090   CLookToRead *p = (CLookToRead *)pp;
00091   size_t size2 = p->size - p->pos;
00092   if (size2 == 0 && *size > 0)
00093   {
00094     p->pos = 0;
00095     if (*size > LookToRead_BUF_SIZE)
00096       *size = LookToRead_BUF_SIZE;
00097     res = p->realStream->Read(p->realStream, p->buf, size);
00098     size2 = p->size = *size;
00099   }
00100   if (size2 < *size)
00101     *size = size2;
00102   *buf = p->buf + p->pos;
00103   return res;
00104 }
00105 
00106 static SRes LookToRead_Skip(void *pp, size_t offset)
00107 {
00108   CLookToRead *p = (CLookToRead *)pp;
00109   p->pos += offset;
00110   return SZ_OK;
00111 }
00112 
00113 static SRes LookToRead_Read(void *pp, void *buf, size_t *size)
00114 {
00115   CLookToRead *p = (CLookToRead *)pp;
00116   size_t rem = p->size - p->pos;
00117   if (rem == 0)
00118     return p->realStream->Read(p->realStream, buf, size);
00119   if (rem > *size)
00120     rem = *size;
00121   memcpy(buf, p->buf + p->pos, rem);
00122   p->pos += rem;
00123   *size = rem;
00124   return SZ_OK;
00125 }
00126 
00127 static SRes LookToRead_Seek(void *pp, Int64 *pos, ESzSeek origin)
00128 {
00129   CLookToRead *p = (CLookToRead *)pp;
00130   p->pos = p->size = 0;
00131   return p->realStream->Seek(p->realStream, pos, origin);
00132 }
00133 
00134 void LookToRead_CreateVTable(CLookToRead *p, int lookahead)
00135 {
00136   p->s.Look = lookahead ?
00137       LookToRead_Look_Lookahead :
00138       LookToRead_Look_Exact;
00139   p->s.Skip = LookToRead_Skip;
00140   p->s.Read = LookToRead_Read;
00141   p->s.Seek = LookToRead_Seek;
00142 }
00143 
00144 void LookToRead_Init(CLookToRead *p)
00145 {
00146   p->pos = p->size = 0;
00147 }
00148 
00149 static SRes SecToLook_Read(void *pp, void *buf, size_t *size)
00150 {
00151   CSecToLook *p = (CSecToLook *)pp;
00152   return LookInStream_LookRead(p->realStream, buf, size);
00153 }
00154 
00155 void SecToLook_CreateVTable(CSecToLook *p)
00156 {
00157   p->s.Read = SecToLook_Read;
00158 }
00159 
00160 static SRes SecToRead_Read(void *pp, void *buf, size_t *size)
00161 {
00162   CSecToRead *p = (CSecToRead *)pp;
00163   return p->realStream->Read(p->realStream, buf, size);
00164 }
00165 
00166 void SecToRead_CreateVTable(CSecToRead *p)
00167 {
00168   p->s.Read = SecToRead_Read;
00169 }