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