Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bio/fd.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/bio.h>
5
6 #if !defined(OPENSSL_NO_POSIX_IO)
7
8 #include <errno.h>
9 #include <string.h>
10
11 #if !defined(OPENSSL_WINDOWS)
12 #include <unistd.h>
13 #else
14 #include <io.h>
15 #endif
16
17 #include <openssl/err.h>
18 #include <openssl/mem.h>
19
20 #include "internal.h"
21 #include "../internal.h"
22
23
24 #if defined(OPENSSL_WINDOWS)
25 #define BORINGSSL_CLOSE _close
26 #define BORINGSSL_LSEEK _lseek
27 #define BORINGSSL_READ _read
28 #define BORINGSSL_WRITE _write
29 #else
30 #define BORINGSSL_CLOSE close
31 #define BORINGSSL_LSEEK lseek
32 #define BORINGSSL_READ read
33 #define BORINGSSL_WRITE write
34 #endif
35
36 BIO *BIO_new_fd(int fd, int close_flag) {
37 BIO *ret = BIO_new(BIO_s_fd());
38 if (ret == NULL) {
39 return NULL;
40 }
41 BIO_set_fd(ret, fd, close_flag);
42 return ret;
43 }
44
45 static int fd_new(BIO *bio) {
46 // num is used to store the file descriptor.
47 bio->num = -1;
48 return 1;
49 }
50
51 static int fd_free(BIO *bio) {
52 if (bio->shutdown) {
53 if (bio->init) {
54 BORINGSSL_CLOSE(bio->num);
55 }
56 bio->init = 0;
57 }
58 return 1;
59 }
60
61 static int fd_read(BIO *b, char *out, int outl) {
62 int ret = 0;
63
64 ret = (int)BORINGSSL_READ(b->num, out, outl);
65 BIO_clear_retry_flags(b);
66 if (ret <= 0) {
67 if (bio_errno_should_retry(ret)) {
68 BIO_set_retry_read(b);
69 }
70 }
71
72 return ret;
73 }
74
75 static int fd_write(BIO *b, const char *in, int inl) {
76 int ret = (int)BORINGSSL_WRITE(b->num, in, inl);
77 BIO_clear_retry_flags(b);
78 if (ret <= 0) {
79 if (bio_errno_should_retry(ret)) {
80 BIO_set_retry_write(b);
81 }
82 }
83
84 return ret;
85 }
86
87 static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
88 long ret = 1;
89 int *ip;
90
91 switch (cmd) {
92 case BIO_CTRL_RESET:
93 num = 0;
94 OPENSSL_FALLTHROUGH;
95 case BIO_C_FILE_SEEK:
96 ret = 0;
97 if (b->init) {
98 ret = (long)BORINGSSL_LSEEK(b->num, num, SEEK_SET);
99 }
100 break;
101 case BIO_C_FILE_TELL:
102 case BIO_CTRL_INFO:
103 ret = 0;
104 if (b->init) {
105 ret = (long)BORINGSSL_LSEEK(b->num, 0, SEEK_CUR);
106 }
107 break;
108 case BIO_C_SET_FD:
109 fd_free(b);
110 b->num = *((int *)ptr);
111 b->shutdown = (int)num;
112 b->init = 1;
113 break;
114 case BIO_C_GET_FD:
115 if (b->init) {
116 ip = (int *)ptr;
117 if (ip != NULL) {
118 *ip = b->num;
119 }
120 return b->num;
121 } else {
122 ret = -1;
123 }
124 break;
125 case BIO_CTRL_GET_CLOSE:
126 ret = b->shutdown;
127 break;
128 case BIO_CTRL_SET_CLOSE:
129 b->shutdown = (int)num;
130 break;
131 case BIO_CTRL_PENDING:
132 case BIO_CTRL_WPENDING:
133 ret = 0;
134 break;
135 case BIO_CTRL_FLUSH:
136 ret = 1;
137 break;
138 default:
139 ret = 0;
140 break;
141 }
142
143 return ret;
144 }
145
146 static int fd_gets(BIO *bp, char *buf, int size) {
147 if (size <= 0) {
148 return 0;
149 }
150
151 char *ptr = buf;
152 char *end = buf + size - 1;
153 while (ptr < end && fd_read(bp, ptr, 1) > 0) {
154 char c = ptr[0];
155 ptr++;
156 if (c == '\n') {
157 break;
158 }
159 }
160
161 ptr[0] = '\0';
162
163 // The output length is bounded by |size|.
164 return (int)(ptr - buf);
165 }
166
167 static const BIO_METHOD methods_fdp = {
168 BIO_TYPE_FD, "file descriptor", fd_write, fd_read, NULL /* puts */,
169 fd_gets, fd_ctrl, fd_new, fd_free, NULL /* callback_ctrl */,
170 };
171
172 const BIO_METHOD *BIO_s_fd(void) { return &methods_fdp; }
173
174 #endif // OPENSSL_NO_POSIX_IO
175
176 int BIO_set_fd(BIO *bio, int fd, int close_flag) {
177 return (int)BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd);
178 }
179
180 int BIO_get_fd(BIO *bio, int *out_fd) {
181 return (int)BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
182 }