-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream_yenc.c
478 lines (378 loc) · 10.1 KB
/
stream_yenc.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
$NiH: stream_yenc.c,v 1.7 2002/04/16 22:46:15 wiz Exp $
stream_yenc.c -- extract and decode yenc 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 <ctype.h>
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include "crc.h"
#include "header.h"
#include "stream.h"
#include "stream_types.h"
#include "util.h"
#define YENC_BEGIN (_yenc_sym[0])
#define YENC_CRC32 (_yenc_sym[1])
#define YENC_END (_yenc_sym[2])
#define YENC_LINE (_yenc_sym[3])
#define YENC_NAME (_yenc_sym[4])
#define YENC_PART (_yenc_sym[5])
#define YENC_PCRC32 (_yenc_sym[6])
#define YENC_SIZE (_yenc_sym[7])
#define YENC_MAX 8
static const char *_yenc_str[YENC_MAX] = {
"begin",
"crc32",
"end",
"line",
"name",
"part",
"pcrc32",
"size",
};
static symbol _yenc_sym[YENC_MAX];
static int _yenc_inited = 0;
enum yenc_state {
Y_HEADER, /* reading header */
Y_PRE, /* body lines before =ybegin */
Y_BEGIN, /* just after =ybegin line */
Y_DATA, /* in data part */
Y_POST /* after =yend line */
};
struct stream_yenc {
stream st;
enum yenc_state state; /* state stream is in */
int part; /* current next part */
long size; /* file size, from header */
long pbegin, pend; /* beginning/ending position of this part */
long pos; /* position in file (amount decoded) */
unsigned long crc, pcrc; /* computed crc and part crc */
char *name; /* file name */
};
static int yenc_close(struct stream_yenc *st);
static token *yenc_get(struct stream_yenc *st);
static unsigned long _yenc_get_hex(char *s);
static int _yenc_get_int(char *s);
static unsigned long _yenc_get_long(char *s);
static void _yenc_handle_begin(struct stream_yenc *this, char *ybegin,
int firstp);
static void _yenc_handle_end(struct stream_yenc *this, char *line);
static void _yenc_handle_part(struct stream_yenc *this, char *line);
static void _yenc_init(void);
static symbol _yenc_parse(char **linep);
stream *
stream_yenc_open(struct stream *source, char *ybegin)
{
struct stream_yenc *this;
this = (struct stream_yenc *)stream_new(sizeof(struct stream_yenc),
yenc_get, yenc_close, source);
this->state = Y_BEGIN;
if (!_yenc_inited)
_yenc_init();
_yenc_handle_begin(this, ybegin, 1);
return (stream *)this;
}
static int
yenc_close(struct stream_yenc *this)
{
/* XXX: skip to EOF? */
stream_free((stream *)this);
return 0;
}
static token *
yenc_get(struct stream_yenc *this)
{
token *t;
enum yenc_state state, old_state;
int i;
char *p;
old_state = this->state;
for (;;) {
t = stream_get(this->st.source);
switch (t->type) {
case TOK_LINE:
if (old_state == Y_HEADER)
break;
else if (strncmp(t->line, "=ybegin ", 8) == 0) {
if (old_state != Y_PRE) {
token_set3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "unexpected =ybegin line");
break;
}
_yenc_handle_begin(this, t->line, 0);
state = Y_BEGIN;
}
else if (strncmp(t->line, "=ypart ", 7) == 0) {
if (old_state != Y_BEGIN) {
token_set3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "unexpected =ypart line");
break;
}
_yenc_handle_part(this, t->line);
state = Y_DATA;
}
else if (strncmp(t->line, "=yend ", 6) == 0) {
if (old_state != Y_DATA && old_state != Y_BEGIN) {
token_set3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "unexpected =yend line");
break;
}
_yenc_handle_end(this, t->line);
state = Y_POST;
if (this->pos == this->size)
return TOKEN_EOF;
}
else {
switch (old_state) {
case Y_BEGIN:
case Y_DATA:
i=0;
p = t->line;
while (*p) {
if (*p == '=') {
if (*p++ == 0) {
token_set3(stream_enqueue((stream *)this),
TOK_ERR, TOK_ERR_ERROR,
"escape char = "
"at end of yenc line");
break;
}
*p -= 64;
}
t->line[i++] = (*p-42)&0xff;
p++;
}
this->state = Y_DATA;
this->pos += i;
this->crc = crc_update(this->crc,
(unsigned char *)t->line, i);
this->pcrc = crc_update(this->pcrc,
(unsigned char *)t->line, i);
return token_set3(&this->st.tok, TOK_DATA, i, t->line);
default:
break;
}
break;
}
break;
case TOK_EOA:
if (old_state != Y_POST)
token_set3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "missing =yend in yenc stream");
state = Y_HEADER;
break;
case TOK_EOF:
token_set3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "missing =yend in yenc stream");
return TOKEN_EOF;
case TOK_EOH:
state = Y_PRE;
break;
default:
return t;
}
old_state = state;
}
}
static unsigned long
_yenc_get_hex(char *s)
{
unsigned long l;
char *p;
errno = 0;
l = strtoul(s, &p, 16);
if (errno == ERANGE || (*p & !isspace(*p))) {
/* XXX: error handling */
}
return l;
}
static int
_yenc_get_int(char *s)
{
int i;
char *p;
errno = 0;
i = strtol(s, &p, 10);
if (errno == ERANGE || (*p & !isspace(*p))) {
/* XXX: error handling */
}
return i;
}
static unsigned long
_yenc_get_long(char *s)
{
unsigned long l;
char *p;
errno = 0;
l = strtol(s, &p, 10);
if (errno == ERANGE || (*p & !isspace(*p))) {
/* XXX: error handling */
}
return l;
}
static void
_yenc_handle_begin(struct stream_yenc *this, char *ybegin, int firstp)
{
symbol s;
char *p;
unsigned long size;
int i;
if (firstp) {
this->part = 0;
this->crc = crc_update(0, NULL, 0);
this->size = -1;
this->pos = 0;
this->name = NULL;
}
this->part++;
this->pbegin = this->pos;
this->pend = -1;
this->pcrc = crc_update(0, NULL, 0);
do {
ybegin = strchr(ybegin, ' ');
s = _yenc_parse(&ybegin);
if (s == YENC_PART) {
if ((i=_yenc_get_int(ybegin)) != this->part) {
token_printf3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "unexpected yEnc part, "
"expected %d, got %d", this->part, i);
}
}
else if (s == YENC_SIZE) {
size = _yenc_get_long(ybegin);
if (firstp)
this->size = size;
else if (this->size != size) {
token_printf3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "yEnc: size mismatch between "
"parts, expected %ld, got %ld", this->size,
size);
}
}
else if (s == YENC_NAME) {
ybegin+=strspn(ybegin, "\" \t");
p = ybegin+strlen(ybegin)-1;
while (strchr("\" \t", *p))
--p;
p[1] = '\0';
if (firstp) {
this->name = xstrdup(ybegin);
token_set(stream_enqueue((stream *)this), TOK_FNAME, ybegin);
}
else {
if (strcmp(this->name, ybegin) != 0) {
token_set3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR,
"yEnc: file name mismatch between parts");
}
}
break;
}
} while (s);
}
static void
_yenc_handle_end(struct stream_yenc *this, char *line)
{
symbol s;
unsigned long size, partcrc;
int i;
do {
line = strchr(line, ' ');
s = _yenc_parse(&line);
if (s == YENC_PART) {
if ((i=_yenc_get_int(line)) != this->part) {
token_printf3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "unexpected yEnc part, "
"expected %d, got %d", this->part, i);
}
}
else if (s == YENC_SIZE) {
size = _yenc_get_long(line);
if (this->pbegin+size != this->pos) {
token_printf3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "yEnc: part size wrong, "
"expected %ld, got %ld", this->pos-this->pbegin,
size);
}
}
else if (s == YENC_PCRC32) {
partcrc = _yenc_get_hex(line);
if (this->pcrc != partcrc) {
token_printf3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "yEnc: part crc wrong, "
"expected %08lx, got %08lx", this->pcrc,
partcrc);
}
}
else if (s == YENC_CRC32) {
partcrc=_yenc_get_hex(line);
if (this->crc != partcrc) {
token_printf3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "yEnc: file crc wrong, "
"expected %08lx, got %08lx", this->crc,
partcrc);
}
}
} while (s);
if (this->pend != -1 && this->pos != this->pend) {
token_printf3(stream_enqueue((stream *)this), TOK_ERR, TOK_ERR_ERROR,
"yEnc: part end wrong, expected %ld, got %ld",
this->pend, this->pos);
}
}
static void
_yenc_handle_part(struct stream_yenc *this, char *line)
{
symbol s;
long size;
do {
line = strchr(line, ' ');
s = _yenc_parse(&line);
if (s == YENC_BEGIN) {
size = _yenc_get_int(line)-1;
if (this->pbegin != size) {
token_printf3(stream_enqueue((stream *)this), TOK_ERR,
TOK_ERR_ERROR, "yEnc part begin wrong, "
"expected %ld, got %ld", this->pbegin, size);
}
}
else if (s == YENC_END)
this->pend = _yenc_get_long(line);
} while (s);
}
static void
_yenc_init(void)
{
int i;
for (i=0; i<YENC_MAX; i++)
_yenc_sym[i] = intern((char *)_yenc_str[i]);
_yenc_inited = 1;
}
static symbol
_yenc_parse(char **linep)
{
char *p, *q;
q = *linep;
if (q == NULL)
return NULL;
q += strspn(q, " \t");
if ((p=strchr(q, '=')) == NULL)
return NULL;
*p = '\0';
*linep = p+1;
return intern(q);
}