BitInputStream.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (C) 2007-2011 Siemens AG
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*******************************************************************
  18. *
  19. * @author Daniel.Peintner.EXT@siemens.com
  20. * @version 0.4
  21. * @contact Joerg.Heuer@siemens.com
  22. *
  23. ********************************************************************/
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdint.h>
  27. #include "EXITypes.h"
  28. #include "BitInputStream.h"
  29. #ifndef BIT_INPUT_STREAM_C
  30. #define BIT_INPUT_STREAM_C
  31. /* internal method to (re)fill buffer */
  32. int readBuffer(bitstream_t* stream)
  33. {
  34. int errn = 0;
  35. if(stream->capacity==0)
  36. {
  37. if ( (*stream->pos) < stream->size ) {
  38. stream->buffer = stream->data[(*stream->pos)++];
  39. stream->capacity = BITS_IN_BYTE;
  40. } else {
  41. errn = -1;
  42. }
  43. }
  44. return errn;
  45. }
  46. int readBits(bitstream_t* stream, uint16_t num_bits, uint32_t* b)
  47. {
  48. int errn = readBuffer(stream);
  49. if (errn < 0) {
  50. return errn;
  51. }
  52. /* read the bits in one step */
  53. if(num_bits <= stream->capacity)
  54. {
  55. stream->capacity -= num_bits;
  56. *b = (stream->buffer >> stream->capacity) & (0xff >> (BITS_IN_BYTE - num_bits));
  57. }
  58. else
  59. {
  60. /* read bits as much as possible */
  61. *b = stream->buffer & (0xff >> (BITS_IN_BYTE - stream->capacity));
  62. num_bits -= stream->capacity;
  63. stream->capacity = 0;
  64. /* read whole bytes */
  65. while(num_bits >= 8)
  66. {
  67. errn = readBuffer(stream);
  68. if (errn < 0) {
  69. return errn;
  70. }
  71. *b = ((*b) << BITS_IN_BYTE) | stream->buffer;
  72. num_bits -= BITS_IN_BYTE;
  73. stream->capacity = 0;
  74. }
  75. /* read the spare bits in the buffer */
  76. if(num_bits>0)
  77. {
  78. errn = readBuffer(stream);
  79. if (errn < 0) {
  80. return errn;
  81. }
  82. *b = ((*b) << num_bits) | (stream->buffer >> (BITS_IN_BYTE - num_bits));
  83. stream->capacity = BITS_IN_BYTE - num_bits;
  84. }
  85. }
  86. return errn;
  87. }
  88. #endif