Debian Codegraph @

crt/s2n/stuffer/s2n_stuffer_file.c

raw symbols SCIP
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
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19
20 #include "error/s2n_errno.h"
21 #include "stuffer/s2n_stuffer.h"
22 #include "utils/s2n_io.h"
23 #include "utils/s2n_safety.h"
24
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
29 /* Make sure we have enough space to write */
30 POSIX_GUARD(s2n_stuffer_skip_write(stuffer, len));
31
32 /* "undo" the skip write */
33 stuffer->write_cursor -= len;
34
35 ssize_t r = 0;
36 POSIX_ENSURE(stuffer->blob.data, S2N_ERR_READ);
37 S2N_IO_RETRY_EINTR(r, read(rfd, stuffer->blob.data + stuffer->write_cursor, len));
38 POSIX_ENSURE(r >= 0, S2N_ERR_READ);
39
40 /* Record just how many bytes we have written */
41 POSIX_ENSURE((size_t) r <= UINT32_MAX, S2N_ERR_INTEGER_OVERFLOW);
42 POSIX_GUARD(s2n_stuffer_skip_write(stuffer, (uint32_t) r));
43 if (bytes_written != NULL) {
44 *bytes_written = r;
45 }
46 return S2N_SUCCESS;
47 }
48
49 int s2n_stuffer_send_to_fd(struct s2n_stuffer *stuffer, const int wfd, const uint32_t len, uint32_t *bytes_sent)
50 {
51 POSIX_PRECONDITION(s2n_stuffer_validate(stuffer));
52
53 /* Make sure we even have the data */
54 POSIX_GUARD(s2n_stuffer_skip_read(stuffer, len));
55
56 /* "undo" the skip read */
57 stuffer->read_cursor -= len;
58
59 ssize_t w = 0;
60 POSIX_ENSURE(stuffer->blob.data, S2N_ERR_WRITE);
61 S2N_IO_RETRY_EINTR(w, write(wfd, stuffer->blob.data + stuffer->read_cursor, len));
62 POSIX_ENSURE(w >= 0, S2N_ERR_WRITE);
63
64 POSIX_ENSURE((size_t) w <= UINT32_MAX - stuffer->read_cursor, S2N_ERR_INTEGER_OVERFLOW);
65 stuffer->read_cursor += w;
66 if (bytes_sent != NULL) {
67 *bytes_sent = w;
68 }
69 return S2N_SUCCESS;
70 }