Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libvcodex/vcextract.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-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 "vchdr.h"
21
22
/* Construct/deconstruct data usable for persistent storage of a handle.
23
**
24
** Written by Kiem-Phong Vo.
25
*/
26
27
#define N_CODERS 1024 /* max #coders */
28
29
typedef struct _store_s
30
{ Vcmethod_t* meth; /* method */
31
Vcchar_t* data; /* associated data */
32
ssize_t dtsz; /* length of data */
33
} Store_t;
34
35
#if __STD_C
36
ssize_t vcextract(Vcodex_t* vc, Void_t** datap)
37
#else
38
ssize_t vcextract(vc, datap)
39
Vcodex_t* vc;
40
Void_t** datap;
41
#endif
42
{
43
ssize_t n, k, dtsz;
44
Store_t store[N_CODERS];
45
Vcchar_t *data;
46
Vcodex_t *coder;
47
Vcmtcode_t mtcd;
48
int rv;
49
char *ident, buf[1024];
50
Vcio_t io;
51
52
if(!(vc->flags&VC_ENCODE) )
53
return -1;
54
55
dtsz = 0; /* get all the encoding strings */
56
for(n = -1, coder = vc; coder; coder = coder->coder)
57
{ if((n += 1) >= N_CODERS)
58
return -1; /* too many continuation coders */
59
60
store[n].meth = coder->meth;
61
if(!(ident = vcgetident(coder->meth, buf, sizeof(buf))) || !ident[0] )
62
return -1;
63
dtsz += strlen(ident) + 1;
64
65
store[n].dtsz = 0;
66
if(coder->meth->eventf)
67
{ mtcd.data = NIL(Vcchar_t*);
68
mtcd.size = 0;
69
rv = (*coder->meth->eventf)(coder, VC_EXTRACT, (Void_t*)(&mtcd));
70
if(rv < 0)
71
return -1;
72
else if(rv > 0)
73
{ if((store[n].dtsz = mtcd.size) < 0 )
74
return -1;
75
store[n].data = mtcd.data;
76
}
77
}
78
79
dtsz += vcsizeu(store[n].dtsz) + store[n].dtsz;
80
}
81
82
if(!(data = vcbuffer(vc, NIL(Vcchar_t*), dtsz, 0)) )
83
return -1;
84
85
/* write out data of all coders */
86
vcioinit(&io, data, dtsz);
87
for(k = 0; k <= n; ++k)
88
{ if(!(ident = vcgetident(store[k].meth, buf, sizeof(buf))) )
89
return -1;
90
vcioputs(&io, ident, strlen(ident)+1);
91
vcioputu(&io, store[k].dtsz);
92
vcioputs(&io, store[k].data, store[k].dtsz);
93
}
94
95
if(datap)
96
*datap = (Void_t*)data;
97
return dtsz;
98
}
99
100
101
#if __STD_C
102
Vcodex_t* vcrestore(Void_t* data, size_t dtsz)
103
#else
104
Vcodex_t* vcrestore(data, dtsz)
105
Void_t* data;
106
size_t dtsz;
107
#endif
108
{
109
Vcodex_t *vc, *coder, *cdr;
110
Vcmethod_t *meth;
111
ssize_t sz, k;
112
char *mt;
113
Void_t *dt;
114
Vcmtcode_t mtcd;
115
int rv;
116
Vcio_t io;
117
118
if(!data || dtsz <= 0 )
119
return NIL(Vcodex_t*);
120
121
vcioinit(&io, data, dtsz);
122
vc = coder = NIL(Vcodex_t*);
123
124
while(vciomore(&io) > 0)
125
{
126
mt = (char*)vcionext(&io);
127
for(sz = vciomore(&io), k = 0; k < sz; ++k)
128
if(mt[k] == 0)
129
break;
130
if(k >= sz)
131
goto error;
132
if(!(meth = vcgetmeth(mt, 1)) )
133
goto error;
134
vcioskip(&io, k+1);
135
136
/* get the initialization data, if any */
137
if((sz = (ssize_t)vciogetu(&io)) < 0 || sz > vciomore(&io))
138
goto error;
139
dt = vcionext(&io);
140
vcioskip(&io, sz);
141
142
cdr = NIL(Vcodex_t*);
143
if(meth->eventf)
144
{ mtcd.data = dt;
145
mtcd.size = sz;
146
mtcd.coder = NIL(Vcodex_t*);
147
rv = (*meth->eventf)(0, VC_RESTORE, (Void_t*)(&mtcd));
148
if(rv < 0 )
149
goto error;
150
else if(rv > 0)
151
cdr = mtcd.coder;
152
}
153
154
if(!cdr && !(cdr = vcopen(0, meth, 0, 0, VC_DECODE)) )
155
{ error:
156
if(vc)
157
vcclose(vc);
158
return NIL(Vcodex_t*);
159
}
160
161
if(!coder)
162
vc = cdr;
163
else
164
{ coder->coder = cdr;
165
coder->flags |= VC_CLOSECODER;
166
}
167
coder = cdr;
168
}
169
170
return vc;
171
}
172
173