12089Ssos/*- 2228976Suqs * 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 1597748Sschweikh * derived from this software without 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 */ 282089Ssos 2930764Scharnier#ifndef lint 3030764Scharnierstatic const char rcsid[] = 3150479Speter "$FreeBSD$"; 3230764Scharnier#endif /* not lint */ 3330764Scharnier 342089Ssos#include <stdio.h> 3523457Sbrian#include <string.h> 3623457Sbrian#include "decode.h" 372089Ssos 3875344Ssobomaxint decode(FILE *fd, char *buffer, int len) 392089Ssos{ 4075344Ssobomax int n, pos = 0, tpos; 4175344Ssobomax char *bp, *p; 4275344Ssobomax char tbuffer[3]; 432089Ssos char temp[128]; 442089Ssos 452089Ssos#define DEC(c) (((c) - ' ') & 0x3f) 462089Ssos 472089Ssos do { 488857Srgrimes if (!fgets(temp, sizeof(temp), fd)) 492089Ssos return(0); 502089Ssos } while (strncmp(temp, "begin ", 6)); 51140159Sdelphij sscanf(temp, "begin %o %s", (unsigned *)&n, temp); 5275344Ssobomax bp = buffer; 532089Ssos for (;;) { 542089Ssos if (!fgets(p = temp, sizeof(temp), fd)) 552089Ssos return(0); 562089Ssos if ((n = DEC(*p)) <= 0) 572089Ssos break; 5875344Ssobomax for (++p; n > 0; p += 4, n -= 3) { 5975344Ssobomax tpos = 0; 602089Ssos if (n >= 3) { 6175344Ssobomax tbuffer[tpos++] = DEC(p[0])<<2 | DEC(p[1])>>4; 6275344Ssobomax tbuffer[tpos++] = DEC(p[1])<<4 | DEC(p[2])>>2; 6375344Ssobomax tbuffer[tpos++] = DEC(p[2])<<6 | DEC(p[3]); 642089Ssos } 652089Ssos else { 662089Ssos if (n >= 1) { 6775344Ssobomax tbuffer[tpos++] = 682089Ssos DEC(p[0])<<2 | DEC(p[1])>>4; 692089Ssos } 702089Ssos if (n >= 2) { 7175344Ssobomax tbuffer[tpos++] = 722089Ssos DEC(p[1])<<4 | DEC(p[2])>>2; 732089Ssos } 742089Ssos if (n >= 3) { 7575344Ssobomax tbuffer[tpos++] = 762089Ssos DEC(p[2])<<6 | DEC(p[3]); 772089Ssos } 782089Ssos } 7975344Ssobomax if (tpos == 0) 8075344Ssobomax continue; 8175344Ssobomax if (tpos + pos > len) { 8275344Ssobomax tpos = len - pos; 8375344Ssobomax /* 8475344Ssobomax * Arrange return value > len to indicate 8575344Ssobomax * overflow. 8675344Ssobomax */ 8775344Ssobomax pos++; 8875344Ssobomax } 8975344Ssobomax bcopy(tbuffer, bp, tpos); 9075344Ssobomax pos += tpos; 9175344Ssobomax bp += tpos; 9275344Ssobomax if (pos > len) 9375344Ssobomax return(pos); 9475344Ssobomax } 952089Ssos } 962089Ssos if (!fgets(temp, sizeof(temp), fd) || strcmp(temp, "end\n")) 972089Ssos return(0); 982089Ssos return(pos); 992089Ssos} 100