ffi-hydrogen 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* \file modp_b16.h
|
|
3
|
+
* \brief High performance encoding and decoding of base 16
|
|
4
|
+
* (hexadecimal 0-9, A-F)
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* <PRE>
|
|
10
|
+
* MODP_B16 -- High performance base16 (hex) Encoder/Decoder
|
|
11
|
+
* https://github.com/client9/stringencoders
|
|
12
|
+
*
|
|
13
|
+
* Copyright © 2005-2016, Nick Galbreath
|
|
14
|
+
* All rights reserved.
|
|
15
|
+
*
|
|
16
|
+
* Released under MIT license. See LICENSE for details.
|
|
17
|
+
* </PRE>
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
#ifndef COM_MODP_STRINGENCODERS_B16
|
|
22
|
+
#define COM_MODP_STRINGENCODERS_B16
|
|
23
|
+
|
|
24
|
+
#include "extern_c_begin.h"
|
|
25
|
+
#include "modp_stdint.h"
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* encode a string into hex (base 16, 0-9,a-f)
|
|
29
|
+
*
|
|
30
|
+
* \param[out] dest the output string. Must have at least modp_b16_encode_len
|
|
31
|
+
* bytes allocated
|
|
32
|
+
* \param[in] str the input string
|
|
33
|
+
* \param[in] len of the input string
|
|
34
|
+
* \return strlen of dest
|
|
35
|
+
*/
|
|
36
|
+
size_t modp_b16_encode(char* dest, const char* str, size_t len);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Decode a hex-encoded string.
|
|
40
|
+
*
|
|
41
|
+
* \param[out] dest output, must have at least modp_b16_decode_len bytes allocated,
|
|
42
|
+
* input must be a multiple of 2, and be different than the source buffer.
|
|
43
|
+
* \param[in] src the hex encoded source
|
|
44
|
+
* \param[in] len the length of the source
|
|
45
|
+
* \return the length of the the output, or -1 if an error
|
|
46
|
+
*/
|
|
47
|
+
size_t modp_b16_decode(char* dest, const char* src, size_t len);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Encode length.
|
|
51
|
+
* 2 x the length of A, round up the next high multiple of 2
|
|
52
|
+
* +1 for null byte added
|
|
53
|
+
*/
|
|
54
|
+
#define modp_b16_encode_len(A) (2 * A + 1)
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Encode string length
|
|
58
|
+
*/
|
|
59
|
+
#define modp_b16_encode_strlen(A) (2 * A)
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Decode string length
|
|
63
|
+
*/
|
|
64
|
+
#define modp_b16_decode_len(A) ((A + 1) / 2)
|
|
65
|
+
|
|
66
|
+
#include "extern_c_end.h"
|
|
67
|
+
|
|
68
|
+
#ifdef __cplusplus
|
|
69
|
+
#include <cstring>
|
|
70
|
+
#include <string>
|
|
71
|
+
|
|
72
|
+
namespace modp {
|
|
73
|
+
|
|
74
|
+
inline std::string b16_encode(const char* s, size_t len)
|
|
75
|
+
{
|
|
76
|
+
std::string x(modp_b16_encode_len(len), '\0');
|
|
77
|
+
size_t d = modp_b16_encode(const_cast<char*>(x.data()), s, len);
|
|
78
|
+
if (d == (size_t)-1) {
|
|
79
|
+
x.clear();
|
|
80
|
+
} else {
|
|
81
|
+
x.erase(d, std::string::npos);
|
|
82
|
+
}
|
|
83
|
+
return x;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
inline std::string b16_encode(const char* s)
|
|
87
|
+
{
|
|
88
|
+
return b16_encode(s, strlen(s));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
inline std::string b16_encode(const std::string& s)
|
|
92
|
+
{
|
|
93
|
+
return b16_encode(s.data(), s.size());
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* hex encode a string (self-modified)
|
|
98
|
+
* \param[in,out] s the input string to be encoded
|
|
99
|
+
* \return a reference to the input string.
|
|
100
|
+
*/
|
|
101
|
+
inline std::string& b16_encode(std::string& s)
|
|
102
|
+
{
|
|
103
|
+
std::string x(b16_encode(s.data(), s.size()));
|
|
104
|
+
s.swap(x);
|
|
105
|
+
return s;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
inline std::string b16_decode(const char* s, size_t len)
|
|
109
|
+
{
|
|
110
|
+
std::string x(len / 2 + 1, '\0');
|
|
111
|
+
size_t d = modp_b16_decode(const_cast<char*>(x.data()), s, len);
|
|
112
|
+
if (d == (size_t)-1) {
|
|
113
|
+
x.clear();
|
|
114
|
+
} else {
|
|
115
|
+
x.erase(d, std::string::npos);
|
|
116
|
+
}
|
|
117
|
+
return x;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
inline std::string b16_decode(const char* s)
|
|
121
|
+
{
|
|
122
|
+
return b16_decode(s, strlen(s));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Decode a hex-encoded string. On error, input string is cleared.
|
|
127
|
+
* This function does not allocate memory.
|
|
128
|
+
*
|
|
129
|
+
* \param[in,out] s the input string
|
|
130
|
+
* \return a reference to the input string
|
|
131
|
+
*/
|
|
132
|
+
inline std::string& b16_decode(std::string& s)
|
|
133
|
+
{
|
|
134
|
+
std::string x(b16_decode(s.data(), s.size()));
|
|
135
|
+
s.swap(x);
|
|
136
|
+
return s;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
inline std::string b16_decode(const std::string& s)
|
|
140
|
+
{
|
|
141
|
+
return b16_decode(s.data(), s.size());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
} /* namespace modp */
|
|
145
|
+
|
|
146
|
+
#endif /* ifdef __cplusplus */
|
|
147
|
+
|
|
148
|
+
#endif /* ifndef modp_b16 */
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
static const uint8_t gsHexEncodeC1[256] = {
|
|
2
|
+
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
|
|
3
|
+
'0', '0', '0', '0', '0', '0', '1', '1', '1', '1',
|
|
4
|
+
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
|
|
5
|
+
'1', '1', '2', '2', '2', '2', '2', '2', '2', '2',
|
|
6
|
+
'2', '2', '2', '2', '2', '2', '2', '2', '3', '3',
|
|
7
|
+
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
|
|
8
|
+
'3', '3', '3', '3', '4', '4', '4', '4', '4', '4',
|
|
9
|
+
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
|
|
10
|
+
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
|
|
11
|
+
'5', '5', '5', '5', '5', '5', '6', '6', '6', '6',
|
|
12
|
+
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
|
|
13
|
+
'6', '6', '7', '7', '7', '7', '7', '7', '7', '7',
|
|
14
|
+
'7', '7', '7', '7', '7', '7', '7', '7', '8', '8',
|
|
15
|
+
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
|
|
16
|
+
'8', '8', '8', '8', '9', '9', '9', '9', '9', '9',
|
|
17
|
+
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
|
|
18
|
+
'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
|
|
19
|
+
'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B',
|
|
20
|
+
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
|
|
21
|
+
'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C',
|
|
22
|
+
'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'D', 'D',
|
|
23
|
+
'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D',
|
|
24
|
+
'D', 'D', 'D', 'D', 'E', 'E', 'E', 'E', 'E', 'E',
|
|
25
|
+
'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E',
|
|
26
|
+
'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F',
|
|
27
|
+
'F', 'F', 'F', 'F', 'F', 'F'
|
|
28
|
+
};
|
|
29
|
+
static const uint8_t gsHexEncodeC2[256] = {
|
|
30
|
+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
31
|
+
'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3',
|
|
32
|
+
'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
|
|
33
|
+
'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7',
|
|
34
|
+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1',
|
|
35
|
+
'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
|
|
36
|
+
'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5',
|
|
37
|
+
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
|
38
|
+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
39
|
+
'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3',
|
|
40
|
+
'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
|
|
41
|
+
'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7',
|
|
42
|
+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1',
|
|
43
|
+
'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
|
|
44
|
+
'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5',
|
|
45
|
+
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
|
46
|
+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
47
|
+
'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3',
|
|
48
|
+
'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
|
|
49
|
+
'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7',
|
|
50
|
+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1',
|
|
51
|
+
'2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
|
|
52
|
+
'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5',
|
|
53
|
+
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
|
|
54
|
+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
55
|
+
'A', 'B', 'C', 'D', 'E', 'F'
|
|
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 uint32_t gsHexDecodeD2[256] = {
|
|
82
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
83
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
84
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
85
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
86
|
+
0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 256, 256,
|
|
87
|
+
256, 256, 256, 256, 256, 160, 176, 192, 208, 224, 240, 256,
|
|
88
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
89
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
90
|
+
256, 160, 176, 192, 208, 224, 240, 256, 256, 256, 256, 256,
|
|
91
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
92
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
93
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
94
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
95
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
96
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
97
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
98
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
99
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
100
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
101
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
102
|
+
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
|
103
|
+
256, 256, 256, 256
|
|
104
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
#include "arraytoc.h"
|
|
2
|
+
#include <stdio.h>
|
|
3
|
+
|
|
4
|
+
static void hexencodemap_char(void)
|
|
5
|
+
{
|
|
6
|
+
static const uint8_t sHexChars[] = "0123456789ABCDEF";
|
|
7
|
+
int i;
|
|
8
|
+
uint8_t e1[256];
|
|
9
|
+
uint8_t e2[256];
|
|
10
|
+
|
|
11
|
+
for (i = 0; i < 256; ++i) {
|
|
12
|
+
e1[i] = 0;
|
|
13
|
+
e2[i] = 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
for (i = 0; i < 256; ++i) {
|
|
17
|
+
e1[i] = sHexChars[i >> 4];
|
|
18
|
+
e2[i] = sHexChars[i & 0x0f];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
char_array_to_c((char*)e1, sizeof(e1), "gsHexEncodeC1");
|
|
22
|
+
char_array_to_c((char*)e2, sizeof(e2), "gsHexEncodeC2");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/* exact same thing as one used on urlencode */
|
|
26
|
+
static void hexdecodemap(void)
|
|
27
|
+
{
|
|
28
|
+
uint32_t i;
|
|
29
|
+
uint32_t map1[256];
|
|
30
|
+
uint32_t map2[256];
|
|
31
|
+
for (i = 0; i < 256; ++i) {
|
|
32
|
+
map1[i] = 256;
|
|
33
|
+
map2[i] = 256;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* digits */
|
|
37
|
+
for (i = '0'; i <= '9'; ++i) {
|
|
38
|
+
map1[i] = i - '0';
|
|
39
|
+
map2[i] = map1[i] << 4;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/* upper */
|
|
43
|
+
for (i = 'A'; i <= 'F'; ++i) {
|
|
44
|
+
map1[i] = i - 'A' + 10;
|
|
45
|
+
map2[i] = map1[i] << 4;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/* lower */
|
|
49
|
+
for (i = 'a'; i <= 'f'; ++i) {
|
|
50
|
+
map1[i] = i - 'a' + 10;
|
|
51
|
+
map2[i] = map1[i] << 4;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
uint32_array_to_c(map1, sizeof(map1) / sizeof(uint32_t), "gsHexDecodeMap");
|
|
55
|
+
|
|
56
|
+
uint32_array_to_c(map2, sizeof(map2) / sizeof(uint32_t), "gsHexDecodeD2");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
int main(void)
|
|
60
|
+
{
|
|
61
|
+
hexencodemap_char();
|
|
62
|
+
|
|
63
|
+
hexdecodemap();
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
|
|
2
|
+
/* vi: set expandtab shiftwidth=4 tabstop=4: */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* \file modp_b2.c
|
|
6
|
+
* <PRE>
|
|
7
|
+
* MODP_B2 - Ascii Binary string encode/decode
|
|
8
|
+
* https://github.com/client9/stringencoders
|
|
9
|
+
*
|
|
10
|
+
* Copyright © 2005-2016 Nick Galbreath
|
|
11
|
+
* All rights reserved.
|
|
12
|
+
*
|
|
13
|
+
* Released under MIT license. See LICENSE for details.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
#include "modp_b2.h"
|
|
17
|
+
#include "config.h"
|
|
18
|
+
#include "modp_b2_data.h"
|
|
19
|
+
#include "modp_stdint.h"
|
|
20
|
+
#include <string.h>
|
|
21
|
+
|
|
22
|
+
size_t modp_b2_encode(char* dest, const char* str, size_t len)
|
|
23
|
+
{
|
|
24
|
+
const uint8_t* orig = (const uint8_t*)str;
|
|
25
|
+
#if 0
|
|
26
|
+
/* THIS IS A STANDARD VERSION */
|
|
27
|
+
static const uint8_t gsBinaryChars[] = "01";
|
|
28
|
+
int i,j;
|
|
29
|
+
for (i = 0; i < len; ++i) {
|
|
30
|
+
for (j = 0; j <= 7; ++j) {
|
|
31
|
+
*dest++ = gsBinaryChars[(orig[i] >> (7-j)) & 1];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
#else
|
|
35
|
+
/* THIS IS 10X FASTER */
|
|
36
|
+
size_t i;
|
|
37
|
+
for (i = 0; i < len; ++i) {
|
|
38
|
+
memcpy((void*)dest, modp_b2_encodemap[orig[i]], (size_t)8);
|
|
39
|
+
dest += 8;
|
|
40
|
+
}
|
|
41
|
+
#endif
|
|
42
|
+
*dest = '\0';
|
|
43
|
+
return len * 8;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
size_t modp_b2_decode(char* dest, const char* str, size_t len)
|
|
47
|
+
{
|
|
48
|
+
char d;
|
|
49
|
+
size_t i;
|
|
50
|
+
int j;
|
|
51
|
+
const size_t buckets = len / 8;
|
|
52
|
+
const size_t leftover = len % 8;
|
|
53
|
+
if (leftover != 0) {
|
|
54
|
+
return (size_t)-1;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (i = 0; i < buckets; ++i) {
|
|
58
|
+
d = 0;
|
|
59
|
+
for (j = 0; j <= 7; ++j) {
|
|
60
|
+
char c = *str++;
|
|
61
|
+
if (c == '1') {
|
|
62
|
+
d ^= (char)(1 << (7 - j));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
*dest++ = d;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return buckets;
|
|
69
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* \file modp_b2.h
|
|
3
|
+
* \brief Encode and decode of base 2 strings ("00001110" to 0x43)
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
* <PRE>
|
|
9
|
+
* MODP_B2 -- Base 2 (binary) encode/decoder
|
|
10
|
+
* https://github.com/client9/stringencoders
|
|
11
|
+
*
|
|
12
|
+
* Copyright © 2007-2016, Nick Galbreath
|
|
13
|
+
* All rights reserved.
|
|
14
|
+
*
|
|
15
|
+
* Released under MIT license. See LICENSE for details.
|
|
16
|
+
* </PRE>
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
#ifndef COM_MODP_STRINGENCODERS_B2
|
|
21
|
+
#define COM_MODP_STRINGENCODERS_B2
|
|
22
|
+
|
|
23
|
+
#include "extern_c_begin.h"
|
|
24
|
+
#include "modp_stdint.h"
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* encode a string into binary (base 2, '0' and '1')
|
|
28
|
+
*
|
|
29
|
+
* \param[out] dest the output string. Must have at least modp_b16_encode_len
|
|
30
|
+
* bytes allocated
|
|
31
|
+
* \param[in] str the input string
|
|
32
|
+
* \param[in] len of the input string
|
|
33
|
+
* \return strlen of dest
|
|
34
|
+
*/
|
|
35
|
+
size_t modp_b2_encode(char* dest, const char* str, size_t len);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Decode a hex-encoded string.
|
|
39
|
+
*
|
|
40
|
+
* \param[out] dest output, must have at least modp_b16_decode_len bytes allocated,
|
|
41
|
+
* input must be a multiple of 2, and be different than the source buffer.
|
|
42
|
+
* \param[in] src the hex encoded source
|
|
43
|
+
* \param[in] len the length of the source
|
|
44
|
+
* \return the length of the the output, or 0 if an error (input size not a multiple of 8)
|
|
45
|
+
*/
|
|
46
|
+
size_t modp_b2_decode(char* dest, const char* src, size_t len);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Encode length.
|
|
50
|
+
* 2 x the length of A, round up the next high multiple of 2
|
|
51
|
+
* +1 for null byte added
|
|
52
|
+
*/
|
|
53
|
+
#define modp_b2_encode_len(A) (8 * A + 1)
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Encode string length
|
|
57
|
+
*/
|
|
58
|
+
#define modp_b2_encode_strlen(A) (8 * A)
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Decode string length
|
|
62
|
+
*/
|
|
63
|
+
#define modp_b2_decode_len(A) ((A + 1) / 8)
|
|
64
|
+
|
|
65
|
+
#include "extern_c_end.h"
|
|
66
|
+
|
|
67
|
+
#ifdef __cplusplus
|
|
68
|
+
#include <string>
|
|
69
|
+
|
|
70
|
+
namespace modp {
|
|
71
|
+
/**
|
|
72
|
+
* hex encode a string (self-modified)
|
|
73
|
+
* \param[in,out] s the input string to be encoded
|
|
74
|
+
* \return a reference to the input string.
|
|
75
|
+
*/
|
|
76
|
+
inline std::string& b2_encode(std::string& s)
|
|
77
|
+
{
|
|
78
|
+
std::string x(modp_b2_encode_len(s.size()), '\0');
|
|
79
|
+
size_t d = modp_b2_encode(const_cast<char*>(x.data()), s.data(), s.size());
|
|
80
|
+
if (d == (size_t)-1) {
|
|
81
|
+
x.clear();
|
|
82
|
+
} else {
|
|
83
|
+
x.erase(d, std::string::npos);
|
|
84
|
+
}
|
|
85
|
+
s.swap(x);
|
|
86
|
+
return s;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
*
|
|
91
|
+
* \param[in] s original data source
|
|
92
|
+
* \return new b2 encoding string
|
|
93
|
+
*/
|
|
94
|
+
inline std::string b2_encode(const std::string& s)
|
|
95
|
+
{
|
|
96
|
+
std::string str(s);
|
|
97
|
+
b2_encode(str);
|
|
98
|
+
return str;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Decode a hex-encoded string. On error, input string is cleared.
|
|
103
|
+
* This function does not allocate memory.
|
|
104
|
+
*
|
|
105
|
+
* \param[in,out] s the input string
|
|
106
|
+
* \return a reference to the input string
|
|
107
|
+
*/
|
|
108
|
+
inline std::string& b2_decode(std::string& s)
|
|
109
|
+
{
|
|
110
|
+
size_t d = modp_b2_decode(const_cast<char*>(s.data()), s.data(), s.size());
|
|
111
|
+
if (d == (size_t)-1) {
|
|
112
|
+
s.clear();
|
|
113
|
+
} else {
|
|
114
|
+
s.erase(d, std::string::npos);
|
|
115
|
+
}
|
|
116
|
+
return s;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
inline std::string b2_decode(const std::string& s)
|
|
120
|
+
{
|
|
121
|
+
std::string x(s);
|
|
122
|
+
b2_decode(x);
|
|
123
|
+
return x;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
} /* namespace modp */
|
|
127
|
+
|
|
128
|
+
#endif /* cplusplus */
|
|
129
|
+
|
|
130
|
+
#endif /* ifndef modp_b2 */
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
static const char* modp_b2_encodemap[] = {
|
|
2
|
+
"00000000", "00000001", "00000010", "00000011", "00000100", "00000101",
|
|
3
|
+
"00000110", "00000111", "00001000", "00001001", "00001010", "00001011",
|
|
4
|
+
"00001100", "00001101", "00001110", "00001111", "00010000", "00010001",
|
|
5
|
+
"00010010", "00010011", "00010100", "00010101", "00010110", "00010111",
|
|
6
|
+
"00011000", "00011001", "00011010", "00011011", "00011100", "00011101",
|
|
7
|
+
"00011110", "00011111", "00100000", "00100001", "00100010", "00100011",
|
|
8
|
+
"00100100", "00100101", "00100110", "00100111", "00101000", "00101001",
|
|
9
|
+
"00101010", "00101011", "00101100", "00101101", "00101110", "00101111",
|
|
10
|
+
"00110000", "00110001", "00110010", "00110011", "00110100", "00110101",
|
|
11
|
+
"00110110", "00110111", "00111000", "00111001", "00111010", "00111011",
|
|
12
|
+
"00111100", "00111101", "00111110", "00111111", "01000000", "01000001",
|
|
13
|
+
"01000010", "01000011", "01000100", "01000101", "01000110", "01000111",
|
|
14
|
+
"01001000", "01001001", "01001010", "01001011", "01001100", "01001101",
|
|
15
|
+
"01001110", "01001111", "01010000", "01010001", "01010010", "01010011",
|
|
16
|
+
"01010100", "01010101", "01010110", "01010111", "01011000", "01011001",
|
|
17
|
+
"01011010", "01011011", "01011100", "01011101", "01011110", "01011111",
|
|
18
|
+
"01100000", "01100001", "01100010", "01100011", "01100100", "01100101",
|
|
19
|
+
"01100110", "01100111", "01101000", "01101001", "01101010", "01101011",
|
|
20
|
+
"01101100", "01101101", "01101110", "01101111", "01110000", "01110001",
|
|
21
|
+
"01110010", "01110011", "01110100", "01110101", "01110110", "01110111",
|
|
22
|
+
"01111000", "01111001", "01111010", "01111011", "01111100", "01111101",
|
|
23
|
+
"01111110", "01111111", "10000000", "10000001", "10000010", "10000011",
|
|
24
|
+
"10000100", "10000101", "10000110", "10000111", "10001000", "10001001",
|
|
25
|
+
"10001010", "10001011", "10001100", "10001101", "10001110", "10001111",
|
|
26
|
+
"10010000", "10010001", "10010010", "10010011", "10010100", "10010101",
|
|
27
|
+
"10010110", "10010111", "10011000", "10011001", "10011010", "10011011",
|
|
28
|
+
"10011100", "10011101", "10011110", "10011111", "10100000", "10100001",
|
|
29
|
+
"10100010", "10100011", "10100100", "10100101", "10100110", "10100111",
|
|
30
|
+
"10101000", "10101001", "10101010", "10101011", "10101100", "10101101",
|
|
31
|
+
"10101110", "10101111", "10110000", "10110001", "10110010", "10110011",
|
|
32
|
+
"10110100", "10110101", "10110110", "10110111", "10111000", "10111001",
|
|
33
|
+
"10111010", "10111011", "10111100", "10111101", "10111110", "10111111",
|
|
34
|
+
"11000000", "11000001", "11000010", "11000011", "11000100", "11000101",
|
|
35
|
+
"11000110", "11000111", "11001000", "11001001", "11001010", "11001011",
|
|
36
|
+
"11001100", "11001101", "11001110", "11001111", "11010000", "11010001",
|
|
37
|
+
"11010010", "11010011", "11010100", "11010101", "11010110", "11010111",
|
|
38
|
+
"11011000", "11011001", "11011010", "11011011", "11011100", "11011101",
|
|
39
|
+
"11011110", "11011111", "11100000", "11100001", "11100010", "11100011",
|
|
40
|
+
"11100100", "11100101", "11100110", "11100111", "11101000", "11101001",
|
|
41
|
+
"11101010", "11101011", "11101100", "11101101", "11101110", "11101111",
|
|
42
|
+
"11110000", "11110001", "11110010", "11110011", "11110100", "11110101",
|
|
43
|
+
"11110110", "11110111", "11111000", "11111001", "11111010", "11111011",
|
|
44
|
+
"11111100", "11111101", "11111110", "11111111"};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#include "arraytoc.h"
|
|
2
|
+
#include <stdio.h>
|
|
3
|
+
|
|
4
|
+
/** \brief make map of a byte to a string of 8 chars
|
|
5
|
+
*
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
static void binary_encodemap(void)
|
|
9
|
+
{
|
|
10
|
+
static const uint8_t sBinaryChars[] = "01";
|
|
11
|
+
int i, j;
|
|
12
|
+
uint8_t buf[9];
|
|
13
|
+
|
|
14
|
+
printf("%s", "static const char* modp_b2_encodemap[] = {\n");
|
|
15
|
+
buf[8] = 0;
|
|
16
|
+
for (i = 0; i < 256; ++i) {
|
|
17
|
+
for (j = 0; j < 8; ++j) {
|
|
18
|
+
buf[j] = sBinaryChars[(i >> (7 - j)) & 1];
|
|
19
|
+
}
|
|
20
|
+
printf("\"%s\"", buf);
|
|
21
|
+
if (i != 255) {
|
|
22
|
+
printf("%s", ", ");
|
|
23
|
+
}
|
|
24
|
+
if ((i + 1) % 6 == 0) {
|
|
25
|
+
printf("%s", "\n");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
printf("%s", "};\n");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
int main(void)
|
|
32
|
+
{
|
|
33
|
+
binary_encodemap();
|
|
34
|
+
|
|
35
|
+
return 0;
|
|
36
|
+
}
|