1 // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
3 var assert = require('assert');
5 var ASN1 = require('./types');
6 var errors = require('./errors');
11 var newInvalidAsn1Error = errors.newInvalidAsn1Error;
17 function Reader(data) {
18 if (!data || !Buffer.isBuffer(data))
19 throw new TypeError('data must be a node Buffer');
22 this._size = data.length;
24 // These hold the "current" state
29 Object.defineProperty(Reader.prototype, 'length', {
31 get: function () { return (this._len); }
34 Object.defineProperty(Reader.prototype, 'offset', {
36 get: function () { return (this._offset); }
39 Object.defineProperty(Reader.prototype, 'remain', {
40 get: function () { return (this._size - this._offset); }
43 Object.defineProperty(Reader.prototype, 'buffer', {
44 get: function () { return (this._buf.slice(this._offset)); }
49 * Reads a single byte and advances offset; you can pass in `true` to make this
50 * a "peek" operation (i.e., get the byte, but don't advance the offset).
52 * @param {Boolean} peek true means don't move offset.
53 * @return {Number} the next byte, null if not enough data.
55 Reader.prototype.readByte = function(peek) {
56 if (this._size - this._offset < 1)
59 var b = this._buf[this._offset] & 0xff;
68 Reader.prototype.peek = function() {
69 return this.readByte(true);
74 * Reads a (potentially) variable length off the BER buffer. This call is
75 * not really meant to be called directly, as callers have to manipulate
76 * the internal buffer afterwards.
78 * As a result of this call, you can call `Reader.length`, until the
79 * next thing called that does a readLength.
81 * @return {Number} the amount of offset to advance the buffer.
82 * @throws {InvalidAsn1Error} on bad ASN.1
84 Reader.prototype.readLength = function(offset) {
85 if (offset === undefined)
86 offset = this._offset;
88 if (offset >= this._size)
91 var lenB = this._buf[offset++] & 0xff;
95 if ((lenB & 0x80) == 0x80) {
99 throw newInvalidAsn1Error('Indefinite length not supported');
102 throw newInvalidAsn1Error('encoding too long');
104 if (this._size - offset < lenB)
108 for (var i = 0; i < lenB; i++)
109 this._len = (this._len << 8) + (this._buf[offset++] & 0xff);
112 // Wasn't a variable length
121 * Parses the next sequence in this BER buffer.
123 * To get the length of the sequence, call `Reader.length`.
125 * @return {Number} the sequence's tag.
127 Reader.prototype.readSequence = function(tag) {
128 var seq = this.peek();
131 if (tag !== undefined && tag !== seq)
132 throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
133 ': got 0x' + seq.toString(16));
135 var o = this.readLength(this._offset + 1); // stored in `length`
144 Reader.prototype.readInt = function() {
145 return this._readTag(ASN1.Integer);
149 Reader.prototype.readBoolean = function() {
150 return (this._readTag(ASN1.Boolean) === 0 ? false : true);
154 Reader.prototype.readEnumeration = function() {
155 return this._readTag(ASN1.Enumeration);
159 Reader.prototype.readString = function(tag, retbuf) {
161 tag = ASN1.OctetString;
168 throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
169 ': got 0x' + b.toString(16));
171 var o = this.readLength(this._offset + 1); // stored in `length`
176 if (this.length > this._size - o)
181 if (this.length === 0)
182 return retbuf ? new Buffer(0) : '';
184 var str = this._buf.slice(this._offset, this._offset + this.length);
185 this._offset += this.length;
187 return retbuf ? str : str.toString('utf8');
190 Reader.prototype.readOID = function(tag) {
194 var b = this.readString(tag, true);
201 for (var i = 0; i < b.length; i++) {
202 var byte = b[i] & 0xff;
205 value += byte & 0x7f;
206 if ((byte & 0x80) == 0) {
212 value = values.shift();
213 values.unshift(value % 40);
214 values.unshift((value / 40) >> 0);
216 return values.join('.');
220 Reader.prototype._readTag = function(tag) {
221 assert.ok(tag !== undefined);
229 throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
230 ': got 0x' + b.toString(16));
232 var o = this.readLength(this._offset + 1); // stored in `length`
237 throw newInvalidAsn1Error('Integer too long: ' + this.length);
239 if (this.length > this._size - o)
243 var fb = this._buf[this._offset];
246 for (var i = 0; i < this.length; i++) {
248 value |= (this._buf[this._offset++] & 0xff);
251 if ((fb & 0x80) == 0x80 && i !== 4)
252 value -= (1 << (i * 8));
261 module.exports = Reader;