/* * bitio.c -- bit based I/O package * (C)Copyright 1998, 99 by Hiroshi Takekawa * This file is part of Enfle. * * Last Modified: Sat Oct 16 22:29:01 1999. * $Id: bitio.c,v 1.1 1999/10/18 12:30:33 sian Exp $ * * Enfle is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Enfle is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include #include "bitio.h" Bit_stream * bitio_init(Archive *ar) { Bit_stream *st; if ((st = calloc(1, sizeof(Bit_stream))) == NULL) return NULL; st->left = st->buf = 0; st->ar = ar; return st; } int bitio_getbit(Bit_stream *p) { if (p->left == 0) { if ((p->buf = archive_getc(p->ar)) == EOF) return -1; p->left = 8; } p->left--; return (p->buf >> p->left) & 1; } int bitio_getbits(Bit_stream *p, int bits) { int c; if (!bits) return 0; while (bits > p->left) { if ((c = archive_getc(p->ar)) == EOF) return -1; p->buf = (p->buf << 8) | c; p->left += 8; } p->left -= bits; return (p->buf >> p->left) & ((1 << bits) - 1); }