You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ia...@apache.org on 2013/07/03 22:39:21 UTC

[1/3] js commit: [CB-4004] Add base64 encoding utility function

Updated Branches:
  refs/heads/master 98a62ff87 -> 0e89b601a


[CB-4004] Add base64 encoding utility function


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/ff218213
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/ff218213
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/ff218213

Branch: refs/heads/master
Commit: ff218213e141c7a42f0ee6b7bce30ef4858f016f
Parents: 98a62ff
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Jul 3 16:33:07 2013 -0400
Committer: Ian Clelland <ic...@chromium.org>
Committed: Wed Jul 3 16:37:24 2013 -0400

----------------------------------------------------------------------
 lib/common/base64.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++
 test/test.base64.js  | 50 +++++++++++++++++++++++++++++++++
 2 files changed, 121 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/ff218213/lib/common/base64.js
----------------------------------------------------------------------
diff --git a/lib/common/base64.js b/lib/common/base64.js
new file mode 100644
index 0000000..38426e6
--- /dev/null
+++ b/lib/common/base64.js
@@ -0,0 +1,71 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+var base64 = exports;
+
+base64.fromArrayBuffer = function(arrayBuffer) {
+  var array = new Uint8Array(arrayBuffer);
+  return uint8ToBase64(array);
+};
+
+//------------------------------------------------------------------------------
+
+/* This code is based on the performance tests at http://jsperf.com/b64tests
+ * This 12-bit-at-a-time algorithm was the best performing version on all
+ * platforms tested.
+ */
+
+var b64_6bit = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+var b64_12bit;
+
+var b64_12bitTable = function() {
+    b64_12bit = [];
+    for (var i=0; i<64; i++) {
+        for (var j=0; j<64; j++) {
+            b64_12bit[i*64+j] = b64_6bit[i] + b64_6bit[j];
+        }
+    }
+    b64_12bitTable = function() { return b64_12bit; };
+    return b64_12bit;
+}
+
+function uint8ToBase64(rawData) {
+    var numBytes = rawData.byteLength;
+    var output="";
+    var segment;
+    var table = b64_12bitTable();
+    for (var i=0;i<numBytes-2;i+=3) {
+        segment = (rawData[i] << 16) + (rawData[i+1] << 8) + rawData[i+2];
+        output += table[segment >> 12];
+        output += table[segment & 0xfff];
+    }
+    if (numBytes - i == 2) {
+        segment = (rawData[i] << 16) + (rawData[i+1] << 8);
+        output += table[segment >> 12];
+        output += b64_6bit[(segment & 0xfff) >> 6];
+        output += '=';
+    } else if (numBytes - i == 1) {
+        segment = (rawData[i] << 16);
+        output += table[segment >> 12];
+        output += '==';
+    }
+    return output;
+}

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/ff218213/test/test.base64.js
----------------------------------------------------------------------
diff --git a/test/test.base64.js b/test/test.base64.js
new file mode 100644
index 0000000..666cafe
--- /dev/null
+++ b/test/test.base64.js
@@ -0,0 +1,50 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+describe("base64", function () {
+    var base64 = require('cordova/base64');
+
+    it("can base64 encode strings correctly", function () {
+        var arrayBuffer = new ArrayBuffer(6),
+            view = new Uint8Array(arrayBuffer);
+        for (var i = 0; i < view.length; i++) {
+            view[i] = i;
+        }
+        expect(base64.fromArrayBuffer(arrayBuffer.slice(0,1))).toBe('AA==');
+        expect(base64.fromArrayBuffer(arrayBuffer.slice(0,2))).toBe('AAE=');
+        expect(base64.fromArrayBuffer(arrayBuffer.slice(0,3))).toBe('AAEC');
+        expect(base64.fromArrayBuffer(arrayBuffer.slice(0,4))).toBe('AAECAw==');
+        expect(base64.fromArrayBuffer(arrayBuffer.slice(0,5))).toBe('AAECAwQ=');
+        expect(base64.fromArrayBuffer(arrayBuffer)).toBe('AAECAwQF');
+    });
+
+    it("can base64 encode a binary string in an ArrayBuffer", function () {
+      var arrayBuffer = new ArrayBuffer(256),
+          view = new Uint8Array(arrayBuffer);
+          base64string = 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=='
+
+      for (var i = 0; i < view.length; i++) {
+        view[i] = i;
+      }
+
+      expect(base64.fromArrayBuffer(arrayBuffer)).toBe(base64string);
+    });
+});


[3/3] js commit: Use new base64 method for iOS and OSX as well

Posted by ia...@apache.org.
Use new base64 method for iOS and OSX as well


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/0e89b601
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/0e89b601
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/0e89b601

Branch: refs/heads/master
Commit: 0e89b601a586877a7ff3308f75e41b6360d9ec12
Parents: 5ba835c
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Jul 3 16:34:22 2013 -0400
Committer: Ian Clelland <ic...@chromium.org>
Committed: Wed Jul 3 16:37:40 2013 -0400

----------------------------------------------------------------------
 lib/ios/exec.js |  9 ++-------
 lib/osx/exec.js | 11 +++--------
 2 files changed, 5 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/0e89b601/lib/ios/exec.js
----------------------------------------------------------------------
diff --git a/lib/ios/exec.js b/lib/ios/exec.js
index 93269c5..be05e94 100644
--- a/lib/ios/exec.js
+++ b/lib/ios/exec.js
@@ -28,6 +28,7 @@
 var cordova = require('cordova'),
     channel = require('cordova/channel'),
     utils = require('cordova/utils'),
+    base64 = require('cordova/base64'),
     jsToNativeModes = {
         IFRAME_NAV: 0,
         XHR_NO_PAYLOAD: 1,
@@ -69,17 +70,11 @@ function massageArgsJsToNative(args) {
        return args;
     }
     var ret = [];
-    var encodeArrayBufferAs8bitString = function(ab) {
-        return String.fromCharCode.apply(null, new Uint8Array(ab));
-    };
-    var encodeArrayBufferAsBase64 = function(ab) {
-        return window.btoa(encodeArrayBufferAs8bitString(ab));
-    };
     args.forEach(function(arg, i) {
         if (utils.typeName(arg) == 'ArrayBuffer') {
             ret.push({
                 'CDVType': 'ArrayBuffer',
-                'data': encodeArrayBufferAsBase64(arg)
+                'data': base64.fromArrayBuffer(arg)
             });
         } else {
             ret.push(arg);

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/0e89b601/lib/osx/exec.js
----------------------------------------------------------------------
diff --git a/lib/osx/exec.js b/lib/osx/exec.js
index 6eebb7b..811848e 100644
--- a/lib/osx/exec.js
+++ b/lib/osx/exec.js
@@ -26,7 +26,8 @@
  */
 var cordova = require('cordova'),
     channel = require('cordova/channel'),
-    utils = require('cordova/utils');
+    utils = require('cordova/utils'),
+    base64 = require('cordova/base64');
 
 
 function massageMessageNativeToJs(message) {
@@ -65,17 +66,11 @@ function massageArgsJsToNative(args) {
        return args;
     }
     var ret = [];
-    var encodeArrayBufferAs8bitString = function(ab) {
-        return String.fromCharCode.apply(null, new Uint8Array(ab));
-    };
-    var encodeArrayBufferAsBase64 = function(ab) {
-        return window.btoa(encodeArrayBufferAs8bitString(ab));
-    };
     args.forEach(function(arg, i) {
         if (utils.typeName(arg) == 'ArrayBuffer') {
             ret.push({
                 'CDVType': 'ArrayBuffer',
-                'data': encodeArrayBufferAsBase64(arg)
+                'data': base64.fromArrayBuffer(arg)
             });
         } else {
             ret.push(arg);


[2/3] js commit: CB 4004: Adding base64 encoding for array buffers, while removing the String expansion

Posted by ia...@apache.org.
CB 4004: Adding base64 encoding for array buffers, while removing the String expansion


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/5ba835cf
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/5ba835cf
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/5ba835cf

Branch: refs/heads/master
Commit: 5ba835cf64a50f48dcdc8ced49fed1a714c098cc
Parents: ff21821
Author: Chris Barton <c....@gmail.com>
Authored: Wed Jun 26 11:23:29 2013 -0700
Committer: Ian Clelland <ic...@chromium.org>
Committed: Wed Jul 3 16:37:40 2013 -0400

----------------------------------------------------------------------
 lib/android/exec.js |  3 ++-
 test/test.base64.js | 13 +++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/5ba835cf/lib/android/exec.js
----------------------------------------------------------------------
diff --git a/lib/android/exec.js b/lib/android/exec.js
index 206c09a..f55b5ad 100644
--- a/lib/android/exec.js
+++ b/lib/android/exec.js
@@ -36,6 +36,7 @@
 var cordova = require('cordova'),
     nativeApiProvider = require('cordova/plugin/android/nativeapiprovider'),
     utils = require('cordova/utils'),
+    base64 = require('cordova/base64'),
     jsToNativeModes = {
         PROMPT: 0,
         JS_OBJECT: 1,
@@ -74,7 +75,7 @@ function androidExec(success, fail, service, action, args) {
     // Process any ArrayBuffers in the args into a string.
     for (var i = 0; i < args.length; i++) {
         if (utils.typeName(args[i]) == 'ArrayBuffer') {
-            args[i] = window.btoa(String.fromCharCode.apply(null, new Uint8Array(args[i])));
+            args[i] = utils.encodeBase64(args[i]);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/5ba835cf/test/test.base64.js
----------------------------------------------------------------------
diff --git a/test/test.base64.js b/test/test.base64.js
index 666cafe..f9b1912 100644
--- a/test/test.base64.js
+++ b/test/test.base64.js
@@ -47,4 +47,17 @@ describe("base64", function () {
 
       expect(base64.fromArrayBuffer(arrayBuffer)).toBe(base64string);
     });
+
+    it("can base64 encode an text string in an ArrayBuffer", function () {
+      var buffer = new Buffer('Some Awesome Test This Is!', 'binary')
+        , base64string = buffer.toString('base64')
+        , arrayBuffer = new ArrayBuffer(buffer.length)
+        , view = new Uint8Array(arrayBuffer);
+
+      for (var i = 0; i < buffer.length; i++) {
+        view[i] = buffer[i];
+      }
+
+      expect(base64.fromArrayBuffer(arrayBuffer)).toBe(base64string);
+    });
 });