decode.c revision 8857
150276Speter/*-
2262629Sdelphij * Copyright (c) 1994 S�ren Schmidt
350276Speter * All rights reserved.
450276Speter *
550276Speter * Redistribution and use in source and binary forms, with or without
650276Speter * modification, are permitted provided that the following conditions
750276Speter * are met:
850276Speter * 1. Redistributions of source code must retain the above copyright
950276Speter *    notice, this list of conditions and the following disclaimer,
1050276Speter *    in this position and unchanged.
1150276Speter * 2. Redistributions in binary form must reproduce the above copyright
1250276Speter *    notice, this list of conditions and the following disclaimer in the
1350276Speter *    documentation and/or other materials provided with the distribution.
1450276Speter * 3. The name of the author may not be used to endorse or promote products
1550276Speter *    derived from this software withough specific prior written permission
1650276Speter *
1750276Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1850276Speter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1950276Speter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2050276Speter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2150276Speter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2250276Speter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2350276Speter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2450276Speter * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2550276Speter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2650276Speter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2750276Speter *
2850276Speter *	$Id: decode.c,v 1.2 1995/01/28 22:18:02 sos Exp $
2950276Speter */
30262629Sdelphij
3150276Speter#include <stdio.h>
3250276Speter
3350276Speterint decode(FILE *fd, char *buffer)
3450276Speter{
3550276Speter	int n, pos = 0;
36262629Sdelphij	char *p;
3750276Speter	char temp[128];
38166124Srafan
39166124Srafan#define	DEC(c)	(((c) - ' ') & 0x3f)
40166124Srafan
41166124Srafan	do {
4250276Speter		if (!fgets(temp, sizeof(temp), fd))
43166124Srafan			return(0);
44166124Srafan	} while (strncmp(temp, "begin ", 6));
45166124Srafan	sscanf(temp, "begin %o %s", &n, temp);
46166124Srafan	for (;;) {
47166124Srafan		if (!fgets(p = temp, sizeof(temp), fd))
48166124Srafan			return(0);
49166124Srafan		if ((n = DEC(*p)) <= 0)
50166124Srafan			break;
51166124Srafan		for (++p; n > 0; p += 4, n -= 3)
52166124Srafan			if (n >= 3) {
53166124Srafan				buffer[pos++] = DEC(p[0])<<2 | DEC(p[1])>>4;
54166124Srafan				buffer[pos++] = DEC(p[1])<<4 | DEC(p[2])>>2;
55166124Srafan				buffer[pos++] = DEC(p[2])<<6 | DEC(p[3]);
56166124Srafan			}
57166124Srafan			else {
58166124Srafan				if (n >= 1) {
59166124Srafan					buffer[pos++] =
60166124Srafan						DEC(p[0])<<2 | DEC(p[1])>>4;
61166124Srafan				}
62166124Srafan				if (n >= 2) {
63166124Srafan					buffer[pos++] =
64166124Srafan						DEC(p[1])<<4 | DEC(p[2])>>2;
65166124Srafan				}
66166124Srafan				if (n >= 3) {
67166124Srafan					buffer[pos++] =
68166124Srafan						DEC(p[2])<<6 | DEC(p[3]);
69166124Srafan				}
70166124Srafan			}
71166124Srafan	}
72166124Srafan	if (!fgets(temp, sizeof(temp), fd) || strcmp(temp, "end\n"))
73166124Srafan		return(0);
74166124Srafan	return(pos);
75166124Srafan}
76166124Srafan