ffi-hydrogen 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +30 -0
- data/.travis.yml +10 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +46 -0
- data/bench/both.rb +86 -0
- data/bench/encode.rb +57 -0
- data/bench/encrypt.rb +80 -0
- data/bench/init.rb +5 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ffi-hydrogen.gemspec +31 -0
- data/lib/ffi/hydrogen.rb +216 -0
- data/vendor/.clang-format +2 -0
- data/vendor/.gitignore +3 -0
- data/vendor/README.md +2 -0
- data/vendor/libhydrogen/.clang-format +95 -0
- data/vendor/libhydrogen/.gitignore +32 -0
- data/vendor/libhydrogen/.travis.yml +22 -0
- data/vendor/libhydrogen/LICENSE +18 -0
- data/vendor/libhydrogen/Makefile +61 -0
- data/vendor/libhydrogen/Makefile.arduino +51 -0
- data/vendor/libhydrogen/README.md +29 -0
- data/vendor/libhydrogen/hydrogen.c +18 -0
- data/vendor/libhydrogen/hydrogen.h +317 -0
- data/vendor/libhydrogen/impl/common.h +316 -0
- data/vendor/libhydrogen/impl/core.h +220 -0
- data/vendor/libhydrogen/impl/gimli-core/portable.h +39 -0
- data/vendor/libhydrogen/impl/gimli-core/sse2.h +97 -0
- data/vendor/libhydrogen/impl/gimli-core.h +25 -0
- data/vendor/libhydrogen/impl/hash.h +138 -0
- data/vendor/libhydrogen/impl/hydrogen_p.h +83 -0
- data/vendor/libhydrogen/impl/kdf.h +20 -0
- data/vendor/libhydrogen/impl/kx.h +441 -0
- data/vendor/libhydrogen/impl/pwhash.h +281 -0
- data/vendor/libhydrogen/impl/random.h +376 -0
- data/vendor/libhydrogen/impl/secretbox.h +236 -0
- data/vendor/libhydrogen/impl/sign.h +207 -0
- data/vendor/libhydrogen/impl/x25519.h +383 -0
- data/vendor/libhydrogen/library.properties +10 -0
- data/vendor/libhydrogen/logo.png +0 -0
- data/vendor/libhydrogen/tests/tests.c +431 -0
- data/vendor/main.c +140 -0
- data/vendor/stringencoders/.gitignore +25 -0
- data/vendor/stringencoders/.travis.yml +13 -0
- data/vendor/stringencoders/AUTHORS +1 -0
- data/vendor/stringencoders/COPYING +2 -0
- data/vendor/stringencoders/ChangeLog +170 -0
- data/vendor/stringencoders/Doxyfile +276 -0
- data/vendor/stringencoders/INSTALL +119 -0
- data/vendor/stringencoders/LICENSE +22 -0
- data/vendor/stringencoders/Makefile.am +3 -0
- data/vendor/stringencoders/NEWS +3 -0
- data/vendor/stringencoders/README +2 -0
- data/vendor/stringencoders/README.md +32 -0
- data/vendor/stringencoders/bootstrap.sh +3 -0
- data/vendor/stringencoders/configure-gcc-hardened.sh +16 -0
- data/vendor/stringencoders/configure.ac +44 -0
- data/vendor/stringencoders/doxy/footer.html +34 -0
- data/vendor/stringencoders/doxy/header.html +85 -0
- data/vendor/stringencoders/indent.sh +9 -0
- data/vendor/stringencoders/javascript/base64-speed.html +43 -0
- data/vendor/stringencoders/javascript/base64-test.html +209 -0
- data/vendor/stringencoders/javascript/base64.html +18 -0
- data/vendor/stringencoders/javascript/base64.js +176 -0
- data/vendor/stringencoders/javascript/qunit.css +119 -0
- data/vendor/stringencoders/javascript/qunit.js +1062 -0
- data/vendor/stringencoders/javascript/urlparse-test.html +367 -0
- data/vendor/stringencoders/javascript/urlparse.js +328 -0
- data/vendor/stringencoders/make-ci.sh +13 -0
- data/vendor/stringencoders/makerelease.sh +16 -0
- data/vendor/stringencoders/python/b85.py +176 -0
- data/vendor/stringencoders/src/Makefile.am +134 -0
- data/vendor/stringencoders/src/arraytoc.c +85 -0
- data/vendor/stringencoders/src/arraytoc.h +43 -0
- data/vendor/stringencoders/src/extern_c_begin.h +3 -0
- data/vendor/stringencoders/src/extern_c_end.h +3 -0
- data/vendor/stringencoders/src/html_named_entities_generator.py +203 -0
- data/vendor/stringencoders/src/modp_ascii.c +159 -0
- data/vendor/stringencoders/src/modp_ascii.h +162 -0
- data/vendor/stringencoders/src/modp_ascii_data.h +84 -0
- data/vendor/stringencoders/src/modp_ascii_gen.c +55 -0
- data/vendor/stringencoders/src/modp_b16.c +125 -0
- data/vendor/stringencoders/src/modp_b16.h +148 -0
- data/vendor/stringencoders/src/modp_b16_data.h +104 -0
- data/vendor/stringencoders/src/modp_b16_gen.c +65 -0
- data/vendor/stringencoders/src/modp_b2.c +69 -0
- data/vendor/stringencoders/src/modp_b2.h +130 -0
- data/vendor/stringencoders/src/modp_b2_data.h +44 -0
- data/vendor/stringencoders/src/modp_b2_gen.c +36 -0
- data/vendor/stringencoders/src/modp_b36.c +108 -0
- data/vendor/stringencoders/src/modp_b36.h +170 -0
- data/vendor/stringencoders/src/modp_b64.c +254 -0
- data/vendor/stringencoders/src/modp_b64.h +236 -0
- data/vendor/stringencoders/src/modp_b64_data.h +477 -0
- data/vendor/stringencoders/src/modp_b64_gen.c +168 -0
- data/vendor/stringencoders/src/modp_b64r.c +254 -0
- data/vendor/stringencoders/src/modp_b64r.h +242 -0
- data/vendor/stringencoders/src/modp_b64r_data.h +477 -0
- data/vendor/stringencoders/src/modp_b64w.c +254 -0
- data/vendor/stringencoders/src/modp_b64w.h +231 -0
- data/vendor/stringencoders/src/modp_b64w_data.h +477 -0
- data/vendor/stringencoders/src/modp_b85.c +109 -0
- data/vendor/stringencoders/src/modp_b85.h +171 -0
- data/vendor/stringencoders/src/modp_b85_data.h +36 -0
- data/vendor/stringencoders/src/modp_b85_gen.c +65 -0
- data/vendor/stringencoders/src/modp_bjavascript.c +65 -0
- data/vendor/stringencoders/src/modp_bjavascript.h +105 -0
- data/vendor/stringencoders/src/modp_bjavascript_data.h +84 -0
- data/vendor/stringencoders/src/modp_bjavascript_gen.c +58 -0
- data/vendor/stringencoders/src/modp_burl.c +228 -0
- data/vendor/stringencoders/src/modp_burl.h +259 -0
- data/vendor/stringencoders/src/modp_burl_data.h +136 -0
- data/vendor/stringencoders/src/modp_burl_gen.c +121 -0
- data/vendor/stringencoders/src/modp_html.c +128 -0
- data/vendor/stringencoders/src/modp_html.h +53 -0
- data/vendor/stringencoders/src/modp_html_named_entities.h +9910 -0
- data/vendor/stringencoders/src/modp_json.c +315 -0
- data/vendor/stringencoders/src/modp_json.h +103 -0
- data/vendor/stringencoders/src/modp_json_data.h +57 -0
- data/vendor/stringencoders/src/modp_json_gen.py +60 -0
- data/vendor/stringencoders/src/modp_mainpage.h +120 -0
- data/vendor/stringencoders/src/modp_numtoa.c +350 -0
- data/vendor/stringencoders/src/modp_numtoa.h +100 -0
- data/vendor/stringencoders/src/modp_qsiter.c +76 -0
- data/vendor/stringencoders/src/modp_qsiter.h +71 -0
- data/vendor/stringencoders/src/modp_stdint.h +43 -0
- data/vendor/stringencoders/src/modp_utf8.c +88 -0
- data/vendor/stringencoders/src/modp_utf8.h +38 -0
- data/vendor/stringencoders/src/modp_xml.c +311 -0
- data/vendor/stringencoders/src/modp_xml.h +166 -0
- data/vendor/stringencoders/src/stringencoders.pc +10 -0
- data/vendor/stringencoders/src/stringencoders.pc.in +10 -0
- data/vendor/stringencoders/test/Makefile.am +113 -0
- data/vendor/stringencoders/test/apr_base64.c +262 -0
- data/vendor/stringencoders/test/apr_base64.h +120 -0
- data/vendor/stringencoders/test/cxx_test.cc +482 -0
- data/vendor/stringencoders/test/minunit.h +82 -0
- data/vendor/stringencoders/test/modp_ascii_test.c +281 -0
- data/vendor/stringencoders/test/modp_b16_test.c +288 -0
- data/vendor/stringencoders/test/modp_b2_test.c +250 -0
- data/vendor/stringencoders/test/modp_b64_test.c +266 -0
- data/vendor/stringencoders/test/modp_b85_test.c +130 -0
- data/vendor/stringencoders/test/modp_bjavascript_test.c +137 -0
- data/vendor/stringencoders/test/modp_burl_test.c +423 -0
- data/vendor/stringencoders/test/modp_html_test.c +296 -0
- data/vendor/stringencoders/test/modp_json_test.c +336 -0
- data/vendor/stringencoders/test/modp_numtoa_test.c +545 -0
- data/vendor/stringencoders/test/modp_qsiter_test.c +280 -0
- data/vendor/stringencoders/test/modp_utf8_test.c +188 -0
- data/vendor/stringencoders/test/modp_xml_test.c +339 -0
- data/vendor/stringencoders/test/speedtest.c +241 -0
- data/vendor/stringencoders/test/speedtest_ascii.c +345 -0
- data/vendor/stringencoders/test/speedtest_msg.c +78 -0
- data/vendor/stringencoders/test/speedtest_numtoa.c +276 -0
- metadata +314 -0
@@ -0,0 +1,228 @@
|
|
1
|
+
/*
|
2
|
+
* <pre>
|
3
|
+
* BFASTURL.c High performance URL encoder/decoder
|
4
|
+
* https://github.com/client9/stringencoders
|
5
|
+
*
|
6
|
+
* Copyright © 2006-2016 Nick Galbreath
|
7
|
+
* All rights reserved.
|
8
|
+
* Released under MIT license. See LICENSE for details.
|
9
|
+
* </PRE>
|
10
|
+
*/
|
11
|
+
|
12
|
+
#include "modp_stdint.h"
|
13
|
+
|
14
|
+
#include "modp_burl.h"
|
15
|
+
#include "modp_burl_data.h"
|
16
|
+
|
17
|
+
size_t modp_burl_encode(char* dest, const char* src, size_t len)
|
18
|
+
{
|
19
|
+
|
20
|
+
const char* deststart = dest;
|
21
|
+
const uint8_t* s = (const uint8_t*)src;
|
22
|
+
const uint8_t* srcend = s + len;
|
23
|
+
char c;
|
24
|
+
uint8_t x;
|
25
|
+
|
26
|
+
if (len == 0) {
|
27
|
+
*dest = '\0';
|
28
|
+
return (size_t)0;
|
29
|
+
}
|
30
|
+
|
31
|
+
while (s < srcend) {
|
32
|
+
x = *s++;
|
33
|
+
c = (char)gsUrlEncodeMap[x];
|
34
|
+
if (c) {
|
35
|
+
*dest++ = c;
|
36
|
+
} else {
|
37
|
+
*dest++ = '%';
|
38
|
+
*dest++ = (char)gsHexEncodeMap1[x];
|
39
|
+
*dest++ = (char)gsHexEncodeMap2[x];
|
40
|
+
/*
|
41
|
+
is the equiv of this
|
42
|
+
static const char sHexChars[] = "0123456789ABCDEF";
|
43
|
+
*dest++ = (char)sHexChars[x >> 4];
|
44
|
+
*dest++ = (char)sHexChars[x & 0x0F];
|
45
|
+
*/
|
46
|
+
}
|
47
|
+
}
|
48
|
+
*dest = '\0';
|
49
|
+
return (size_t)(dest - deststart); /* compute "strlen" of dest. */
|
50
|
+
}
|
51
|
+
|
52
|
+
/**
|
53
|
+
* The implementation is identical except it uses a
|
54
|
+
* different array
|
55
|
+
*/
|
56
|
+
size_t modp_burl_min_encode(char* dest, const char* src, size_t len)
|
57
|
+
{
|
58
|
+
|
59
|
+
const char* deststart = dest;
|
60
|
+
const uint8_t* s = (const uint8_t*)src;
|
61
|
+
const uint8_t* srcend = s + len;
|
62
|
+
char c;
|
63
|
+
uint8_t x;
|
64
|
+
|
65
|
+
if (len == 0) {
|
66
|
+
*dest = '\0';
|
67
|
+
return (size_t)0;
|
68
|
+
}
|
69
|
+
|
70
|
+
while (s < srcend) {
|
71
|
+
x = *s++;
|
72
|
+
c = (char)(gsUrlEncodeMinMap[x]); /** CHANGE HERE **/
|
73
|
+
if (c) {
|
74
|
+
*dest++ = c;
|
75
|
+
} else {
|
76
|
+
*dest++ = '%';
|
77
|
+
*dest++ = (char)gsHexEncodeMap1[x];
|
78
|
+
*dest++ = (char)(gsHexEncodeMap2[x]);
|
79
|
+
/*
|
80
|
+
is the equiv of this
|
81
|
+
static const char sHexChars[] = "0123456789ABCDEF";
|
82
|
+
*dest++ = sHexChars[x >> 4];
|
83
|
+
*dest++ = sHexChars[x & 0x0F];
|
84
|
+
*/
|
85
|
+
}
|
86
|
+
}
|
87
|
+
*dest = '\0';
|
88
|
+
return (size_t)(dest - deststart); /* compute "strlen" of dest. */
|
89
|
+
}
|
90
|
+
|
91
|
+
/**
|
92
|
+
* Give exact size of encoded output string
|
93
|
+
* without doing the encoding
|
94
|
+
*/
|
95
|
+
size_t modp_burl_encode_strlen(const char* src, const size_t len)
|
96
|
+
{
|
97
|
+
size_t count = 0;
|
98
|
+
const char* srcend = src + len;
|
99
|
+
|
100
|
+
if (len == 0) {
|
101
|
+
return (size_t)0;
|
102
|
+
}
|
103
|
+
|
104
|
+
while (src < srcend) {
|
105
|
+
if (gsUrlEncodeMap[(uint8_t)*src++]) {
|
106
|
+
count++;
|
107
|
+
} else {
|
108
|
+
count += 3;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
return count;
|
112
|
+
}
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Give exact size of encoded output string
|
116
|
+
* without doing the encoding
|
117
|
+
*/
|
118
|
+
size_t modp_burl_min_encode_strlen(const char* src, const size_t len)
|
119
|
+
{
|
120
|
+
size_t count = 0;
|
121
|
+
const char* srcend = src + len;
|
122
|
+
|
123
|
+
if (len == 0) {
|
124
|
+
return (size_t)0;
|
125
|
+
}
|
126
|
+
|
127
|
+
while (src < srcend) {
|
128
|
+
if (gsUrlEncodeMinMap[(uint8_t)*src++]) {
|
129
|
+
count++;
|
130
|
+
} else {
|
131
|
+
count += 3;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
return count;
|
135
|
+
}
|
136
|
+
|
137
|
+
size_t modp_burl_decode(char* dest, const char* s, size_t len)
|
138
|
+
{
|
139
|
+
uint32_t d = 0; /* used for decoding %XX */
|
140
|
+
const uint8_t* src = (const uint8_t*)s;
|
141
|
+
const char* deststart = dest;
|
142
|
+
const uint8_t* srcend = (const uint8_t*)(src + len);
|
143
|
+
const uint8_t* srcendloop = (const uint8_t*)(srcend - 2);
|
144
|
+
|
145
|
+
if (len == 0) {
|
146
|
+
*dest = '\0';
|
147
|
+
return (size_t)0;
|
148
|
+
}
|
149
|
+
|
150
|
+
while (src < srcendloop) {
|
151
|
+
switch (*src) {
|
152
|
+
case '+':
|
153
|
+
*dest++ = ' ';
|
154
|
+
src++;
|
155
|
+
break;
|
156
|
+
case '%':
|
157
|
+
d = (gsHexDecodeMap[(uint32_t)(*(src + 1))] << 4) | gsHexDecodeMap[(uint32_t)(*(src + 2))];
|
158
|
+
if (d < 256) { /* if one of the hex chars is bad, d >= 256 */
|
159
|
+
*dest = (char)d;
|
160
|
+
dest++;
|
161
|
+
src += 3;
|
162
|
+
} else {
|
163
|
+
*dest++ = '%';
|
164
|
+
src++;
|
165
|
+
}
|
166
|
+
break;
|
167
|
+
default:
|
168
|
+
*dest++ = (char)*src++;
|
169
|
+
}
|
170
|
+
}
|
171
|
+
|
172
|
+
/* handle last two chars
|
173
|
+
* dont decode "%XX"
|
174
|
+
*/
|
175
|
+
while (src < srcend) {
|
176
|
+
switch (*src) {
|
177
|
+
case '+':
|
178
|
+
*dest++ = ' ';
|
179
|
+
src++;
|
180
|
+
break;
|
181
|
+
default:
|
182
|
+
*dest++ = (char)(*src++);
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
*dest = '\0';
|
187
|
+
return (size_t)(dest - deststart); /* compute "strlen" of dest. */
|
188
|
+
}
|
189
|
+
|
190
|
+
size_t modp_burl_decode_raw(char* dest, const char* s, size_t len)
|
191
|
+
{
|
192
|
+
uint32_t d = 0; /* used for decoding %XX */
|
193
|
+
const uint8_t* src = (const uint8_t*)s;
|
194
|
+
const char* deststart = dest;
|
195
|
+
const uint8_t* srcend = (const uint8_t*)(src + len);
|
196
|
+
const uint8_t* srcendloop = (const uint8_t*)(srcend - 2);
|
197
|
+
|
198
|
+
if (len == 0) {
|
199
|
+
*dest = '\0';
|
200
|
+
return (size_t)0;
|
201
|
+
}
|
202
|
+
|
203
|
+
while (src < srcendloop) {
|
204
|
+
if (*src == '%') {
|
205
|
+
d = (gsHexDecodeMap[(uint32_t)(*(src + 1))] << 4) | gsHexDecodeMap[(uint32_t)(*(src + 2))];
|
206
|
+
if (d < 256) { /* if one of the hex chars is bad, d >= 256 */
|
207
|
+
*dest = (char)d;
|
208
|
+
dest++;
|
209
|
+
src += 3;
|
210
|
+
} else {
|
211
|
+
*dest++ = '%';
|
212
|
+
src++;
|
213
|
+
}
|
214
|
+
} else {
|
215
|
+
*dest++ = (char)*src++;
|
216
|
+
}
|
217
|
+
}
|
218
|
+
|
219
|
+
/* handle last two chars
|
220
|
+
* dont decode "%XX"
|
221
|
+
*/
|
222
|
+
while (src < srcend) {
|
223
|
+
*dest++ = (char)(*src++);
|
224
|
+
}
|
225
|
+
|
226
|
+
*dest = '\0';
|
227
|
+
return (size_t)(dest - deststart); /* compute "strlen" of dest. */
|
228
|
+
}
|
@@ -0,0 +1,259 @@
|
|
1
|
+
/**
|
2
|
+
* \file modp_burl.h
|
3
|
+
* \brief high performance URL encoding and decoding
|
4
|
+
*
|
5
|
+
*/
|
6
|
+
|
7
|
+
/*
|
8
|
+
* <PRE>
|
9
|
+
* High Performance URL Encoder/Decoder
|
10
|
+
*
|
11
|
+
* Copyright © 2006-2016 Nick Galbreath
|
12
|
+
* All rights reserved.
|
13
|
+
*
|
14
|
+
* https://github.com/client9/stringencoders
|
15
|
+
*
|
16
|
+
* Released under MIT license. See LICENSE for details.
|
17
|
+
* </PRE>
|
18
|
+
*/
|
19
|
+
|
20
|
+
#ifndef COM_MODP_STRINGENCODERS_BURL
|
21
|
+
#define COM_MODP_STRINGENCODERS_BURL
|
22
|
+
|
23
|
+
/* size_t declaration */
|
24
|
+
|
25
|
+
#include <stddef.h>
|
26
|
+
|
27
|
+
#ifdef __cplusplus
|
28
|
+
#ifndef MODP_C_BEGIN_DECLS
|
29
|
+
#define MODP_C_BEGIN_DECLS extern "C" {
|
30
|
+
#define MODP_C_END_DECLS }
|
31
|
+
#endif
|
32
|
+
#else
|
33
|
+
#define MODP_C_BEGIN_DECLS
|
34
|
+
#define MODP_C_END_DECLS
|
35
|
+
#endif
|
36
|
+
|
37
|
+
MODP_C_BEGIN_DECLS
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Url encode a string. This uses a very strict definition of url
|
41
|
+
* encoding. The only characters NOT encoded are A-Z, a-z, 0-9, "-",
|
42
|
+
* "_", ".", along with the space char getting mapped to "+".
|
43
|
+
* Everything else is escaped using "%HEXHEX" format. This is
|
44
|
+
* identical to the implementation of php's urlencode and nearly
|
45
|
+
* identical to Java's UrlEncoder class (they do not escape '*' for
|
46
|
+
* some reason).
|
47
|
+
*
|
48
|
+
* \param[out] dest output string. Must
|
49
|
+
* \param[in] str The input string
|
50
|
+
* \param[in] len The length of the input string, excluding any
|
51
|
+
* final null byte.
|
52
|
+
*/
|
53
|
+
size_t modp_burl_encode(char* dest, const char* str, size_t len);
|
54
|
+
|
55
|
+
/**
|
56
|
+
* Url encode a string. This uses a minimal definition of url
|
57
|
+
* encoding. This works similar to the previous function except '~',
|
58
|
+
* '!', '$', '\'', '(', ')', '*', ',', ';', ':', '@', '/', '?' are NOT
|
59
|
+
* escaped. This will allow decoding by standard url-decoders and
|
60
|
+
* make the encoded urls more readable.
|
61
|
+
*
|
62
|
+
* \param[out] dest output string. Must
|
63
|
+
* \param[in] str The input string
|
64
|
+
* \param[in] len The length of the input string, excluding any
|
65
|
+
* final null byte.
|
66
|
+
*/
|
67
|
+
size_t modp_burl_min_encode(char* dest, const char* str, size_t len);
|
68
|
+
|
69
|
+
/** \brief get size of output string w/o doing actual encoding
|
70
|
+
*
|
71
|
+
* \param[in] src input string, not null
|
72
|
+
* \param[in] len length of input string
|
73
|
+
* \return length of output string NOT including any final null byte
|
74
|
+
*/
|
75
|
+
size_t modp_burl_min_encode_strlen(const char* src, const size_t len);
|
76
|
+
|
77
|
+
/**
|
78
|
+
* Provides the maximum size for output string given
|
79
|
+
* and input size of A bytes.
|
80
|
+
*/
|
81
|
+
#define modp_burl_encode_len(A) (3 * A + 1)
|
82
|
+
|
83
|
+
/**
|
84
|
+
* Given the exact size of output string.
|
85
|
+
*
|
86
|
+
* Can be used to allocate the right amount of memory for
|
87
|
+
* modp_burl_encode. Be sure to add 1 byte for final null.
|
88
|
+
*
|
89
|
+
* This is somewhat expensive since it examines every character
|
90
|
+
* in the input string
|
91
|
+
*
|
92
|
+
* \param[in] str The input string
|
93
|
+
* \param[in] len THe length of the input string, excluding any
|
94
|
+
* final null byte (i.e. strlen(str))
|
95
|
+
* \return the size of the output string, excluding the final
|
96
|
+
* null byte.
|
97
|
+
*/
|
98
|
+
size_t modp_burl_encode_strlen(const char* str, const size_t len);
|
99
|
+
|
100
|
+
/**
|
101
|
+
* URL Decode a string
|
102
|
+
*
|
103
|
+
* \param[out] dest The output string. Must be at least (len + 1)
|
104
|
+
* bytes allocated. This may be the same as the input buffer.
|
105
|
+
* \param[in] str The input string that is URL encoded.
|
106
|
+
* \param[in] len The length of the input string (excluding final
|
107
|
+
* null byte)
|
108
|
+
* \return the strlen of the output string.
|
109
|
+
*/
|
110
|
+
size_t modp_burl_decode(char* dest, const char* str, size_t len);
|
111
|
+
|
112
|
+
/**
|
113
|
+
* URL Decode a string, '+' is preserved
|
114
|
+
*
|
115
|
+
* \param[out] dest The output string. Must be at least (len + 1)
|
116
|
+
* bytes allocated. This may be the same as the input buffer.
|
117
|
+
* \param[in] str The input string that is URL encoded.
|
118
|
+
* \param[in] len The length of the input string (excluding final
|
119
|
+
* null byte)
|
120
|
+
* \return the strlen of the output string.
|
121
|
+
*/
|
122
|
+
size_t modp_burl_decode_raw(char* dest, const char* str, size_t len);
|
123
|
+
|
124
|
+
/**
|
125
|
+
* Returns memory required to decoded a url-encoded
|
126
|
+
* string of length A.
|
127
|
+
*
|
128
|
+
*/
|
129
|
+
#define modp_burl_decode_len(A) (A + 1)
|
130
|
+
|
131
|
+
MODP_C_END_DECLS
|
132
|
+
|
133
|
+
#ifdef __cplusplus
|
134
|
+
#include <cstring>
|
135
|
+
#include <string>
|
136
|
+
|
137
|
+
namespace modp {
|
138
|
+
|
139
|
+
inline std::string url_encode(const char* s, size_t len)
|
140
|
+
{
|
141
|
+
std::string x(modp_burl_encode_len(len), '\0');
|
142
|
+
size_t d = modp_burl_encode(const_cast<char*>(x.data()), s, len);
|
143
|
+
x.erase(d, std::string::npos);
|
144
|
+
return x;
|
145
|
+
}
|
146
|
+
|
147
|
+
inline std::string url_encode(const char* s)
|
148
|
+
{
|
149
|
+
return url_encode(s, strlen(s));
|
150
|
+
}
|
151
|
+
|
152
|
+
inline std::string url_encode(const std::string& s)
|
153
|
+
{
|
154
|
+
return url_encode(s.data(), s.size());
|
155
|
+
}
|
156
|
+
|
157
|
+
/**
|
158
|
+
* Standard (maximal) url encoding.
|
159
|
+
*
|
160
|
+
* \param[in,out] s the string to be encoded
|
161
|
+
* \return a reference to the input string
|
162
|
+
*/
|
163
|
+
inline std::string& url_encode(std::string& s)
|
164
|
+
{
|
165
|
+
std::string x(url_encode(s.data(), s.size()));
|
166
|
+
s.swap(x);
|
167
|
+
return s;
|
168
|
+
}
|
169
|
+
|
170
|
+
/**
|
171
|
+
* Minimal Url Encoding
|
172
|
+
*
|
173
|
+
* \param[in,out] s the string to be encoded
|
174
|
+
* \return a reference to the input string
|
175
|
+
*/
|
176
|
+
inline std::string& url_min_encode(std::string& s)
|
177
|
+
{
|
178
|
+
std::string x(modp_burl_encode_len(s.size()), '\0');
|
179
|
+
size_t d = modp_burl_min_encode(const_cast<char*>(x.data()), s.data(), s.size());
|
180
|
+
x.erase(d, std::string::npos);
|
181
|
+
s.swap(x);
|
182
|
+
return s;
|
183
|
+
}
|
184
|
+
|
185
|
+
inline std::string url_min_encode(const std::string& s)
|
186
|
+
{
|
187
|
+
std::string x(modp_burl_encode_len(s.size()), '\0');
|
188
|
+
size_t d = modp_burl_min_encode(const_cast<char*>(x.data()), s.data(), s.size());
|
189
|
+
x.erase(d, std::string::npos);
|
190
|
+
return x;
|
191
|
+
}
|
192
|
+
|
193
|
+
/**
|
194
|
+
* Url decode a string.
|
195
|
+
* This function does not allocate memory.
|
196
|
+
*
|
197
|
+
* \param[in,out] s the string to be decoded
|
198
|
+
* \return a reference to the input string.
|
199
|
+
* There is no error case, bad characters are passed through
|
200
|
+
*/
|
201
|
+
inline std::string& url_decode(std::string& s)
|
202
|
+
{
|
203
|
+
size_t d = modp_burl_decode(const_cast<char*>(s.data()), s.data(), s.size());
|
204
|
+
s.erase(d, std::string::npos);
|
205
|
+
return s;
|
206
|
+
}
|
207
|
+
|
208
|
+
inline std::string& url_decode_raw(std::string& s)
|
209
|
+
{
|
210
|
+
size_t d = modp_burl_decode_raw(const_cast<char*>(s.data()), s.data(), s.size());
|
211
|
+
s.erase(d, std::string::npos);
|
212
|
+
return s;
|
213
|
+
}
|
214
|
+
|
215
|
+
inline std::string url_decode(const char* str)
|
216
|
+
{
|
217
|
+
std::string s(str);
|
218
|
+
url_decode(s);
|
219
|
+
return s;
|
220
|
+
}
|
221
|
+
|
222
|
+
inline std::string url_decode_raw(const char* str)
|
223
|
+
{
|
224
|
+
std::string s(str);
|
225
|
+
url_decode_raw(s);
|
226
|
+
return s;
|
227
|
+
}
|
228
|
+
|
229
|
+
inline std::string url_decode(const char* str, size_t len)
|
230
|
+
{
|
231
|
+
std::string s(str, len);
|
232
|
+
url_decode(s);
|
233
|
+
return s;
|
234
|
+
}
|
235
|
+
|
236
|
+
inline std::string url_decode_raw(const char* str, size_t len)
|
237
|
+
{
|
238
|
+
std::string s(str, len);
|
239
|
+
url_decode_raw(s);
|
240
|
+
return s;
|
241
|
+
}
|
242
|
+
|
243
|
+
inline std::string url_decode(const std::string& s)
|
244
|
+
{
|
245
|
+
std::string x(s);
|
246
|
+
url_decode(x);
|
247
|
+
return x;
|
248
|
+
}
|
249
|
+
|
250
|
+
inline std::string url_decode_raw(const std::string& s)
|
251
|
+
{
|
252
|
+
std::string x(s);
|
253
|
+
url_decode_raw(x);
|
254
|
+
return x;
|
255
|
+
}
|
256
|
+
}
|
257
|
+
#endif
|
258
|
+
|
259
|
+
#endif
|
@@ -0,0 +1,136 @@
|
|
1
|
+
static const uint8_t gsUrlEncodeMap[256] = {
|
2
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
3
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
4
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
5
|
+
'\0', '\0', '+', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
6
|
+
'\0', '\0', '\0', '\0', '\0', '-', '.', '\0', '0', '1',
|
7
|
+
'2', '3', '4', '5', '6', '7', '8', '9', '\0', '\0',
|
8
|
+
'\0', '\0', '\0', '\0', '\0', 'A', 'B', 'C', 'D', 'E',
|
9
|
+
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
10
|
+
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
|
11
|
+
'Z', '\0', '\0', '\0', '\0', '_', '\0', 'a', 'b', 'c',
|
12
|
+
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
13
|
+
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
|
14
|
+
'x', 'y', 'z', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
15
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
16
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
17
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
18
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
19
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
20
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
21
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
22
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
23
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
24
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
25
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
26
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
27
|
+
'\0', '\0', '\0', '\0', '\0', '\0'
|
28
|
+
};
|
29
|
+
static const uint8_t gsUrlEncodeMinMap[256] = {
|
30
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
31
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
32
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
33
|
+
'\0', '\0', '+', '!', '\0', '\0', '$', '\0', '\0', '\0',
|
34
|
+
'(', ')', '*', '\0', ',', '-', '.', '/', '0', '1',
|
35
|
+
'2', '3', '4', '5', '6', '7', '8', '9', ':', ';',
|
36
|
+
'\0', '\0', '\0', '?', '@', 'A', 'B', 'C', 'D', 'E',
|
37
|
+
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
38
|
+
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
|
39
|
+
'Z', '\0', '\0', '\0', '\0', '_', '\0', 'a', 'b', 'c',
|
40
|
+
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
41
|
+
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
|
42
|
+
'x', 'y', 'z', '\0', '\0', '\0', '~', '\0', '\0', '\0',
|
43
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
44
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
45
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
46
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
47
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
48
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
49
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
50
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
51
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
52
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
53
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
54
|
+
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
|
55
|
+
'\0', '\0', '\0', '\0', '\0', '\0'
|
56
|
+
};
|
57
|
+
static const uint32_t gsHexDecodeMap[256] = {
|
58
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
59
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
60
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
61
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
62
|
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 256, 256,
|
63
|
+
256, 256, 256, 256, 256, 10, 11, 12, 13, 14, 15, 256,
|
64
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
65
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
66
|
+
256, 10, 11, 12, 13, 14, 15, 256, 256, 256, 256, 256,
|
67
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
68
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
69
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
70
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
71
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
72
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
73
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
74
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
75
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
76
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
77
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
78
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
79
|
+
256, 256, 256, 256
|
80
|
+
};
|
81
|
+
static const uint8_t gsHexEncodeMap1[256] = {
|
82
|
+
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
|
83
|
+
'0', '0', '0', '0', '0', '0', '1', '1', '1', '1',
|
84
|
+
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
|
85
|
+
'1', '1', '2', '2', '2', '2', '2', '2', '2', '2',
|
86
|
+
'2', '2', '2', '2', '2', '2', '2', '2', '3', '3',
|
87
|
+
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
|
88
|
+
'3', '3', '3', '3', '4', '4', '4', '4', '4', '4',
|
89
|
+
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
|
90
|
+
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
|
91
|
+
'5', '5', '5', '5', '5', '5', '6', '6', '6', '6',
|
92
|
+
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
|
93
|
+
'6', '6', '7', '7', '7', '7', '7', '7', '7', '7',
|
94
|
+
'7', '7', '7', '7', '7', '7', '7', '7', '8', '8',
|
95
|
+
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
|
96
|
+
'8', '8', '8', '8', '9', '9', '9', '9', '9', '9',
|
97
|
+
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
|
98
|
+
'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
|
99
|
+
'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B',
|
100
|
+
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
|
101
|
+
'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C',
|
102
|
+
'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'D', 'D',
|
103
|
+
'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D',
|
104
|
+
'D', 'D', 'D', 'D', 'E', 'E', 'E', 'E', 'E', 'E',
|
105
|
+
'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E',
|
106
|
+
'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F',
|
107
|
+
'F', 'F', 'F', 'F', 'F', 'F'
|
108
|
+
};
|
109
|
+
static const uint8_t gsHexEncodeMap2[256] = {
|
110
|
+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
111
|
+
'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3',
|
112
|
+
'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
|
113
|
+
'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7',
|
114
|
+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1',
|
115
|
+
'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
|
116
|
+
'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5',
|
117
|
+
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
118
|
+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
119
|
+
'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3',
|
120
|
+
'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
|
121
|
+
'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7',
|
122
|
+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1',
|
123
|
+
'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
|
124
|
+
'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5',
|
125
|
+
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
126
|
+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
127
|
+
'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3',
|
128
|
+
'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
|
129
|
+
'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7',
|
130
|
+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1',
|
131
|
+
'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
|
132
|
+
'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5',
|
133
|
+
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
134
|
+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
135
|
+
'A', 'B', 'C', 'D', 'E', 'F'
|
136
|
+
};
|