bitinput.c 1.3 KB

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