Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libvcodex/Vcdelta/vcdtable.c
1810 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 "vcdhdr.h"
21
22
/* Encoding and decoding a code table based on differences to another one.
23
** This allows efficient embedding of a code table in the compressed data.
24
**
25
** Written by Kiem-Phong Vo ([email protected])
26
*/
27
28
#if __STD_C
29
static void vcdtbl2str(Vcdtable_t* tbl, Void_t* argstr)
30
#else
31
static void vcdtbl2str(tbl, argstr)
32
Vcdtable_t* tbl;
33
Void_t* argstr;
34
#endif
35
{
36
int i;
37
Vcchar_t* str = (Vcchar_t*)argstr;
38
Vcdcode_t* code = tbl->code;
39
40
for(i = 0; i < 256; ++i)
41
*str++ = code[i].inst1.type;
42
for(i = 0; i < 256; ++i)
43
*str++ = code[i].inst2.type;
44
for(i = 0; i < 256; ++i)
45
*str++ = code[i].inst1.size;
46
for(i = 0; i < 256; ++i)
47
*str++ = code[i].inst2.size;
48
for(i = 0; i < 256; ++i)
49
*str++ = code[i].inst1.mode;
50
for(i = 0; i < 256; ++i)
51
*str++ = code[i].inst2.mode;
52
}
53
54
#if __STD_C
55
static void vcdstr2tbl(Vcdtable_t* tbl, Void_t* argstr)
56
#else
57
static void vcdstr2tbl(tbl, argstr)
58
Vcdtable_t* tbl;
59
Void_t* argstr;
60
#endif
61
{
62
int i;
63
Vcchar_t* str = (Vcchar_t*)argstr;
64
Vcdcode_t* code = tbl->code;
65
66
for(i = 0; i < 256; ++i)
67
code[i].inst1.type = *str++;
68
for(i = 0; i < 256; ++i)
69
code[i].inst2.type = *str++;
70
for(i = 0; i < 256; ++i)
71
code[i].inst1.size = *str++;
72
for(i = 0; i < 256; ++i)
73
code[i].inst2.size = *str++;
74
for(i = 0; i < 256; ++i)
75
code[i].inst1.mode = *str++;
76
for(i = 0; i < 256; ++i)
77
code[i].inst2.mode = *str++;
78
}
79
80
#if __STD_C
81
ssize_t vcdputtable(Vcdtable_t* tbl, Void_t* buf, size_t n)
82
#else
83
ssize_t vcdputtable(tbl, buf, n)
84
Vcdtable_t* tbl;
85
Void_t* buf;
86
size_t n;
87
#endif
88
{
89
Vcodex_t* vc;
90
ssize_t rv;
91
Vcdisc_t disc;
92
Vcchar_t srcbuf[VCD_TBLSIZE], tarbuf[VCD_TBLSIZE], *del;
93
Vcio_t io;
94
95
_vcdtblinit();
96
97
/* convert the standard table to a string */
98
vcdtbl2str(_Vcdtbl, srcbuf);
99
disc.data = srcbuf;
100
disc.size = sizeof(srcbuf);
101
disc.eventf = NIL(Vcevent_f);
102
if(!(vc = vcopen(&disc, Vcdelta, NIL(Void_t*), NIL(Vcodex_t*), VC_ENCODE)) )
103
return -1;
104
105
/* convert the new table to a string, then encode it against _Vcdtbl */
106
vcdtbl2str(tbl, tarbuf);
107
vcioinit(&io, buf, n);
108
if((rv = vcapply(vc, tarbuf, sizeof(tarbuf), &del)) > 0)
109
{ if(rv+2 > n)
110
rv = -1;
111
else
112
{ vcioputc(&io, tbl->s_near);
113
vcioputc(&io, tbl->s_same);
114
vcioputs(&io, del, rv);
115
rv += 2;
116
}
117
}
118
119
vcclose(vc);
120
121
return rv;
122
}
123
124
125
#if __STD_C
126
int vcdgettable(Vcdtable_t* tbl, Void_t* buf, size_t n)
127
#else
128
int vcdgettable(tbl, buf, n)
129
Vcdtable_t* tbl;
130
Void_t* buf;
131
size_t n;
132
#endif
133
{
134
Vcodex_t* vc;
135
ssize_t rv;
136
Vcdisc_t disc;
137
Vcchar_t srcbuf[VCD_TBLSIZE], *tarbuf;
138
Vcio_t io;
139
140
_vcdtblinit();
141
142
if(n <= 2)
143
return -1;
144
145
vcdtbl2str(_Vcdtbl, srcbuf);
146
disc.data = srcbuf;
147
disc.size = sizeof(srcbuf);
148
disc.eventf = NIL(Vcevent_f);
149
if(!(vc = vcopen(&disc, Vcdelta, NIL(Void_t*), NIL(Vcodex_t*), VC_DECODE)) )
150
return -1;
151
152
vcioinit(&io, buf, n);
153
tbl->s_near = vciogetc(&io);
154
tbl->s_same = vciogetc(&io);
155
if((rv = vcapply(vc, vcionext(&io), vciomore(&io), &tarbuf)) == VCD_TBLSIZE)
156
vcdstr2tbl(tbl, tarbuf);
157
else rv = -1;
158
159
vcclose(vc);
160
161
return rv < 0 ? -1 : 0;
162
}
163
164