s2n_stuffer_file.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <sys/mman.h>
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include "error/s2n_errno.h"
  22. #include "stuffer/s2n_stuffer.h"
  23. #include "utils/s2n_io.h"
  24. #include "utils/s2n_safety.h"
  25. int s2n_stuffer_recv_from_fd(struct s2n_stuffer *stuffer, const int rfd, const uint32_t len, uint32_t *bytes_written)
  26. {
  27. POSIX_PRECONDITION(s2n_stuffer_validate(stuffer));
  28. /* Make sure we have enough space to write */
  29. POSIX_GUARD(s2n_stuffer_skip_write(stuffer, len));
  30. /* "undo" the skip write */
  31. stuffer->write_cursor -= len;
  32. ssize_t r = 0;
  33. POSIX_ENSURE(stuffer->blob.data, S2N_ERR_READ);
  34. S2N_IO_RETRY_EINTR(r, read(rfd, stuffer->blob.data + stuffer->write_cursor, len));
  35. POSIX_ENSURE(r >= 0, S2N_ERR_READ);
  36. /* Record just how many bytes we have written */
  37. POSIX_ENSURE((size_t) r <= UINT32_MAX, S2N_ERR_INTEGER_OVERFLOW);
  38. POSIX_GUARD(s2n_stuffer_skip_write(stuffer, (uint32_t) r));
  39. if (bytes_written != NULL) {
  40. *bytes_written = r;
  41. }
  42. return S2N_SUCCESS;
  43. }
  44. int s2n_stuffer_send_to_fd(struct s2n_stuffer *stuffer, const int wfd, const uint32_t len, uint32_t *bytes_sent)
  45. {
  46. POSIX_PRECONDITION(s2n_stuffer_validate(stuffer));
  47. /* Make sure we even have the data */
  48. POSIX_GUARD(s2n_stuffer_skip_read(stuffer, len));
  49. /* "undo" the skip read */
  50. stuffer->read_cursor -= len;
  51. ssize_t w = 0;
  52. POSIX_ENSURE(stuffer->blob.data, S2N_ERR_WRITE);
  53. S2N_IO_RETRY_EINTR(w, write(wfd, stuffer->blob.data + stuffer->read_cursor, len));
  54. POSIX_ENSURE(w >= 0, S2N_ERR_WRITE);
  55. POSIX_ENSURE((size_t) w <= UINT32_MAX - stuffer->read_cursor, S2N_ERR_INTEGER_OVERFLOW);
  56. stuffer->read_cursor += w;
  57. if (bytes_sent != NULL) {
  58. *bytes_sent = w;
  59. }
  60. return S2N_SUCCESS;
  61. }
  62. int s2n_stuffer_alloc_ro_from_fd(struct s2n_stuffer *stuffer, int rfd)
  63. {
  64. POSIX_ENSURE_MUT(stuffer);
  65. struct stat st = { 0 };
  66. POSIX_ENSURE(fstat(rfd, &st) >= 0, S2N_ERR_FSTAT);
  67. POSIX_ENSURE_GT(st.st_size, 0);
  68. POSIX_ENSURE_LTE((uint64_t) st.st_size, UINT32_MAX);
  69. uint8_t *map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, rfd, 0);
  70. POSIX_ENSURE(map != MAP_FAILED, S2N_ERR_MMAP);
  71. struct s2n_blob b = { 0 };
  72. POSIX_GUARD(s2n_blob_init(&b, map, (uint32_t) st.st_size));
  73. return s2n_stuffer_init(stuffer, &b);
  74. }
  75. int s2n_stuffer_alloc_ro_from_file(struct s2n_stuffer *stuffer, const char *file)
  76. {
  77. POSIX_ENSURE_MUT(stuffer);
  78. POSIX_ENSURE_REF(file);
  79. int fd = 0;
  80. S2N_IO_RETRY_EINTR(fd, open(file, O_RDONLY));
  81. POSIX_ENSURE(fd >= 0, S2N_ERR_OPEN);
  82. int r = s2n_stuffer_alloc_ro_from_fd(stuffer, fd);
  83. POSIX_GUARD(close(fd));
  84. return r;
  85. }