BitInputStream.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2007-2018 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 2017-03-02
  21. * @contact Richard.Kuntschke@siemens.com
  22. *
  23. * <p>Code generated by EXIdizer</p>
  24. * <p>Schema: V2G_CI_MsgDef.xsd</p>
  25. *
  26. *
  27. ********************************************************************/
  28. #include "BitInputStream.h"
  29. #include "EXIConfig.h"
  30. #include "EXITypes.h"
  31. #include "ErrorCodes.h"
  32. #ifndef BIT_INPUT_STREAM_C
  33. #define BIT_INPUT_STREAM_C
  34. /* internal method to (re)fill buffer */
  35. static int readBuffer(bitstream_t* stream)
  36. {
  37. int errn = 0;
  38. if(stream->capacity==0)
  39. {
  40. #if EXI_STREAM == BYTE_ARRAY
  41. if ( (*stream->pos) < stream->size ) {
  42. stream->buffer = stream->data[(*stream->pos)++];
  43. stream->capacity = BITS_IN_BYTE;
  44. } else {
  45. errn = EXI_ERROR_INPUT_STREAM_EOF;
  46. }
  47. #endif
  48. #if EXI_STREAM == FILE_STREAM
  49. stream->buffer = (uint8_t)(getc(stream->file));
  50. /* EOF cannot be used, 0xFF valid value */
  51. if ( feof(stream->file) || ferror(stream->file) ) {
  52. errn = EXI_ERROR_INPUT_STREAM_EOF;
  53. } else {
  54. stream->capacity = BITS_IN_BYTE;
  55. }
  56. #endif
  57. }
  58. return errn;
  59. }
  60. int readBits(bitstream_t* stream, size_t num_bits, uint32_t* b)
  61. {
  62. int errn = readBuffer(stream);
  63. if (errn == 0) {
  64. /* read the bits in one step */
  65. if(num_bits <= stream->capacity) {
  66. stream->capacity = (uint8_t)(stream->capacity - num_bits);
  67. *b = (uint32_t)((stream->buffer >> stream->capacity) & (0xff >> (BITS_IN_BYTE - num_bits)));
  68. } else {
  69. /* read bits as much as possible */
  70. *b = (uint32_t)(stream->buffer & (0xff >> (BITS_IN_BYTE - stream->capacity)));
  71. num_bits = (num_bits - stream->capacity);
  72. stream->capacity = 0;
  73. /* read whole bytes */
  74. while(errn == 0 && num_bits >= 8)
  75. {
  76. errn = readBuffer(stream);
  77. *b = ((*b) << BITS_IN_BYTE) | stream->buffer;
  78. num_bits = (num_bits - BITS_IN_BYTE);
  79. stream->capacity = 0;
  80. }
  81. /* read the spare bits in the buffer */
  82. if(errn == 0 && num_bits > 0)
  83. {
  84. errn = readBuffer(stream);
  85. if (errn == 0) {
  86. *b = ( (*b) << num_bits) | (uint8_t)(stream->buffer >> (BITS_IN_BYTE - num_bits)) ;
  87. stream->capacity = (uint8_t)(BITS_IN_BYTE - num_bits);
  88. }
  89. }
  90. }
  91. }
  92. return errn;
  93. }
  94. #endif