File: zio.c

package info (click to toggle)
xli 1.17.0+20061110-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,540 kB
  • sloc: ansic: 25,840; makefile: 11
file content (850 lines) | stat: -rw-r--r-- 20,956 bytes parent folder | download
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/* zio.c:
 *
 * this properly opens and reads from an image file, compressed or otherwise.
 *
 * jim frost 10.03.89
 *
 * this was hacked on 09.12.90 to cache reads and to use stdin.
 *
 * Copyright 1989, 1990 Jim Frost.  See included file "copyright.h" for
 * complete copyright information.
 *
 * Re-worked by Graeme W. Gill on 91/12/04 to add zrewind, zunread and
 * better performance of per character reads.
 * 
 * The uu decoder is loosely based on uuexplode-1.2.c
 * by Michael Bergman (ebcrmb@ebc.ericsson.se),
 * which is based on kiss 1.0 by Kevin Yang.
 *
 */

#include "copyright.h"
#include "xli.h"
#include <ctype.h>
#include <string.h>

#ifdef VMS
#define NO_UNCOMPRESS		/* VMS doesn't have uncompress */
#endif

/* ANSI C doesn't declare popen in stdio.h ! */
FILE *popen(const char *, const char *);

#define MAX_ZFILES 32
#define UUSTARTLEN 100		/* Lines to look though before assuming not uuencoded */

static int uuread(ZFILE *zf, byte *buf, int len);
static int uugetline(ZFILE *zf, byte *obuf);
static boolean uuCheck(byte *icp, int len);
static int uuExplodeLine(byte *icp, byte *ocp);
static int buflen(byte *buf, int len);

static ZFILE ZFileTable[MAX_ZFILES];
static boolean ZForceCache = FALSE;

int zread(ZFILE *zf, byte *buf, int len)
{
	int lentoread = len;
	struct cache *dataptr = zf->dataptr;

	while (len > 0 && !zf->eof) {
		/* Read any data in cache buffers or aux buffer */
		if (zf->bufptr < zf->endptr) {
			int readlen = zf->endptr - zf->bufptr;
			if (readlen > len)
				readlen = len;
			bcopy(zf->bufptr, buf, readlen);
			buf += readlen;
			len -= readlen;
			zf->bufptr += readlen;
			continue;
		}
		/* else we have run out of buffered data */

		/* If we were reading from an aux buffer, */
		/* retore the previous state */
		if (zf->auxb != NULL) {		/* restore previous buffer */
			if (zf->auxb != zf->buf)	/* if not getc buffer */
				lfree(zf->auxb);	/* was un-read buffer */
			zf->auxb = NULL;
			zf->bufptr = zf->oldbufptr;
			zf->endptr = zf->oldendptr;
			continue;
		}
		/* If this is a newly opened file or we are */
		/* no longer reading previously cached data */
		if (dataptr == NULL) {
			if (zf->nocache) {	/* If the cache is turned off, read directly */
				if (len > 1) {	/* more than a byte being read */
					len -= uuread(zf, buf, len);
					if (len > 0)
						zf->eof = TRUE;
					continue;
				}
				/* else we are reading one byte directly - this is a little */
				/* inefficient :-). Buffer it instead. */
				zf->bufptr = zf->buf;
				zf->endptr = zf->bufptr + uuread(zf, zf->bufptr, BUFSIZ);
				if (zf->endptr == zf->bufptr)
					zf->eof = TRUE;
				continue;
			}
			/* else fall through to deal with first cached buffered read */
		} else {	/* we must move on to the next cache buffer */
			if (zf->bufptr == NULL) {	/* If this is a first re-read of the cache */
				zf->bufptr = dataptr->buf;	/* init the pointers */
				zf->endptr = dataptr->end;
				continue;
			}
			/* If there are more buffers, then advance */
			/* to the next one */
			if (dataptr->next != NULL) {
				/* advance to the next buffer */
				dataptr = zf->dataptr = dataptr->next;
				zf->bufptr = dataptr->buf;
				zf->endptr = dataptr->end;
				continue;
			}
			/* There are no more buffers */
			if (dataptr->eof) {
				zf->eof = TRUE;		/* we know that was the last one */
				continue;
			}
		}

		/* We have come to the end of the cache buffers or */
		/* we are doing the first cached read. */

		/* If the cache is off, switch it fully */
		/* out of the picture from now on */
		if (zf->nocache) {	/* read directly */
			zf->dataptr = dataptr = NULL;
			zf->bufptr = NULL;
			zf->endptr = NULL;
			continue;
		}
		/* We are caching and have to read another buffer or maybe we have */
		/* to read the very first buffer (zf->data and dataptr == NULL) */
		zf->dataptr = (struct cache *) lmalloc(sizeof(struct cache));
		zf->dataptr->next = NULL;
		zf->dataptr->eof = FALSE;
		if (zf->data == NULL)	/* If very first read */
			zf->data = zf->dataptr;		/* set to first allocated buffer */
		else		/* link buffer into chain */
			dataptr->next = zf->dataptr;
		dataptr = zf->dataptr;
		zf->bufptr = dataptr->buf;
		zf->endptr = dataptr->end = zf->bufptr + uuread(zf, zf->bufptr, BUFSIZ);
		dataptr->eof = ((zf->endptr - zf->bufptr) < BUFSIZ);
	}
	return (lentoread - len);
}


/* append the given buffer data to the files read cache */
/* (Allows reading blocks directly from input stream while */
/*  maintaining cache data) */
static void _zaptocache(ZFILE *zf, char *cbuf, int blen)
{
	struct cache *newdat, *dat;
	int len;
	while (blen > 0) {	/* until we've stashed all of input */
		len = blen;
		if (len > BUFSIZ)
			len = BUFSIZ;

		/* create new cache data block */
		newdat = (struct cache *) lmalloc(sizeof(struct cache));
		bcopy(cbuf, newdat->buf, len);
		newdat->end = newdat->buf + len;
		newdat->next = NULL;
		newdat->eof = FALSE;

		/* append it to cache buffers */
		if (zf->data == NULL) {		/* nothing read yet */
			zf->data = newdat;
			zf->dataptr = zf->data;
		} else {
			for (dat = zf->data; dat->next != NULL; dat = dat->next);	/* go to end of chain */
			dat->next = newdat;
			dat->eof = FALSE;
		}
		cbuf += len;
		blen -= len;
	}
}

/* Return the files EOF status */

int zeof(ZFILE *zf)
{
	return zf->eof;
}

void zclearerr(ZFILE *zf)
{
	clearerr((zf)->stream);
	zf->eof = 0;
}


/* reset a read cache (reset means really close the file)
 */

void zreset(char *filename)
{
	int a;
	ZFILE *zf;

	if (TRUE == ZForceCache)
		return;

	/* if NULL filename, reset the entire table
	 */
	if (!filename) {
		for (a = 0; a < MAX_ZFILES; a++)
			if (ZFileTable[a].filename)
				zreset(ZFileTable[a].filename);
		return;
	}
	for (zf = ZFileTable; zf < (ZFileTable + MAX_ZFILES); zf++)
		if (zf->filename && !strcmp(filename, zf->filename))
			break;

	if (zf == (ZFileTable + MAX_ZFILES))	/* no go joe */
		return;

	_zreset(zf);
}

/* reset by file descriptor */
void _zreset(ZFILE *zf)
{
	struct cache *old;
	if (zf->dataptr != zf->data)
		fprintf(stderr, "zreset: warning: ZFILE for %s was not closed properly\n",
			zf->filename);
	while (zf->data) {
		old = zf->data;
		zf->data = zf->data->next;
		lfree((byte *) old);
	}
	lfree((byte *) zf->filename);
	zf->filename = NULL;
	zf->data = NULL;
	zf->dataptr = NULL;
	zf->nocache = FALSE;
	if (zf->auxb != NULL && zf->auxb != zf->buf)
		lfree((byte *) zf->auxb);
	zf->auxb = NULL;
	zf->bufptr = NULL;
	zf->endptr = NULL;
	zf->eof = FALSE;

	switch (zf->type) {
	case ZSTANDARD:
		fclose(zf->stream);
		break;
#ifndef NO_UNCOMPRESS
	case ZPIPE:
		pclose(zf->stream);
		break;
#endif				/* NO_UNCOMPRESS */
	case ZSTDIN:
		break;
	default:
		fprintf(stderr, "zreset: bad ZFILE structure\n");
		exit(1);
	}
}

/* discard all data that has been read. Return to state just after file was opened */
void _zclear(ZFILE *zf)
{
	struct cache *old;

	if (zf->auxb != NULL && zf->auxb != zf->buf)
		lfree(zf->auxb);

	while (zf->data) {
		old = zf->data;
		zf->data = zf->data->next;
		free(old);
	}
	zf->auxb = NULL;
	zf->bufptr = NULL;
	zf->endptr = NULL;
	zf->eof = FALSE;
	zf->data = NULL;
	zf->dataptr = NULL;
	zf->nocache = FALSE;
}

ZFILE *zopen(char *name)
{
	ZFILE *zf;

	/* look for filename in open file table
	 */

	for (zf = ZFileTable; zf < (ZFileTable + MAX_ZFILES); zf++)
		if (zf->filename && !strcmp(name, zf->filename)) {

			/* if we try to reopen a file whose caching was
			 * disabled, warn the user and try to recover.
			 * we cannot recover if it was stdin.
			 */

			if (zf->nocache) {
				if (zf->type == ZSTDIN) {
					fprintf(stderr, "zopen: caching was disabled by previous caller; can't reopen stdin\n");
					return (NULL);
				}
				fprintf(stderr, "zopen: warning: caching was disabled by previous caller\n");
				zreset(zf->filename);	/* remove entry and treat like new open */
				break;
			}
			if (zf->dataptr != zf->data)
				fprintf(stderr, "zopen: warning: file doubly opened\n");
			zf->dataptr = zf->data;		/* re-start with cache if it exists */
			if (zf->auxb != NULL && zf->auxb != zf->buf)
				lfree((byte *) zf->auxb);
			zf->auxb = NULL;
			zf->bufptr = NULL;
			zf->endptr = NULL;
			zf->eof = FALSE;
			return (zf);
		}
	/* find unused ZFileTable entry
	 */

	for (zf = ZFileTable; zf < (ZFileTable + MAX_ZFILES) && zf->filename; zf++)
		/* EMPTY */
		;

	if (zf >= (ZFileTable + MAX_ZFILES)) {
		fprintf(stderr, "zopen: no more files available\n");
		exit(1);
	}
	zf->filename = dupString(name);
	if (!_zopen(zf)) {	/* failed */
		lfree(zf->filename);
		zf->filename = NULL;
		return (NULL);
	}
	return (zf);
}

/* Do an open given a file pointer */
/* Return TRUE if open suceeded */
boolean
_zopen(ZFILE *zf)
{
	char *name = zf->filename;
	char uuibuf[UULEN], uudest[UULEN], uudummy[UULEN];
	int uumode, uutry = UUSTARTLEN;

	zf->data = NULL;
	zf->dataptr = NULL;
	zf->auxb = NULL;
	zf->bufptr = NULL;
	zf->endptr = NULL;
	zf->nocache = FALSE;
	zf->eof = FALSE;
	zf->uudecode = FALSE;

	/* file filename is `stdin' then use stdin
	 */

	if (!strcmp(name, "stdin")) {
		zf->type = ZSTDIN;
		zf->stream = stdin;
	} else {
		char const *cmd = 0;

#ifndef NO_UNCOMPRESS
		/* if filename ends in `.Z' then open pipe to uncompress.  if
		 * your system doesn't have uncompress you can define
		 * NO_UNCOMPRESS and it just won't check for this.
		 */

#ifdef HAVE_GUNZIP
		if (
			(strlen(name) > 3 &&
			!strcasecmp(".gz", name + strlen(name) - 3)) ||
			(strlen(name) > 2 &&
			!strcasecmp(".Z", name + strlen(name) - 2))
		) {
			cmd = "gunzip -c ";
		}
#else
		/* it's a unix compressed file, so use uncompress */
		if ((strlen(name) > (unsigned) 2) &&
				!strcmp(".Z", name + (strlen(name) - 2))) {
			cmd = "uncompress -c ";
		}
#endif /* HAVE_GUNZIP */
#ifdef HAVE_BUNZIP2
		if (strlen(name) > 4 &&
				!strcasecmp(".bz2", name + strlen(name) - 4)) {
			cmd = "bunzip2 -c ";
		}
#endif /* HAVE_BUNZIP2 */
#endif /* NO_UNCOMPRESS */

		if (cmd) {
			char *buf, *s, *t;

			/* protect in single quotes, replacing single quotes
			 * with '"'"', so worst-case expansion is 5x
	  		 */
			buf = (char *) lmalloc(
				strlen(cmd) + 1 + 5 * strlen(name) + 1 + 1);
			strcpy(buf, cmd);
			s = buf + strlen(buf);
			*s++ = '\'';
			for (t = name; *t; ++t) {
				if ('\'' == *t) {
					strcpy(s, "'\"'\"'");
					s += strlen(s);
				} else {
					*s++ = *t;
				}
			}
			*s++ = '\'';
			*s = '\0';

			zf->type = ZPIPE;
			zf->stream = popen(buf, "r");
			lfree(buf);
		} else {
			/* default to normal stream
			 */
			zf->type = ZSTANDARD;
#ifdef VMS
			zf->stream = fopen(name, "r", "ctx=bin", "ctx=stm",
				"rfm=stmlf");
#else
			zf->stream = fopen(name, "r");
#endif
		}
	}

	if (!zf->stream) {
		return (FALSE);
	}

	/* File is now open, so see if it is a uuencoded file */
	while (uutry-- > 0) {
		int blen;
		if ((blen = fread(uuibuf, 1, UULEN, zf->stream)) > 0) {
			_zaptocache(zf, uuibuf, blen);	/* keep zfile data cached */
			if (!strncmp(uuibuf, "begin ", 6)
			    && isdigit(uuibuf[6])
			    && isdigit(uuibuf[7])
			    && isdigit(uuibuf[8])) {
				/* Found a begin line */
				/* Extract filename and file mode       */
				if ((sscanf(uuibuf, "begin %o%[ ]%s", &uumode, uudummy, uudest)) != 3) {
					continue;	/* it's not right */
				}
				/* Need access to verbose flag to print the following */
				/* printf("uuencoded file '%s' being decoded\n",uudest); */
				_zclear(zf);	/* discard all data read */
				zf->uudecode = TRUE;
				zf->uunext = zf->uuend = &zf->uubuf[0];
				zf->uustate = UUBODY;
				return (TRUE);
			}
		}
	}

	/* Not uu encodede */
	return (TRUE);
}

/* The function zgetc, rather than the macro */
int _zgetc(ZFILE *zf)
{
	unsigned char c;

	if (zf->bufptr < zf->endptr)
		return *zf->bufptr++;
	if (zread(zf, &c, 1) > 0)
		return (c);
	else
		return (EOF);
}

char *zgets(char *buf, unsigned int size, ZFILE *zf)
{
	if ((--size) <= 0)
		return NULL;
	{
		int ssize = zf->endptr - zf->bufptr;
		if (ssize > 0) {	/* can do this fast */
			register byte *bp = zf->bufptr, *be;
			be = zf->bufptr + (size < ssize ? size : ssize);
			while (bp < be) {
				if (*bp++ == '\n')
					break;
			}
			if (bp < be || size < ssize) {
				bcopy(zf->bufptr, buf, bp - zf->bufptr);
				buf[bp - zf->bufptr] = '\000';
				zf->bufptr = bp;
				return ((char *) buf);
			}
		}
	}
	/* do this slower */
	{
		register char *cp = buf;
		register int c;
		while (cp < (buf + size) && (c = zgetc(zf)) != EOF) {
			*cp++ = c;
			if (c == '\n')
				break;
		}
		if (cp > buf) {	/* we read something */
			*cp = '\000';
			return ((char *) buf);
		}
	}
	return (NULL);
}

/* Return a block of data back to the input stream.
 * Usefull if a load routine does its own buffering
 * and wants to return what is left after it has read an image.
 */
void zunread(ZFILE *zf, byte const *buf, int len)
{
	byte *new;
	int sizeaxb;		/* size of aux buffer */
	int noinaxb;		/* number unread in aux buffer */
	int nofraxb;		/* number free in aux buffer */

	if (len == 0)
		return;
	if (zf->auxb != NULL) {
		sizeaxb = zf->endptr - zf->auxb;	/* size of aux buffer */
		noinaxb = zf->endptr - zf->bufptr;	/* number unread in aux buffer */
		nofraxb = zf->bufptr - zf->auxb;	/* number free in aux buffer */
	} else {
		zf->oldbufptr = zf->bufptr;
		zf->oldendptr = zf->endptr;
		sizeaxb = noinaxb = nofraxb = 0;
	}
	if (len <= nofraxb) {	/* no need to alloc more */
		zf->bufptr -= len;
		bcopy(buf, zf->bufptr, len);
	} else {		/* need some more space */
		int extra = 0;
		if (len < 100)
			extra = 100;
		new = (byte *) lmalloc(extra + noinaxb + len);
		bcopy(buf, new + extra, len);	/* copy new aux data */
		if (noinaxb != 0) {	/* copy old data */
			bcopy(zf->bufptr, new + extra + len, noinaxb);
			if (zf->auxb != NULL && zf->auxb != zf->buf)
				lfree(zf->auxb);
		}
		zf->auxb = new;
		zf->bufptr = new + extra;
		zf->endptr = new + extra + noinaxb + len;
	}
	zf->eof = FALSE;
}


/* this turns off caching when an image has been identified and we will not
 * need to re-open it
 */

void znocache(ZFILE *zf)
{
	if (FALSE == ZForceCache)
		zf->nocache = TRUE;
}

void zforcecache(boolean v)
{
	ZForceCache = v;
}

/* reset cache pointers in a ZFILE.  nothing is actually reset until a
 * zreset() is called with the filename.
 */

void zclose(ZFILE *zf)
{
	zf->dataptr = zf->data;
	if (zf->auxb != NULL && zf->auxb != zf->buf)
		lfree(zf->auxb);
	zf->auxb = NULL;
	zf->bufptr = NULL;
	zf->endptr = NULL;
	zf->eof = FALSE;
}

/* close the file and then re-open it. */
/* Return TRUE on sucess. */
boolean
_zreopen(ZFILE *zf)
{
	char *tname;
	tname = dupString(zf->filename);
	_zreset(zf);
	zf->filename = tname;
	if (!_zopen(zf)) {	/* failed */
		lfree(zf->filename);
		zf->filename = NULL;
		return (FALSE);
	}
	return (TRUE);
}

/* Rewind the cached file. Warn the user if 
 * this is not likely to work.
 * Return TRUE on sucess.
 */

boolean
zrewind(ZFILE *zf)
{
	if (zf->nocache) {
		if (zf->type == ZSTDIN) {
			fprintf(stderr, "zrewind: caching was disabled by previous caller; can't rewind\n");
			return (FALSE);
		}
		fprintf(stderr, "zrewind: warning: caching was disabled by previous caller\n");
		return !_zreopen(zf);
	}
	zf->dataptr = zf->data;
	if (zf->auxb != NULL && zf->auxb != zf->buf)
		lfree(zf->auxb);
	zf->auxb = NULL;
	zf->bufptr = NULL;
	zf->endptr = NULL;
	zf->eof = FALSE;
	return TRUE;
}

/* Read in a buffer from the possibly uuencoded input stream */
int uuread(ZFILE *zf, byte *buf, int len)
{
	int cl, ld = 0;
	if (!zf->uudecode) {
		if (zf->type == ZSTANDARD)
			return fread(buf, 1, len, zf->stream);

		/* Work around for SVR4 fread() bug */
		for (;;) {
			ld += cl = fread(buf, 1, len - ld, zf->stream);
			if (ld == len)
				break;
			buf += cl;
			if (feof(zf->stream))
				break;
		}
		return ld;
	}
	for (;;) {
		if (zf->uunext >= zf->uuend) {	/* if buffer empty */
			zf->uunext = zf->uubuf;
			zf->uuend = zf->uubuf + uugetline(zf, zf->uubuf);
			if (zf->uunext >= zf->uuend) {	/* nothing read */
				return ld;
			}
		}
		/* Copy what we got into the buffer */
		cl = zf->uuend - zf->uunext;	/* copy length */
		if (cl > (len - ld))
			cl = (len - ld);
		bcopy(zf->uunext, buf, cl);
		zf->uunext += cl;
		ld += cl;	/* bump length done */
		buf += cl;
		if (ld >= len) {	/* Finished */
			return ld;
		}
	}
}

/* Get a line of uudecoded bytes from the input stream */

#define SHORTALOWANCE 6		/* alowance for truncation of ' 's at the end of lines */

int uugetline(ZFILE *zf, byte *obuf)
{
	int ulen, len;		/* number of uu encoding characters */
	byte ibuf[UULEN];	/* raw line buffer */
	int nib = 0;		/* number in buffer */

	for (;;) {		/* until we return something */
		if (zf->uustate == UUEOF) {	/* there's nothing more to read */
			return 0;
		}
		if (fgets((char *) ibuf, UULEN, zf->stream) == NULL) {
			fprintf(stderr, "uudecode: unexpected EOF\n");
			zf->uustate = UUEOF;
			return 0;
		}
		len = strlen((char *) ibuf) - 2;
		ulen = ((int) (((*ibuf) - ' ') & 077) * 4 + 2) / 3;
		if (*ibuf == 'M' && (len + SHORTALOWANCE) >= ulen && uuCheck(ibuf + 1, ulen <= len ? ulen : len)) {
			if (len < ulen) {	/* we have a slightly short line */
				fprintf(stderr, "uudecode: warning, short line\n");
				while (len < ulen)
					ibuf[++len] = ' ';
			}
			nib = uuExplodeLine(ibuf, obuf);
			zf->uustate = UUBODY;
			return nib;
		}
		/* Found a possibly dodgey line */
		if (zf->uustate == UUSKIP) {
			continue;	/* Skip any non 'M' lines */
		}
		if ((*ibuf != ' ' && *ibuf != '`') || (len != 0 && len != 1)) {
			/* not a blank before end line */

			/* Is zf->uustate == UUBODY and possible dodgy line */
			if (*ibuf > 'L' || *ibuf <= ' ' || len < ulen || !uuCheck(ibuf + 1, ulen)) {
				/* not a possible short line */
				zf->uustate = UUSKIP;
				continue;
			}
			nib = uuExplodeLine(ibuf, obuf);	/* decode possible short line */

			/* if this is a valid short line, then we expect a */
			/* specific line followed by an end line */
			if (fgets((char *) ibuf, UULEN, zf->stream) == NULL) {
				fprintf(stderr, "uudecode: unexpected EOF\n");
				zf->uustate = UUEOF;
				return nib;
			}
			len = strlen((char *) ibuf) - 2;
			ulen = ((int) (((*ibuf) - ' ') & 077) * 4 + 2) / 3;
			if (*ibuf == 'M' && (len + SHORTALOWANCE) >= ulen && uuCheck(ibuf + 1, ulen <= len ? ulen : len)) {
				/* Found an 'M' line - previous line must be garbage */
				if (len < ulen) {	/* we have a slightly short line */
					fprintf(stderr, "uudecode: warning, short line\n");
					while (len < ulen)
						ibuf[++len] = ' ';
				}
				nib = uuExplodeLine(ibuf, obuf);
				zf->uustate = UUBODY;
				return nib;
			}
			if ((*ibuf != ' ' && *ibuf != '`') || (len != 0 && len != 1)) {
				/* Not found correct thing after short line. Assume garbage */
				nib = 0;
				zf->uustate = UUSKIP;
				continue;
			}
		}
		/* Check for an end line */
		if (fgets((char *) ibuf, UULEN, zf->stream) == NULL) {
			fprintf(stderr, "uudecode: unexpected EOF\n");
			zf->uustate = UUEOF;
			return nib;
		}
		len = strlen((char *) ibuf) - 2;
		ulen = ((int) (((*ibuf) - ' ') & 077) * 4 + 2) / 3;
		if (*ibuf == 'M' && (len + SHORTALOWANCE) >= ulen && uuCheck(ibuf + 1, ulen <= len ? ulen : len)) {
			/* Found an 'M' line - previous lines must be garbage */
			if (len < ulen) {	/* we have a slightly short line */
				fprintf(stderr, "uudecode: warning, short line\n");
				while (len < ulen)
					ibuf[++len] = ' ';
			}
			nib = uuExplodeLine(ibuf, obuf);
			zf->uustate = UUBODY;
			return nib;
		}
		if (len != 2 || strncmp((char *) ibuf, "end", 3)) {
			/* Not found correct thing after short line. Assume garbage */
			nib = 0;
			zf->uustate = UUSKIP;
			continue;
		}
		/* Found end line, assume short line was ok */
		zf->uustate = UUEOF;
		return nib;
	}
}

#define DEC(C)	((unsigned)(((C) - ' ') & 077))

int uuExplodeLine(byte *icp, byte *ocp)
{
	unsigned int x, y, z;
	int i, len;
	i = len = DEC(*icp);
	++icp;

	while (i > 0) {
		if (i >= 3) {
			x = (DEC(*icp) << 2);
			icp++;
			x |= (DEC(*icp) >> 4);
			y = (DEC(*icp) << 4);
			icp++;
			y |= (DEC(*icp) >> 2);
			z = (DEC(*icp) << 6);
			icp++;
			z |= (DEC(*icp));
			*ocp++ = x;
			*ocp++ = y;
			*ocp++ = z;
		} else if (i >= 2) {
			x = (DEC(*icp) << 2);
			icp++;
			x |= (DEC(*icp) >> 4);
			y = (DEC(*icp) << 4);
			icp++;
			y |= (DEC(*icp) >> 2);
			*ocp++ = x;
			*ocp++ = y;
		} else if (i >= 1) {
			x = (DEC(*icp) << 2);
			icp++;
			x |= (DEC(*icp) >> 4);
			*ocp++ = x;
		}
		icp++;
		i -= 3;
	}
	return len;
}

/* Check for valid uu encoding characters */
boolean
uuCheck(byte *icp, int len)
{
	for (; len > 0; len--, icp++)
		if (*icp >= 'a' || *icp < ' ')
			return FALSE;
	return TRUE;
}


/* Find the length of a gets buffer */
/* Return the negative length if it looks like binary */
/* junk */
static int buflen(byte *buf, int len)
{
	int i, j = 0;
	len--;			/* allow for '\000' at end */
	for (i = 0; i < len && *buf != '\n'; i++, buf++) {
		if (*buf == '\000')
			j = 1;
	}
	if (i < len)
		i++;		/* count '\n' */
	if (j)
		return -i;
	return i;
}