Branch data Line data Source code
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 : 1355 : { 27 [ - + ][ + - ]: 1355 : POSIX_PRECONDITION(s2n_stuffer_validate(stuffer)); 28 : : 29 : : /* Make sure we have enough space to write */ 30 [ - + ]: 1355 : POSIX_GUARD(s2n_stuffer_skip_write(stuffer, len)); 31 : : 32 : : /* "undo" the skip write */ 33 : 1355 : stuffer->write_cursor -= len; 34 : : 35 : 1355 : ssize_t r = 0; 36 [ - + ][ # # ]: 1355 : POSIX_ENSURE(stuffer->blob.data, S2N_ERR_READ); 37 [ + + ][ - + ]: 1355 : S2N_IO_RETRY_EINTR(r, read(rfd, stuffer->blob.data + stuffer->write_cursor, len)); 38 [ + - ][ + + ]: 1355 : POSIX_ENSURE(r >= 0, S2N_ERR_READ); 39 : : 40 : : /* Record just how many bytes we have written */ 41 [ # # ][ - + ]: 9 : POSIX_ENSURE((size_t) r <= UINT32_MAX, S2N_ERR_INTEGER_OVERFLOW); 42 [ - + ]: 9 : POSIX_GUARD(s2n_stuffer_skip_write(stuffer, (uint32_t) r)); 43 [ + + ]: 9 : if (bytes_written != NULL) { 44 : 1 : *bytes_written = r; 45 : 1 : } 46 : 9 : return S2N_SUCCESS; 47 : 9 : } 48 : : 49 : : int s2n_stuffer_send_to_fd(struct s2n_stuffer *stuffer, const int wfd, const uint32_t len, uint32_t *bytes_sent) 50 : 1288 : { 51 [ - + ][ + - ]: 1288 : POSIX_PRECONDITION(s2n_stuffer_validate(stuffer)); 52 : : 53 : : /* Make sure we even have the data */ 54 [ - + ]: 1288 : POSIX_GUARD(s2n_stuffer_skip_read(stuffer, len)); 55 : : 56 : : /* "undo" the skip read */ 57 : 1288 : stuffer->read_cursor -= len; 58 : : 59 : 1288 : ssize_t w = 0; 60 [ + + ][ + - ]: 1288 : POSIX_ENSURE(stuffer->blob.data, S2N_ERR_WRITE); 61 [ - + ][ # # ]: 1286 : S2N_IO_RETRY_EINTR(w, write(wfd, stuffer->blob.data + stuffer->read_cursor, len)); 62 [ - + ][ # # ]: 1286 : POSIX_ENSURE(w >= 0, S2N_ERR_WRITE); 63 : : 64 [ - + ][ # # ]: 1286 : POSIX_ENSURE((size_t) w <= UINT32_MAX - stuffer->read_cursor, S2N_ERR_INTEGER_OVERFLOW); 65 : 1286 : stuffer->read_cursor += w; 66 [ - + ]: 1286 : if (bytes_sent != NULL) { 67 : 0 : *bytes_sent = w; 68 : 0 : } 69 : 1286 : return S2N_SUCCESS; 70 : 1286 : }