// Hex JavaScript decoder | |
// Copyright (c) 2008 Lapo Luchini <lapo@lapo.it> | |
// Permission to use, copy, modify, and/or distribute this software for any | |
// purpose with or without fee is hereby granted, provided that the above | |
// copyright notice and this permission notice appear in all copies. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
Hex = {}; | |
Hex.decode = function(a) { | |
if (Hex.decoder == undefined) { | |
var hex = "0123456789ABCDEF"; | |
var allow = " \f\n\r\t\u00A0\u2028\u2029"; | |
var dec = []; | |
for (var i = 0; i < 16; ++i) | |
dec[hex.charAt(i)] = i; | |
hex = hex.toLowerCase(); | |
for (var i = 10; i < 16; ++i) | |
dec[hex.charAt(i)] = i; | |
for (var i = 0; i < allow.length; ++i) | |
dec[allow.charAt(i)] = -1; | |
Hex.decoder = dec; | |
} | |
var out = []; | |
var bits = 0, char_count = 0; | |
for (var i = 0; i < a.length; ++i) { | |
var c = a.charAt(i); | |
if (c == '=') | |
break; | |
c = Hex.decoder[c]; | |
if (c == -1) | |
continue; | |
if (c == undefined) | |
throw 'Illegal character at offset ' + i; | |
bits |= c; | |
if (++char_count >= 2) { | |
out[out.length] = bits; | |
bits = 0; | |
char_count = 0; | |
} else { | |
bits <<= 4; | |
} | |
} | |
if (char_count) | |
throw "Hex encoding incomplete: 4 bits missing"; | |
return out; | |
} |