'use strict';
//
// Parsing hex strings. Yeah.
//
// So basically we need this because of a bug in WebSQL:
// https://code.google.com/p/chromium/issues/detail?id=422690
// https://bugs.webkit.org/show_bug.cgi?id=137637
//
// UTF-8 and UTF-16 are provided as separate functions
// for meager performance improvements
//
function decodeUtf8(str) {
return decodeURIComponent(window.escape(str));
}
function hexToInt(charCode) {
// '0'-'9' is 48-57
// 'A'-'F' is 65-70
// SQLite will only give us uppercase hex
return charCode < 65 ? (charCode - 48) : (charCode - 55);
}
// Example:
// pragma encoding=utf8;
// select hex('A');
// returns '41'
function parseHexUtf8(str, start, end) {
var result = '';
while (start < end) {
result += String.fromCharCode(
(hexToInt(str.charCodeAt(start++)) << 4) |
hexToInt(str.charCodeAt(start++)));
}
return result;
}
// Example:
// pragma encoding=utf16;
// select hex('A');
// returns '4100'
// notice that the 00 comes after the 41 (i.e. it's swizzled)
function parseHexUtf16(str, start, end) {
var result = '';
while (start < end) {
// UTF-16, so swizzle the bytes
result += String.fromCharCode(
(hexToInt(str.charCodeAt(start + 2)) << 12) |
(hexToInt(str.charCodeAt(start + 3)) << 8) |
(hexToInt(str.charCodeAt(start)) << 4) |
hexToInt(str.charCodeAt(start + 1)));
start += 4;
}
return result;
}
function parseHexString(str, encoding) {
if (encoding === 'UTF-8') {
return decodeUtf8(parseHexUtf8(str, 0, str.length));
} else {
return parseHexUtf16(str, 0, str.length);
}
}
module.exports = parseHexString; |