github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/clients/chrome/clip-it-good/base64.js (about)

     1  /*
     2  Copyright (c) 2008 Fred Palmer fred.palmer_at_gmail.com
     3  
     4  Permission is hereby granted, free of charge, to any person
     5  obtaining a copy of this software and associated documentation
     6  files (the "Software"), to deal in the Software without
     7  restriction, including without limitation the rights to use,
     8  copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  copies of the Software, and to permit persons to whom the
    10  Software is furnished to do so, subject to the following
    11  conditions:
    12  
    13  The above copyright notice and this permission notice shall be
    14  included in all copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    17  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    18  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    19  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    20  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    21  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    22  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    23  OTHER DEALINGS IN THE SOFTWARE.
    24  */
    25  function StringBuffer()
    26  { 
    27      this.buffer = []; 
    28  } 
    29  
    30  StringBuffer.prototype.append = function append(string)
    31  { 
    32      this.buffer.push(string); 
    33      return this; 
    34  }; 
    35  
    36  StringBuffer.prototype.toString = function toString()
    37  { 
    38      return this.buffer.join(""); 
    39  }; 
    40  
    41  var Base64 =
    42  {
    43      codex : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    44  
    45      encode : function (input)
    46      {
    47          var output = new StringBuffer();
    48  
    49          var enumerator = new Utf8EncodeEnumerator(input);
    50          while (enumerator.moveNext())
    51          {
    52              var chr1 = enumerator.current;
    53  
    54              enumerator.moveNext();
    55              var chr2 = enumerator.current;
    56  
    57              enumerator.moveNext();
    58              var chr3 = enumerator.current;
    59  
    60              var enc1 = chr1 >> 2;
    61              var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    62              var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    63              var enc4 = chr3 & 63;
    64  
    65              if (isNaN(chr2))
    66              {
    67                  enc3 = enc4 = 64;
    68              }
    69              else if (isNaN(chr3))
    70              {
    71                  enc4 = 64;
    72              }
    73  
    74              output.append(this.codex.charAt(enc1) + this.codex.charAt(enc2) + this.codex.charAt(enc3) + this.codex.charAt(enc4));
    75          }
    76  
    77          return output.toString();
    78      },
    79  
    80      decode : function (input)
    81      {
    82          // TypedArray usage added by brett@haxor.com 11/27/2010
    83          var size = 0;
    84          var buffer = new ArrayBuffer(input.length);
    85          var output = new Uint8Array(buffer, 0);
    86  
    87          var enumerator = new Base64DecodeEnumerator(input);
    88          while (enumerator.moveNext()) {
    89              output[size++] = enumerator.current;
    90          }
    91  
    92          // There is nothing in the TypedArray spec to copy/subset a buffer,
    93          // so we have to do a copy to ensure that typedarray.buffer is the
    94          // correct length when passed to XmlHttpRequest methods, etc.
    95          var outputBuffer = new ArrayBuffer(size);
    96          var outputArray = new Uint8Array(outputBuffer, 0);
    97          for (var i = 0; i < size; i++) {
    98            outputArray[i] = output[i];
    99          }
   100          return outputArray;
   101      }
   102  }
   103  
   104  
   105  function Utf8EncodeEnumerator(input)
   106  {
   107      this._input = input;
   108      this._index = -1;
   109      this._buffer = [];
   110  }
   111  
   112  Utf8EncodeEnumerator.prototype =
   113  {
   114      current: Number.NaN,
   115  
   116      moveNext: function()
   117      {
   118          if (this._buffer.length > 0)
   119          {
   120              this.current = this._buffer.shift();
   121              return true;
   122          }
   123          else if (this._index >= (this._input.length - 1))
   124          {
   125              this.current = Number.NaN;
   126              return false;
   127          }
   128          else
   129          {
   130              var charCode = this._input.charCodeAt(++this._index);
   131  
   132              // "\r\n" -> "\n"
   133              //
   134              if ((charCode == 13) && (this._input.charCodeAt(this._index + 1) == 10))
   135              {
   136                  charCode = 10;
   137                  this._index += 2;
   138              }
   139  
   140              if (charCode < 128)
   141              {
   142                  this.current = charCode;
   143              }
   144              else if ((charCode > 127) && (charCode < 2048))
   145              {
   146                  this.current = (charCode >> 6) | 192;
   147                  this._buffer.push((charCode & 63) | 128);
   148              }
   149              else
   150              {
   151                  this.current = (charCode >> 12) | 224;
   152                  this._buffer.push(((charCode >> 6) & 63) | 128);
   153                  this._buffer.push((charCode & 63) | 128);
   154              }
   155  
   156              return true;
   157          }
   158      }
   159  }
   160  
   161  function Base64DecodeEnumerator(input)
   162  {
   163      this._input = input;
   164      this._index = -1;
   165      this._buffer = [];
   166  }
   167  
   168  Base64DecodeEnumerator.prototype =
   169  {
   170      current: 64,
   171  
   172      moveNext: function()
   173      {
   174          if (this._buffer.length > 0)
   175          {
   176              this.current = this._buffer.shift();
   177              return true;
   178          }
   179          else if (this._index >= (this._input.length - 1))
   180          {
   181              this.current = 64;
   182              return false;
   183          }
   184          else
   185          {
   186              var enc1 = Base64.codex.indexOf(this._input.charAt(++this._index));
   187              var enc2 = Base64.codex.indexOf(this._input.charAt(++this._index));
   188              var enc3 = Base64.codex.indexOf(this._input.charAt(++this._index));
   189              var enc4 = Base64.codex.indexOf(this._input.charAt(++this._index));
   190  
   191              var chr1 = (enc1 << 2) | (enc2 >> 4);
   192              var chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
   193              var chr3 = ((enc3 & 3) << 6) | enc4;
   194  
   195              this.current = chr1;
   196  
   197              if (enc3 != 64)
   198                  this._buffer.push(chr2);
   199  
   200              if (enc4 != 64)
   201                  this._buffer.push(chr3);
   202  
   203              return true;
   204          }
   205      }
   206  };