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,159 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* modp_ascii.c
|
|
3
|
+
* <PRE>
|
|
4
|
+
* MODP_ASCII - Ascii transformations (upper/lower, etc)
|
|
5
|
+
* https://github.com/client9/stringencoders
|
|
6
|
+
*
|
|
7
|
+
* Copyright © 2007-16 Nick Galbreath -- nickg [at] client9 [dot] com
|
|
8
|
+
* MIT LICENSE
|
|
9
|
+
*
|
|
10
|
+
* </PRE>
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
#include "modp_ascii.h"
|
|
14
|
+
#include "modp_ascii_data.h"
|
|
15
|
+
#include "modp_stdint.h"
|
|
16
|
+
|
|
17
|
+
void modp_toupper_copy(char* dest, const char* str, size_t len)
|
|
18
|
+
{
|
|
19
|
+
size_t i;
|
|
20
|
+
uint32_t eax, ebx;
|
|
21
|
+
const uint8_t* ustr = (const uint8_t*)str;
|
|
22
|
+
const size_t leftover = len % 4;
|
|
23
|
+
const size_t imax = len / 4;
|
|
24
|
+
const uint32_t* s = (const uint32_t*)str;
|
|
25
|
+
uint32_t* d = (uint32_t*)dest;
|
|
26
|
+
for (i = 0; i != imax; ++i) {
|
|
27
|
+
eax = s[i];
|
|
28
|
+
/*
|
|
29
|
+
* This is based on the algorithm by Paul Hsieh
|
|
30
|
+
* http://www.azillionmonkeys.com/qed/asmexample.html
|
|
31
|
+
*/
|
|
32
|
+
ebx = (0x7f7f7f7fu & eax) + 0x05050505u;
|
|
33
|
+
ebx = (0x7f7f7f7fu & ebx) + 0x1a1a1a1au;
|
|
34
|
+
ebx = ((ebx & ~eax) >> 2) & 0x20202020u;
|
|
35
|
+
*d++ = eax - ebx;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
i = imax * 4;
|
|
39
|
+
dest = (char*)d;
|
|
40
|
+
switch (leftover) {
|
|
41
|
+
case 3:
|
|
42
|
+
*dest++ = (char)gsToUpperMap[ustr[i++]];
|
|
43
|
+
/* fall through */
|
|
44
|
+
case 2:
|
|
45
|
+
*dest++ = (char)gsToUpperMap[ustr[i++]];
|
|
46
|
+
/* fall through */
|
|
47
|
+
case 1:
|
|
48
|
+
*dest++ = (char)gsToUpperMap[ustr[i]];
|
|
49
|
+
/* fall through */
|
|
50
|
+
case 0:
|
|
51
|
+
*dest = '\0';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void modp_tolower_copy(char* dest, const char* str, size_t len)
|
|
56
|
+
{
|
|
57
|
+
size_t i;
|
|
58
|
+
uint32_t eax, ebx;
|
|
59
|
+
const uint8_t* ustr = (const uint8_t*)str;
|
|
60
|
+
const size_t leftover = len % 4;
|
|
61
|
+
const size_t imax = len / 4;
|
|
62
|
+
const uint32_t* s = (const uint32_t*)str;
|
|
63
|
+
uint32_t* d = (uint32_t*)dest;
|
|
64
|
+
for (i = 0; i != imax; ++i) {
|
|
65
|
+
eax = s[i];
|
|
66
|
+
/*
|
|
67
|
+
* This is based on the algorithm by Paul Hsieh
|
|
68
|
+
* http://www.azillionmonkeys.com/qed/asmexample.html
|
|
69
|
+
*/
|
|
70
|
+
ebx = (0x7f7f7f7fu & eax) + 0x25252525u;
|
|
71
|
+
ebx = (0x7f7f7f7fu & ebx) + 0x1a1a1a1au;
|
|
72
|
+
ebx = ((ebx & ~eax) >> 2) & 0x20202020u;
|
|
73
|
+
*d++ = eax + ebx;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
i = imax * 4;
|
|
77
|
+
dest = (char*)d;
|
|
78
|
+
switch (leftover) {
|
|
79
|
+
case 3:
|
|
80
|
+
*dest++ = (char)gsToLowerMap[ustr[i++]];
|
|
81
|
+
/* fall through */
|
|
82
|
+
case 2:
|
|
83
|
+
*dest++ = (char)gsToLowerMap[ustr[i++]];
|
|
84
|
+
/* fall through */
|
|
85
|
+
case 1:
|
|
86
|
+
*dest++ = (char)gsToLowerMap[ustr[i]];
|
|
87
|
+
/* fall through */
|
|
88
|
+
case 0:
|
|
89
|
+
*dest = '\0';
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
void modp_toupper(char* str, size_t len)
|
|
94
|
+
{
|
|
95
|
+
modp_toupper_copy(str, str, len);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
void modp_tolower(char* str, size_t len)
|
|
99
|
+
{
|
|
100
|
+
modp_tolower_copy(str, str, len);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
void modp_toprint_copy(char* dest, const char* str, size_t len)
|
|
104
|
+
{
|
|
105
|
+
size_t i;
|
|
106
|
+
uint8_t c1, c2, c3, c4;
|
|
107
|
+
|
|
108
|
+
const size_t leftover = len % 4;
|
|
109
|
+
const size_t imax = len - leftover;
|
|
110
|
+
const uint8_t* s = (const uint8_t*)str;
|
|
111
|
+
for (i = 0; i != imax; i += 4) {
|
|
112
|
+
/*
|
|
113
|
+
* it's important to make these variables
|
|
114
|
+
* it helps the optimizer to figure out what to do
|
|
115
|
+
*/
|
|
116
|
+
c1 = s[i];
|
|
117
|
+
c2 = s[i + 1];
|
|
118
|
+
c3 = s[i + 2];
|
|
119
|
+
c4 = s[i + 3];
|
|
120
|
+
dest[0] = (char)gsToPrintMap[c1];
|
|
121
|
+
dest[1] = (char)gsToPrintMap[c2];
|
|
122
|
+
dest[2] = (char)gsToPrintMap[c3];
|
|
123
|
+
dest[3] = (char)gsToPrintMap[c4];
|
|
124
|
+
dest += 4;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
switch (leftover) {
|
|
128
|
+
case 3:
|
|
129
|
+
*dest++ = (char)gsToPrintMap[s[i++]];
|
|
130
|
+
/* fall through */
|
|
131
|
+
case 2:
|
|
132
|
+
*dest++ = (char)gsToPrintMap[s[i++]];
|
|
133
|
+
/* fall through */
|
|
134
|
+
case 1:
|
|
135
|
+
*dest++ = (char)gsToPrintMap[s[i]];
|
|
136
|
+
/* fall through */
|
|
137
|
+
case 0:
|
|
138
|
+
*dest = '\0';
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
void modp_toprint(char* str, size_t len)
|
|
143
|
+
{
|
|
144
|
+
modp_toprint_copy(str, str, len);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
size_t modp_rtrim(char* str, size_t len)
|
|
148
|
+
{
|
|
149
|
+
while (len) {
|
|
150
|
+
char c = str[len - 1];
|
|
151
|
+
if (c == ' ' || c == '\n' || c == '\t' || c == '\r') {
|
|
152
|
+
str[len - 1] = '\0';
|
|
153
|
+
len -= 1;
|
|
154
|
+
} else {
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return len;
|
|
159
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* \file modp_ascii.h
|
|
3
|
+
* \brief Simple ASCII manipulations including upper and lower casing,
|
|
4
|
+
* white space trimming, and conversion to "printable" characters.
|
|
5
|
+
*
|
|
6
|
+
* blah blah blah
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* <PRE>
|
|
11
|
+
* MODP_ASCII -- Simple ascii manipulation (uppercase, lowercase, etc)
|
|
12
|
+
* https://github.com/client9/stringencoders
|
|
13
|
+
*
|
|
14
|
+
* Copyright © 2007-2016, Nick Galbreath -- nickg [at] client9 [dot] com
|
|
15
|
+
* All rights reserved.
|
|
16
|
+
*
|
|
17
|
+
* Released under MIT license. See LICENSE for details.
|
|
18
|
+
* </PRE>
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
#ifndef COM_MODP_STRINGENCODERS_ASCII
|
|
23
|
+
#define COM_MODP_STRINGENCODERS_ASCII
|
|
24
|
+
|
|
25
|
+
#include "extern_c_begin.h"
|
|
26
|
+
#include "modp_stdint.h"
|
|
27
|
+
|
|
28
|
+
/*
|
|
29
|
+
* \param[in,out] str the input string
|
|
30
|
+
* \param[in] len the length of input string (the strlen)
|
|
31
|
+
*/
|
|
32
|
+
void modp_toupper(char* str, size_t len);
|
|
33
|
+
|
|
34
|
+
/** \brief make lower case copy of input string
|
|
35
|
+
*
|
|
36
|
+
* \param[out] dest output buffer, with at least 'len + 1' bytes allocated
|
|
37
|
+
* \param[in] str the input string
|
|
38
|
+
* \param[in] len the length of input string (the strlen)
|
|
39
|
+
*
|
|
40
|
+
* Please make sure dest has been allocation with at least 'len+1'
|
|
41
|
+
* bytes. This appends a trailing NULL character at the end of
|
|
42
|
+
* dest!
|
|
43
|
+
*
|
|
44
|
+
* This is based on the algorithm by Paul Hsieh
|
|
45
|
+
* http://www.azillionmonkeys.com/qed/asmexample.html
|
|
46
|
+
*/
|
|
47
|
+
void modp_toupper_copy(char* dest, const char* str, size_t len);
|
|
48
|
+
|
|
49
|
+
/** \brief lower case a string in place
|
|
50
|
+
*
|
|
51
|
+
* \param[in,out] str the input string
|
|
52
|
+
* \param[in] len the length of input string (the strlen)
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
void modp_tolower(char* str, size_t len);
|
|
56
|
+
|
|
57
|
+
/** \brief make lower case copy of input string
|
|
58
|
+
*
|
|
59
|
+
* \param[out] dest output buffer, with at least 'len + 1' bytes allocated
|
|
60
|
+
* \param[in] str the input string
|
|
61
|
+
* \param[in] len the length of input string (the strlen)
|
|
62
|
+
*
|
|
63
|
+
* Please make sure dest has been allocation with at least 'len+1'
|
|
64
|
+
* bytes. This appends a trailing NULL character at the end of
|
|
65
|
+
* dest!
|
|
66
|
+
*
|
|
67
|
+
* This is based on the algorithm by Paul Hsieh
|
|
68
|
+
* http://www.azillionmonkeys.com/qed/asmexample.html
|
|
69
|
+
*/
|
|
70
|
+
void modp_tolower_copy(char* dest, const char* str, size_t len);
|
|
71
|
+
|
|
72
|
+
/** \brief turn a string into 7-bit printable ascii.
|
|
73
|
+
*
|
|
74
|
+
* By "printable" we means all characters between 32 and 126.
|
|
75
|
+
* All other values are turned into '?'
|
|
76
|
+
*
|
|
77
|
+
* \param[in,out] str the input string
|
|
78
|
+
* \param[in] len the length of input string (the strlen)
|
|
79
|
+
*
|
|
80
|
+
*/
|
|
81
|
+
void modp_toprint(char* str, size_t len);
|
|
82
|
+
|
|
83
|
+
/** \brief make a printable copy of a string
|
|
84
|
+
*
|
|
85
|
+
* By "printable" we means all characters between 32 and 126.
|
|
86
|
+
* All other values are turned into '?'
|
|
87
|
+
*
|
|
88
|
+
* \param[out] dest output buffer, with at least 'len + 1' bytes allocated
|
|
89
|
+
* \param[in] str the input string
|
|
90
|
+
* \param[in] len the length of input string (the strlen)
|
|
91
|
+
*
|
|
92
|
+
* Please make sure dest has been allocation with at least 'len+1'
|
|
93
|
+
* bytes. This appends a trailing NULL character at the end of
|
|
94
|
+
* dest!
|
|
95
|
+
*/
|
|
96
|
+
void modp_toprint_copy(char* dest, const char* str, size_t len);
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* \brief remove trailing whitespace from a string
|
|
100
|
+
* \param[in,out] str string to be stripped
|
|
101
|
+
* \param[in] len the size of the input
|
|
102
|
+
* \return the size of the output, not including any ending null byte.
|
|
103
|
+
*/
|
|
104
|
+
size_t modp_rtrim(char* str, size_t len);
|
|
105
|
+
|
|
106
|
+
#include "extern_c_end.h"
|
|
107
|
+
|
|
108
|
+
#ifdef __cplusplus
|
|
109
|
+
#include <string>
|
|
110
|
+
|
|
111
|
+
namespace modp {
|
|
112
|
+
|
|
113
|
+
inline std::string& toupper(std::string& str)
|
|
114
|
+
{
|
|
115
|
+
modp_toupper(const_cast<char*>(str.c_str()), str.size());
|
|
116
|
+
return str;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
inline std::string toupper(const std::string& str)
|
|
120
|
+
{
|
|
121
|
+
std::string s(str.size(), '\0');
|
|
122
|
+
modp_toupper_copy(const_cast<char*>(s.data()), str.data(), str.size());
|
|
123
|
+
return s;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
inline std::string tolower(const std::string& str)
|
|
127
|
+
{
|
|
128
|
+
std::string s(str.size(), '\0');
|
|
129
|
+
modp_tolower_copy(const_cast<char*>(s.data()), str.data(), str.size());
|
|
130
|
+
return s;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
inline std::string& tolower(std::string& str)
|
|
134
|
+
{
|
|
135
|
+
modp_tolower(const_cast<char*>(str.c_str()), str.size());
|
|
136
|
+
return str;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
inline std::string toprint(const std::string& str)
|
|
140
|
+
{
|
|
141
|
+
std::string s(str.size(), '\0');
|
|
142
|
+
modp_toprint_copy(const_cast<char*>(s.data()), str.data(), str.size());
|
|
143
|
+
return s;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
inline std::string& toprint(std::string& str)
|
|
147
|
+
{
|
|
148
|
+
modp_toprint(const_cast<char*>(str.c_str()), str.size());
|
|
149
|
+
return str;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
inline std::string& rtrim(std::string& s)
|
|
153
|
+
{
|
|
154
|
+
size_t d = modp_rtrim(const_cast<char*>(s.data()), s.size());
|
|
155
|
+
s.erase(d, std::string::npos);
|
|
156
|
+
return s;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
#endif /* __cplusplus */
|
|
161
|
+
|
|
162
|
+
#endif /* MODP_ASCII */
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
static const uint8_t gsToUpperMap[256] = {
|
|
2
|
+
'\0', 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, '\t',
|
|
3
|
+
'\n', 0x0b, 0x0c, '\r', 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
|
|
4
|
+
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
|
|
5
|
+
0x1e, 0x1f, ' ', '!', '"', '#', '$', '%', '&', '\'',
|
|
6
|
+
'(', ')', '*', '+', ',', '-', '.', '/', '0', '1',
|
|
7
|
+
'2', '3', '4', '5', '6', '7', '8', '9', ':', ';',
|
|
8
|
+
'<', '=', '>', '?', '@', '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', '[', '\\', ']', '^', '_', '`', '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', '{', '|', '}', '~', 0x7f, 0x80, 0x81,
|
|
15
|
+
0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b,
|
|
16
|
+
0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
|
|
17
|
+
0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
|
|
18
|
+
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
|
|
19
|
+
0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
|
|
20
|
+
0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd,
|
|
21
|
+
0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
|
|
22
|
+
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1,
|
|
23
|
+
0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
|
|
24
|
+
0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5,
|
|
25
|
+
0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
|
|
26
|
+
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
|
|
27
|
+
0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
|
|
28
|
+
};
|
|
29
|
+
static const uint8_t gsToLowerMap[256] = {
|
|
30
|
+
'\0', 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, '\t',
|
|
31
|
+
'\n', 0x0b, 0x0c, '\r', 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
|
|
32
|
+
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
|
|
33
|
+
0x1e, 0x1f, ' ', '!', '"', '#', '$', '%', '&', '\'',
|
|
34
|
+
'(', ')', '*', '+', ',', '-', '.', '/', '0', '1',
|
|
35
|
+
'2', '3', '4', '5', '6', '7', '8', '9', ':', ';',
|
|
36
|
+
'<', '=', '>', '?', '@', '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', '[', '\\', ']', '^', '_', '`', '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', '{', '|', '}', '~', 0x7f, 0x80, 0x81,
|
|
43
|
+
0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b,
|
|
44
|
+
0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
|
|
45
|
+
0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
|
|
46
|
+
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
|
|
47
|
+
0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
|
|
48
|
+
0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd,
|
|
49
|
+
0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
|
|
50
|
+
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1,
|
|
51
|
+
0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
|
|
52
|
+
0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5,
|
|
53
|
+
0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
|
|
54
|
+
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
|
|
55
|
+
0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
|
|
56
|
+
};
|
|
57
|
+
static const uint8_t gsToPrintMap[256] = {
|
|
58
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
59
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
60
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
61
|
+
'?', '?', ' ', '!', '"', '#', '$', '%', '&', '\'',
|
|
62
|
+
'(', ')', '*', '+', ',', '-', '.', '/', '0', '1',
|
|
63
|
+
'2', '3', '4', '5', '6', '7', '8', '9', ':', ';',
|
|
64
|
+
'<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E',
|
|
65
|
+
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
|
66
|
+
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
|
|
67
|
+
'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c',
|
|
68
|
+
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
|
69
|
+
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
|
|
70
|
+
'x', 'y', 'z', '{', '|', '}', '~', '?', '?', '?',
|
|
71
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
72
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
73
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
74
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
75
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
76
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
77
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
78
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
79
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
80
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
81
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
82
|
+
'?', '?', '?', '?', '?', '?', '?', '?', '?', '?',
|
|
83
|
+
'?', '?', '?', '?', '?', '?'
|
|
84
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#include "arraytoc.h"
|
|
2
|
+
#include <stdio.h>
|
|
3
|
+
|
|
4
|
+
static void modp_toupper_map(void)
|
|
5
|
+
{
|
|
6
|
+
size_t i = 0;
|
|
7
|
+
char map[256];
|
|
8
|
+
for (i = 0; i < sizeof(map); ++i) {
|
|
9
|
+
if (i >= 'a' && i <= 'z') {
|
|
10
|
+
map[i] = (char)(i - 32);
|
|
11
|
+
} else {
|
|
12
|
+
map[i] = (char)(i);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
char_array_to_c(map, sizeof(map), "gsToUpperMap");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static void modp_tolower_map(void)
|
|
20
|
+
{
|
|
21
|
+
size_t i = 0;
|
|
22
|
+
char map[256];
|
|
23
|
+
for (i = 0; i < sizeof(map); ++i) {
|
|
24
|
+
if (i >= 'A' && i <= 'Z') {
|
|
25
|
+
map[i] = (char)(i + 32);
|
|
26
|
+
} else {
|
|
27
|
+
map[i] = (char)i;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
char_array_to_c(map, sizeof(map), "gsToLowerMap");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static void modp_toprint_map(void)
|
|
35
|
+
{
|
|
36
|
+
size_t i = 0;
|
|
37
|
+
char map[256];
|
|
38
|
+
for (i = 0; i < sizeof(map); ++i) {
|
|
39
|
+
if (i < 32 || i > 126) {
|
|
40
|
+
map[i] = '?';
|
|
41
|
+
} else {
|
|
42
|
+
map[i] = (char)i;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
char_array_to_c(map, sizeof(map), "gsToPrintMap");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
int main(void)
|
|
50
|
+
{
|
|
51
|
+
modp_toupper_map();
|
|
52
|
+
modp_tolower_map();
|
|
53
|
+
modp_toprint_map();
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* \file
|
|
3
|
+
* <PRE>
|
|
4
|
+
* MODP_B16 - High performance base16 encoder/decoder
|
|
5
|
+
* https://github.com/client9/stringencoders
|
|
6
|
+
*
|
|
7
|
+
* Copyright © 2005-2016 Nick Galbreath
|
|
8
|
+
* All rights reserved.
|
|
9
|
+
* Released under MIT license. See LICENSE for details.
|
|
10
|
+
* </PRE>
|
|
11
|
+
*/
|
|
12
|
+
#include "modp_b16.h"
|
|
13
|
+
#include "config.h"
|
|
14
|
+
#include "modp_b16_data.h"
|
|
15
|
+
#include "modp_stdint.h"
|
|
16
|
+
|
|
17
|
+
size_t modp_b16_encode(char* dest, const char* str, size_t len)
|
|
18
|
+
{
|
|
19
|
+
size_t i;
|
|
20
|
+
const size_t buckets = len >> 2; /* i.e. i / 4 */
|
|
21
|
+
const size_t leftover = len & 0x03; /* i.e. i % 4 */
|
|
22
|
+
const uint8_t* srcChar;
|
|
23
|
+
uint8_t* p = (uint8_t*)dest;
|
|
24
|
+
uint8_t t1, t2, t3, t4;
|
|
25
|
+
const uint32_t* srcInt = (const uint32_t*)str;
|
|
26
|
+
uint32_t x;
|
|
27
|
+
for (i = 0; i < buckets; ++i) {
|
|
28
|
+
x = *srcInt++;
|
|
29
|
+
/* t1 = *s++; t2 = *s++; t3 = *s++; t4 = *s++; */
|
|
30
|
+
#ifdef WORDS_BIGENDIAN
|
|
31
|
+
t1 = (uint8_t)(x >> 24);
|
|
32
|
+
t2 = (uint8_t)(x >> 16);
|
|
33
|
+
t3 = (uint8_t)(x >> 8);
|
|
34
|
+
t4 = (uint8_t)x;
|
|
35
|
+
#else
|
|
36
|
+
t4 = (uint8_t)(x >> 24);
|
|
37
|
+
t3 = (uint8_t)(x >> 16);
|
|
38
|
+
t2 = (uint8_t)(x >> 8);
|
|
39
|
+
t1 = (uint8_t)x;
|
|
40
|
+
#endif
|
|
41
|
+
*p++ = gsHexEncodeC1[t1];
|
|
42
|
+
*p++ = gsHexEncodeC2[t1];
|
|
43
|
+
*p++ = gsHexEncodeC1[t2];
|
|
44
|
+
*p++ = gsHexEncodeC2[t2];
|
|
45
|
+
*p++ = gsHexEncodeC1[t3];
|
|
46
|
+
*p++ = gsHexEncodeC2[t3];
|
|
47
|
+
*p++ = gsHexEncodeC1[t4];
|
|
48
|
+
*p++ = gsHexEncodeC2[t4];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
srcChar = (const uint8_t*)srcInt;
|
|
52
|
+
switch (leftover) {
|
|
53
|
+
case 0:
|
|
54
|
+
break;
|
|
55
|
+
case 1:
|
|
56
|
+
t1 = (uint8_t)*srcChar;
|
|
57
|
+
*p++ = gsHexEncodeC1[t1];
|
|
58
|
+
*p++ = gsHexEncodeC2[t1];
|
|
59
|
+
break;
|
|
60
|
+
case 2:
|
|
61
|
+
t1 = (uint8_t)*srcChar++;
|
|
62
|
+
t2 = (uint8_t)*srcChar;
|
|
63
|
+
*p++ = gsHexEncodeC1[t1];
|
|
64
|
+
*p++ = gsHexEncodeC2[t1];
|
|
65
|
+
*p++ = gsHexEncodeC1[t2];
|
|
66
|
+
*p++ = gsHexEncodeC2[t2];
|
|
67
|
+
break;
|
|
68
|
+
default: /* case 3 */
|
|
69
|
+
t1 = (uint8_t)*srcChar++;
|
|
70
|
+
t2 = (uint8_t)*srcChar++;
|
|
71
|
+
t3 = (uint8_t)*srcChar;
|
|
72
|
+
*p++ = gsHexEncodeC1[t1];
|
|
73
|
+
*p++ = gsHexEncodeC2[t1];
|
|
74
|
+
*p++ = gsHexEncodeC1[t2];
|
|
75
|
+
*p++ = gsHexEncodeC2[t2];
|
|
76
|
+
*p++ = gsHexEncodeC1[t3];
|
|
77
|
+
*p++ = gsHexEncodeC2[t3];
|
|
78
|
+
}
|
|
79
|
+
*p = '\0';
|
|
80
|
+
return (size_t)(p - (uint8_t*)dest);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
size_t modp_b16_decode(char* dest, const char* str, size_t len)
|
|
84
|
+
{
|
|
85
|
+
size_t i;
|
|
86
|
+
uint8_t t0, t1, t2, t3;
|
|
87
|
+
uint8_t* p = (uint8_t*)dest;
|
|
88
|
+
uint32_t val1, val2;
|
|
89
|
+
const uint8_t* s = (const uint8_t*)str;
|
|
90
|
+
const size_t buckets = len >> 2; /* i.e. len / 4 */
|
|
91
|
+
const size_t leftover = len & 0x03; /* i.e. len % 4 */
|
|
92
|
+
if (leftover & 0x01) { /* i.e if leftover is odd, */
|
|
93
|
+
/* leftover==1 || leftover == 3 */
|
|
94
|
+
return (size_t)-1;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/* read 4 bytes, output 2.
|
|
98
|
+
* Note on PPC G4, GCC 4.0, it's quite a bit faster to
|
|
99
|
+
* NOT use t0,t1,t2,t3, and just put the *s++ in the gsHexDecodeMap
|
|
100
|
+
* lookup
|
|
101
|
+
*/
|
|
102
|
+
for (i = 0; i < buckets; ++i) {
|
|
103
|
+
t0 = *s++;
|
|
104
|
+
t1 = *s++;
|
|
105
|
+
t2 = *s++;
|
|
106
|
+
t3 = *s++;
|
|
107
|
+
val1 = gsHexDecodeD2[t0] | gsHexDecodeMap[t1];
|
|
108
|
+
val2 = gsHexDecodeD2[t2] | gsHexDecodeMap[t3];
|
|
109
|
+
if (val1 > 0xff || val2 > 0xff) {
|
|
110
|
+
return (size_t)-1;
|
|
111
|
+
}
|
|
112
|
+
*p++ = (uint8_t)val1;
|
|
113
|
+
*p++ = (uint8_t)val2;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (leftover == 2) {
|
|
117
|
+
val1 = gsHexDecodeD2[s[0]] | gsHexDecodeMap[s[1]];
|
|
118
|
+
if (val1 > 0xff) {
|
|
119
|
+
return (size_t)-1;
|
|
120
|
+
}
|
|
121
|
+
*p++ = (uint8_t)val1;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return (size_t)(p - (uint8_t*)dest);
|
|
125
|
+
}
|