• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SRC_MAIN_NATIVE_COMPAT_H_
18 #define CONSCRYPT_SRC_MAIN_NATIVE_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 typedef long ssize_t;
53 #define strerror_r(errnum, buf, buflen) strerror_s(buf, buflen, errnum)
54 #define strcasecmp _stricmp
55 
56 // Windows doesn't define this either *sigh*...
vasprintf(char ** ret,const char * format,va_list args)57 inline int vasprintf(char **ret, const char *format, va_list args) {
58     va_list copy;
59     va_copy(copy, args);
60     *ret = nullptr;
61 
62     int count = vsnprintf(nullptr, 0, format, args);
63     if (count >= 0) {
64         char *buffer = static_cast<char *>(malloc((std::size_t)count + 1));
65         if (buffer == nullptr) {
66             count = -1;
67         } else if ((count = vsnprintf(buffer, static_cast<std::size_t>(count + 1), format, copy)) <
68                    0) {
69             free(buffer);
70         } else {
71             *ret = buffer;
72         }
73     }
74     va_end(copy);  // Each va_start() or va_copy() needs a va_end()
75 
76     return count;
77 }
78 
asprintf(char ** strp,const char * fmt,...)79 inline int asprintf(char **strp, const char *fmt, ...) {
80     va_list ap;
81     va_start(ap, fmt);
82     int r = vasprintf(strp, fmt, ap);
83     va_end(ap);
84     return r;
85 }
86 
gettimeofday(struct timeval * tp,struct timezone *)87 inline int gettimeofday(struct timeval *tp, struct timezone *) {
88     // Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing
89     // zero's
90     // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
91     // until 00:00:00 January 1, 1970
92     static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
93 
94     SYSTEMTIME system_time;
95     FILETIME file_time;
96     uint64_t time;
97 
98     GetSystemTime(&system_time);
99     SystemTimeToFileTime(&system_time, &file_time);
100     time = ((uint64_t)file_time.dwLowDateTime);
101     time += ((uint64_t)file_time.dwHighDateTime) << 32;
102 
103     tp->tv_sec = (long)((time - EPOCH) / 10000000L);
104     tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
105     return 0;
106 }
107 #endif  // _WIN32
108 
109 #endif  // CONSCRYPT_SRC_MAIN_NATIVE_COMPAT_H_
110