Permalink
Browse files

never ever mess with a buffer

  • Loading branch information...
1 parent 10b885c commit 4a5150a8ceea088cb1b1e1faa00a66f380998179 @bjyoungblood bjyoungblood committed Dec 14, 2011
Showing with 32 additions and 12 deletions.
  1. +32 −12 lib/pdu-writer.js
View
@@ -48,22 +48,26 @@ exports.write = function(pdu) {
} else if (field.type === "c-string") {
// get the actual byte length of the string, then add 1 for the
// null terminator
-
- if (typeof field.value !== 'string') {
- try {
- field.value = field.value.toString();
- } catch (error) {
- throw "Could not cast to string";
+
+ // Don't try to do anything to a buffer
+ if (!Buffer.isBuffer(field.value)) {
+ if (typeof field.value === 'string') {
+ field.value = new Buffer(field.value);
+ } else {
+ try {
+ field.value = new Buffer(field.value.toString());
+ } catch (ex) {
+ throw "Could not cast value for " + field.name + " to string";
+ }
}
}
-
+
if (typeof field.value === 'undefined') {
length = 1;
} else {
- length += Buffer.byteLength(field.value) + 1;
+ length += field.value.length + 1;
}
} else if (field.type === "string") {
-
if (typeof field.value !== 'string') {
try {
field.value = field.value.toString();
@@ -72,10 +76,23 @@ exports.write = function(pdu) {
}
}
+ // Don't try to do anything to a buffer
+ if (!Buffer.isBuffer(field.value)) {
+ if (typeof field.value === 'string') {
+ field.value = new Buffer(field.value);
+ } else {
+ try {
+ field.value = new Buffer(field.value.toString());
+ } catch (ex) {
+ throw "Could not cast value for " + field.name + " to string";
+ }
+ }
+ }
+
if (typeof field.value === 'undefined') {
length = 0;
} else {
- length += Buffer.byteLength(field.value);
+ length += field.value.length;
}
}
}
@@ -151,13 +168,16 @@ exports.write = function(pdu) {
buffer.writeUInt8(0x0, offset);
offset += 1;
} else {
- offset += buffer.write(field.value, offset);
+ field.value.copy(buffer, offset);
+ offset += field.value.length;
+
buffer.writeUInt8(0x0, offset);
offset += 1;
}
} else if (field.type === "string") {
if (field.value.length !== 0) {
- offset += buffer.write(field.value, offset);
+ field.value.copy(buffer, offset);
+ offset += field.value.length;
}
}
}

0 comments on commit 4a5150a

Please sign in to comment.