Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
string_coding.h
Go to the documentation of this file.
1
// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
6
// * Redistributions of source code must retain the above copyright
7
// notice, this list of conditions and the following disclaimer.
8
// * Redistributions in binary form must reproduce the above copyright
9
// notice, this list of conditions and the following disclaimer in the
10
// documentation and/or other materials provided with the distribution.
11
// * Neither the name of the Andrey N. Sabelnikov nor the
12
// names of its contributors may be used to endorse or promote products
13
// derived from this software without specific prior written permission.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
//
26
27
28
#ifndef _STRING_CODING_H_
29
#define _STRING_CODING_H_
30
31
#include <string>
32
33
namespace
epee
34
{
35
namespace
string_encoding
36
{
37
inline
std::string
convert_to_ansii
(
const
std::wstring& str_from)
38
{
39
40
std::string
res
(str_from.begin(), str_from.end());
41
return
res
;
42
/*
43
std::string result;
44
std::locale loc;
45
for(unsigned int i= 0; i < str_from.size(); ++i)
46
{
47
result += std::use_facet<std::ctype<wchar_t> >(loc).narrow(str_from[i]);
48
}
49
return result;
50
*/
51
52
//return boost::lexical_cast<std::string>(str_from);
53
/*
54
std::string str_trgt;
55
if(!str_from.size())
56
return str_trgt;
57
int cb = ::WideCharToMultiByte( code_page, 0, str_from.data(), (__int32)str_from.size(), 0, 0, 0, 0 );
58
if(!cb)
59
return str_trgt;
60
str_trgt.resize(cb);
61
::WideCharToMultiByte( code_page, 0, str_from.data(), (int)str_from.size(),
62
(char*)str_trgt.data(), (int)str_trgt.size(), 0, 0);
63
return str_trgt;*/
64
}
65
66
inline
std::string
convert_to_ansii
(
const
std::string& str_from)
67
{
68
return
str_from;
69
}
70
71
inline
std::wstring
convert_to_unicode
(
const
std::string& str_from)
72
{
73
std::wstring result;
74
std::locale loc;
75
for
(
unsigned
int
i= 0; i < str_from.size(); ++i)
76
{
77
result += std::use_facet<std::ctype<wchar_t> >(loc).widen(str_from[i]);
78
}
79
return
result;
80
81
//return boost::lexical_cast<std::wstring>(str_from);
82
/*
83
std::wstring str_trgt;
84
if(!str_from.size())
85
return str_trgt;
86
87
int cb = ::MultiByteToWideChar( code_page, 0, str_from.data(), (int)str_from.size(), 0, 0 );
88
if(!cb)
89
return str_trgt;
90
91
str_trgt.resize(cb);
92
::MultiByteToWideChar( code_page, 0, str_from.data(),(int)str_from.size(),
93
(wchar_t*)str_trgt.data(),(int)str_trgt.size());
94
return str_trgt;*/
95
}
96
inline
std::wstring
convert_to_unicode
(
const
std::wstring& str_from)
97
{
98
return
str_from;
99
}
100
101
template
<
class
target_
string
>
102
inline
target_string
convert_to_t
(
const
std::wstring& str_from);
103
104
template
<>
105
inline
std::string
convert_to_t<std::string>
(
const
std::wstring& str_from)
106
{
107
return
convert_to_ansii
(str_from);
108
}
109
110
template
<>
111
inline
std::wstring
convert_to_t<std::wstring>
(
const
std::wstring& str_from)
112
{
113
return
str_from;
114
}
115
116
template
<
class
target_
string
>
117
inline
target_string
convert_to_t
(
const
std::string& str_from);
118
119
template
<>
120
inline
std::string
convert_to_t<std::string>
(
const
std::string& str_from)
121
{
122
return
str_from;
123
}
124
125
template
<>
126
inline
std::wstring
convert_to_t<std::wstring>
(
const
std::string& str_from)
127
{
128
return
convert_to_unicode
(str_from);
129
}
130
131
inline
132
std::string&
base64_chars
()
133
{
134
135
static
std::string chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
136
"abcdefghijklmnopqrstuvwxyz"
137
"0123456789+/"
;
138
139
return
chars;
140
141
}
142
143
inline
144
std::string
base64_encode
(
unsigned
char
const
* bytes_to_encode,
size_t
in_len) {
145
std::string ret;
146
int
i = 0;
147
int
j = 0;
148
unsigned
char
char_array_3[3];
149
unsigned
char
char_array_4[4];
150
151
while
(in_len--) {
152
char_array_3[i++] = *(bytes_to_encode++);
153
if
(i == 3) {
154
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
155
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
156
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
157
char_array_4[3] = char_array_3[2] & 0x3f;
158
159
for
(i = 0; (i <4) ; i++)
160
ret +=
base64_chars
()[char_array_4[i]];
161
i = 0;
162
}
163
}
164
165
if
(i)
166
{
167
for
(j = i; j < 3; j++)
168
char_array_3[j] =
'\0'
;
169
170
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
171
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
172
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
173
char_array_4[3] = char_array_3[2] & 0x3f;
174
175
for
(j = 0; (j < i + 1); j++)
176
ret +=
base64_chars
()[char_array_4[j]];
177
178
while
((i++ < 3))
179
ret +=
'='
;
180
181
}
182
183
return
ret;
184
185
}
186
187
inline
188
std::string
base64_encode
(
const
std::string& str)
189
{
190
return
base64_encode
((
unsigned
char
const
* )str.data(), str.size());
191
}
192
193
inline
bool
is_base64
(
unsigned
char
c) {
194
return
(isalnum(c) || (c ==
'+'
) || (c ==
'/'
));
195
}
196
197
198
inline
199
std::string
base64_decode
(std::string
const
& encoded_string) {
200
size_t
in_len = encoded_string.size();
201
size_t
i = 0;
202
size_t
j = 0;
203
size_t
in_ = 0;
204
unsigned
char
char_array_4[4], char_array_3[3];
205
std::string ret;
206
207
while
(in_len-- && ( encoded_string[in_] !=
'='
) &&
is_base64
(encoded_string[in_])) {
208
char_array_4[i++] = encoded_string[in_]; in_++;
209
if
(i ==4) {
210
for
(i = 0; i <4; i++)
211
char_array_4[i] = (
unsigned
char
)
base64_chars
().find(char_array_4[i]);
212
213
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
214
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
215
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
216
217
for
(i = 0; (i < 3); i++)
218
ret += char_array_3[i];
219
i = 0;
220
}
221
}
222
223
if
(i) {
224
for
(j = i; j <4; j++)
225
char_array_4[j] = 0;
226
227
for
(j = 0; j <4; j++)
228
char_array_4[j] = (
unsigned
char
)
base64_chars
().find(char_array_4[j]);
229
230
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
231
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
232
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
233
234
for
(j = 0; (j < i - 1); j++) ret += char_array_3[j];
235
}
236
237
return
ret;
238
}
239
240
}
241
}
242
243
#endif
//_STRING_CODING_H_
res
const char * res
Definition
hmac_keccak.cpp:41
epee::string_encoding
Definition
string_coding.h:36
epee::string_encoding::base64_chars
std::string & base64_chars()
Definition
string_coding.h:132
epee::string_encoding::convert_to_unicode
std::wstring convert_to_unicode(const std::string &str_from)
Definition
string_coding.h:71
epee::string_encoding::is_base64
bool is_base64(unsigned char c)
Definition
string_coding.h:193
epee::string_encoding::convert_to_t< std::wstring >
std::wstring convert_to_t< std::wstring >(const std::wstring &str_from)
Definition
string_coding.h:111
epee::string_encoding::convert_to_t
target_string convert_to_t(const std::wstring &str_from)
epee::string_encoding::base64_encode
std::string base64_encode(unsigned char const *bytes_to_encode, size_t in_len)
Definition
string_coding.h:144
epee::string_encoding::convert_to_t< std::string >
std::string convert_to_t< std::string >(const std::wstring &str_from)
Definition
string_coding.h:105
epee::string_encoding::convert_to_ansii
std::string convert_to_ansii(const std::wstring &str_from)
Definition
string_coding.h:37
epee::string_encoding::base64_decode
std::string base64_decode(std::string const &encoded_string)
Definition
string_coding.h:199
epee
Definition
ado_db_helper.h:67
contrib
epee
include
string_coding.h
Generated on
for Electroneum by
1.17.0