Permalink
Browse files

fixes

  • Loading branch information...
oberstet committed May 23, 2017
1 parent 37fb323 commit 080a05ca4ee7936a2b0c6f0df41af73fa8016746
Showing with 37 additions and 42 deletions.
  1. +37 −42 lib/util.js
View
@@ -16,72 +16,67 @@ var log = require('./log.js');
var when = require('when');
function _base64_to_uint8array (input) {
var raw = new Buffer(input, 'base64');
var arr = new Uint8Array(new ArrayBuffer(raw.length));
for(i = 0; i < raw.length; i++) {
arr[i] = raw[i];
}
return arr;
};
exports.base64_to_uint8array = _base64_to_uint8array;
function _string_to_uint8array (str) {
var raw = new Buffer(str, 'utf8');
var arr = new Uint8Array(new ArrayBuffer(raw.length));
for(i = 0; i < raw.length; i++) {
arr[i] = raw[i];
}
return arr;
};
exports.string_to_uint8array = _string_to_uint8array
/// Convert base64 string to array of bytes.
function _atob (s) {
return new Uint8Array(atob(s).split("").map(function(c) { return c.charCodeAt(0); }));
if (s) {
return new Uint8Array(atob(s).split("").map(function(c) { return c.charCodeAt(0); }));
} else {
return null;
}
}
exports.atob = _atob
/// Convert array of bytes to base64 string.
function _btoa (b) {
return btoa(String.fromCharCode.apply(null, b));
if (b) {
return btoa(String.fromCharCode.apply(null, b));
} else {
return null;
}
}
exports.btoa = _btoa
/// Convert array of bytes to hex string.
function _btoh (bytes) {
var res = '';
for (var i = 0; i < bytes.length; ++i) {
res += ('0' + (bytes[i] & 0xFF).toString(16)).slice(-2);
if (bytes) {
var res = '';
for (var i = 0; i < bytes.length; ++i) {
res += ('0' + (bytes[i] & 0xFF).toString(16)).slice(-2);
}
return res;
} else {
return null;
}
return res;
}
exports.btoh = _btoh
/// Convert hex string to array of bytes.
function _htob (hex) {
if (typeof hex !== 'string') {
throw new TypeError('Expected input to be a string')
}
if (hex) {
if (typeof hex !== 'string') {
throw new TypeError('Expected input to be a string')
}
if ((hex.length % 2) !== 0) {
throw new RangeError('Expected string to be an even number of characters')
}
if ((hex.length % 2) !== 0) {
throw new RangeError('Expected string to be an even number of characters')
}
var view = new Uint8Array(hex.length / 2)
var view = new Uint8Array(hex.length / 2)
for (var i = 0; i < hex.length; i += 2) {
view[i / 2] = parseInt(hex.substring(i, i + 2), 16)
}
for (var i = 0; i < hex.length; i += 2) {
view[i / 2] = parseInt(hex.substring(i, i + 2), 16)
}
return view
return view
} else {
return null;
}
}
exports.htob = _htob

0 comments on commit 080a05c

Please sign in to comment.