1 /*
2 * Copyright (C) 2016 The Android Open Source Project
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 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef CONSCRYPT_COMPAT_H_
18 #define CONSCRYPT_COMPAT_H_
19
20 #if defined(ANDROID) && !defined(CONSCRYPT_OPENJDK)
21 // We want the XSI-compliant strerror_r (it's more portable across NDK versions,
22 // and the only one available until android-23), not the GNU one. We haven't
23 // actually defined _GNU_SOURCE ourselves, but the compiler adds it
24 // automatically when building C++.
25 //
26 // Including this header out of the normal order to make sure we import it the
27 // right way.
28 #undef _GNU_SOURCE
29 #include <string.h>
30
31 // OTOH, we need to have _GNU_SOURCE defined to pick up asprintf from stdio.h.
32 #define _GNU_SOURCE
33 #include <stdio.h>
34
35 #include <android/log.h>
36
37 #else /* !ANDROID || CONSCRYPT_OPENJDK */
38
39 #include <stdio.h>
40 #include <string.h>
41
42 #endif /* !ANDROID || CONSCRYPT_OPENJDK */
43
44 #ifdef _WIN32
45
46 #include <stdarg.h>
47 #include <stdint.h>
48 #include <stdlib.h>
49 #include <winsock2.h>
50 #include <cstddef>
51
52 // NOLINTNEXTLINE(runtime/int)
53 typedef long ssize_t;
54 #define strerror_r(errnum, buf, buflen) strerror_s(buf, buflen, errnum)
55 #define strcasecmp _stricmp
56
57 // Windows doesn't define this either *sigh*...
vasprintf(char ** ret,const char * format,va_list args)58 inline int vasprintf(char **ret, const char *format, va_list args) {
59 va_list copy;
60 va_copy(copy, args);
61 *ret = nullptr;
62
63 int count = vsnprintf(nullptr, 0, format, args);
64 if (count >= 0) {
65 char *buffer = static_cast<char *>(malloc((std::size_t)count + 1));
66 if (buffer == nullptr) {
67 count = -1;
68 } else if ((count = vsnprintf(buffer, static_cast<std::size_t>(count + 1), format, copy)) <
69 0) {
70 free(buffer);
71 } else {
72 *ret = buffer;
73 }
74 }
75 va_end(copy); // Each va_start() or va_copy() needs a va_end()
76
77 return count;
78 }
79
asprintf(char ** strp,const char * fmt,...)80 inline int asprintf(char **strp, const char *fmt, ...) {
81 va_list ap;
82 va_start(ap, fmt);
83 int r = vasprintf(strp, fmt, ap);
84 va_end(ap);
85 return r;
86 }
87
gettimeofday(struct timeval * tp,struct timezone *)88 inline int gettimeofday(struct timeval *tp, struct timezone *) {
89 // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing
90 // zero's
91 // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
92 // until 00:00:00 January 1, 1970
93 static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
94
95 SYSTEMTIME system_time;
96 FILETIME file_time;
97 uint64_t time;
98
99 GetSystemTime(&system_time);
100 SystemTimeToFileTime(&system_time, &file_time);
101 time = ((uint64_t)file_time.dwLowDateTime);
102 time += ((uint64_t)file_time.dwHighDateTime) << 32;
103
104 tp->tv_sec = static_cast<long>((time - EPOCH) / 10000000L); // NOLINT(runtime/int)
105 tp->tv_usec = static_cast<long>(system_time.wMilliseconds * 1000); // NOLINT(runtime/int)
106 return 0;
107 }
108 #endif // _WIN32
109
110 #endif // CONSCRYPT_COMPAT_H_
111