1 // ==========================================================================
  2 // Project:   The M-Project - Mobile HTML5 Application Framework
  3 // Copyright: (c) 2010 M-Way Solutions GmbH. All rights reserved.
  4 //            (c) 2011 panacoda GmbH. All rights reserved.
  5 // Creator:   Dominik
  6 // Date:      11.11.2010
  7 // License:   Dual licensed under the MIT or GPL Version 2 licenses.
  8 //            http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE
  9 //            http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE
 10 // ==========================================================================
 11 
 12 m_require('core/foundation/object.js');
 13 
 14 /**
 15  * @class
 16  *
 17  * This prototype defines decoding and encoding mechanisms based on the Base64 algorithm. You
 18  * normally don't call this object respectively its methods directly, but let M.Cypher handle
 19  * this.
 20  *
 21  * @extends M.Object
 22  */
 23 M.Base64 = M.Object.extend(
 24 /** @scope M.Base64.prototype */ {
 25 
 26     /**
 27      * The type of this object.
 28      *
 29      * @type String
 30      */
 31     type: 'M.Base64',
 32 
 33     /**
 34      * The key string for the base 64 decoding and encoding.
 35      *
 36      * @type String
 37      */
 38     keyString: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",    
 39 
 40     /**
 41      * This method encodes a given input string, using the base64 encoding.
 42      *
 43      * @param {String} input The string to be encoded.
 44      * @returns {String} The base64 encoded string.
 45      */
 46     encode: function(input) {
 47         var output = '';
 48         var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
 49         var i = 0;
 50 
 51         input = M.Cypher.utf8_encode(input);
 52 
 53         while (i < input.length) {
 54             chr1 = input.charCodeAt(i++);
 55             chr2 = input.charCodeAt(i++);
 56             chr3 = input.charCodeAt(i++);
 57 
 58             enc1 = chr1 >> 2;
 59             enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
 60             enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
 61             enc4 = chr3 & 63;
 62 
 63             if(isNaN(chr2)) {
 64                 enc3 = enc4 = 64;
 65             } else if(isNaN(chr3)) {
 66                 enc4 = 64;
 67             }
 68 
 69             output += this.keyString.charAt(enc1) + this.keyString.charAt(enc2) + this.keyString.charAt(enc3) + this.keyString.charAt(enc4);
 70         }
 71 
 72         return output;
 73     },
 74 
 75     /**
 76      * This method decodes a given input string, using the base64 decoding.
 77      *
 78      * @param {String} input The string to be decoded.
 79      * @returns {String} The base64 decoded string.
 80      */
 81     decode: function(input) {
 82         var output = "";
 83         var chr1, chr2, chr3;
 84         var enc1, enc2, enc3, enc4;
 85         var i = 0;
 86 
 87         input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 88 
 89         while (i < input.length) {
 90             enc1 = this.keyString.indexOf(input.charAt(i++));
 91             enc2 = this.keyString.indexOf(input.charAt(i++));
 92             enc3 = this.keyString.indexOf(input.charAt(i++));
 93             enc4 = this.keyString.indexOf(input.charAt(i++));
 94 
 95             chr1 = (enc1 << 2) | (enc2 >> 4);
 96             chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
 97             chr3 = ((enc3 & 3) << 6) | enc4;
 98 
 99             output = output + String.fromCharCode(chr1);
100 
101             if(enc3 != 64) {
102                 output = output + String.fromCharCode(chr2);
103             }
104             
105             if(enc4 != 64) {
106                 output = output + String.fromCharCode(chr3);
107             }
108         }
109 
110         return M.Cypher.utf8_decode(output);
111     }
112 
113 });