ByteDecoderChannel.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (C) 2007-2012 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 2012-01-31
  22. * @contact Joerg.Heuer@siemens.com
  23. *
  24. * <p>Code generated by EXIdizer</p>
  25. ********************************************************************/
  26. #include "DecoderChannel.h"
  27. #include "CoderChannel.h"
  28. #include "BitInputStream.h"
  29. #include "EXITypes.h"
  30. #ifndef BYTE_DECODER_CHANNEL_C
  31. #define BYTE_DECODER_CHANNEL_C
  32. #if EXI_ALIGNMENT == BYTE_ALIGNMENT
  33. int decode(bitstream_t* stream, uint8_t* b) {
  34. int errn = 0;
  35. #if EXI_STREAM == BYTE_ARRAY
  36. if ( (*stream->pos) < stream->size ) {
  37. *b = stream->data[(*stream->pos)++];
  38. } else {
  39. errn = EXI_ERROR_INPUT_STREAM_EOF;
  40. }
  41. #endif /* EXI_STREAM == BYTE_ARRAY */
  42. #if EXI_STREAM == FILE_STREAM
  43. *b = getc(stream->file);
  44. /* EOF cannot be used, 0xFF valid value */
  45. if ( feof(stream->file) || ferror(stream->file) ) {
  46. return EXI_ERROR_INPUT_STREAM_EOF;
  47. }
  48. #endif /* EXI_STREAM == FILE_STREAM */
  49. return errn;
  50. }
  51. int decodeBoolean(bitstream_t* stream, int* b) {
  52. uint8_t bb;
  53. int errn = decode(stream, &bb);
  54. *b = (bb == 0) ? 0 : 1;
  55. return errn;
  56. }
  57. /**
  58. * Decodes and returns an n-bit unsigned integer using the minimum number of
  59. * bytes required for n bits.
  60. */
  61. int decodeNBitUnsignedInteger(bitstream_t* stream, uint16_t nbits, uint32_t* uint32) {
  62. uint16_t bitsRead = 0;
  63. uint8_t b;
  64. int errn = 0;
  65. *uint32 = 0;
  66. while (bitsRead < nbits) {
  67. errn = decode(stream, &b);
  68. if (errn != 0) {
  69. return errn;
  70. }
  71. *uint32 += (b << bitsRead);
  72. bitsRead += 8;
  73. }
  74. return errn;
  75. }
  76. #endif
  77. #endif