1 /* Copyright (c) 2020, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/base.h>
16 
17 #include <errno.h>
18 #include <limits.h>
19 #include <stdio.h>
20 
21 #include <algorithm>
22 
23 #include "internal.h"
24 
25 #if defined(OPENSSL_WINDOWS)
26 #include <io.h>
27 #else
28 #include <fcntl.h>
29 #include <unistd.h>
30 #endif
31 
32 
OpenFD(const char * path,int flags)33 ScopedFD OpenFD(const char *path, int flags) {
34 #if defined(OPENSSL_WINDOWS)
35   return ScopedFD(_open(path, flags));
36 #else
37   int fd;
38   do {
39     fd = open(path, flags);
40   } while (fd == -1 && errno == EINTR);
41   return ScopedFD(fd);
42 #endif
43 }
44 
CloseFD(int fd)45 void CloseFD(int fd) {
46 #if defined(OPENSSL_WINDOWS)
47   _close(fd);
48 #else
49   close(fd);
50 #endif
51 }
52 
ReadFromFD(int fd,size_t * out_bytes_read,void * out,size_t num)53 bool ReadFromFD(int fd, size_t *out_bytes_read, void *out, size_t num) {
54 #if defined(OPENSSL_WINDOWS)
55   // On Windows, the buffer must be at most |INT_MAX|. See
56   // https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/read?view=vs-2019
57   int ret = _read(fd, out, std::min(size_t{INT_MAX}, num));
58 #else
59   ssize_t ret;
60   do {
61     ret = read(fd, out, num);
62   } while (ret == -1 && errno == EINVAL);
63 #endif
64 
65   if (ret < 0) {
66     *out_bytes_read = 0;
67     return false;
68   }
69   *out_bytes_read = ret;
70   return true;
71 }
72 
WriteToFD(int fd,size_t * out_bytes_written,const void * in,size_t num)73 bool WriteToFD(int fd, size_t *out_bytes_written, const void *in, size_t num) {
74 #if defined(OPENSSL_WINDOWS)
75   // The documentation for |_write| does not say the buffer must be at most
76   // |INT_MAX|, but clamp it to |INT_MAX| instead of |UINT_MAX| in case.
77   int ret = _write(fd, in, std::min(size_t{INT_MAX}, num));
78 #else
79   ssize_t ret;
80   do {
81     ret = write(fd, in, num);
82   } while (ret == -1 && errno == EINVAL);
83 #endif
84 
85   if (ret < 0) {
86     *out_bytes_written = 0;
87     return false;
88   }
89   *out_bytes_written = ret;
90   return true;
91 }
92 
FDToFILE(ScopedFD fd,const char * mode)93 ScopedFILE FDToFILE(ScopedFD fd, const char *mode) {
94   ScopedFILE ret;
95 #if defined(OPENSSL_WINDOWS)
96   ret.reset(_fdopen(fd.get(), mode));
97 #else
98   ret.reset(fdopen(fd.get(), mode));
99 #endif
100   // |fdopen| takes ownership of |fd| on success.
101   if (ret) {
102     fd.release();
103   }
104   return ret;
105 }
106