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.
Files changed (160) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +30 -0
  5. data/.travis.yml +10 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +72 -0
  9. data/Rakefile +46 -0
  10. data/bench/both.rb +86 -0
  11. data/bench/encode.rb +57 -0
  12. data/bench/encrypt.rb +80 -0
  13. data/bench/init.rb +5 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/ffi-hydrogen.gemspec +31 -0
  17. data/lib/ffi/hydrogen.rb +216 -0
  18. data/vendor/.clang-format +2 -0
  19. data/vendor/.gitignore +3 -0
  20. data/vendor/README.md +2 -0
  21. data/vendor/libhydrogen/.clang-format +95 -0
  22. data/vendor/libhydrogen/.gitignore +32 -0
  23. data/vendor/libhydrogen/.travis.yml +22 -0
  24. data/vendor/libhydrogen/LICENSE +18 -0
  25. data/vendor/libhydrogen/Makefile +61 -0
  26. data/vendor/libhydrogen/Makefile.arduino +51 -0
  27. data/vendor/libhydrogen/README.md +29 -0
  28. data/vendor/libhydrogen/hydrogen.c +18 -0
  29. data/vendor/libhydrogen/hydrogen.h +317 -0
  30. data/vendor/libhydrogen/impl/common.h +316 -0
  31. data/vendor/libhydrogen/impl/core.h +220 -0
  32. data/vendor/libhydrogen/impl/gimli-core/portable.h +39 -0
  33. data/vendor/libhydrogen/impl/gimli-core/sse2.h +97 -0
  34. data/vendor/libhydrogen/impl/gimli-core.h +25 -0
  35. data/vendor/libhydrogen/impl/hash.h +138 -0
  36. data/vendor/libhydrogen/impl/hydrogen_p.h +83 -0
  37. data/vendor/libhydrogen/impl/kdf.h +20 -0
  38. data/vendor/libhydrogen/impl/kx.h +441 -0
  39. data/vendor/libhydrogen/impl/pwhash.h +281 -0
  40. data/vendor/libhydrogen/impl/random.h +376 -0
  41. data/vendor/libhydrogen/impl/secretbox.h +236 -0
  42. data/vendor/libhydrogen/impl/sign.h +207 -0
  43. data/vendor/libhydrogen/impl/x25519.h +383 -0
  44. data/vendor/libhydrogen/library.properties +10 -0
  45. data/vendor/libhydrogen/logo.png +0 -0
  46. data/vendor/libhydrogen/tests/tests.c +431 -0
  47. data/vendor/main.c +140 -0
  48. data/vendor/stringencoders/.gitignore +25 -0
  49. data/vendor/stringencoders/.travis.yml +13 -0
  50. data/vendor/stringencoders/AUTHORS +1 -0
  51. data/vendor/stringencoders/COPYING +2 -0
  52. data/vendor/stringencoders/ChangeLog +170 -0
  53. data/vendor/stringencoders/Doxyfile +276 -0
  54. data/vendor/stringencoders/INSTALL +119 -0
  55. data/vendor/stringencoders/LICENSE +22 -0
  56. data/vendor/stringencoders/Makefile.am +3 -0
  57. data/vendor/stringencoders/NEWS +3 -0
  58. data/vendor/stringencoders/README +2 -0
  59. data/vendor/stringencoders/README.md +32 -0
  60. data/vendor/stringencoders/bootstrap.sh +3 -0
  61. data/vendor/stringencoders/configure-gcc-hardened.sh +16 -0
  62. data/vendor/stringencoders/configure.ac +44 -0
  63. data/vendor/stringencoders/doxy/footer.html +34 -0
  64. data/vendor/stringencoders/doxy/header.html +85 -0
  65. data/vendor/stringencoders/indent.sh +9 -0
  66. data/vendor/stringencoders/javascript/base64-speed.html +43 -0
  67. data/vendor/stringencoders/javascript/base64-test.html +209 -0
  68. data/vendor/stringencoders/javascript/base64.html +18 -0
  69. data/vendor/stringencoders/javascript/base64.js +176 -0
  70. data/vendor/stringencoders/javascript/qunit.css +119 -0
  71. data/vendor/stringencoders/javascript/qunit.js +1062 -0
  72. data/vendor/stringencoders/javascript/urlparse-test.html +367 -0
  73. data/vendor/stringencoders/javascript/urlparse.js +328 -0
  74. data/vendor/stringencoders/make-ci.sh +13 -0
  75. data/vendor/stringencoders/makerelease.sh +16 -0
  76. data/vendor/stringencoders/python/b85.py +176 -0
  77. data/vendor/stringencoders/src/Makefile.am +134 -0
  78. data/vendor/stringencoders/src/arraytoc.c +85 -0
  79. data/vendor/stringencoders/src/arraytoc.h +43 -0
  80. data/vendor/stringencoders/src/extern_c_begin.h +3 -0
  81. data/vendor/stringencoders/src/extern_c_end.h +3 -0
  82. data/vendor/stringencoders/src/html_named_entities_generator.py +203 -0
  83. data/vendor/stringencoders/src/modp_ascii.c +159 -0
  84. data/vendor/stringencoders/src/modp_ascii.h +162 -0
  85. data/vendor/stringencoders/src/modp_ascii_data.h +84 -0
  86. data/vendor/stringencoders/src/modp_ascii_gen.c +55 -0
  87. data/vendor/stringencoders/src/modp_b16.c +125 -0
  88. data/vendor/stringencoders/src/modp_b16.h +148 -0
  89. data/vendor/stringencoders/src/modp_b16_data.h +104 -0
  90. data/vendor/stringencoders/src/modp_b16_gen.c +65 -0
  91. data/vendor/stringencoders/src/modp_b2.c +69 -0
  92. data/vendor/stringencoders/src/modp_b2.h +130 -0
  93. data/vendor/stringencoders/src/modp_b2_data.h +44 -0
  94. data/vendor/stringencoders/src/modp_b2_gen.c +36 -0
  95. data/vendor/stringencoders/src/modp_b36.c +108 -0
  96. data/vendor/stringencoders/src/modp_b36.h +170 -0
  97. data/vendor/stringencoders/src/modp_b64.c +254 -0
  98. data/vendor/stringencoders/src/modp_b64.h +236 -0
  99. data/vendor/stringencoders/src/modp_b64_data.h +477 -0
  100. data/vendor/stringencoders/src/modp_b64_gen.c +168 -0
  101. data/vendor/stringencoders/src/modp_b64r.c +254 -0
  102. data/vendor/stringencoders/src/modp_b64r.h +242 -0
  103. data/vendor/stringencoders/src/modp_b64r_data.h +477 -0
  104. data/vendor/stringencoders/src/modp_b64w.c +254 -0
  105. data/vendor/stringencoders/src/modp_b64w.h +231 -0
  106. data/vendor/stringencoders/src/modp_b64w_data.h +477 -0
  107. data/vendor/stringencoders/src/modp_b85.c +109 -0
  108. data/vendor/stringencoders/src/modp_b85.h +171 -0
  109. data/vendor/stringencoders/src/modp_b85_data.h +36 -0
  110. data/vendor/stringencoders/src/modp_b85_gen.c +65 -0
  111. data/vendor/stringencoders/src/modp_bjavascript.c +65 -0
  112. data/vendor/stringencoders/src/modp_bjavascript.h +105 -0
  113. data/vendor/stringencoders/src/modp_bjavascript_data.h +84 -0
  114. data/vendor/stringencoders/src/modp_bjavascript_gen.c +58 -0
  115. data/vendor/stringencoders/src/modp_burl.c +228 -0
  116. data/vendor/stringencoders/src/modp_burl.h +259 -0
  117. data/vendor/stringencoders/src/modp_burl_data.h +136 -0
  118. data/vendor/stringencoders/src/modp_burl_gen.c +121 -0
  119. data/vendor/stringencoders/src/modp_html.c +128 -0
  120. data/vendor/stringencoders/src/modp_html.h +53 -0
  121. data/vendor/stringencoders/src/modp_html_named_entities.h +9910 -0
  122. data/vendor/stringencoders/src/modp_json.c +315 -0
  123. data/vendor/stringencoders/src/modp_json.h +103 -0
  124. data/vendor/stringencoders/src/modp_json_data.h +57 -0
  125. data/vendor/stringencoders/src/modp_json_gen.py +60 -0
  126. data/vendor/stringencoders/src/modp_mainpage.h +120 -0
  127. data/vendor/stringencoders/src/modp_numtoa.c +350 -0
  128. data/vendor/stringencoders/src/modp_numtoa.h +100 -0
  129. data/vendor/stringencoders/src/modp_qsiter.c +76 -0
  130. data/vendor/stringencoders/src/modp_qsiter.h +71 -0
  131. data/vendor/stringencoders/src/modp_stdint.h +43 -0
  132. data/vendor/stringencoders/src/modp_utf8.c +88 -0
  133. data/vendor/stringencoders/src/modp_utf8.h +38 -0
  134. data/vendor/stringencoders/src/modp_xml.c +311 -0
  135. data/vendor/stringencoders/src/modp_xml.h +166 -0
  136. data/vendor/stringencoders/src/stringencoders.pc +10 -0
  137. data/vendor/stringencoders/src/stringencoders.pc.in +10 -0
  138. data/vendor/stringencoders/test/Makefile.am +113 -0
  139. data/vendor/stringencoders/test/apr_base64.c +262 -0
  140. data/vendor/stringencoders/test/apr_base64.h +120 -0
  141. data/vendor/stringencoders/test/cxx_test.cc +482 -0
  142. data/vendor/stringencoders/test/minunit.h +82 -0
  143. data/vendor/stringencoders/test/modp_ascii_test.c +281 -0
  144. data/vendor/stringencoders/test/modp_b16_test.c +288 -0
  145. data/vendor/stringencoders/test/modp_b2_test.c +250 -0
  146. data/vendor/stringencoders/test/modp_b64_test.c +266 -0
  147. data/vendor/stringencoders/test/modp_b85_test.c +130 -0
  148. data/vendor/stringencoders/test/modp_bjavascript_test.c +137 -0
  149. data/vendor/stringencoders/test/modp_burl_test.c +423 -0
  150. data/vendor/stringencoders/test/modp_html_test.c +296 -0
  151. data/vendor/stringencoders/test/modp_json_test.c +336 -0
  152. data/vendor/stringencoders/test/modp_numtoa_test.c +545 -0
  153. data/vendor/stringencoders/test/modp_qsiter_test.c +280 -0
  154. data/vendor/stringencoders/test/modp_utf8_test.c +188 -0
  155. data/vendor/stringencoders/test/modp_xml_test.c +339 -0
  156. data/vendor/stringencoders/test/speedtest.c +241 -0
  157. data/vendor/stringencoders/test/speedtest_ascii.c +345 -0
  158. data/vendor/stringencoders/test/speedtest_msg.c +78 -0
  159. data/vendor/stringencoders/test/speedtest_numtoa.c +276 -0
  160. metadata +314 -0
@@ -0,0 +1,171 @@
1
+ /**
2
+ * \file modp_b85.h
3
+ * \brief Base 85 encoding and decoding
4
+ *
5
+ * This provides a endian-safe base85 encode/decode operations. This
6
+ * means, the result will be the same on x86 or ibm/sparc chips.
7
+ */
8
+
9
+ /*
10
+ * <pre>
11
+ * High Performance Base85 Encoder / Decoder
12
+ *
13
+ * Copyright &copy; 2006-2016 Nick Galbreath -- nickg [at] client9 [dot] com
14
+ * All rights reserved.
15
+ *
16
+ * https://github.com/client9/stringencoders/
17
+ *
18
+ * Released under MIT license. See LICENSE for details.
19
+ * </PRE>
20
+ *
21
+ * This provides a endian-safe base85 encode/decode operations. This
22
+ * means, the result will be the same on x86 or ibm/sparc chips.
23
+ *
24
+ * (Note: making it endian-specifc only results in a 5% savings in
25
+ * the decode operation, so why bother)
26
+ */
27
+
28
+ #ifndef COM_MODP_STRINGENCODERS_B85
29
+ #define COM_MODP_STRINGENCODERS_B85
30
+ #include "extern_c_begin.h"
31
+ #include "modp_stdint.h"
32
+
33
+ /**
34
+ * \brief base 85 encode
35
+ *
36
+ * \param[out] dest should have at least b85fast_encode_len memory allocated
37
+ * \param[in] src input string
38
+ * \param[in] len input string length, must be a multiple of 4
39
+ * \return the strlen of the destination, or -1 if error
40
+ *
41
+ */
42
+ size_t modp_b85_encode(char* dest, const char* src, size_t len);
43
+
44
+ /**
45
+ * \brief Base 85 decode
46
+ * \param[out] dest -- destination locations. May equal input.
47
+ * \param[in] src -- source b85data
48
+ * \param len -- length of source
49
+ * \return -1 on decoding error, length of output otherwise
50
+ * No ending null is added
51
+ */
52
+ size_t modp_b85_decode(char* dest, const char* src, size_t len);
53
+
54
+ /**
55
+ * \brief Returns the amount of memory to allocate for encoding the input
56
+ * string.
57
+ *
58
+ */
59
+ #define modp_b85_encode_len(A) ((A + 3) / 4 * 5 + 1)
60
+
61
+ /**
62
+ * \brief Return output strlen, without a NULL
63
+ */
64
+ #define modp_b85_encode_strlen(A) ((A + 3) / 4 * 5)
65
+
66
+ /**
67
+ * \brief Return the amount of memory to allocate for decoding a base 85
68
+ * encoded string.
69
+ *
70
+ */
71
+ #define modp_b85_decode_len(A) ((A + 4) / 5 * 4)
72
+
73
+ #include "extern_c_end.h"
74
+
75
+ #ifdef __cplusplus
76
+ #include <cstring>
77
+ #include <string>
78
+
79
+ namespace modp {
80
+
81
+ /**
82
+ *
83
+ * \param[in] s the input data
84
+ * \param[in] len the length of input
85
+ * \return b85 encoded string
86
+ */
87
+ inline std::string b85_encode(const char* s, size_t len)
88
+ {
89
+ std::string x(modp_b85_encode_len(len), '\0');
90
+ size_t d = modp_b85_encode(const_cast<char*>(x.data()), s, len);
91
+ if (d == (size_t)-1) {
92
+ x.clear();
93
+ } else {
94
+ x.erase(d, std::string::npos);
95
+ }
96
+ return x;
97
+ }
98
+
99
+ /**
100
+ * \param[in] null-terminated c-string input
101
+ * \return b85 encoded string
102
+ */
103
+ inline std::string b85_encode(const char* s)
104
+ {
105
+ return b85_encode(s, strlen(s));
106
+ }
107
+
108
+ /**
109
+ * /param[in,out] s the string to encode
110
+ * /return a reference to the input string, empty if error
111
+ */
112
+ inline std::string& b85_encode(std::string& s)
113
+ {
114
+ std::string x(b85_encode(s.data(), s.size()));
115
+ s.swap(x);
116
+ return s;
117
+ }
118
+
119
+ /**
120
+ * \param[in] s the input string
121
+ * \return base85 encoded string
122
+ */
123
+ inline std::string b85_encode(const std::string& s)
124
+ {
125
+ return b85_encode(s.data(), s.size());
126
+ }
127
+
128
+ /**
129
+ * Base85 decode a string.
130
+ * This function does not allocate memory.
131
+ *
132
+ * \param s the string to decode
133
+ * \return a reference to the input string. The string is empty
134
+ * if an error occurred.
135
+ */
136
+ inline std::string& b85_decode(std::string& s)
137
+ {
138
+ size_t d = modp_b85_decode(const_cast<char*>(s.data()), s.data(),
139
+ s.size());
140
+ if (d == (size_t)-1) {
141
+ s.clear();
142
+ } else {
143
+ s.erase(d, std::string::npos);
144
+ }
145
+ return s;
146
+ }
147
+
148
+ inline std::string b85_decode(const std::string& s)
149
+ {
150
+ std::string x(s);
151
+ b85_decode(x);
152
+ return x;
153
+ }
154
+
155
+ inline std::string b85_decode(const char* s, size_t len)
156
+ {
157
+ std::string x(s, len);
158
+ return b85_decode(x);
159
+ }
160
+
161
+ inline std::string b85_decode(const char* s)
162
+ {
163
+ std::string x(s);
164
+ return b85_decode(x);
165
+ }
166
+
167
+ } /* namespace modp */
168
+
169
+ #endif /* ifdef __cplusplus */
170
+
171
+ #endif /* ifndef MODP_B85 */
@@ -0,0 +1,36 @@
1
+ /* do not edit -- autogenerated from b85gen */
2
+ static const uint32_t gsCharToInt[256] = {
3
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
4
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
5
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 0, 99, 1,
6
+ 2, 3, 99, 4, 5, 6, 7, 8, 99, 9, 10, 11,
7
+ 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 99,
8
+ 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
9
+ 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
10
+ 47, 48, 49, 50, 51, 52, 53, 54, 99, 55, 56, 57,
11
+ 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
12
+ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
13
+ 82, 83, 84, 99, 99, 99, 99, 99, 99, 99, 99, 99,
14
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
15
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
16
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
17
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
18
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
19
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
20
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
21
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
22
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
23
+ 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
24
+ 99, 99, 99, 99
25
+ };
26
+ static const uint8_t gsIntToChar[85] = {
27
+ '!', '#', '$', '%', '\'', '(', ')', '*', '+', '-',
28
+ '.', '/', '0', '1', '2', '3', '4', '5', '6', '7',
29
+ '8', '9', ':', '<', '=', '>', '?', '@', 'A', 'B',
30
+ 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
31
+ 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
32
+ 'W', 'X', 'Y', 'Z', '[', ']', '^', '_', '`', 'a',
33
+ 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
34
+ 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
35
+ 'v', 'w', 'x', 'y', 'z'
36
+ };
@@ -0,0 +1,65 @@
1
+ #include "arraytoc.h"
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <string.h>
5
+ /**
6
+ * raw data to base85 char map
7
+ */
8
+ static char gsIntToChar[85];
9
+
10
+ /**
11
+ * base 85 char to int
12
+ */
13
+ static uint32_t gsCharToInt[256];
14
+
15
+ /*
16
+ * Set up the above arrays
17
+ */
18
+ static void initTables(void)
19
+ {
20
+ int i = 0;
21
+ int j = 0;
22
+ for (i = 0; i < 256; i++) {
23
+ gsCharToInt[i] = 99;
24
+ }
25
+
26
+ /* i < 33 or '!' is unprintable
27
+ * 127 is an unprintable character
28
+ */
29
+ for (i = '!', j = 0; j < 85 && i < 127; ++i) {
30
+
31
+ /* You can have 8 restrictions in the following line.
32
+ * Traditional postscript removes: ';', '&', '\', '"'
33
+ * so that 'last' character is 'y' ('z' is special)
34
+ * For web/cookie applications, I recommend those plus ','
35
+ */
36
+ if (i == ';' || i == '&' || i == '\\' || i == '"' || i == ',') {
37
+ continue;
38
+ }
39
+ gsIntToChar[j] = (char)i;
40
+ gsCharToInt[i] = (uint32_t)j;
41
+ ++j;
42
+ }
43
+
44
+ if (j != 85) {
45
+ fprintf(stderr, "Error in base85 table. You probably had too many restrictions\n");
46
+ exit(1);
47
+ }
48
+ }
49
+
50
+ /**
51
+ * beginning headers
52
+ */
53
+ static void printStart(void)
54
+ {
55
+ printf("/* do not edit -- autogenerated from b85gen */\n");
56
+ }
57
+
58
+ int main(void)
59
+ {
60
+ initTables();
61
+ printStart();
62
+ uint32_array_to_c(gsCharToInt, sizeof(gsCharToInt) / sizeof(uint32_t), "gsCharToInt");
63
+ char_array_to_c(gsIntToChar, sizeof(gsIntToChar), "gsIntToChar");
64
+ return 0;
65
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * \file
3
+ * <pre>
4
+ * modp_bjavascript.c High performance URL encoder/decoder
5
+ * https://github.com/client9/stringencoders
6
+ *
7
+ * Copyright &copy; 2006-2016 Nick Galbreath
8
+ * All rights reserved.
9
+ * Released under MIT license. See LICENSE for details.
10
+ * </PRE>
11
+ */
12
+ #include "modp_bjavascript.h"
13
+ #include "modp_bjavascript_data.h"
14
+ #include "modp_stdint.h"
15
+
16
+ size_t modp_bjavascript_encode(char* dest, const char* src, size_t len)
17
+ {
18
+ const char* deststart = dest;
19
+ const uint8_t* s = (const uint8_t*)src;
20
+ const uint8_t* srcend = s + len;
21
+ uint8_t x;
22
+ uint8_t val;
23
+
24
+ /* if 0, do nothing
25
+ * if 'A', hex escape
26
+ * else, \\ + value
27
+ */
28
+ while (s < srcend) {
29
+ x = *s++;
30
+ val = gsJavascriptEncodeMap[x];
31
+ if (val == 0) {
32
+ *dest++ = (char)x;
33
+ } else if (val == 'A') {
34
+ *dest++ = '\\';
35
+ *dest++ = 'x';
36
+ *dest++ = (char)(gsHexEncodeMap1[x]);
37
+ *dest++ = (char)(gsHexEncodeMap2[x]);
38
+ } else {
39
+ *dest++ = '\\';
40
+ *dest++ = (char)val;
41
+ }
42
+ }
43
+ *dest = '\0';
44
+ return (size_t)(dest - deststart);
45
+ }
46
+
47
+ size_t modp_bjavascript_encode_strlen(const char* src, size_t len)
48
+ {
49
+ const uint8_t* s = (const uint8_t*)src;
50
+ const uint8_t* srcend = s + len;
51
+ size_t count = 0;
52
+ uint8_t val;
53
+
54
+ while (s < srcend) {
55
+ val = gsJavascriptEncodeMap[*s++];
56
+ if (val == 0) {
57
+ count++;
58
+ } else if (val == 'A') {
59
+ count += 4;
60
+ } else {
61
+ count += 2;
62
+ }
63
+ }
64
+ return count;
65
+ }
@@ -0,0 +1,105 @@
1
+ /**
2
+ * \file modp_bjavascript.h
3
+ * \brief "C-string" to "javascript-string" encoder
4
+ *
5
+ * Used in emitting dynamically-generated javascript. Given a regular
6
+ * C-string which might contain binary, the encoder will emit a string
7
+ * that can be used inside a javascript string. For example:
8
+ *
9
+ * \code
10
+ * printf("var foo = '%s';", modp_bjavascript_encode(mystring, len));
11
+ * \endcode
12
+ *
13
+ * The "b" in "modp_bjavascript" is due to legacy reasons. It doesn't
14
+ * mean anything
15
+ *
16
+ * There is no decoder.
17
+ */
18
+
19
+ /*
20
+ * <PRE>
21
+ * High Performance c-string to javascript-string encoder
22
+ *
23
+ * Copyright &copy; 2006-2016 Nick Galbreath
24
+ * All rights reserved.
25
+ *
26
+ * https://github.com/client9/stringencoders
27
+ *
28
+ * Released under MIT license. See LICENSE for details.
29
+ * </PRE>
30
+ */
31
+
32
+ #ifndef COM_MODP_STRINGENCODERS_BJAVASCRIPT
33
+ #define COM_MODP_STRINGENCODERS_BJAVASCRIPT
34
+
35
+ #include "modp_stdint.h"
36
+
37
+ #include "extern_c_begin.h"
38
+
39
+ /**
40
+ * "javascript" encode a stirng
41
+ * This takes a c-string and does character escaping
42
+ * so it can be put into a var js_string = '...';
43
+ *
44
+ * \param[out] dest output string. Must
45
+ * \param[in] str The input string
46
+ * \param[in] len The length of the input string, excluding any
47
+ * final null byte.
48
+ */
49
+ size_t modp_bjavascript_encode(char* dest, const char* str, size_t len);
50
+
51
+ #define modp_bjavascript_encode_len(A) (4 * A + 1)
52
+
53
+ /**
54
+ * Given the exact size of output string.
55
+ *
56
+ * Can be used to allocate the right amount of memory for
57
+ * modp_burl_encode. Be sure to add 1 byte for final null.
58
+ *
59
+ * This is somewhat expensive since it examines every character
60
+ * in the input string
61
+ *
62
+ * \param[in] str The input string
63
+ * \param[in] len THe length of the input string, excluding any
64
+ * final null byte (i.e. strlen(str))
65
+ * \return the size of the output string, excluding the final
66
+ * null byte.
67
+ */
68
+ size_t modp_bjavascript_encode_strlen(const char* str, size_t len);
69
+
70
+ #include "extern_c_end.h"
71
+
72
+ #ifdef __cplusplus
73
+ #include <cstring>
74
+ #include <string>
75
+ namespace modp {
76
+
77
+ inline std::string javascript_encode(const char* s, size_t len)
78
+ {
79
+ std::string x(modp_bjavascript_encode_len(len), '\0');
80
+ size_t d = modp_bjavascript_encode(const_cast<char*>(x.data()), s, len);
81
+ x.erase(d, std::string::npos);
82
+ return x;
83
+ }
84
+
85
+ inline std::string javascript_encode(const char* s)
86
+ {
87
+ return javascript_encode(s, strlen(s));
88
+ }
89
+
90
+ inline std::string& javascript_encode(std::string& s)
91
+ {
92
+ std::string x(javascript_encode(s.data(), s.size()));
93
+ s.swap(x);
94
+ return s;
95
+ }
96
+
97
+ inline std::string javascript_encode(const std::string& s)
98
+ {
99
+ return javascript_encode(s.data(), s.size());
100
+ }
101
+
102
+ } /* namespace modp */
103
+ #endif /* __cplusplus */
104
+
105
+ #endif /* modp_bjavascript */
@@ -0,0 +1,84 @@
1
+ static const uint8_t gsJavascriptEncodeMap[256] = {
2
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'b', 't',
3
+ 'n', 'v', 'f', 'r', 'A', 'A', 'A', 'A', 'A', 'A',
4
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
5
+ 'A', 'A', '\0', '\0', '"', '\0', '\0', '\0', '\0', '\'',
6
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
7
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
8
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
9
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
10
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
11
+ '\0', '\0', '\\', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
12
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
13
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
14
+ '\0', '\0', '\0', '\0', '\0', '\0', '\0', 'A', 'A', 'A',
15
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
16
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
17
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
18
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
19
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
20
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
21
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
22
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
23
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
24
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
25
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
26
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
27
+ 'A', 'A', 'A', 'A', 'A', 'A'
28
+ };
29
+ static const uint8_t gsHexEncodeMap1[256] = {
30
+ '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
31
+ '0', '0', '0', '0', '0', '0', '1', '1', '1', '1',
32
+ '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
33
+ '1', '1', '2', '2', '2', '2', '2', '2', '2', '2',
34
+ '2', '2', '2', '2', '2', '2', '2', '2', '3', '3',
35
+ '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
36
+ '3', '3', '3', '3', '4', '4', '4', '4', '4', '4',
37
+ '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
38
+ '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
39
+ '5', '5', '5', '5', '5', '5', '6', '6', '6', '6',
40
+ '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
41
+ '6', '6', '7', '7', '7', '7', '7', '7', '7', '7',
42
+ '7', '7', '7', '7', '7', '7', '7', '7', '8', '8',
43
+ '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
44
+ '8', '8', '8', '8', '9', '9', '9', '9', '9', '9',
45
+ '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
46
+ 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
47
+ 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B',
48
+ 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
49
+ 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C',
50
+ 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'D', 'D',
51
+ 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D', 'D',
52
+ 'D', 'D', 'D', 'D', 'E', 'E', 'E', 'E', 'E', 'E',
53
+ 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E',
54
+ 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F', 'F',
55
+ 'F', 'F', 'F', 'F', 'F', 'F'
56
+ };
57
+ static const uint8_t gsHexEncodeMap2[256] = {
58
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
59
+ 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3',
60
+ '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
61
+ 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7',
62
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1',
63
+ '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
64
+ 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5',
65
+ '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
66
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
67
+ 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3',
68
+ '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
69
+ 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7',
70
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1',
71
+ '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
72
+ 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5',
73
+ '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
74
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
75
+ 'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3',
76
+ '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
77
+ 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7',
78
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', '0', '1',
79
+ '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
80
+ 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5',
81
+ '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
82
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
83
+ 'A', 'B', 'C', 'D', 'E', 'F'
84
+ };
@@ -0,0 +1,58 @@
1
+ /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2
+ /* vi: set expandtab shiftwidth=4 tabstop=4: */
3
+
4
+ #include "arraytoc.h"
5
+
6
+ static void hexencodemap(void)
7
+ {
8
+ static const char sHexChars[] = "0123456789ABCDEF";
9
+ int i;
10
+ char hexEncode1[256];
11
+ char hexEncode2[256];
12
+ for (i = 0; i < 256; ++i) {
13
+ hexEncode1[i] = sHexChars[i >> 4];
14
+ hexEncode2[i] = sHexChars[i & 0x0f];
15
+ }
16
+
17
+ char_array_to_c(hexEncode1, sizeof(hexEncode1), "gsHexEncodeMap1");
18
+ char_array_to_c(hexEncode2, sizeof(hexEncode2), "gsHexEncodeMap2");
19
+ }
20
+
21
+ static void jsencodemap(void)
22
+ {
23
+ int i;
24
+ char jsEncodeMap[256];
25
+
26
+ /* set everything to "as is" */
27
+ for (i = 0; i < 256; ++i) {
28
+ jsEncodeMap[i] = 0;
29
+ }
30
+
31
+ /* chars that need hex escaping */
32
+ for (i = 0; i < 32; ++i) {
33
+ jsEncodeMap[i] = 'A';
34
+ }
35
+ for (i = 127; i < 256; ++i) {
36
+ jsEncodeMap[i] = 'A';
37
+ }
38
+
39
+ /* items that have special escaping */
40
+ jsEncodeMap[0x08] = 'b';
41
+ jsEncodeMap[0x09] = 't';
42
+ jsEncodeMap[0x0a] = 'n';
43
+ jsEncodeMap[0x0b] = 'v';
44
+ jsEncodeMap[0x0c] = 'f';
45
+ jsEncodeMap[0x0d] = 'r';
46
+ jsEncodeMap[0x5c] = '\\'; /* blackslash gets escaped */
47
+ jsEncodeMap[0x22] = '"'; /* dquote gets escaped */
48
+ jsEncodeMap[0x27] = '\''; /* squote gets escaped */
49
+
50
+ char_array_to_c(jsEncodeMap, sizeof(jsEncodeMap), "gsJavascriptEncodeMap");
51
+ }
52
+
53
+ int main(void)
54
+ {
55
+ jsencodemap();
56
+ hexencodemap();
57
+ return 0;
58
+ }