wpmu-ajaxdata.js
9 years ago
wpmu-binary.js
9 years ago
wpmu-card-list.js
9 years ago
wpmu-ui-hooks.js
9 years ago
wpmu-ui-progress.js
9 years ago
wpmu-ui-window.js
9 years ago
wpmu-ui.js
9 years ago
wpmu-vnav.js
9 years ago
wpmu-binary.js
165 lines
| 1 | /*! |
| 2 | * WPMU Dev UI library |
| 3 | * (Philipp Stracker for WPMU Dev) |
| 4 | * |
| 5 | * This module provides the WpmUiBinary object that is used to |
| 6 | * serialize/deserialize data in base64. |
| 7 | * |
| 8 | * @version 1.0.0 |
| 9 | * @author Philipp Stracker for WPMU Dev |
| 10 | * @requires jQuery |
| 11 | */ |
| 12 | /*global jQuery:false */ |
| 13 | /*global window:false */ |
| 14 | /*global document:false */ |
| 15 | /*global XMLHttpRequest:false */ |
| 16 | |
| 17 | (function( wpmUi ) { |
| 18 | |
| 19 | /*===============================*\ |
| 20 | =================================== |
| 21 | == == |
| 22 | == UTF8-DATA == |
| 23 | == == |
| 24 | =================================== |
| 25 | \*===============================*/ |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Handles conversions of binary <-> text. |
| 32 | * |
| 33 | * @type WpmUiBinary |
| 34 | * @since 1.0.0 |
| 35 | */ |
| 36 | wpmUi.WpmUiBinary = function() { |
| 37 | var map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; |
| 38 | |
| 39 | wpmUi.WpmUiBinary.utf8_encode = function utf8_encode( string ) { |
| 40 | if ( typeof string !== 'string' ) { |
| 41 | return string; |
| 42 | } else { |
| 43 | string = string.replace(/\r\n/g, "\n"); |
| 44 | } |
| 45 | var output = '', i = 0, charCode; |
| 46 | |
| 47 | for ( i; i < string.length; i++ ) { |
| 48 | charCode = string.charCodeAt(i); |
| 49 | |
| 50 | if ( charCode < 128 ) { |
| 51 | output += String.fromCharCode( charCode ); |
| 52 | } else if ( (charCode > 127) && (charCode < 2048) ) { |
| 53 | output += String.fromCharCode( (charCode >> 6) | 192 ); |
| 54 | output += String.fromCharCode( (charCode & 63) | 128 ); |
| 55 | } else { |
| 56 | output += String.fromCharCode( (charCode >> 12) | 224 ); |
| 57 | output += String.fromCharCode( ((charCode >> 6) & 63) | 128 ); |
| 58 | output += String.fromCharCode( (charCode & 63) | 128 ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return output; |
| 63 | }; |
| 64 | |
| 65 | wpmUi.WpmUiBinary.utf8_decode = function utf8_decode( string ) { |
| 66 | if ( typeof string !== 'string' ) { |
| 67 | return string; |
| 68 | } |
| 69 | |
| 70 | var output = '', i = 0, charCode = 0; |
| 71 | |
| 72 | while ( i < string.length ) { |
| 73 | charCode = string.charCodeAt(i); |
| 74 | |
| 75 | if ( charCode < 128 ) { |
| 76 | output += String.fromCharCode( charCode ); |
| 77 | i += 1; |
| 78 | } else if ( (charCode > 191) && (charCode < 224) ) { |
| 79 | output += String.fromCharCode(((charCode & 31) << 6) | (string.charCodeAt(i + 1) & 63)); |
| 80 | i += 2; |
| 81 | } else { |
| 82 | output += String.fromCharCode(((charCode & 15) << 12) | ((string.charCodeAt(i + 1) & 63) << 6) | (string.charCodeAt(i + 2) & 63)); |
| 83 | i += 3; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return output; |
| 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * Converts a utf-8 string into an base64 encoded string |
| 92 | * |
| 93 | * @since 1.0.15 |
| 94 | * @param string input A string with any encoding. |
| 95 | * @return string |
| 96 | */ |
| 97 | wpmUi.WpmUiBinary.base64_encode = function base64_encode( input ) { |
| 98 | if ( typeof input !== 'string' ) { |
| 99 | return input; |
| 100 | } else { |
| 101 | input = wpmUi.WpmUiBinary.utf8_encode( input ); |
| 102 | } |
| 103 | var output = '', a, b, c, d, e, f, g, i = 0; |
| 104 | |
| 105 | while ( i < input.length ) { |
| 106 | a = input.charCodeAt(i++); |
| 107 | b = input.charCodeAt(i++); |
| 108 | c = input.charCodeAt(i++); |
| 109 | d = a >> 2; |
| 110 | e = ((a & 3) << 4) | (b >> 4); |
| 111 | f = ((b & 15) << 2) | (c >> 6); |
| 112 | g = c & 63; |
| 113 | |
| 114 | if ( isNaN( b ) ) { |
| 115 | f = g = 64; |
| 116 | } else if ( isNaN( c ) ) { |
| 117 | g = 64; |
| 118 | } |
| 119 | |
| 120 | output += map.charAt( d ) + map.charAt( e ) + map.charAt( f ) + map.charAt( g ); |
| 121 | } |
| 122 | |
| 123 | return output; |
| 124 | }; |
| 125 | |
| 126 | /** |
| 127 | * Converts a base64 string into the original (binary) data |
| 128 | * |
| 129 | * @since 1.0.15 |
| 130 | * @param string input Base 64 encoded text |
| 131 | * @return string |
| 132 | */ |
| 133 | wpmUi.WpmUiBinary.base64_decode = function base64_decode( input ) { |
| 134 | if ( typeof input !== 'string' ) { |
| 135 | return input; |
| 136 | } else { |
| 137 | input.replace(/[^A-Za-z0-9\+\/\=]/g, ''); |
| 138 | } |
| 139 | var output = '', a, b, c, d, e, f, g, i = 0; |
| 140 | |
| 141 | while ( i < input.length ) { |
| 142 | d = map.indexOf( input.charAt( i++ ) ); |
| 143 | e = map.indexOf( input.charAt( i++ ) ); |
| 144 | f = map.indexOf( input.charAt( i++ ) ); |
| 145 | g = map.indexOf( input.charAt( i++ ) ); |
| 146 | |
| 147 | a = (d << 2) | (e >> 4); |
| 148 | b = ((e & 15) << 4) | (f >> 2); |
| 149 | c = ((f & 3) << 6) | g; |
| 150 | |
| 151 | output += String.fromCharCode( a ); |
| 152 | if ( f !== 64 ) { |
| 153 | output += String.fromCharCode( b ); |
| 154 | } |
| 155 | if ( g !== 64 ) { |
| 156 | output += String.fromCharCode( c ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return wpmUi.WpmUiBinary.utf8_decode( output ); |
| 161 | }; |
| 162 | |
| 163 | }; /* ** End: WpmUiBinary ** */ |
| 164 | |
| 165 | }( window.wpmUi = window.wpmUi || {} )); |