Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
never ever mess with a buffer
  • Loading branch information
bjyoungblood committed Dec 14, 2011
1 parent 10b885c commit 4a5150a
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions lib/pdu-writer.js
Expand Up @@ -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();
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit 4a5150a

Please sign in to comment.