ByteStream.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.3
  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 BYTE_STREAM_C
  32. #define BYTE_STREAM_C
  33. int readBytesFromFile(const char * filename, uint8_t* data, size_t size, size_t pos) {
  34. FILE* f;
  35. int character;
  36. f = fopen(filename, "rb");
  37. if (f == NULL) {
  38. printf("\n[Error] no valid file handle !\n");
  39. return -1;
  40. } else {
  41. /* read bytes */
  42. while ((character = getc(f)) != EOF) {
  43. if (pos >= size) {
  44. return -1;
  45. }
  46. data[pos++] = (uint8_t) character;
  47. /* printf("%u \n", character); */
  48. }
  49. fclose(f);
  50. }
  51. return pos;
  52. }
  53. int writeBytesToFile(uint8_t* data, size_t len, const char * filename) {
  54. size_t rlen;
  55. FILE* f = fopen(filename, "wb+");
  56. if (f == NULL) {
  57. return -1;
  58. } else {
  59. rlen = fwrite(data, sizeof(uint8_t), len, f);
  60. fflush(f);
  61. fclose(f);
  62. if(rlen == len) {
  63. return 0;
  64. } else {
  65. return -1;
  66. }
  67. }
  68. }
  69. #endif