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