Merge pull request #3769 from evincarofautumn/fix-verify-before-allocs
[mono.git] / mono / btls / btls-android-utils.c
1 // Copied from Chromium: https://src.chromium.org/svn/trunk/src/base/os_compat_android.cc
2
3 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
6
7 #if defined(__ANDROID__)
8
9 #include <asm/unistd.h>
10 #include <errno.h>
11 #include <math.h>
12 #include <sys/stat.h>
13 #include <sys/syscall.h>
14
15 #if !defined(__LP64__)
16 #include <time64.h>
17 #endif
18
19 #if !defined(__LP64__)
20 // 32-bit Android has only timegm64() and not timegm().
21 // We replicate the behaviour of timegm() when the result overflows time_t.
22 time_t timegm(struct tm* const t) {
23   // time_t is signed on Android.
24   static const time_t kTimeMax = ~(1L << (sizeof(time_t) * CHAR_BIT - 1));
25   static const time_t kTimeMin = (1L << (sizeof(time_t) * CHAR_BIT - 1));
26   time64_t result = timegm64(t);
27   if (result < kTimeMin || result > kTimeMax)
28     return -1;
29   return result;
30 }
31 #endif
32
33 #endif