BitInputStream.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*******************************************************************
  19. *
  20. * @author Daniel.Peintner.EXT@siemens.com
  21. * @version 2011-12-02
  22. * @contact Joerg.Heuer@siemens.com
  23. *
  24. * <p>Code generated by EXIdizer</p>
  25. ********************************************************************/
  26. #include "EXITypes.h"
  27. #include "BitInputStream.h"
  28. #ifndef BIT_INPUT_STREAM_C
  29. #define BIT_INPUT_STREAM_C
  30. /* internal method to (re)fill buffer */
  31. int readBuffer(bitstream_t* stream)
  32. {
  33. int errn = 0;
  34. if(stream->capacity==0)
  35. {
  36. #if EXI_STREAM == BYTE_ARRAY
  37. if ( (*stream->pos) < stream->size ) {
  38. stream->buffer = stream->data[(*stream->pos)++];
  39. stream->capacity = BITS_IN_BYTE;
  40. } else {
  41. errn = EXI_ERROR_INPUT_STREAM_EOF;
  42. }
  43. #endif
  44. #if EXI_STREAM == FILE_STREAM
  45. stream->buffer = getc(stream->file);
  46. /* EOF cannot be used, 0xFF valid value */
  47. if ( feof(stream->file) || ferror(stream->file) ) {
  48. return EXI_ERROR_INPUT_STREAM_EOF;
  49. }
  50. stream->capacity = BITS_IN_BYTE;
  51. #endif
  52. }
  53. return errn;
  54. }
  55. int readBits(bitstream_t* stream, uint16_t num_bits, uint32_t* b)
  56. {
  57. int errn = readBuffer(stream);
  58. if (errn < 0) {
  59. return errn;
  60. }
  61. /* read the bits in one step */
  62. if(num_bits <= stream->capacity)
  63. {
  64. stream->capacity -= num_bits;
  65. *b = (stream->buffer >> stream->capacity) & (0xff >> (BITS_IN_BYTE - num_bits));
  66. }
  67. else
  68. {
  69. /* read bits as much as possible */
  70. *b = stream->buffer & (0xff >> (BITS_IN_BYTE - stream->capacity));
  71. num_bits -= stream->capacity;
  72. stream->capacity = 0;
  73. /* read whole bytes */
  74. while(num_bits >= 8)
  75. {
  76. errn = readBuffer(stream);
  77. if (errn < 0) {
  78. return errn;
  79. }
  80. *b = ((*b) << BITS_IN_BYTE) | stream->buffer;
  81. num_bits -= BITS_IN_BYTE;
  82. stream->capacity = 0;
  83. }
  84. /* read the spare bits in the buffer */
  85. if(num_bits>0)
  86. {
  87. errn = readBuffer(stream);
  88. if (errn < 0) {
  89. return errn;
  90. }
  91. *b = ((*b) << num_bits) | (stream->buffer >> (BITS_IN_BYTE - num_bits));
  92. stream->capacity = BITS_IN_BYTE - num_bits;
  93. }
  94. }
  95. return errn;
  96. }
  97. #endif