Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented utf8, sscii and base64 to the Buffer module.

  • Loading branch information...
commit adc670f74db3b0dc01c2b29220bea76dda02995f 1 parent bb42767
@toots authored
Showing with 113 additions and 0 deletions.
  1. +112 −0 builtins/buffer.js
  2. +1 −0  package.json
View
112 builtins/buffer.js
@@ -12,6 +12,118 @@ function toHex(n) {
return n.toString(16);
}
+function utf8ToBytes(str) {
+ var byteArray = [];
+ for (var i = 0; i < str.length; i++)
+ if (str.charCodeAt(i) <= 0x7F)
+ byteArray.push(str.charCodeAt(i));
+ else {
+ var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
+ for (var j = 0; j < h.length; j++)
+ byteArray.push(parseInt(h[j], 16));
+ }
+
+ return byteArray;
+}
+
+function asciiToBytes(str) {
+ var byteArray = []
+ for (var i = 0; i < str.length; i++ )
+ // Node's code seems to be doing this and not & 0x7F..
+ byteArray.push( str.charCodeAt(i) & 0xFF );
+
+ return byteArray;
+}
+
+function base64ToBytes(str) {
+ return require("base64-js").toByteArray(str);
+}
+
+SlowBuffer.byteLength = function (str, encoding) {
+ switch (encoding || "utf8") {
+ case 'hex':
+ return str.length / 2;
+
+ case 'utf8':
+ case 'utf-8':
+ return utf8ToBytes(str).length;
+
+ case 'ascii':
+ return str.length;
+
+ case 'base64':
+ return base64ToBytes(str).length;
+
+ default:
+ throw new Error('Unknown encoding');
+ }
+};
+
+function blitBuffer(src, dst, offset, length) {
+ var pos, i = 0;
+ while (i < length) {
+ if ((i+offset >= dst.length) || (i >= src.length))
+ break;
+
+ dst[i + offset] = src[i];
+ i++;
+ }
+ return i;
+}
+
+SlowBuffer.prototype.utf8Write = function (string, offset, length) {
+ var bytes, pos;
+ return SlowBuffer._charsWritten = blitBuffer(utf8ToBytes(string), this, offset, length);
+};
+
+SlowBuffer.prototype.asciiWrite = function (string, offset, length) {
+ var bytes, pos;
+ return SlowBuffer._charsWritten = blitBuffer(asciiToBytes(string), this, offset, length);
+};
+
+SlowBuffer.prototype.base64Write = function (string, offset, length) {
+ var bytes, pos;
+ return SlowBuffer._charsWritten = blitBuffer(base64ToBytes(string), this, offset, length);
+};
+
+SlowBuffer.prototype.base64Slice = function (start, end) {
+ var bytes = Array.prototype.slice.apply(this, arguments)
+ return require("base64-js").fromByteArray(bytes);
+}
+
+function decodeUtf8Char(str) {
+ try {
+ return decodeURIComponent(str);
+ } catch (err) {
+ return String.fromCharCode(0xFFFD); // UTF 8 invalid char
+ }
+}
+
+SlowBuffer.prototype.utf8Slice = function () {
+ var bytes = Array.prototype.slice.apply(this, arguments);
+ var res = "";
+ var tmp = "";
+ var i = 0;
+ while (i < bytes.length) {
+ if (bytes[i] <= 0x7F) {
+ res += decodeUtf8Char(tmp) + String.fromCharCode(bytes[i]);
+ tmp = "";
+ } else
+ tmp += "%" + bytes[i].toString(16);
+
+ i++;
+ }
+
+ return res + decodeUtf8Char(tmp);
+}
+
+SlowBuffer.prototype.asciiSlice = function () {
+ var bytes = Array.prototype.slice.apply(this, arguments);
+ var ret = "";
+ for (var i = 0; i < bytes.length; i++)
+ ret += String.fromCharCode(bytes[i]);
+ return ret;
+}
SlowBuffer.prototype.inspect = function() {
var out = [],
View
1  package.json
@@ -24,6 +24,7 @@
"javascript"
],
"dependencies" : {
+ "base64-js" : "0.0.2",
"detective" : "~0.1.0",
"deputy" : "0.0.x",
"resolve" : "~0.2.0",
Please sign in to comment.
Something went wrong with that request. Please try again.