plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

bitinput.c (1356B)


      1 #include	<u.h>
      2 #include	<libc.h>
      3 #include	<bio.h>
      4 #include	"sky.h"
      5 
      6 static int hufvals[] = {
      7 	 1,  1,  1,  1,  1,  1,  1,  1,
      8 	 2,  2,  2,  2,  2,  2,  2,  2,
      9 	 4,  4,  4,  4,  4,  4,  4,  4,
     10 	 8,  8,  8,  8,  8,  8,  8,  8,
     11 	 3,  3,  3,  3,  5,  5,  5,  5,
     12 	10, 10, 10, 10, 12, 12, 12, 12,
     13 	15, 15, 15, 15,  6,  6,  7,  7,
     14 	 9,  9, 11, 11, 13, 13,  0, 14
     15 };
     16 
     17 static int huflens[] = {
     18 	3, 3, 3, 3, 3, 3, 3, 3,
     19 	3, 3, 3, 3, 3, 3, 3, 3,
     20 	3, 3, 3, 3, 3, 3, 3, 3,
     21 	3, 3, 3, 3, 3, 3, 3, 3,
     22 	4, 4, 4, 4, 4, 4, 4, 4,
     23 	4, 4, 4, 4, 4, 4, 4, 4,
     24 	4, 4, 4, 4, 5, 5, 5, 5,
     25 	5, 5, 5, 5, 5, 5, 6, 6
     26 };
     27 
     28 static	int	buffer;
     29 static	int	bits_to_go;		/* Number of bits still in buffer */
     30 
     31 void
     32 start_inputing_bits(void)
     33 {
     34 	bits_to_go = 0;
     35 }
     36 
     37 int
     38 input_huffman(Biobuf *infile)
     39 {
     40 	int c;
     41 
     42 	if(bits_to_go < 6) {
     43 		c = Bgetc(infile);
     44 		if(c < 0) {
     45 			fprint(2, "input_huffman: unexpected EOF\n");
     46 			exits("format");
     47 		}
     48 		buffer = (buffer<<8) | c;
     49 		bits_to_go += 8;
     50 	}
     51 	c = (buffer >> (bits_to_go-6)) & 0x3f;
     52 	bits_to_go -= huflens[c];
     53 	return hufvals[c];
     54 }
     55 
     56 int
     57 input_nybble(Biobuf *infile)
     58 {
     59 	int c;
     60 
     61 	if(bits_to_go < 4) {
     62 		c = Bgetc(infile);
     63 		if(c < 0){
     64 			fprint(2, "input_nybble: unexpected EOF\n");
     65 			exits("format");
     66 		}
     67 		buffer = (buffer<<8) | c;
     68 		bits_to_go += 8;
     69 	}
     70 
     71 	/*
     72 	 * pick off the first 4 bits
     73 	 */
     74 	bits_to_go -= 4;
     75 	return (buffer>>bits_to_go) & 0x0f;
     76 }