Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libpz/sfdczip.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1998-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
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
22
/*
23
* sfio { lzw gzip pzip bzip } discipline wrapper
24
*/
25
26
#include "pzlib.h"
27
28
#include <sfdcbzip.h>
29
30
#define METH_bzip 'b'
31
#define METH_gzip 'g'
32
#define METH_lzw 'l'
33
#define METH_pzip 'p'
34
#define METH_qzip 'q'
35
36
/*
37
* push the sfio discipline named by meth:
38
*
39
* bzip
40
* gzip [--[no]crc]
41
* lzw
42
* pzip [--[no]crc] [--] [ partition ]
43
* qzip [--] [ record-size ]
44
*
45
* return:
46
* >0 discipline pushed
47
* 0 discipline not needed
48
* <0 error
49
*/
50
51
int
52
sfdczip(Sfio_t* sp, const char* path, register const char* meth, Error_f errorf)
53
{
54
const char* part;
55
const char* mesg;
56
int r;
57
int zip;
58
int len;
59
unsigned long flags;
60
Pzdisc_t disc;
61
62
if (meth)
63
{
64
if (part = (const char*)strchr(meth, ' '))
65
len = part - meth;
66
else
67
len = strlen(meth);
68
zip = 0;
69
switch ((len<<8)|meth[0])
70
{
71
case (4<<8)|'b':
72
if (strneq(meth, "bzip", len))
73
zip = METH_bzip;
74
break;
75
case (4<<8)|'g':
76
if (strneq(meth, "gzip", len))
77
zip = METH_gzip;
78
break;
79
case (3<<8)|'l':
80
if (strneq(meth, "lzw", len))
81
zip = METH_lzw;
82
break;
83
case (4<<8)|'p':
84
if (strneq(meth, "pzip", len))
85
zip = METH_pzip;
86
break;
87
#if 0
88
case (4<<8)|'q':
89
if (strneq(meth, "qzip", len))
90
zip = METH_qzip;
91
break;
92
#endif
93
}
94
}
95
else
96
{
97
/*
98
* defer to sfdcpzip() for SF_READ recognition
99
*/
100
101
zip = METH_pzip;
102
part = 0;
103
}
104
if (!zip)
105
{
106
mesg = ERROR_dictionary("unknown compress discipline method");
107
r = -1;
108
}
109
else
110
{
111
mesg = ERROR_dictionary("compress discipline error");
112
flags = 0;
113
if (part)
114
for (;;)
115
{
116
while (part[0] == ' ')
117
part++;
118
if (part[0] != '-' || part[1] != '-')
119
{
120
if (!part[0])
121
part = 0;
122
break;
123
}
124
part += 2;
125
if (part[0] == ' ')
126
{
127
while (part[0] == ' ')
128
part++;
129
if (!part[0])
130
part = 0;
131
break;
132
}
133
if (!part[0])
134
{
135
part = 0;
136
break;
137
}
138
if (part[0] == 'n' && part[1] == 'o')
139
{
140
part += 2;
141
r = 0;
142
}
143
else
144
r = 1;
145
if (!(meth = (const char*)strchr(part, ' ')))
146
meth = part + strlen(part);
147
switch (meth - part)
148
{
149
case 3:
150
if (strneq(part, "crc", 3))
151
switch (zip)
152
{
153
case METH_gzip:
154
if (!r)
155
flags |= SFGZ_NOCRC;
156
break;
157
case METH_pzip:
158
if (r)
159
flags |= PZ_CRC;
160
break;
161
}
162
break;
163
}
164
part = meth;
165
}
166
if (!path)
167
{
168
if (sp == sfstdin)
169
path = "/dev/stdin";
170
else if (sp == sfstdout)
171
path = "/dev/stdout";
172
else if (sfset(sp, 0, 0) & SF_READ)
173
path = "input";
174
else
175
path = "output";
176
}
177
switch (zip)
178
{
179
case METH_bzip:
180
r = sfdcbzip(sp, flags);
181
break;
182
case METH_gzip:
183
r = sfdcgzip(sp, flags);
184
break;
185
case METH_lzw:
186
r = sfdclzw(sp, flags);
187
break;
188
case METH_pzip:
189
memset(&disc, 0, sizeof(disc));
190
if ((sfset(sp, 0, 0) & SF_WRITE) && !(disc.partition = part))
191
{
192
mesg = ERROR_dictionary("partition file operand required");
193
r = -1;
194
}
195
else
196
{
197
disc.version = PZ_VERSION;
198
disc.errorf = errorf;
199
r = sfdcpzip(sp, path, flags, &disc);
200
}
201
break;
202
}
203
if (r > 0)
204
sfset(sp, SF_SHARE, 0);
205
}
206
if (r < 0 && errorf)
207
(*errorf)(NiL, NiL, 2, "%s: %s: %s", path, meth, mesg);
208
return r;
209
}
210
211