1
0

ByteDecoderChannel.c 2.4 KB

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