read_file.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @file read_file.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Function for reading a file into memory using stdio.
  32. */
  33. #ifndef BADVPN_MISC_READ_FILE_H
  34. #define BADVPN_MISC_READ_FILE_H
  35. #include <stddef.h>
  36. #include <stdint.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. static int read_file (const char *file, uint8_t **out_data, size_t *out_len)
  40. {
  41. FILE *f = fopen(file, "r");
  42. if (!f) {
  43. goto fail0;
  44. }
  45. size_t buf_len = 0;
  46. size_t buf_size = 128;
  47. uint8_t *buf = (uint8_t *)malloc(buf_size);
  48. if (!buf) {
  49. goto fail1;
  50. }
  51. while (1) {
  52. if (buf_len == buf_size) {
  53. if (2 > SIZE_MAX / buf_size) {
  54. goto fail;
  55. }
  56. size_t newsize = 2 * buf_size;
  57. uint8_t *newbuf = (uint8_t *)realloc(buf, newsize);
  58. if (!newbuf) {
  59. goto fail;
  60. }
  61. buf = newbuf;
  62. buf_size = newsize;
  63. }
  64. size_t bytes = fread(buf + buf_len, 1, buf_size - buf_len, f);
  65. if (bytes == 0) {
  66. if (feof(f)) {
  67. break;
  68. }
  69. goto fail;
  70. }
  71. buf_len += bytes;
  72. }
  73. fclose(f);
  74. *out_data = buf;
  75. *out_len = buf_len;
  76. return 1;
  77. fail:
  78. free(buf);
  79. fail1:
  80. fclose(f);
  81. fail0:
  82. return 0;
  83. }
  84. #endif