-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.cpp
192 lines (166 loc) · 4.55 KB
/
utils.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (c) 2022 Novemus Band. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
#include "utils.h"
#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/opensslv.h>
#include <openssl/md5.h>
#include <vector>
#include <sstream>
namespace plexus { namespace utils {
template <bool no_nl, bool url_cvt>
std::string to_base64_impl(const void* data, size_t length)
{
BIO* b64 = BIO_new(BIO_f_base64());
BIO* out = BIO_new(BIO_s_mem());
if(no_nl)
{
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
}
out = BIO_push(b64, out);
BIO_write(out, data, (int) length);
(void) BIO_flush(out);
char* data_ptr(0);
size_t data_size = BIO_get_mem_data(out, &data_ptr);
if(url_cvt)
{
for(char* p=data_ptr, *end=(data_ptr+data_size); p!=end; ++p)
{
switch(*p)
{
case '/': *p = '_';
break;
case '+': *p = '-';
break;
}
}
}
std::string res(data_ptr, data_size);
BIO_free_all(out);
return res;
}
std::string to_base64(const void* data, size_t length)
{
return to_base64_impl<false,false>(data, length);
}
std::string to_base64_no_nl(const void* data, size_t length)
{
return to_base64_impl<true,false>(data, length);
}
std::string to_base64_url(const void* data, size_t length)
{
return to_base64_impl<true,true>(data,length);
}
template <bool no_nl, bool url_cvt>
std::string from_base64_impl(const char* data, size_t length)
{
BIO* b64 = BIO_new(BIO_f_base64());
BIO* out = BIO_new(BIO_s_mem());
BIO* in = BIO_new_mem_buf((void*) data, (int) length);
if(url_cvt)
{
char* data_ptr(0);
size_t data_size = BIO_get_mem_data(in, &data_ptr);
for(char* p=data_ptr, *end=(data_ptr+data_size); p!=end; ++p)
{
switch(*p)
{
case '_': *p = '/';
break;
case '-': *p = '+';
break;
}
}
}
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
if(!no_nl)
{
for (const char* ptr = data; ptr < data + length; ++ptr)
if (*ptr == '\r' || *ptr == '\n')
{
BIO_clear_flags(b64, BIO_FLAGS_BASE64_NO_NL);
break;
}
}
b64 = BIO_push(b64, in);
char inbuf[512];
int ret = 0;
while(true)
{
ret = BIO_read(b64, inbuf, 512);
if (ret <= 0)
break;
BIO_write(out, inbuf, ret);
}
char* data_ptr(0);
size_t data_size = BIO_get_mem_data(out, &data_ptr);
std::string res(data_ptr, data_size);
BIO_free(in);
BIO_free(out);
BIO_free(b64);
return res;
}
std::string from_base64(const char* data, size_t length)
{
return from_base64_impl<false,false>(data,length);
}
std::string from_base64_url(const char* data, size_t length)
{
return from_base64_impl<true,true>(data,length);
}
std::string format(const char* fmt, ...)
{
va_list args1;
va_start(args1, fmt);
va_list args2;
va_copy(args2, args1);
std::vector<char> buf(1 + std::vsnprintf(nullptr, 0, fmt, args1));
va_end(args1);
std::vsnprintf(buf.data(), buf.size(), fmt, args2);
va_end(args2);
return buf.data();
}
std::string format(const char* fmt, const boost::posix_time::ptime& time)
{
std::stringstream out;
out.imbue(std::locale(std::cout.getloc(), new boost::posix_time::time_facet(fmt)));
out << time;
return out.str();
}
std::string format(const char* fmt, const std::chrono::system_clock::time_point& time)
{
std::time_t tt = std::chrono::system_clock::to_time_t(time);
std::tm tm = *std::gmtime(&tt);
std::stringstream ss;
ss << std::put_time(&tm, fmt);
return ss.str();
}
std::string to_hexadecimal(const void* data, size_t len)
{
std::stringstream out;
for (size_t i = 0; i < len; ++i)
{
out << std::setw(2) << std::setfill('0') << std::hex << (int)((uint8_t*)data)[i];
}
return out.str();
}
std::string get_email_address(const std::string& email)
{
std::smatch match;
if (std::regex_search(email, match, std::regex("[\\w\\s]*\\<([^\\<]+@[^\\>]+)\\>\\s*")))
{
return match[1].str();
}
return email;
}
}}