ffi-hydrogen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,44 @@
1
+ AC_INIT(stringencoders, [v3.10.3], [nickg -at- client9 -dot- com])
2
+ AC_PREREQ(2.68)
3
+ AM_INIT_AUTOMAKE
4
+ AC_PROG_LIBTOOL
5
+ AC_CONFIG_HEADERS(config.h)
6
+ AC_CONFIG_MACRO_DIR([m4])
7
+
8
+ AC_PROG_CC
9
+ AC_PROG_CXX
10
+ AC_PROG_INSTALL
11
+
12
+ AC_C_CONST
13
+ AC_C_BIGENDIAN
14
+ AC_TYPE_SIZE_T
15
+ AC_CHECK_FUNCS([memset htonl strlen])
16
+ AC_ARG_ENABLE(gcov, AC_HELP_STRING([--enable-gcov],[turn on code coverage analysis tools]))
17
+
18
+ B64WCHARS="-_."
19
+ AC_ARG_WITH([b64wchars], AC_HELP_STRING([--with-b64wchars=3CHARS],[change b64 web extra chars, default is '-_.']),
20
+ [B64WCHARS=$withval], [])
21
+
22
+ EXTRACFLAGS="-Isrc -Wall -Wextra -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wconversion"
23
+ # -Wstrict-prototypes
24
+ # -Wmissing-prototypes
25
+
26
+ if test "x$enable_gcov" = "xyes";
27
+ then
28
+ EXTRACFLAGS="$EXTRACFLAGS -fprofile-arcs -ftest-coverage"
29
+ dnl Turn off optimization so code coverage tool
30
+ dnl can get accurate line numbers
31
+ CFLAGS=`echo "$EXTRACFLAGS" | sed -e 's/-O[0-9]*//g'`
32
+ CFLAGS="$CFLAGS -O0 -fno-inline"
33
+ fi
34
+ CFLAGS="$EXTRACFLAGS $CFLAGS"
35
+ CXXFLAGS="$CFLAGS"
36
+ AC_SUBST(B64WCHARS)
37
+ AC_CONFIG_FILES([
38
+ Makefile
39
+ src/Makefile
40
+ test/Makefile
41
+ src/stringencoders.pc
42
+ ])
43
+ AC_PROG_MAKE_SET
44
+ AC_OUTPUT
@@ -0,0 +1,34 @@
1
+ <!-- end doxygen, start footer -->
2
+ <!-- start disqus.md -->
3
+ <div id="disqus_thread"></div>
4
+ <script type="text/javascript">
5
+ /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
6
+ var disqus_shortname = 'client9'; // required: replace example with your forum shortname
7
+
8
+ /* * * DON'T EDIT BELOW THIS LINE * * */
9
+ (function() {
10
+ var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
11
+ dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
12
+ (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
13
+ })();
14
+ </script>
15
+ <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
16
+ <a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
17
+ <!-- end disqus.md -->
18
+ </div>
19
+ </div>
20
+ <div id="right">
21
+ </div><!-- end right -->
22
+ <div id="footer">
23
+ <ul>
24
+ <li><a href="/">client9</a></li>
25
+ <li><a href="/about/">about</a></li>
26
+ <li><a href="/archives/">archives</a></li>
27
+ <li><a href="/books/cida/">books</a></li>
28
+ <li><a href="/patents/">patents</a></li>
29
+ <li><a href="/projects/">projects</a></li>
30
+ </ul>
31
+ </div>
32
+ </div><!-- end body -->
33
+ </body>
34
+ </html>
@@ -0,0 +1,85 @@
1
+ <!doctype html>
2
+ <html>
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>$title</title>
6
+ <link rel="icon" href="/favicon.ico" />
7
+ <link rel="alternate" type="application/atom+xml" title="RSS Feed for client9.com" href="/atom.xml" />
8
+ <style>
9
+ body { color: #000; font-family: proxima-nova, proxima-nova-1, proxima-nova-2, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; }
10
+
11
+ body { color: #000; font-family: georgia, "times new roman", times, serif; }
12
+ blockquote { font-size: small; }
13
+
14
+ .date {
15
+ margin-top: 0px;
16
+ margin-bottom: 0px;
17
+ font-size:small;
18
+ color:grey;
19
+ font-family: arial
20
+ }
21
+ #page { width: 900px; margin-left:auto; margin-right:auto; padding-left:5pm; padding-right: 5px; }
22
+ #left { float: left; width: 890px; padding-right: 5px; }
23
+
24
+ #footer { clear: both; }
25
+ #footer ul { margin: 0; text-align:center;}
26
+ #footer ul li {display: inline }
27
+ #disqus_thread { margin:10px; padding: 10px}
28
+
29
+
30
+ #client9_header { margin: 0px; color: #000; font-family: georgia, "times new roman", times, serif;}
31
+ #client9_header a {text-decoration: none; color: black }
32
+ #client9_header a:active { text-decoration: underline }
33
+ #client9_header a:hover { text-decoration: underline }
34
+ #client9_header h1 { float:left; font-size: 300%; margin: 0px; margin-bottom:0px;}
35
+ #client9_header h2 { font-size: 200%; margin: 0px }
36
+ #client9_header ul { float: left; margin: 0; padding: 0px}
37
+ #client9_header ul li { display: inline; float: left; margin-left: 0px; margin-right: 5px; padding: 0px}
38
+
39
+ #post a { text-transform: underline; color: #004276; }
40
+
41
+ .readmore {text-transform: uppercase; font-size: x-small; font-family: arial; color: #004276; }
42
+
43
+ #post-pagination { font-family: arial; text-transform: uppercase; margin-bottom: 30px; font-size:small}
44
+
45
+ .previous { display: inline; float:left; margin-top: 0px; margin-left: 20px }
46
+ .next { display: inline; float:right; margin-top: 0px; margin-right: 20px; }
47
+ .disabled { color: #AAA; }
48
+ .pagination { margin-bottom: 20px; }
49
+
50
+ #mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; }
51
+ </style>
52
+ <LINK href="doxygen.css" rel="stylesheet" type="text/css" />
53
+ <LINK href="tabs.css" rel="stylesheet" type="text/css" />
54
+ <script type="text/javascript">
55
+ var _gaq = _gaq || [];
56
+ _gaq.push(['_setAccount', 'UA-33553605-1']);
57
+ _gaq.push(['_trackPageview']);
58
+
59
+ (function() {
60
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
61
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
62
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
63
+ })();
64
+ </script>
65
+ </head>
66
+ <body>
67
+ <div id="page">
68
+
69
+ <div id="client9_header">
70
+ <h1 id="client9_h1"><a href="/">client9</a></h1>
71
+ <div style="float:left; margin-left: 15px; margin-top: 10px;">
72
+ <span>Software Engineering by Nick Galbreath</span>
73
+ <ul>
74
+ <li><a href="/about/">about</a></li>
75
+ <li><a href="/archives/">archives</a></li>
76
+ <li><a href="/books/cida/">books</a></li>
77
+ <li><a href="/patents/">patents</a></li>
78
+ <li><a href="/projects/">projects</a></li>
79
+ </ul>
80
+ </div><!-- end float -->
81
+ <h2 id="client9_h2" style="margin-top:0px; clear:both"><a href="/projects/stringencoders/">stringencoders</a></h2>
82
+ </div><!-- end client9_header -->
83
+
84
+ <div id="left" style="margin-top: 10px; clear:both">
85
+ <div id="post">
@@ -0,0 +1,9 @@
1
+ #!/bin/sh
2
+ set -ex
3
+ if [ -z "${INDENT}" ]; then
4
+ INDENT=clang-format
5
+ fi
6
+ ${INDENT} --version
7
+ find . -name '*.[ch]' | xargs ${INDENT} -i -style=WebKit
8
+ git status --porcelain
9
+
@@ -0,0 +1,43 @@
1
+ <html>
2
+ <head>
3
+ <script src="base64.js" type="text/javascript"></script>
4
+ <title>base64 encode speeed</title>
5
+ </head>
6
+ <body>
7
+ <pre>
8
+ <script>
9
+ var t0 = Date.now();
10
+ for (var i = 0; i < 10000; ++i) {
11
+ base64.encode('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/');
12
+ }
13
+ document.writeln("JS encode: " + (Date.now() - t0));
14
+
15
+ if (window.btoa) {
16
+ var t0 = Date.now();
17
+ for (var i = 0; i < 10000; ++i) {
18
+ window.btoa('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/');
19
+ }
20
+ document.writeln("Native encode: " + (Date.now() - t0));
21
+ }
22
+
23
+
24
+ var x = base64.encode('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/');
25
+
26
+ var t0 = Date.now();
27
+ for (var i = 0; i < 10000; ++i) {
28
+ base64.decode(x);
29
+ }
30
+ document.writeln("JS decode: " + (Date.now() - t0));
31
+
32
+
33
+ if (window.atob) {
34
+ var t0 = Date.now();
35
+ for (var i = 0; i < 10000; ++i) {
36
+ window.atob(x);
37
+ }
38
+ document.writeln("Native decode: " + (Date.now() - t0));
39
+ }
40
+ </script>
41
+ </pre>
42
+ </body>
43
+ </html>
@@ -0,0 +1,209 @@
1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2
+ "http://www.w3.org/TR/html4/loose.dtd">
3
+ <html>
4
+ <head>
5
+ <script src="http://code.jquery.com/jquery-latest.js"></script>
6
+ <script src="base64.js"></script>
7
+ <link rel="stylesheet" href="qunit.css" type="text/css" media="screen" />
8
+ <script type="text/javascript" src="qunit.js"></script>
9
+ <script type="text/javascript">
10
+
11
+ var use_window = false;
12
+ if (use_window) {
13
+ encode = window.btoa;
14
+ decode = window.atob;
15
+ } else {
16
+ encode = base64.encode;
17
+ decode = base64.decode;
18
+ }
19
+
20
+ $(document).ready(function(){
21
+
22
+ module("Error Handling");
23
+
24
+ test("Encode empty input", function() {
25
+ equals(encode(''), '')
26
+ });
27
+
28
+ test("Encode non-stringinput", function() {
29
+ equals(encode(2), 'Mg==')
30
+ });
31
+
32
+ test("Encode undefined input", function() {
33
+ try {
34
+ encode();
35
+ ok(false, "expected exception");
36
+ } catch (e) {
37
+ ok(true, "got exception: " + e);
38
+ }
39
+ });
40
+
41
+ test("Encode invalid binary input", function() {
42
+ try {
43
+ encode("\u0100");
44
+ ok(false, "expected exception");
45
+ } catch (e) {
46
+ ok(true, "got exception: " + e);
47
+ }
48
+ });
49
+
50
+
51
+ test("Decode empty input", function() {
52
+ equals(decode(''), '')
53
+ });
54
+
55
+ // this is actually the same as "2" without padding
56
+ test("Decode non-string input1", function() {
57
+ try {
58
+ decode(2);
59
+ } catch (e) {
60
+ ok(true, "Got exception: " + e);
61
+ }
62
+ });
63
+
64
+ // same as "2222"
65
+ test("Decode non-string input2", function() {
66
+ equals(decode(2222).length, 3)
67
+ });
68
+
69
+ test("Decode non-multiple of 4 input length", function() {
70
+ try {
71
+ decode('AAAAA');
72
+ ok(false, "Excepted exception");
73
+ } catch (e) {
74
+ ok(true, "Got exception: " + e);
75
+ }
76
+ });
77
+
78
+ test("Decode invalid base64 input", function() {
79
+ try {
80
+ decode("\u0100");
81
+ ok(false, "expected exception");
82
+ } catch (e) {
83
+ ok(true, "got exception: " + e);
84
+ }
85
+ });
86
+
87
+ module("Correctness");
88
+
89
+ test("Encoding Length", function() {
90
+ var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis lorem at tellus sollicitudin pretium eu vitae ante. Maecenas tempus urna nec lacus vestibulum pharetra. Nunc in ullamcorper lectus. Nunc eu libero augue. Aliquam pellentesque libero ac nunc imperdiet vitae consequat dui eleifend. Nulla et magna id lacus iaculis sollicitudin id et leo. Vestibulum interdum neque ut sapien luctus malesuada eget aliquet m";
91
+ for (var i = 0; i < text.length; ++i) {
92
+ var s = text.substr(0,i);
93
+ var b64 = window.btoa(s)
94
+ equals(b64, base64.encode(s));
95
+ equals(s, base64.decode(b64));
96
+ }
97
+ });
98
+
99
+ test("Random Bytes", function() {
100
+ // random 3 bytes
101
+ // doing increment of 1 makes browser angry
102
+ for (var i = 0; i < 256; i += 7) {
103
+ for (var j = 0; j < 256; j += 9) {
104
+ for (var k = 0; k < 256; k += 11) {
105
+ var s = String.fromCharCode(i,j,k);
106
+ var b64 = window.btoa(s)
107
+ equals(b64,base64.encode(s));
108
+ equals(s,base64.decode(b64));
109
+ }
110
+ }
111
+ }
112
+ });
113
+
114
+ module("Exceptions");
115
+ // make sure what is thrown matches the types
116
+
117
+ test("Syntax Error", function() {
118
+
119
+ var e1, e2,x;
120
+ try {
121
+ base64.encode();
122
+ ok(false, "expected exception");
123
+ } catch (x) {
124
+ // odd scoping issue, have to copy exception object
125
+ e1 = x;
126
+ }
127
+
128
+ try {
129
+ window.btoa()
130
+ ok(false, "expected exception");
131
+ } catch (x) {
132
+ // odd scoping issue, have to copy exception object
133
+ e2 = x;
134
+ }
135
+
136
+ equals(typeof e1, typeof e2);
137
+ ok(e1 instanceof SyntaxError, "Our error is an instance of SyntaxError");
138
+ ok(e2 instanceof SyntaxError, "Native error is an instance of SyntaxError");
139
+ equals(e1.toString(), e2.toString());
140
+ });
141
+
142
+
143
+ test("DOM Exception", function() {
144
+
145
+ var e1, e2,x;
146
+ try {
147
+ base64.encode('\u1000');
148
+ ok(false, "expected exception");
149
+ } catch (x) {
150
+ // odd scoping issue, have to copy exception object
151
+ e1 = x;
152
+ }
153
+
154
+ try {
155
+ window.btoa('\u1000')
156
+ ok(false, "expected exception");
157
+ } catch (x) {
158
+ // odd scoping issue, have to copy exception object
159
+ e2 = x;
160
+ }
161
+
162
+ equals(typeof e1, typeof e2);
163
+ equals(e1.toString(), e2.toString());
164
+ equals(e1.code, e2.code);
165
+
166
+ });
167
+
168
+
169
+ test("Decode Exception", function() {
170
+
171
+ var e1, e2,x;
172
+ try {
173
+ base64.decode('\u1000');
174
+ ok(false, "expected exception");
175
+ } catch (x) {
176
+ // odd scoping issue, have to copy exception object
177
+ e1 = x;
178
+ }
179
+
180
+ try {
181
+ window.atob('\u1000')
182
+ ok(false, "expected exception");
183
+ } catch (x) {
184
+ // odd scoping issue, have to copy exception object
185
+ e2 = x;
186
+ }
187
+
188
+ equals(typeof e1, typeof e2);
189
+ ok(e1 instanceof Error, "Our error is an instance of Error");
190
+
191
+ // there is no spec for this, just a test to see if it is
192
+ // this fails!
193
+ ok(e2 instanceof Error, "Native error is an instance of Error");
194
+
195
+ // this passes
196
+ ok(e2 instanceof DOMException, "Native error is an instance of DOMException");
197
+ equals(e1.toString(), e2.toString());
198
+ });
199
+
200
+ });
201
+ </script>
202
+ </head>
203
+ <body>
204
+ <h1 id="qunit-header">Base64 window.[atob|btoa] tests</h1>
205
+ <h2 id="qunit-banner"></h2>
206
+ <h2 id="qunit-userAgent"></h2>
207
+ <ol id="qunit-tests"></ol>
208
+ </body>
209
+ </html>
@@ -0,0 +1,18 @@
1
+ <html>
2
+ <head>
3
+ <script src="base64.js" type="text/javascript"></script>
4
+ </head>
5
+ <body>
6
+ <pre>
7
+ <script>
8
+ var orig = "\u0000\u0000\u0000\u0001abcedeffsdfsdfs";
9
+ var b64 = window.btoa(orig);
10
+ var d = window.atob(b64);
11
+ document.writeln(orig);
12
+ document.writeln(b64);
13
+ document.writeln(base64.encode(orig));
14
+ document.writeln(base64.decode(b64));
15
+ </script>
16
+ </pre>
17
+ </body>
18
+ </html>
@@ -0,0 +1,176 @@
1
+ /*
2
+ * Copyright (c) 2010 Nick Galbreath
3
+ * http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person
6
+ * obtaining a copy of this software and associated documentation
7
+ * files (the "Software"), to deal in the Software without
8
+ * restriction, including without limitation the rights to use,
9
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the
11
+ * Software is furnished to do so, subject to the following
12
+ * conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be
15
+ * included in all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ * OTHER DEALINGS IN THE SOFTWARE.
25
+ */
26
+
27
+ /* base64 encode/decode compatible with window.btoa/atob
28
+ *
29
+ * window.atob/btoa is a Firefox extension to convert binary data (the "b")
30
+ * to base64 (ascii, the "a").
31
+ *
32
+ * It is also found in Safari and Chrome. It is not available in IE.
33
+ *
34
+ * if (!window.btoa) window.btoa = base64.encode
35
+ * if (!window.atob) window.atob = base64.decode
36
+ *
37
+ * The original spec's for atob/btoa are a bit lacking
38
+ * https://developer.mozilla.org/en/DOM/window.atob
39
+ * https://developer.mozilla.org/en/DOM/window.btoa
40
+ *
41
+ * window.btoa and base64.encode takes a string where charCodeAt is [0,255]
42
+ * If any character is not [0,255], then an DOMException(5) is thrown.
43
+ *
44
+ * window.atob and base64.decode take a base64-encoded string
45
+ * If the input length is not a multiple of 4, or contains invalid characters
46
+ * then an DOMException(5) is thrown.
47
+ */
48
+ var base64 = {};
49
+ base64.PADCHAR = '=';
50
+ base64.ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
51
+
52
+ base64.makeDOMException = function() {
53
+ // sadly in FF,Safari,Chrome you can't make a DOMException
54
+ var e, tmp;
55
+
56
+ try {
57
+ return new DOMException(DOMException.INVALID_CHARACTER_ERR);
58
+ } catch (tmp) {
59
+ // not available, just passback a duck-typed equiv
60
+ // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error
61
+ // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error/prototype
62
+ var ex = new Error("DOM Exception 5");
63
+
64
+ // ex.number and ex.description is IE-specific.
65
+ ex.code = ex.number = 5;
66
+ ex.name = ex.description = "INVALID_CHARACTER_ERR";
67
+
68
+ // Safari/Chrome output format
69
+ ex.toString = function() { return 'Error: ' + ex.name + ': ' + ex.message; };
70
+ return ex;
71
+ }
72
+ }
73
+
74
+ base64.getbyte64 = function(s,i) {
75
+ // This is oddly fast, except on Chrome/V8.
76
+ // Minimal or no improvement in performance by using a
77
+ // object with properties mapping chars to value (eg. 'A': 0)
78
+ var idx = base64.ALPHA.indexOf(s.charAt(i));
79
+ if (idx === -1) {
80
+ throw base64.makeDOMException();
81
+ }
82
+ return idx;
83
+ }
84
+
85
+ base64.decode = function(s) {
86
+ // convert to string
87
+ s = '' + s;
88
+ var getbyte64 = base64.getbyte64;
89
+ var pads, i, b10;
90
+ var imax = s.length
91
+ if (imax === 0) {
92
+ return s;
93
+ }
94
+
95
+ if (imax % 4 !== 0) {
96
+ throw base64.makeDOMException();
97
+ }
98
+
99
+ pads = 0
100
+ if (s.charAt(imax - 1) === base64.PADCHAR) {
101
+ pads = 1;
102
+ if (s.charAt(imax - 2) === base64.PADCHAR) {
103
+ pads = 2;
104
+ }
105
+ // either way, we want to ignore this last block
106
+ imax -= 4;
107
+ }
108
+
109
+ var x = [];
110
+ for (i = 0; i < imax; i += 4) {
111
+ b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) |
112
+ (getbyte64(s,i+2) << 6) | getbyte64(s,i+3);
113
+ x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff, b10 & 0xff));
114
+ }
115
+
116
+ switch (pads) {
117
+ case 1:
118
+ b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) | (getbyte64(s,i+2) << 6);
119
+ x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff));
120
+ break;
121
+ case 2:
122
+ b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12);
123
+ x.push(String.fromCharCode(b10 >> 16));
124
+ break;
125
+ }
126
+ return x.join('');
127
+ }
128
+
129
+ base64.getbyte = function(s,i) {
130
+ var x = s.charCodeAt(i);
131
+ if (x > 255) {
132
+ throw base64.makeDOMException();
133
+ }
134
+ return x;
135
+ }
136
+
137
+ base64.encode = function(s) {
138
+ if (arguments.length !== 1) {
139
+ throw new SyntaxError("Not enough arguments");
140
+ }
141
+ var padchar = base64.PADCHAR;
142
+ var alpha = base64.ALPHA;
143
+ var getbyte = base64.getbyte;
144
+
145
+ var i, b10;
146
+ var x = [];
147
+
148
+ // convert to string
149
+ s = '' + s;
150
+
151
+ var imax = s.length - s.length % 3;
152
+
153
+ if (s.length === 0) {
154
+ return s;
155
+ }
156
+ for (i = 0; i < imax; i += 3) {
157
+ b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8) | getbyte(s,i+2);
158
+ x.push(alpha.charAt(b10 >> 18));
159
+ x.push(alpha.charAt((b10 >> 12) & 0x3F));
160
+ x.push(alpha.charAt((b10 >> 6) & 0x3f));
161
+ x.push(alpha.charAt(b10 & 0x3f));
162
+ }
163
+ switch (s.length - imax) {
164
+ case 1:
165
+ b10 = getbyte(s,i) << 16;
166
+ x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +
167
+ padchar + padchar);
168
+ break;
169
+ case 2:
170
+ b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8);
171
+ x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +
172
+ alpha.charAt((b10 >> 6) & 0x3f) + padchar);
173
+ break;
174
+ }
175
+ return x.join('');
176
+ }