cgv
Loading...
Searching...
No Matches
convert.cxx
1#include "convert.h"
2#ifdef _WIN32
3#include <windows.h>
4#include <Winnls.h>
5#endif
6#include <iostream>
7
8namespace cgv {
9 namespace utils {
10
11std::wstring str2wstr(const std::string& s)
12{
13 std::wstring ws;
14 ws.resize(s.size());
15#ifdef _WIN32
16 if (!s.empty()) {
17 int n = MultiByteToWideChar(CP_UTF8,0,s.c_str(),(int)s.size(),&ws[0],(int)ws.size());
18 ws.resize(n);
19 }
20#else
21 std::cerr << "str2wstr(const std::string& s) not implemented" << std::endl;
22#endif
23 return ws;
24}
25
26std::string wstr2str(const std::wstring& ws)
27{
28 std::string s;
29 s.resize(4*ws.size());
30#ifdef _WIN32
31 if (!ws.empty()) {
32 // BOOL default_char_used;
33 char default_char = '#';
34 int n = WideCharToMultiByte(CP_UTF8,0,ws.c_str(), (int)ws.size(),&s[0],(int)s.size(),NULL/*&default_char*/, NULL/*&default_char_used*/);
35 if (n == 0) {
36 switch (GetLastError()) {
37 case ERROR_INSUFFICIENT_BUFFER : std::cerr << "A supplied buffer size was not large enough, or it was incorrectly set to NULL." << std::endl; break;
38 case ERROR_INVALID_FLAGS : std::cerr << "The values supplied for flags were not valid." << std::endl; break;
39 case ERROR_INVALID_PARAMETER : std::cerr << "Any of the parameter values was invalid." << std::endl; break;
40 case ERROR_NO_UNICODE_TRANSLATION : std::cerr << "Invalid Unicode was found in a string." << std::endl; break;
41 }
42 }
43 s.resize(n);
44 }
45#else
46 std::cerr << "wstr2str(const std::wstring& ws) not implemented" << std::endl;
47#endif
48 return s;
49}
50
51static const std::string base64_chars =
52"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
53"abcdefghijklmnopqrstuvwxyz"
54"0123456789+/";
55
56
57static inline bool is_base64(unsigned char c) {
58 return (isalnum(c) || (c == '+') || (c == '/'));
59}
60
61std::string encode_base64(const std::string& s) {
62 std::string ret;
63 int i = 0;
64 int j = 0;
65 unsigned char char_array_3[3];
66 unsigned char char_array_4[4];
67 const char* bytes_to_encode = s.c_str();
68 size_t in_len = s.length();
69 while (in_len--) {
70 char_array_3[i++] = *(bytes_to_encode++);
71 if (i == 3) {
72 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
73 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
74 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
75 char_array_4[3] = char_array_3[2] & 0x3f;
76
77 for (i = 0; (i < 4); i++)
78 ret += base64_chars[char_array_4[i]];
79 i = 0;
80 }
81 }
82
83 if (i)
84 {
85 for (j = i; j < 3; j++)
86 char_array_3[j] = '\0';
87
88 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
89 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
90 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
91 char_array_4[3] = char_array_3[2] & 0x3f;
92
93 for (j = 0; (j < i + 1); j++)
94 ret += base64_chars[char_array_4[j]];
95
96 while ((i++ < 3))
97 ret += '=';
98
99 }
100
101 return ret;
102
103}
104
105std::string decode_base64(std::string const& encoded_string) {
106 int in_len = (int)encoded_string.size();
107 int i = 0;
108 int j = 0;
109 int in_ = 0;
110 unsigned char char_array_4[4], char_array_3[3];
111 std::string ret;
112
113 while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
114 char_array_4[i++] = encoded_string[in_]; in_++;
115 if (i == 4) {
116 for (i = 0; i < 4; i++)
117 char_array_4[i] = static_cast<unsigned char>(base64_chars.find(char_array_4[i]));
118
119 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
120 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
121 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
122
123 for (i = 0; (i < 3); i++)
124 ret += char_array_3[i];
125 i = 0;
126 }
127 }
128
129 if (i) {
130 for (j = i; j < 4; j++)
131 char_array_4[j] = 0;
132
133 for (j = 0; j < 4; j++)
134 char_array_4[j] = static_cast<unsigned char>(base64_chars.find(char_array_4[j]));
135
136 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
137 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
138 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
139
140 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
141 }
142
143 return ret;
144}
145
146 }
147}
std::string encode_base64(const std::string &s)
encode a string into base64
Definition convert.cxx:61
std::string decode_base64(std::string const &encoded_string)
decode a base64 encoded string
Definition convert.cxx:105
std::wstring str2wstr(const std::string &s)
convert a 8-bit string to a 16-bit string
Definition convert.cxx:11
std::string wstr2str(const std::wstring &ws)
convert a 16-bit string to a 8-bit string
Definition convert.cxx:26
the cgv namespace
Definition print.h:11