1 | /*
|
---|
2 | * base64-arraybuffer
|
---|
3 | * https://github.com/niklasvh/base64-arraybuffer
|
---|
4 | *
|
---|
5 | * Copyright (c) 2012 Niklas von Hertzen
|
---|
6 | * Licensed under the MIT license.
|
---|
7 | */
|
---|
8 | (function(chars){
|
---|
9 | "use strict";
|
---|
10 |
|
---|
11 | exports.encode = function(arraybuffer) {
|
---|
12 | var bytes = new Uint8Array(arraybuffer),
|
---|
13 | i, len = bytes.length, base64 = "";
|
---|
14 |
|
---|
15 | for (i = 0; i < len; i+=3) {
|
---|
16 | base64 += chars[bytes[i] >> 2];
|
---|
17 | base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
|
---|
18 | base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
|
---|
19 | base64 += chars[bytes[i + 2] & 63];
|
---|
20 | }
|
---|
21 |
|
---|
22 | if ((len % 3) === 2) {
|
---|
23 | base64 = base64.substring(0, base64.length - 1) + "=";
|
---|
24 | } else if (len % 3 === 1) {
|
---|
25 | base64 = base64.substring(0, base64.length - 2) + "==";
|
---|
26 | }
|
---|
27 |
|
---|
28 | return base64;
|
---|
29 | };
|
---|
30 |
|
---|
31 | exports.decode = function(base64) {
|
---|
32 | var bufferLength = base64.length * 0.75,
|
---|
33 | len = base64.length, i, p = 0,
|
---|
34 | encoded1, encoded2, encoded3, encoded4;
|
---|
35 |
|
---|
36 | if (base64[base64.length - 1] === "=") {
|
---|
37 | bufferLength--;
|
---|
38 | if (base64[base64.length - 2] === "=") {
|
---|
39 | bufferLength--;
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | var arraybuffer = new ArrayBuffer(bufferLength),
|
---|
44 | bytes = new Uint8Array(arraybuffer);
|
---|
45 |
|
---|
46 | for (i = 0; i < len; i+=4) {
|
---|
47 | encoded1 = chars.indexOf(base64[i]);
|
---|
48 | encoded2 = chars.indexOf(base64[i+1]);
|
---|
49 | encoded3 = chars.indexOf(base64[i+2]);
|
---|
50 | encoded4 = chars.indexOf(base64[i+3]);
|
---|
51 |
|
---|
52 | bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
|
---|
53 | bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
|
---|
54 | bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
|
---|
55 | }
|
---|
56 |
|
---|
57 | return arraybuffer;
|
---|
58 | };
|
---|
59 | })("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
|
---|