cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject [1/3] js commit: [CB-4004] Add base64 encoding utility function
Date Wed, 03 Jul 2013 20:39:21 GMT
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 <iclelland@chromium.org>
Authored: Wed Jul 3 16:33:07 2013 -0400
Committer: Ian Clelland <iclelland@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);
+    });
+});


Mime
View raw message