CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
7zStream.cc
Go to the documentation of this file.
1 /* 7zStream.c -- 7z Stream functions
2 2010-03-11 : Igor Pavlov : Public domain */
3 
4 #include <string.h>
5 
6 #include "Types.h"
7 
8 SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType)
9 {
10  while (size != 0)
11  {
12  size_t processed = size;
13  RINOK(stream->Read(stream, buf, &processed));
14  if (processed == 0)
15  return errorType;
16  buf = (void *)((Byte *)buf + processed);
17  size -= processed;
18  }
19  return SZ_OK;
20 }
21 
23 {
24  return SeqInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
25 }
26 
28 {
29  size_t processed = 1;
30  RINOK(stream->Read(stream, buf, &processed));
31  return (processed == 1) ? SZ_OK : SZ_ERROR_INPUT_EOF;
32 }
33 
35 {
36  Int64 t = offset;
37  return stream->Seek(stream, &t, SZ_SEEK_SET);
38 }
39 
41 {
42  const void *lookBuf;
43  if (*size == 0)
44  return SZ_OK;
45  RINOK(stream->Look(stream, &lookBuf, size));
46  memcpy(buf, lookBuf, *size);
47  return stream->Skip(stream, *size);
48 }
49 
50 SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType)
51 {
52  while (size != 0)
53  {
54  size_t processed = size;
55  RINOK(stream->Read(stream, buf, &processed));
56  if (processed == 0)
57  return errorType;
58  buf = (void *)((Byte *)buf + processed);
59  size -= processed;
60  }
61  return SZ_OK;
62 }
63 
65 {
66  return LookInStream_Read2(stream, buf, size, SZ_ERROR_INPUT_EOF);
67 }
68 
69 static SRes LookToRead_Look_Lookahead(void *pp, const void **buf, size_t *size)
70 {
71  SRes res = SZ_OK;
72  CLookToRead *p = (CLookToRead *)pp;
73  size_t size2 = p->size - p->pos;
74  if (size2 == 0 && *size > 0)
75  {
76  p->pos = 0;
77  size2 = LookToRead_BUF_SIZE;
78  res = p->realStream->Read(p->realStream, p->buf, &size2);
79  p->size = size2;
80  }
81  if (size2 < *size)
82  *size = size2;
83  *buf = p->buf + p->pos;
84  return res;
85 }
86 
87 static SRes LookToRead_Look_Exact(void *pp, const void **buf, size_t *size)
88 {
89  SRes res = SZ_OK;
90  CLookToRead *p = (CLookToRead *)pp;
91  size_t size2 = p->size - p->pos;
92  if (size2 == 0 && *size > 0)
93  {
94  p->pos = 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;
99  }
100  if (size2 < *size)
101  *size = size2;
102  *buf = p->buf + p->pos;
103  return res;
104 }
105 
106 static SRes LookToRead_Skip(void *pp, size_t offset)
107 {
108  CLookToRead *p = (CLookToRead *)pp;
109  p->pos += offset;
110  return SZ_OK;
111 }
112 
113 static SRes LookToRead_Read(void *pp, void *buf, size_t *size)
114 {
115  CLookToRead *p = (CLookToRead *)pp;
116  size_t rem = p->size - p->pos;
117  if (rem == 0)
118  return p->realStream->Read(p->realStream, buf, size);
119  if (rem > *size)
120  rem = *size;
121  memcpy(buf, p->buf + p->pos, rem);
122  p->pos += rem;
123  *size = rem;
124  return SZ_OK;
125 }
126 
127 static SRes LookToRead_Seek(void *pp, Int64 *pos, ESzSeek origin)
128 {
129  CLookToRead *p = (CLookToRead *)pp;
130  p->pos = p->size = 0;
131  return p->realStream->Seek(p->realStream, pos, origin);
132 }
133 
134 void LookToRead_CreateVTable(CLookToRead *p, int lookahead)
135 {
136  p->s.Look = lookahead ?
139  p->s.Skip = LookToRead_Skip;
140  p->s.Read = LookToRead_Read;
141  p->s.Seek = LookToRead_Seek;
142 }
143 
145 {
146  p->pos = p->size = 0;
147 }
148 
149 static SRes SecToLook_Read(void *pp, void *buf, size_t *size)
150 {
151  CSecToLook *p = (CSecToLook *)pp;
152  return LookInStream_LookRead(p->realStream, buf, size);
153 }
154 
156 {
157  p->s.Read = SecToLook_Read;
158 }
159 
160 static SRes SecToRead_Read(void *pp, void *buf, size_t *size)
161 {
162  CSecToRead *p = (CSecToRead *)pp;
163  return p->realStream->Read(p->realStream, buf, size);
164 }
165 
167 {
168  p->s.Read = SecToRead_Read;
169 }
Byte buf[(1<< 14)]
Definition: Types.h:198
#define SZ_ERROR_INPUT_EOF
Definition: Types.h:32
static SRes LookToRead_Read(void *pp, void *buf, size_t *size)
Definition: 7zStream.cc:113
unsigned long long int UInt64
Definition: Types.h:83
tuple pp
Definition: createTree.py:15
SRes SeqInStream_ReadByte(ISeqInStream *stream, Byte *buf)
Definition: 7zStream.cc:27
#define RINOK(x)
Definition: Types.h:52
SRes SeqInStream_Read(ISeqInStream *stream, void *buf, size_t size)
Definition: 7zStream.cc:22
static SRes LookToRead_Look_Lookahead(void *pp, const void **buf, size_t *size)
Definition: 7zStream.cc:69
static SRes SecToLook_Read(void *pp, void *buf, size_t *size)
Definition: 7zStream.cc:149
SRes(* Seek)(void *p, Int64 *pos, ESzSeek origin)
Definition: Types.h:166
void LookToRead_CreateVTable(CLookToRead *p, int lookahead)
Definition: 7zStream.cc:134
static SRes LookToRead_Look_Exact(void *pp, const void **buf, size_t *size)
Definition: 7zStream.cc:87
void SecToLook_CreateVTable(CSecToLook *p)
Definition: 7zStream.cc:155
SRes(* Skip)(void *p, size_t offset)
Definition: Types.h:175
static SRes SecToRead_Read(void *pp, void *buf, size_t *size)
Definition: 7zStream.cc:160
ILookInStream s
Definition: Types.h:194
SRes SeqInStream_Read2(ISeqInStream *stream, void *buf, size_t size, SRes errorType)
Definition: 7zStream.cc:8
SRes LookInStream_SeekTo(ILookInStream *stream, UInt64 offset)
Definition: 7zStream.cc:34
void LookToRead_Init(CLookToRead *p)
Definition: 7zStream.cc:144
int SRes
Definition: Types.h:43
static SRes LookToRead_Skip(void *pp, size_t offset)
Definition: 7zStream.cc:106
ISeekInStream * realStream
Definition: Types.h:195
ESzSeek
Definition: Types.h:156
#define SZ_OK
Definition: Types.h:25
static SRes LookToRead_Seek(void *pp, Int64 *pos, ESzSeek origin)
Definition: 7zStream.cc:127
ILookInStream * realStream
Definition: Types.h:207
unsigned int offset(bool)
#define LookToRead_BUF_SIZE
Definition: Types.h:190
SRes LookInStream_Read2(ILookInStream *stream, void *buf, size_t size, SRes errorType)
Definition: 7zStream.cc:50
SRes(* Read)(void *p, void *buf, size_t *size)
Definition: Types.h:178
SRes LookInStream_Read(ILookInStream *stream, void *buf, size_t size)
Definition: 7zStream.cc:64
long long int Int64
Definition: Types.h:82
size_t size
Definition: Types.h:197
ILookInStream * realStream
Definition: Types.h:215
ISeqInStream s
Definition: Types.h:206
ISeqInStream s
Definition: Types.h:214
SRes(* Read)(void *p, void *buf, size_t *size)
Definition: Types.h:165
size_t pos
Definition: Types.h:196
SRes(* Seek)(void *p, Int64 *pos, ESzSeek origin)
Definition: Types.h:180
SRes LookInStream_LookRead(ILookInStream *stream, void *buf, size_t *size)
Definition: 7zStream.cc:40
unsigned char Byte
Definition: Types.h:55
SRes(* Read)(void *p, void *buf, size_t *size)
Definition: Types.h:139
SRes(* Look)(void *p, const void **buf, size_t *size)
Definition: Types.h:171
tuple size
Write out results.
void SecToRead_CreateVTable(CSecToRead *p)
Definition: 7zStream.cc:166