1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#include "sfdchdr.h"
/* Discipline to treat a contiguous segment of a stream as a stream
** in its own right. The hard part in all this is to allow multiple
** segments of the stream to be used as substreams at the same time.
**
** Written by David G. Korn and Kiem-Phong Vo (03/18/1998)
*/
typedef struct _subfile_s
{
Sfdisc_t disc; /* sfio discipline */
Sfio_t* parent; /* parent stream */
Sfoff_t offset; /* starting offset */
Sfoff_t extent; /* size wanted */
Sfoff_t here; /* current seek location */
} Subfile_t;
#if __STD_C
static ssize_t streamio(Sfio_t* f, Void_t* buf, size_t n, Sfdisc_t* disc, int type)
#else
static ssize_t streamio(f, buf, n, disc, type)
Sfio_t* f;
Void_t* buf;
size_t n;
Sfdisc_t* disc;
int type;
#endif
{
reg Subfile_t *su;
reg Sfoff_t here, parent;
reg ssize_t io;
su = (Subfile_t*)disc;
/* read just what we need */
if(su->extent >= 0 && (ssize_t)n > (io = (ssize_t)(su->extent - su->here)) )
n = io;
if(n <= 0)
return n;
/* save current location in parent stream */
parent = sfsk(f,(Sfoff_t)0,SEEK_CUR,disc);
/* read data */
here = su->here + su->offset;
if(sfsk(f,here,SEEK_SET,disc) != here)
io = 0;
else
{ if(type == SF_WRITE)
io = sfwr(f,buf,n,disc);
else io = sfrd(f,buf,n,disc);
if(io > 0)
su->here += io;
}
/* restore parent current position */
sfsk(f,parent,SEEK_SET,disc);
return io;
}
#if __STD_C
static ssize_t streamwrite(Sfio_t* f, const Void_t* buf, size_t n, Sfdisc_t* disc)
#else
static ssize_t streamwrite(f, buf, n, disc)
Sfio_t* f;
Void_t* buf;
size_t n;
Sfdisc_t* disc;
#endif
{
return streamio(f,(Void_t*)buf,n,disc,SF_WRITE);
}
#if __STD_C
static ssize_t streamread(Sfio_t* f, Void_t* buf, size_t n, Sfdisc_t* disc)
#else
static ssize_t streamread(f, buf, n, disc)
Sfio_t* f;
Void_t* buf;
size_t n;
Sfdisc_t* disc;
#endif
{
return streamio(f,buf,n,disc,SF_READ);
}
#if __STD_C
static Sfoff_t streamseek(Sfio_t* f, Sfoff_t pos, int type, Sfdisc_t* disc)
#else
static Sfoff_t streamseek(f, pos, type, disc)
Sfio_t* f;
Sfoff_t pos;
int type;
Sfdisc_t* disc;
#endif
{
reg Subfile_t* su;
reg Sfoff_t here, parent;
su = (Subfile_t*)disc;
switch(type)
{
case SEEK_SET:
here = 0;
break;
case SEEK_CUR:
here = su->here;
break;
case SEEK_END:
if(su->extent >= 0)
here = su->extent;
else
{ parent = sfsk(f,(Sfoff_t)0,SEEK_CUR,disc);
if((here = sfsk(f,(Sfoff_t)0,SEEK_END,disc)) < 0)
return -1;
else here -= su->offset;
sfsk(f,parent,SEEK_SET,disc);
}
break;
default:
return -1;
}
pos += here;
if(pos < 0 || (su->extent >= 0 && pos >= su->extent))
return -1;
return (su->here = pos);
}
#if __STD_C
static int streamexcept(Sfio_t* f, int type, Void_t* data, Sfdisc_t* disc)
#else
static int streamexcept(f, type, data, disc)
Sfio_t* f;
int type;
Void_t* data;
Sfdisc_t* disc;
#endif
{
if(type == SF_FINAL || type == SF_DPOP)
free(disc);
return 0;
}
#if __STD_C
Sfio_t* sfdcsubstream(Sfio_t* f, Sfio_t* parent, Sfoff_t offset, Sfoff_t extent)
#else
Sfio_t* sfdcsubstream(f, parent, offset, extent)
Sfio_t* f; /* stream */
Sfio_t* parent; /* parent stream */
Sfoff_t offset; /* offset in f */
Sfoff_t extent; /* desired size */
#endif
{
reg Sfio_t* sp;
reg Subfile_t* su;
reg Sfoff_t here;
/* establish that we can seek to offset */
if((here = sfseek(parent,(Sfoff_t)0,SEEK_CUR)) < 0 || sfseek(parent,offset,SEEK_SET) < 0)
return 0;
else sfseek(parent,here,SEEK_SET);
sfpurge(parent);
if (!(sp = f) && !(sp = sfnew(NIL(Sfio_t*), NIL(Void_t*), (size_t)SF_UNBOUND, dup(sffileno(parent)), parent->flags)))
return 0;
if(!(su = (Subfile_t*)malloc(sizeof(Subfile_t))))
{ if(sp != f)
sfclose(sp);
return 0;
}
memset(su, 0, sizeof(*su));
su->disc.readf = streamread;
su->disc.writef = streamwrite;
su->disc.seekf = streamseek;
su->disc.exceptf = streamexcept;
su->parent = parent;
su->offset = offset;
su->extent = extent;
if(sfdisc(sp, (Sfdisc_t*)su) != (Sfdisc_t*)su)
{ free(su);
if(sp != f)
sfclose(sp);
return 0;
}
return sp;
}
|