Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libvdelta/vdio.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1995-2012 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* *
19
***********************************************************************/
20
#include "vdelhdr.h"
21
22
/* IO subsystem for the delta routines
23
**
24
** Written by Kiem-Phong Vo (12/15/94)
25
*/
26
27
#if __STD_C
28
static void _vdinit(reg Vdio_t* io)
29
#else
30
static void _vdinit(io)
31
reg Vdio_t* io;
32
#endif
33
{
34
if((io->data = (uchar*)io->delta->data) )
35
io->size = io->delta->size;
36
else
37
{ io->data = io->buf;
38
io->size = sizeof(io->buf);
39
}
40
io->next = io->data;
41
io->endb = io->data + io->size;
42
io->left = io->delta->size;
43
}
44
45
#if __STD_C
46
static int _vdfilbuf(reg Vdio_t* io)
47
#else
48
static int _vdfilbuf(io)
49
reg Vdio_t* io;
50
#endif
51
{ reg int n;
52
53
if(!io->data)
54
_vdinit(io);
55
56
if(io->data != io->buf) /* all data was given in core */
57
return REMAIN(io);
58
59
if ((n = SIZE(io)) > LEFT(io))
60
n = LEFT(io);
61
if((n = (*READF(io))(DATA(io),n,HERE(io),DELTA(io))) > 0)
62
{ ENDB(io) = (NEXT(io) = DATA(io)) + n;
63
HERE(io) += n;
64
LEFT(io) -= n;
65
}
66
return n;
67
}
68
69
#if __STD_C
70
static int _vdflsbuf(reg Vdio_t* io)
71
#else
72
static int _vdflsbuf(io)
73
reg Vdio_t* io;
74
#endif
75
{ reg int n;
76
77
if(!io->data )
78
_vdinit(io);
79
80
if(io->data != io->buf) /* all space was given */
81
return REMAIN(io);
82
83
if((n = NEXT(io) - DATA(io)) > 0 &&
84
(*WRITEF(io))(DATA(io),n,HERE(io),DELTA(io)) != n)
85
return -1;
86
87
HERE(io) += n;
88
NEXT(io) = DATA(io);
89
return SIZE(io);
90
}
91
92
#if __STD_C
93
static ulong _vdgetu(reg Vdio_t* io, reg ulong v)
94
#else
95
static ulong _vdgetu(io,v)
96
reg Vdio_t* io;
97
reg ulong v;
98
#endif
99
{ reg int c;
100
101
for(v &= I_MORE-1;;)
102
{ if((c = VDGETC(io)) < 0)
103
return (ulong)(-1L);
104
if(!(c&I_MORE) )
105
return ((v<<I_SHIFT) | c);
106
v = (v<<I_SHIFT) | (c & (I_MORE-1));
107
}
108
}
109
110
#if __STD_C
111
static int _vdputu(reg Vdio_t* io, ulong v)
112
#else
113
static int _vdputu(io, v)
114
reg Vdio_t* io;
115
reg ulong v;
116
#endif
117
{
118
reg uchar *s, *next;
119
reg int len;
120
uchar c[sizeof(ulong)+1];
121
122
s = next = &c[sizeof(c)-1];
123
*s = I_CODE(v);
124
while((v >>= I_SHIFT) )
125
*--s = I_CODE(v)|I_MORE;
126
len = (next-s) + 1;
127
128
if(REMAIN(io) < len && _vdflsbuf(io) < len)
129
return -1;
130
131
next = io->next;
132
switch(len)
133
{
134
default: memcpy((Void_t*)next,(Void_t*)s,len); next += len; break;
135
case 3: *next++ = *s++;
136
case 2: *next++ = *s++;
137
case 1: *next++ = *s;
138
}
139
io->next = next;
140
141
return len;
142
}
143
144
#if __STD_C
145
static int _vdread(Vdio_t* io, reg uchar* s, reg int n)
146
#else
147
static int _vdread(io, s, n)
148
Vdio_t* io;
149
reg uchar* s;
150
reg int n;
151
#endif
152
{
153
reg uchar* next;
154
reg int r, m;
155
156
for(m = n; m > 0; )
157
{ if((r = REMAIN(io)) <= 0 && (r = _vdfilbuf(io)) <= 0)
158
break;
159
if(r > m)
160
r = m;
161
162
next = io->next;
163
MEMCPY(s,next,r);
164
io->next = next;
165
166
m -= r;
167
}
168
return n-m;
169
}
170
171
#if __STD_C
172
static int _vdwrite(Vdio_t* io, reg uchar* s, reg int n)
173
#else
174
static int _vdwrite(io, s, n)
175
Vdio_t* io;
176
reg uchar* s;
177
reg int n;
178
#endif
179
{
180
reg uchar* next;
181
reg int w, m;
182
183
for(m = n; m > 0; )
184
{ if((w = REMAIN(io)) <= 0 && (w = _vdflsbuf(io)) <= 0)
185
break;
186
if(w > m)
187
w = m;
188
189
next = io->next;
190
MEMCPY(next,s,w);
191
io->next = next;
192
193
m -= w;
194
}
195
return n-m;
196
}
197
198
199
Vdbufio_t _Vdbufio =
200
{ _vdfilbuf,
201
_vdflsbuf,
202
_vdgetu,
203
_vdputu,
204
_vdread,
205
_vdwrite
206
};
207
208