Skip to content

Commit

Permalink
Merge pull request #3 from dgwynne/master
Browse files Browse the repository at this point in the history
add BerReader.readOID()
  • Loading branch information
mcavage committed Jan 6, 2012
2 parents 5601d27 + 278ff85 commit a89b180
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
42 changes: 42 additions & 0 deletions lib/ber/reader.js
Expand Up @@ -179,6 +179,48 @@ Reader.prototype.readString = function(tag, retbuf) {
return retbuf ? str : str.toString('utf8');
};

Reader.prototype.readOID = function(tag) {
if (!tag)
tag = ASN1.OID;

var b = this.peek();
if (b === null)
return null;

if (b !== tag)
throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
': got 0x' + b.toString(16));

var o = this.readLength(this._offset + 1); // stored in `length`
if (o === null)
return null;

if (this.length > this._size - o)
return null;

this._offset = o;

var values = [ ];
var value = 0;

for (var i = 0; i < this.length; i++) {
var byte = this._buf[this._offset++] & 0xff;

value <<= 7;
value += byte & 0x7f;
if ((byte & 0x80) == 0) {
values.push(value);
value = 0;
}
}

value = values.shift();
values.unshift(value % 40);
values.unshift((value / 40) >> 0);

return values.join('.');
}


Reader.prototype._readTag = function(tag) {
assert.ok(tag !== undefined);
Expand Down
8 changes: 7 additions & 1 deletion lib/ber/writer.js
Expand Up @@ -189,11 +189,17 @@ Writer.prototype.writeOID = function(s, tag) {
bytes.push((octet >>> 14) | 0x80);
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
bytes.push(octet & 0x7F);
} else if (id < 268435456) {
} else if (octet < 268435456) {
bytes.push((octet >>> 21) | 0x80);
bytes.push(((octet >>> 14) | 0x80) & 0xFF);
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
bytes.push(octet & 0x7F);
} else {
bytes.push(((octet >>> 28) | 0x80) & 0xFF);
bytes.push(((octet >>> 21) | 0x80) & 0xFF);
bytes.push(((octet >>> 14) | 0x80) & 0xFF);
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
bytes.push(octet & 0x7F);
}
}

Expand Down

0 comments on commit a89b180

Please sign in to comment.