-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream_decode.c
195 lines (151 loc) · 4.1 KB
/
stream_decode.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
186
187
188
189
190
191
192
193
194
195
/*
$NiH: stream_decode.c,v 1.5 2002/04/16 22:46:12 wiz Exp $
stream_decode.c -- low-level decoding
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 "stream.h"
#include "stream_types.h"
#include "util.h"
enum decode_state { DS_OK, DS_ILL, DS_EOF };
struct stream_decode {
stream st;
int *table; /* decoding table to use */
enum decode_state state; /* state we're in */
char *buf; /* buffer for decoded data */
int buf_alen; /* allocation length of buf */
int rest; /* partially decoded quadruple */
int no; /* number of characters in rest */
};
static int dec_close(struct stream_decode *st);
static int dec_fill(struct stream_decode *this, int i, int no, int rest);
static token *dec_get(struct stream_decode *st);
stream *
stream_decode_open(struct stream *source, int *table)
{
struct stream_decode *this;
this = (struct stream_decode *)stream_new(sizeof(struct stream_decode),
dec_get, dec_close, source);
this->table = table;
this->rest = this->no = 0;
this->buf = NULL;
this->buf_alen = 0;
this->state = DS_OK;
return (stream *)this;
}
static int
dec_close(struct stream_decode *this)
{
/* XXX: skip to EOF? */
stream_free((stream *)this);
return 0;
}
static token *
dec_get(struct stream_decode *this)
{
token *t;
int rest, no, b, i;
switch (this->state) {
case DS_ILL:
/* XXX: specify exact error */
this->state = DS_EOF;
return token_printf3(&this->st.tok, TOK_ERR, TOK_ERR_ERROR,
"illegal character in encoded data");
case DS_EOF:
return TOKEN_EOF;
case DS_OK:
t = stream_get(this->st.source);
rest = this->rest;
no = this->no;
i = 0;
if (t->type != TOK_LINE) {
if (no) {
i = dec_fill(this, 0, no, rest);
token_set3(stream_enqueue((stream *)this),
TOK_DATA, i, this->buf);
this->rest = this->no = 0;
}
return t;
}
while (*t->line) {
b = this->table[(unsigned char)*(t->line++)];
if (b < 0) {
switch (b) {
case DEC_END:
if (this->state != DS_OK)
this->state = DS_ILL;
else if (no == 0)
this->state = DS_EOF;
else
this->state = DS_ILL;
break;
case DEC_PAD:
if (this->state == DS_OK) {
switch (no) {
case 0:
case 1:
this->state = DS_ILL;
break;
case 2:
case 3:
this->state = DS_EOF;
break;
}
}
break;
default:
/* XXX: recover, don't abort decoding */
no = 0;
this->state = DS_ILL;
}
}
else {
rest = (rest << 6) | (b & 0x3f);
no++;
}
if (no == 4 || this->state != DS_OK) {
i += dec_fill(this, i, no, rest);
rest = no = 0;
}
}
}
this->rest = rest;
this->no = no;
return token_set3(&this->st.tok, TOK_DATA, i, this->buf);
}
static int
dec_fill(struct stream_decode *this, int i, int no, int rest)
{
if (i+2 >= this->buf_alen) {
this->buf_alen = (this->buf_alen ? this->buf_alen*2 : 64);
this->buf = xrealloc(this->buf, this->buf_alen);
}
switch (no) {
case 2:
this->buf[i++] = rest >> 4;
break;
case 3:
this->buf[i++] = rest >> 10;
this->buf[i++] = (rest>>2) & 0xff;
break;
case 4:
this->buf[i++] = rest >> 16;
this->buf[i++] = (rest>>8) & 0xff;
this->buf[i++] = (rest & 0xff);
break;
}
return no-1;
}