BitStream.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (C) 2007-2010 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.2
  21. * @contact Joerg.Heuer@siemens.com
  22. *
  23. ********************************************************************/
  24. #define _CRT_SECURE_NO_DEPRECATE 1
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include <string.h>
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include "EXITypes.h"
  31. #ifndef BIT_STREAM_C
  32. #define BIT_STREAM_C
  33. int toBitstream(const char * filename, bitstream_t* bitstream) {
  34. FILE* f;
  35. int character;
  36. size_t len = 0, pos = 0, i;
  37. f = fopen(filename, "rb");
  38. if (f == NULL) {
  39. printf("\n[Error] no valid file handle !\n");
  40. return -1;
  41. } else {
  42. /* detect file size */
  43. while ((character = getc(f)) != EOF) {
  44. /* printf("%u \n", character); */
  45. len++;
  46. }
  47. fclose(f);
  48. /* printf("%u Zeichen", len); */
  49. /* setup stream */
  50. bitstream->data = malloc(sizeof(uint8_t) * len);
  51. bitstream->size = len;
  52. bitstream->pos = &pos;
  53. bitstream->buffer = 0;
  54. bitstream->capacity = 8;
  55. /* read file byte per byte */
  56. f = fopen(filename, "rb");
  57. i = 0;
  58. while ((character = getc(f)) != EOF) {
  59. bitstream->data[i] = (uint8_t) character;
  60. i++;
  61. }
  62. fclose(f);
  63. }
  64. return 0;
  65. }
  66. int writeBytesToFile(uint8_t* data, size_t len, const char * filename) {
  67. size_t rlen;
  68. FILE* f = fopen(filename, "wb+");
  69. if (f == NULL) {
  70. return -1;
  71. } else {
  72. rlen = fwrite(data, sizeof(uint8_t), len, f);
  73. fflush(f);
  74. fclose(f);
  75. if(rlen == len) {
  76. return 0;
  77. } else {
  78. return -1;
  79. }
  80. }
  81. }
  82. int writeBitstreamToFile(bitstream_t* bitsream, const char * filename) {
  83. return writeBytesToFile(bitsream->data, bitsream->size, filename);
  84. }
  85. #endif