From 7e2fdbd51f821a9409802c19c6c792a530a7afa3 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Sat, 14 Nov 2009 17:59:34 -0800 Subject: [PATCH] Base 64 encoding --- encodings/base64/base64.htm | 76 ++++++++++++++++++++++++++++ encodings/base64/base64.js | 98 +++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 encodings/base64/base64.htm create mode 100644 encodings/base64/base64.js diff --git a/encodings/base64/base64.htm b/encodings/base64/base64.htm new file mode 100644 index 0000000..8bd0b82 --- /dev/null +++ b/encodings/base64/base64.htm @@ -0,0 +1,76 @@ + + +Base64 Tests + + + + + + + + + +

Doubly Linked List Tests

+ + + diff --git a/encodings/base64/base64.js b/encodings/base64/base64.js new file mode 100644 index 0000000..f5ff660 --- /dev/null +++ b/encodings/base64/base64.js @@ -0,0 +1,98 @@ +/* + * Base 64 implementation in JavaScript + * Copyright (c) 2009 Nicholas C. Zakas. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Base64-encodes a string of text. + * @param {String} text The text to encode + * @return {String} The base64-encoded string. + */ +function base64Encode(text){ + + //helper function to left-pad an array with zeros + function padLeft(bits, length){ + while (bits.length < length){ + bits.unshift("0"); + } + return bits; + } + + //helper function to right-pad an array with zeros + function padRight(bits, length){ + while (bits.length < length){ + bits.push("0"); + } + return bits; + } + + //local variables + var digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", + part, index, + i=0, j=0, + padding="", + quantaCount, + bits = [], + result = []; + + //create an array of binary digits representing the text + for (; i < text.length; i++){ + part = text.charCodeAt(i).toString(2); + bits = bits.concat(padLeft(part.split(""), 8)); + } + + //figure out how many 24-bit quanta are in the array + quantaCount = Math.floor(bits.length / 24); + + //encode all bits + encodeBits: while(true){ + + //must encode one complete quanta at a time + for(i=0; i < quantaCount; i++){ + for (j=0; j < 4; j++){ + part = bits.splice(0, 6).join(""); + index = parseInt(part,2); + result.push(digits[index]); + } + } + + //take care of any extra bits + switch(bits.length){ + case 8: + padRight(bits, 12); + padding = "=="; + continue encodeBits; + case 16: + padRight(bits, 18); + padding = "="; + continue encodeBits; + default: + break encodeBits; + } + } + + //add any padding to the result + result.push(padding); + + //return a string + return result.join(""); + +} \ No newline at end of file