react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / http-signature / node_modules / asn1 / lib / ber / reader.js
81159 views// Copyright 2011 Mark Cavage <[email protected]> All rights reserved.12var assert = require('assert');34var ASN1 = require('./types');5var errors = require('./errors');678///--- Globals910var newInvalidAsn1Error = errors.newInvalidAsn1Error;11121314///--- API1516function Reader(data) {17if (!data || !Buffer.isBuffer(data))18throw new TypeError('data must be a node Buffer');1920this._buf = data;21this._size = data.length;2223// These hold the "current" state24this._len = 0;25this._offset = 0;2627var self = this;28this.__defineGetter__('length', function() { return self._len; });29this.__defineGetter__('offset', function() { return self._offset; });30this.__defineGetter__('remain', function() {31return self._size - self._offset;32});33this.__defineGetter__('buffer', function() {34return self._buf.slice(self._offset);35});36}373839/**40* Reads a single byte and advances offset; you can pass in `true` to make this41* a "peek" operation (i.e., get the byte, but don't advance the offset).42*43* @param {Boolean} peek true means don't move offset.44* @return {Number} the next byte, null if not enough data.45*/46Reader.prototype.readByte = function(peek) {47if (this._size - this._offset < 1)48return null;4950var b = this._buf[this._offset] & 0xff;5152if (!peek)53this._offset += 1;5455return b;56};575859Reader.prototype.peek = function() {60return this.readByte(true);61};626364/**65* Reads a (potentially) variable length off the BER buffer. This call is66* not really meant to be called directly, as callers have to manipulate67* the internal buffer afterwards.68*69* As a result of this call, you can call `Reader.length`, until the70* next thing called that does a readLength.71*72* @return {Number} the amount of offset to advance the buffer.73* @throws {InvalidAsn1Error} on bad ASN.174*/75Reader.prototype.readLength = function(offset) {76if (offset === undefined)77offset = this._offset;7879if (offset >= this._size)80return null;8182var lenB = this._buf[offset++] & 0xff;83if (lenB === null)84return null;8586if ((lenB & 0x80) == 0x80) {87lenB &= 0x7f;8889if (lenB == 0)90throw newInvalidAsn1Error('Indefinite length not supported');9192if (lenB > 4)93throw newInvalidAsn1Error('encoding too long');9495if (this._size - offset < lenB)96return null;9798this._len = 0;99for (var i = 0; i < lenB; i++)100this._len = (this._len << 8) + (this._buf[offset++] & 0xff);101102} else {103// Wasn't a variable length104this._len = lenB;105}106107return offset;108};109110111/**112* Parses the next sequence in this BER buffer.113*114* To get the length of the sequence, call `Reader.length`.115*116* @return {Number} the sequence's tag.117*/118Reader.prototype.readSequence = function(tag) {119var seq = this.peek();120if (seq === null)121return null;122if (tag !== undefined && tag !== seq)123throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +124': got 0x' + seq.toString(16));125126var o = this.readLength(this._offset + 1); // stored in `length`127if (o === null)128return null;129130this._offset = o;131return seq;132};133134135Reader.prototype.readInt = function() {136return this._readTag(ASN1.Integer);137};138139140Reader.prototype.readBoolean = function() {141return (this._readTag(ASN1.Boolean) === 0 ? false : true);142};143144145Reader.prototype.readEnumeration = function() {146return this._readTag(ASN1.Enumeration);147};148149150Reader.prototype.readString = function(tag, retbuf) {151if (!tag)152tag = ASN1.OctetString;153154var b = this.peek();155if (b === null)156return null;157158if (b !== tag)159throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +160': got 0x' + b.toString(16));161162var o = this.readLength(this._offset + 1); // stored in `length`163164if (o === null)165return null;166167if (this.length > this._size - o)168return null;169170this._offset = o;171172if (this.length === 0)173return '';174175var str = this._buf.slice(this._offset, this._offset + this.length);176this._offset += this.length;177178return retbuf ? str : str.toString('utf8');179};180181Reader.prototype.readOID = function(tag) {182if (!tag)183tag = ASN1.OID;184185var b = this.peek();186if (b === null)187return null;188189if (b !== tag)190throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +191': got 0x' + b.toString(16));192193var o = this.readLength(this._offset + 1); // stored in `length`194if (o === null)195return null;196197if (this.length > this._size - o)198return null;199200this._offset = o;201202var values = [];203var value = 0;204205for (var i = 0; i < this.length; i++) {206var byte = this._buf[this._offset++] & 0xff;207208value <<= 7;209value += byte & 0x7f;210if ((byte & 0x80) == 0) {211values.push(value);212value = 0;213}214}215216value = values.shift();217values.unshift(value % 40);218values.unshift((value / 40) >> 0);219220return values.join('.');221};222223224Reader.prototype._readTag = function(tag) {225assert.ok(tag !== undefined);226227var b = this.peek();228229if (b === null)230return null;231232if (b !== tag)233throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +234': got 0x' + b.toString(16));235236var o = this.readLength(this._offset + 1); // stored in `length`237if (o === null)238return null;239240if (this.length > 4)241throw newInvalidAsn1Error('Integer too long: ' + this.length);242243if (this.length > this._size - o)244return null;245this._offset = o;246247var fb = this._buf[this._offset++];248var value = 0;249250value = fb & 0x7F;251for (var i = 1; i < this.length; i++) {252value <<= 8;253value |= (this._buf[this._offset++] & 0xff);254}255256if ((fb & 0x80) == 0x80)257value = -value;258259return value;260};261262263264///--- Exported API265266module.exports = Reader;267268269