angular-utf8-base64.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 Andrey Bezyazychniy
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. 'use strict';
  21. angular.module('ab-base64',[]).constant('base64', (function() {
  22. /*
  23. * Encapsulation of Vassilis Petroulias's base64.js library for AngularJS
  24. * Original notice included below
  25. */
  26. /*
  27. Copyright Vassilis Petroulias [DRDigit]
  28. Licensed under the Apache License, Version 2.0 (the "License");
  29. you may not use this file except in compliance with the License.
  30. You may obtain a copy of the License at
  31. http://www.apache.org/licenses/LICENSE-2.0
  32. Unless required by applicable law or agreed to in writing, software
  33. distributed under the License is distributed on an "AS IS" BASIS,
  34. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  35. See the License for the specific language governing permissions and
  36. limitations under the License.
  37. */
  38. var B64 = {
  39. alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  40. lookup: null,
  41. ie: /MSIE /.test(navigator.userAgent),
  42. ieo: /MSIE [67]/.test(navigator.userAgent),
  43. encode: function (s) {
  44. /* jshint bitwise:false */
  45. var buffer = B64.toUtf8(s),
  46. position = -1,
  47. result,
  48. len = buffer.length,
  49. nan0, nan1, nan2, enc = [, , , ];
  50. if (B64.ie) {
  51. result = [];
  52. while (++position < len) {
  53. nan0 = buffer[position];
  54. nan1 = buffer[++position];
  55. enc[0] = nan0 >> 2;
  56. enc[1] = ((nan0 & 3) << 4) | (nan1 >> 4);
  57. if (isNaN(nan1))
  58. enc[2] = enc[3] = 64;
  59. else {
  60. nan2 = buffer[++position];
  61. enc[2] = ((nan1 & 15) << 2) | (nan2 >> 6);
  62. enc[3] = (isNaN(nan2)) ? 64 : nan2 & 63;
  63. }
  64. result.push(B64.alphabet.charAt(enc[0]), B64.alphabet.charAt(enc[1]), B64.alphabet.charAt(enc[2]), B64.alphabet.charAt(enc[3]));
  65. }
  66. return result.join('');
  67. } else {
  68. result = '';
  69. while (++position < len) {
  70. nan0 = buffer[position];
  71. nan1 = buffer[++position];
  72. enc[0] = nan0 >> 2;
  73. enc[1] = ((nan0 & 3) << 4) | (nan1 >> 4);
  74. if (isNaN(nan1))
  75. enc[2] = enc[3] = 64;
  76. else {
  77. nan2 = buffer[++position];
  78. enc[2] = ((nan1 & 15) << 2) | (nan2 >> 6);
  79. enc[3] = (isNaN(nan2)) ? 64 : nan2 & 63;
  80. }
  81. result += B64.alphabet[enc[0]] + B64.alphabet[enc[1]] + B64.alphabet[enc[2]] + B64.alphabet[enc[3]];
  82. }
  83. return result;
  84. }
  85. },
  86. decode: function (s) {
  87. /* jshint bitwise:false */
  88. s = s.replace(/\s/g, '');
  89. if (s.length % 4)
  90. throw new Error('InvalidLengthError: decode failed: The string to be decoded is not the correct length for a base64 encoded string.');
  91. if(/[^A-Za-z0-9+\/=\s]/g.test(s))
  92. throw new Error('InvalidCharacterError: decode failed: The string contains characters invalid in a base64 encoded string.');
  93. var buffer = B64.fromUtf8(s),
  94. position = 0,
  95. result,
  96. len = buffer.length;
  97. if (B64.ieo) {
  98. result = [];
  99. while (position < len) {
  100. if (buffer[position] < 128)
  101. result.push(String.fromCharCode(buffer[position++]));
  102. else if (buffer[position] > 191 && buffer[position] < 224)
  103. result.push(String.fromCharCode(((buffer[position++] & 31) << 6) | (buffer[position++] & 63)));
  104. else
  105. result.push(String.fromCharCode(((buffer[position++] & 15) << 12) | ((buffer[position++] & 63) << 6) | (buffer[position++] & 63)));
  106. }
  107. return result.join('');
  108. } else {
  109. result = '';
  110. while (position < len) {
  111. if (buffer[position] < 128)
  112. result += String.fromCharCode(buffer[position++]);
  113. else if (buffer[position] > 191 && buffer[position] < 224)
  114. result += String.fromCharCode(((buffer[position++] & 31) << 6) | (buffer[position++] & 63));
  115. else
  116. result += String.fromCharCode(((buffer[position++] & 15) << 12) | ((buffer[position++] & 63) << 6) | (buffer[position++] & 63));
  117. }
  118. return result;
  119. }
  120. },
  121. toUtf8: function (s) {
  122. /* jshint bitwise:false */
  123. var position = -1,
  124. len = s.length,
  125. chr, buffer = [];
  126. if (/^[\x00-\x7f]*$/.test(s)) while (++position < len)
  127. buffer.push(s.charCodeAt(position));
  128. else while (++position < len) {
  129. chr = s.charCodeAt(position);
  130. if (chr < 128)
  131. buffer.push(chr);
  132. else if (chr < 2048)
  133. buffer.push((chr >> 6) | 192, (chr & 63) | 128);
  134. else
  135. buffer.push((chr >> 12) | 224, ((chr >> 6) & 63) | 128, (chr & 63) | 128);
  136. }
  137. return buffer;
  138. },
  139. fromUtf8: function (s) {
  140. /* jshint bitwise:false */
  141. var position = -1,
  142. len, buffer = [],
  143. enc = [, , , ];
  144. if (!B64.lookup) {
  145. len = B64.alphabet.length;
  146. B64.lookup = {};
  147. while (++position < len)
  148. B64.lookup[B64.alphabet.charAt(position)] = position;
  149. position = -1;
  150. }
  151. len = s.length;
  152. while (++position < len) {
  153. enc[0] = B64.lookup[s.charAt(position)];
  154. enc[1] = B64.lookup[s.charAt(++position)];
  155. buffer.push((enc[0] << 2) | (enc[1] >> 4));
  156. enc[2] = B64.lookup[s.charAt(++position)];
  157. if (enc[2] === 64)
  158. break;
  159. buffer.push(((enc[1] & 15) << 4) | (enc[2] >> 2));
  160. enc[3] = B64.lookup[s.charAt(++position)];
  161. if (enc[3] === 64)
  162. break;
  163. buffer.push(((enc[2] & 3) << 6) | enc[3]);
  164. }
  165. return buffer;
  166. }
  167. };
  168. var B64url = {
  169. decode: function(input) {
  170. // Replace non-url compatible chars with base64 standard chars
  171. input = input
  172. .replace(/-/g, '+')
  173. .replace(/_/g, '/');
  174. // Pad out with standard base64 required padding characters
  175. var pad = input.length % 4;
  176. if(pad) {
  177. if(pad === 1) {
  178. throw new Error('InvalidLengthError: Input base64url string is the wrong length to determine padding');
  179. }
  180. input += new Array(5-pad).join('=');
  181. }
  182. return B64.decode(input);
  183. },
  184. encode: function(input) {
  185. var output = B64.encode(input);
  186. return output
  187. .replace(/\+/g, '-')
  188. .replace(/\//g, '_')
  189. .split('=', 1)[0];
  190. }
  191. };
  192. return {
  193. decode: B64.decode,
  194. encode: B64.encode,
  195. urldecode: B64url.decode,
  196. urlencode: B64url.encode,
  197. };
  198. })());