decode.c revision 8857
12089Ssos/*-
22089Ssos * Copyright (c) 1994 S�ren Schmidt
32089Ssos * All rights reserved.
42089Ssos *
52089Ssos * Redistribution and use in source and binary forms, with or without
62089Ssos * modification, are permitted provided that the following conditions
72089Ssos * are met:
82089Ssos * 1. Redistributions of source code must retain the above copyright
95994Ssos *    notice, this list of conditions and the following disclaimer,
105994Ssos *    in this position and unchanged.
112089Ssos * 2. Redistributions in binary form must reproduce the above copyright
122089Ssos *    notice, this list of conditions and the following disclaimer in the
132089Ssos *    documentation and/or other materials provided with the distribution.
142089Ssos * 3. The name of the author may not be used to endorse or promote products
152089Ssos *    derived from this software withough specific prior written permission
162089Ssos *
172089Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
182089Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192089Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202089Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
212089Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
222089Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232089Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242089Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252089Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
262089Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272089Ssos *
288857Srgrimes *	$Id: decode.c,v 1.2 1995/01/28 22:18:02 sos Exp $
292089Ssos */
302089Ssos
312089Ssos#include <stdio.h>
322089Ssos
332089Ssosint decode(FILE *fd, char *buffer)
342089Ssos{
352089Ssos	int n, pos = 0;
362089Ssos	char *p;
372089Ssos	char temp[128];
382089Ssos
392089Ssos#define	DEC(c)	(((c) - ' ') & 0x3f)
402089Ssos
412089Ssos	do {
428857Srgrimes		if (!fgets(temp, sizeof(temp), fd))
432089Ssos			return(0);
442089Ssos	} while (strncmp(temp, "begin ", 6));
452089Ssos	sscanf(temp, "begin %o %s", &n, temp);
462089Ssos	for (;;) {
472089Ssos		if (!fgets(p = temp, sizeof(temp), fd))
482089Ssos			return(0);
492089Ssos		if ((n = DEC(*p)) <= 0)
502089Ssos			break;
512089Ssos		for (++p; n > 0; p += 4, n -= 3)
522089Ssos			if (n >= 3) {
532089Ssos				buffer[pos++] = DEC(p[0])<<2 | DEC(p[1])>>4;
542089Ssos				buffer[pos++] = DEC(p[1])<<4 | DEC(p[2])>>2;
552089Ssos				buffer[pos++] = DEC(p[2])<<6 | DEC(p[3]);
562089Ssos			}
572089Ssos			else {
582089Ssos				if (n >= 1) {
592089Ssos					buffer[pos++] =
602089Ssos						DEC(p[0])<<2 | DEC(p[1])>>4;
612089Ssos				}
622089Ssos				if (n >= 2) {
638857Srgrimes					buffer[pos++] =
642089Ssos						DEC(p[1])<<4 | DEC(p[2])>>2;
652089Ssos				}
662089Ssos				if (n >= 3) {
672089Ssos					buffer[pos++] =
682089Ssos						DEC(p[2])<<6 | DEC(p[3]);
692089Ssos				}
702089Ssos			}
712089Ssos	}
722089Ssos	if (!fgets(temp, sizeof(temp), fd) || strcmp(temp, "end\n"))
732089Ssos		return(0);
742089Ssos	return(pos);
752089Ssos}
76