Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libvdelta/vd01/vdio01.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1995-2011 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 "vdelhdr01.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
}
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 = (*READF(io))(DATA(io),SIZE(io),HERE(io),DELTA(io))) > 0)
60
{ ENDB(io) = (NEXT(io) = DATA(io)) + n;
61
HERE(io) += n;
62
}
63
return n;
64
}
65
66
#if __STD_C
67
static int _vdflsbuf(reg Vdio_t* io)
68
#else
69
static int _vdflsbuf(io)
70
reg Vdio_t* io;
71
#endif
72
{ reg int n;
73
74
if(!io->data )
75
_vdinit(io);
76
77
if(io->data != io->buf) /* all space was given */
78
return REMAIN(io);
79
80
if((n = NEXT(io) - DATA(io)) > 0 &&
81
(*WRITEF(io))(DATA(io),n,HERE(io),DELTA(io)) != n)
82
return -1;
83
84
HERE(io) += n;
85
NEXT(io) = DATA(io);
86
return SIZE(io);
87
}
88
89
#if __STD_C
90
static ulong _vdgetu(reg Vdio_t* io, reg ulong v)
91
#else
92
static ulong _vdgetu(io,v)
93
reg Vdio_t* io;
94
reg ulong v;
95
#endif
96
{ reg int c;
97
98
for(v &= I_MORE-1;;)
99
{ if((c = VDGETC(io)) < 0)
100
return (ulong)(-1L);
101
if(!(c&I_MORE) )
102
return ((v<<I_SHIFT) | c);
103
v = (v<<I_SHIFT) | (c & (I_MORE-1));
104
}
105
}
106
107
#if __STD_C
108
static int _vdputu(reg Vdio_t* io, ulong v)
109
#else
110
static int _vdputu(io, v)
111
reg Vdio_t* io;
112
reg ulong v;
113
#endif
114
{
115
reg uchar *s, *next;
116
reg int len;
117
uchar c[sizeof(ulong)+1];
118
119
s = next = &c[sizeof(c)-1];
120
*s = I_CODE(v);
121
while((v >>= I_SHIFT) )
122
*--s = I_CODE(v)|I_MORE;
123
len = (next-s) + 1;
124
125
if(REMAIN(io) < len && _vdflsbuf(io) < len)
126
return -1;
127
128
next = io->next;
129
switch(len)
130
{
131
default: memcpy((Void_t*)next,(Void_t*)s,len); next += len; break;
132
case 3: *next++ = *s++;
133
case 2: *next++ = *s++;
134
case 1: *next++ = *s;
135
}
136
io->next = next;
137
138
return len;
139
}
140
141
#if __STD_C
142
static int _vdread(Vdio_t* io, reg uchar* s, reg int n)
143
#else
144
static int _vdread(io, s, n)
145
Vdio_t* io;
146
reg uchar* s;
147
reg int n;
148
#endif
149
{
150
reg uchar* next;
151
reg int r, m;
152
153
for(m = n; m > 0; )
154
{ if((r = REMAIN(io)) <= 0 && (r = _vdfilbuf(io)) <= 0)
155
break;
156
if(r > m)
157
r = m;
158
159
next = io->next;
160
MEMCPY(s,next,r);
161
io->next = next;
162
163
m -= r;
164
}
165
return n-m;
166
}
167
168
#if __STD_C
169
static int _vdwrite(Vdio_t* io, reg uchar* s, reg int n)
170
#else
171
static int _vdwrite(io, s, n)
172
Vdio_t* io;
173
reg uchar* s;
174
reg int n;
175
#endif
176
{
177
reg uchar* next;
178
reg int w, m;
179
180
for(m = n; m > 0; )
181
{ if((w = REMAIN(io)) <= 0 && (w = _vdflsbuf(io)) <= 0)
182
break;
183
if(w > m)
184
w = m;
185
186
next = io->next;
187
MEMCPY(next,s,w);
188
io->next = next;
189
190
m -= w;
191
}
192
return n-m;
193
}
194
195
196
Vdbufio_t _Vdbufio_01 =
197
{ _vdfilbuf,
198
_vdflsbuf,
199
_vdgetu,
200
_vdputu,
201
_vdread,
202
_vdwrite
203
};
204
205