react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / public-encrypt / node_modules / parse-asn1 / node_modules / asn1.js / lib / asn1 / base / buffer.js
80647 viewsvar inherits = require('inherits');1var Reporter = require('../base').Reporter;2var Buffer = require('buffer').Buffer;34function DecoderBuffer(base, options) {5Reporter.call(this, options);6if (!Buffer.isBuffer(base)) {7this.error('Input not Buffer');8return;9}1011this.base = base;12this.offset = 0;13this.length = base.length;14}15inherits(DecoderBuffer, Reporter);16exports.DecoderBuffer = DecoderBuffer;1718DecoderBuffer.prototype.save = function save() {19return { offset: this.offset, reporter: Reporter.prototype.save.call(this) };20};2122DecoderBuffer.prototype.restore = function restore(save) {23// Return skipped data24var res = new DecoderBuffer(this.base);25res.offset = save.offset;26res.length = this.offset;2728this.offset = save.offset;29Reporter.prototype.restore.call(this, save.reporter);3031return res;32};3334DecoderBuffer.prototype.isEmpty = function isEmpty() {35return this.offset === this.length;36};3738DecoderBuffer.prototype.readUInt8 = function readUInt8(fail) {39if (this.offset + 1 <= this.length)40return this.base.readUInt8(this.offset++, true);41else42return this.error(fail || 'DecoderBuffer overrun');43}4445DecoderBuffer.prototype.skip = function skip(bytes, fail) {46if (!(this.offset + bytes <= this.length))47return this.error(fail || 'DecoderBuffer overrun');4849var res = new DecoderBuffer(this.base);5051// Share reporter state52res._reporterState = this._reporterState;5354res.offset = this.offset;55res.length = this.offset + bytes;56this.offset += bytes;57return res;58}5960DecoderBuffer.prototype.raw = function raw(save) {61return this.base.slice(save ? save.offset : this.offset, this.length);62}6364function EncoderBuffer(value, reporter) {65if (Array.isArray(value)) {66this.length = 0;67this.value = value.map(function(item) {68if (!item || item.constructor.name !== 'EncoderBuffer')69item = new EncoderBuffer(item, reporter);70this.length += item.length;71return item;72}, this);73} else if (typeof value === 'number') {74if (!(0 <= value && value <= 0xff))75return reporter.error('non-byte EncoderBuffer value');76this.value = value;77this.length = 1;78} else if (typeof value === 'string') {79this.value = value;80this.length = Buffer.byteLength(value);81} else if (Buffer.isBuffer(value)) {82this.value = value;83this.length = value.length;84} else {85return reporter.error('Unsupported type: ' + typeof value);86}87}88exports.EncoderBuffer = EncoderBuffer;8990EncoderBuffer.prototype.join = function join(out, offset) {91if (!out)92out = new Buffer(this.length);93if (!offset)94offset = 0;9596if (this.length === 0)97return out;9899if (Array.isArray(this.value)) {100this.value.forEach(function(item) {101item.join(out, offset);102offset += item.length;103});104} else {105if (typeof this.value === 'number')106out[offset] = this.value;107else if (typeof this.value === 'string')108out.write(this.value, offset);109else if (Buffer.isBuffer(this.value))110this.value.copy(out, offset);111offset += this.length;112}113114return out;115};116117118