bitinput.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include "sky.h"
  13. static int hufvals[] = {
  14. 1, 1, 1, 1, 1, 1, 1, 1,
  15. 2, 2, 2, 2, 2, 2, 2, 2,
  16. 4, 4, 4, 4, 4, 4, 4, 4,
  17. 8, 8, 8, 8, 8, 8, 8, 8,
  18. 3, 3, 3, 3, 5, 5, 5, 5,
  19. 10, 10, 10, 10, 12, 12, 12, 12,
  20. 15, 15, 15, 15, 6, 6, 7, 7,
  21. 9, 9, 11, 11, 13, 13, 0, 14,
  22. };
  23. static int huflens[] = {
  24. 3, 3, 3, 3, 3, 3, 3, 3,
  25. 3, 3, 3, 3, 3, 3, 3, 3,
  26. 3, 3, 3, 3, 3, 3, 3, 3,
  27. 3, 3, 3, 3, 3, 3, 3, 3,
  28. 4, 4, 4, 4, 4, 4, 4, 4,
  29. 4, 4, 4, 4, 4, 4, 4, 4,
  30. 4, 4, 4, 4, 5, 5, 5, 5,
  31. 5, 5, 5, 5, 5, 5, 6, 6,
  32. };
  33. static int buffer;
  34. static int bits_to_go; /* Number of bits still in buffer */
  35. void
  36. start_inputing_bits(void)
  37. {
  38. bits_to_go = 0;
  39. }
  40. int
  41. input_huffman(Biobuf *infile)
  42. {
  43. int c;
  44. if(bits_to_go < 6) {
  45. c = Bgetc(infile);
  46. if(c < 0) {
  47. fprint(2, "input_huffman: unexpected EOF\n");
  48. exits("format");
  49. }
  50. buffer = (buffer<<8) | c;
  51. bits_to_go += 8;
  52. }
  53. c = (buffer >> (bits_to_go-6)) & 0x3f;
  54. bits_to_go -= huflens[c];
  55. return hufvals[c];
  56. }
  57. int
  58. input_nybble(Biobuf *infile)
  59. {
  60. int c;
  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. * pick off the first 4 bits
  72. */
  73. bits_to_go -= 4;
  74. return (buffer>>bits_to_go) & 0x0f;
  75. }