-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream_uuextract.c
185 lines (144 loc) · 4.21 KB
/
stream_uuextract.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
$NiH: stream_uuextract.c,v 1.8 2002/05/10 21:09:22 wiz Exp $
stream_uuextract.c -- extrace uuencode data
Copyright (C) 2002 Dieter Baron and Thomas Klausner
This file is part of cg, a program to assemble and decode binary Usenet
postings. The authors can be contacted at <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stddef.h>
#include <string.h>
#include "header.h"
#include "stream.h"
#include "stream_types.h"
#include "util.h"
enum uu_state { UU_FULL, UU_SHORT, UU_EMPTY, UU_END, UU_POST, UU_PRE,
UU_HEADER };
struct stream_uux {
stream st;
enum uu_state state;
char buf[66];
};
static int uux_close(struct stream_uux *st);
static token *uux_get(struct stream_uux *st);
stream *
stream_uuextract_open(struct stream *source)
{
struct stream_uux *this;
this = (struct stream_uux *)stream_new(sizeof(struct stream_uux),
uux_get, uux_close, source);
this->state = UU_FULL;
return (stream *)this;
}
static int
uux_close(struct stream_uux *this)
{
/* XXX: skip to EOF? */
stream_free((stream *)this);
return 0;
}
static token *
uux_get(struct stream_uux *this)
{
token *t;
int len, line_kept, nbytes;
enum uu_state state, old_state;
old_state = this->state;
line_kept = 0;
for (;;) {
t = stream_get(this->st.source);
switch (t->type) {
case TOK_LINE:
len = strlen(t->line);
if (old_state == UU_HEADER)
state = UU_HEADER;
else if (t->line[0] == 'M' && len == 61)
state = UU_FULL;
else if (len == 1 && (t->line[0] == ' ' || t->line[0] == '`'))
state = UU_EMPTY;
else if (strcmp(t->line, "end") == 0)
state = UU_END;
else if (t->line[0] > ' ' && t->line[0] < 'M'
&& (len-1+3)/4 == (t->line[0]-' '+2)/3)
state = UU_SHORT;
else
state = UU_POST;
switch (state) {
case UU_HEADER:
break;
case UU_END:
if (line_kept)
token_set(stream_enqueue((stream *)this),
TOK_LINE, this->buf);
return TOKEN_EOF;
case UU_EMPTY:
if (old_state == UU_EMPTY) {
token_set3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_WARNING,
"end expected after empty line");
}
break;
case UU_FULL:
if (old_state != UU_FULL && old_state != UU_PRE) {
/* error: end or empty expected */
token_set3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "long line unexpected");
}
this->state = UU_FULL;
return token_set(&this->st.tok, TOK_LINE, t->line+1);
case UU_SHORT:
if (old_state != UU_FULL && old_state != UU_PRE) {
token_set3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "unexpected short line");
}
strcpy(this->buf, t->line+1);
/* some encoders output complete quadruples; truncate them */
nbytes = t->line[0] - ' ';
this->buf[nbytes/3*4 + nbytes%3+1] = '\0';
line_kept = 1;
break;
case UU_POST:
if (old_state == UU_PRE)
state = old_state;
break;
default:
break;
}
break;
case TOK_EOA:
state = UU_HEADER;
break;
case TOK_EOF:
if (line_kept)
token_set(stream_enqueue((stream *)this), TOK_LINE, this->buf);
token_set3(stream_enqueue((stream *)this), TOK_ERR, TOK_ERR_ERROR,
"missing end in uu stream");
return TOKEN_EOF;
case TOK_EOH:
state = UU_PRE;
break;
default:
return t;
#if 0
token_set3(stream_enqueue((stream *)this), TOK_ERR, TOK_ERR_ERROR,
"unexpected token type");
if (old_state == UU_PRE || old_state == UU_HEADER)
state = old_state;
else
state = UU_POST;
break;
#endif
}
old_state = state;
}
}