corrade-http-templates – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
15 | eva | 1 | /*! |
2 | * jquery.base64.js 0.1 - https://github.com/yckart/jquery.base64.js |
||
3 | * Makes Base64 en & -decoding simpler as it is. |
||
4 | * |
||
5 | * Based upon: https://gist.github.com/Yaffle/1284012 |
||
6 | * |
||
7 | * Copyright (c) 2012 Yannick Albert (http://yckart.com) |
||
8 | * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php). |
||
9 | * 2013/02/10 |
||
10 | **/ |
||
11 | ;(function($) { |
||
12 | |||
13 | var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", |
||
14 | a256 = '', |
||
15 | r64 = [256], |
||
16 | r256 = [256], |
||
17 | i = 0; |
||
18 | |||
19 | var UTF8 = { |
||
20 | |||
21 | /** |
||
22 | * Encode multi-byte Unicode string into utf-8 multiple single-byte characters |
||
23 | * (BMP / basic multilingual plane only) |
||
24 | * |
||
25 | * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars |
||
26 | * |
||
27 | * @param {String} strUni Unicode string to be encoded as UTF-8 |
||
28 | * @returns {String} encoded string |
||
29 | */ |
||
30 | encode: function(strUni) { |
||
31 | // use regular expressions & String.replace callback function for better efficiency |
||
32 | // than procedural approaches |
||
33 | var strUtf = strUni.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz |
||
34 | function(c) { |
||
35 | var cc = c.charCodeAt(0); |
||
36 | return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f); |
||
37 | }) |
||
38 | .replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz |
||
39 | function(c) { |
||
40 | var cc = c.charCodeAt(0); |
||
41 | return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3F, 0x80 | cc & 0x3f); |
||
42 | }); |
||
43 | return strUtf; |
||
44 | }, |
||
45 | |||
46 | /** |
||
47 | * Decode utf-8 encoded string back into multi-byte Unicode characters |
||
48 | * |
||
49 | * @param {String} strUtf UTF-8 string to be decoded back to Unicode |
||
50 | * @returns {String} decoded string |
||
51 | */ |
||
52 | decode: function(strUtf) { |
||
53 | // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char! |
||
54 | var strUni = strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars |
||
55 | function(c) { // (note parentheses for precence) |
||
56 | var cc = ((c.charCodeAt(0) & 0x0f) << 12) | ((c.charCodeAt(1) & 0x3f) << 6) | (c.charCodeAt(2) & 0x3f); |
||
57 | return String.fromCharCode(cc); |
||
58 | }) |
||
59 | .replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars |
||
60 | function(c) { // (note parentheses for precence) |
||
61 | var cc = (c.charCodeAt(0) & 0x1f) << 6 | c.charCodeAt(1) & 0x3f; |
||
62 | return String.fromCharCode(cc); |
||
63 | }); |
||
64 | return strUni; |
||
65 | } |
||
66 | }; |
||
67 | |||
68 | while(i < 256) { |
||
69 | var c = String.fromCharCode(i); |
||
70 | a256 += c; |
||
71 | r256[i] = i; |
||
72 | r64[i] = b64.indexOf(c); |
||
73 | ++i; |
||
74 | } |
||
75 | |||
76 | function code(s, discard, alpha, beta, w1, w2) { |
||
77 | s = String(s); |
||
78 | var buffer = 0, |
||
79 | i = 0, |
||
80 | length = s.length, |
||
81 | result = '', |
||
82 | bitsInBuffer = 0; |
||
83 | |||
84 | while(i < length) { |
||
85 | var c = s.charCodeAt(i); |
||
86 | c = c < 256 ? alpha[c] : -1; |
||
87 | |||
88 | buffer = (buffer << w1) + c; |
||
89 | bitsInBuffer += w1; |
||
90 | |||
91 | while(bitsInBuffer >= w2) { |
||
92 | bitsInBuffer -= w2; |
||
93 | var tmp = buffer >> bitsInBuffer; |
||
94 | result += beta.charAt(tmp); |
||
95 | buffer ^= tmp << bitsInBuffer; |
||
96 | } |
||
97 | ++i; |
||
98 | } |
||
99 | if(!discard && bitsInBuffer > 0) result += beta.charAt(buffer << (w2 - bitsInBuffer)); |
||
100 | return result; |
||
101 | } |
||
102 | |||
103 | var Plugin = $.base64 = function(dir, input, encode) { |
||
104 | return input ? Plugin[dir](input, encode) : dir ? null : this; |
||
105 | }; |
||
106 | |||
107 | Plugin.btoa = Plugin.encode = function(plain, utf8encode) { |
||
108 | plain = Plugin.raw === false || Plugin.utf8encode || utf8encode ? UTF8.encode(plain) : plain; |
||
109 | plain = code(plain, false, r256, b64, 8, 6); |
||
110 | return plain + '===='.slice((plain.length % 4) || 4); |
||
111 | }; |
||
112 | |||
113 | Plugin.atob = Plugin.decode = function(coded, utf8decode) { |
||
114 | coded = String(coded).split('='); |
||
115 | var i = coded.length; |
||
116 | do {--i; |
||
117 | coded[i] = code(coded[i], true, r64, a256, 6, 8); |
||
118 | } while (i > 0); |
||
119 | coded = coded.join(''); |
||
120 | return Plugin.raw === false || Plugin.utf8decode || utf8decode ? UTF8.decode(coded) : coded; |
||
121 | }; |
||
122 | }(jQuery)); |