/
util.cpp
150 lines (124 loc) · 3.41 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "util.h"
#include <climits>
#include <cstdio>
#include "Enclave_t.h"
#include "sgx_lfence.h"
int printf(const char *fmt, ...) {
char buf[BUFSIZ] = {'\0'};
va_list ap;
va_start(ap, fmt);
int ret = vsnprintf(buf, BUFSIZ, fmt, ap);
va_end(ap);
ocall_print_string(buf);
return ret;
}
/** From https://stackoverflow.com/a/8362718 */
std::string string_format(const std::string &fmt, ...) {
int size=BUFSIZ;
std::string str;
va_list ap;
while (1) {
str.resize(size);
va_start(ap, fmt);
int n = vsnprintf(&str[0], size, fmt.c_str(), ap);
va_end(ap);
if (n > -1 && n < size)
return str;
if (n > -1)
size = n + 1;
else
size *= 2;
}
}
void exit(int exit_code) {
ocall_exit(exit_code);
}
void ocall_malloc(size_t size, uint8_t **ret) {
unsafe_ocall_malloc(size, ret);
// Guard against overwriting enclave memory
assert(sgx_is_outside_enclave(*ret, size) == 1);
sgx_lfence();
}
void print_bytes(uint8_t *ptr, uint32_t len) {
for (uint32_t i = 0; i < len; i++) {
printf("%u", *(ptr + i));
printf(" - ");
}
printf("\n");
}
int cmp(const uint8_t *value1, const uint8_t *value2, uint32_t len) {
for (uint32_t i = 0; i < len; i++) {
if (*(value1+i) != *(value2+i)) {
return -1;
}
}
return 0;
}
// basically a memset 0
void clear(uint8_t *dest, uint32_t len) {
for (uint32_t i = 0; i < len; i++) {
*(dest + i) = 0;
}
}
/// From http://git.musl-libc.org/cgit/musl/tree/src/time/__secs_to_tm.c?h=v0.9.15
/* 2000-03-01 (mod 400 year, immediately after feb29 */
#define LEAPOCH (946684800LL + 86400*(31+29))
#define DAYS_PER_400Y (365*400 + 97)
#define DAYS_PER_100Y (365*100 + 24)
#define DAYS_PER_4Y (365*4 + 1)
int secs_to_tm(long long t, struct tm *tm) {
long long days, secs;
int remdays, remsecs, remyears;
int qc_cycles, c_cycles, q_cycles;
int years, months;
int wday, yday, leap;
static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29};
/* Reject time_t values whose year would overflow int */
if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL)
return -1;
secs = t - LEAPOCH;
days = secs / 86400;
remsecs = secs % 86400;
if (remsecs < 0) {
remsecs += 86400;
days--;
}
wday = (3+days)%7;
if (wday < 0) wday += 7;
qc_cycles = days / DAYS_PER_400Y;
remdays = days % DAYS_PER_400Y;
if (remdays < 0) {
remdays += DAYS_PER_400Y;
qc_cycles--;
}
c_cycles = remdays / DAYS_PER_100Y;
if (c_cycles == 4) c_cycles--;
remdays -= c_cycles * DAYS_PER_100Y;
q_cycles = remdays / DAYS_PER_4Y;
if (q_cycles == 25) q_cycles--;
remdays -= q_cycles * DAYS_PER_4Y;
remyears = remdays / 365;
if (remyears == 4) remyears--;
remdays -= remyears * 365;
leap = !remyears && (q_cycles || !c_cycles);
yday = remdays + 31 + 28 + leap;
if (yday >= 365+leap) yday -= 365+leap;
years = remyears + 4*q_cycles + 100*c_cycles + 400*qc_cycles;
for (months=0; days_in_month[months] <= remdays; months++)
remdays -= days_in_month[months];
if (years+100 > INT_MAX || years+100 < INT_MIN)
return -1;
tm->tm_year = years + 100;
tm->tm_mon = months + 2;
if (tm->tm_mon >= 12) {
tm->tm_mon -=12;
tm->tm_year++;
}
tm->tm_mday = remdays + 1;
tm->tm_wday = wday;
tm->tm_yday = yday;
tm->tm_hour = remsecs / 3600;
tm->tm_min = remsecs / 60 % 60;
tm->tm_sec = remsecs % 60;
return 0;
}