const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.cjs');
const require_errors$1 = require('./errors.cjs');
const require_types$1 = require('./types.cjs');
const require_safer$1 = require('../../../safer-buffer/safer.cjs');

//#region ../../node_modules/asn1/lib/ber/writer.js
var require_writer = /* @__PURE__ */ require_rolldown_runtime.__commonJS({ "../../node_modules/asn1/lib/ber/writer.js": ((exports, module) => {
	var assert = require("assert");
	var Buffer = require_safer$1.default.Buffer;
	var ASN1 = require_types$1.default;
	var newInvalidAsn1Error = require_errors$1.default.newInvalidAsn1Error;
	var DEFAULT_OPTS = {
		size: 1024,
		growthFactor: 8
	};
	function merge(from, to) {
		assert.ok(from);
		assert.equal(typeof from, "object");
		assert.ok(to);
		assert.equal(typeof to, "object");
		Object.getOwnPropertyNames(from).forEach(function(key) {
			if (to[key]) return;
			var value = Object.getOwnPropertyDescriptor(from, key);
			Object.defineProperty(to, key, value);
		});
		return to;
	}
	function Writer(options) {
		options = merge(DEFAULT_OPTS, options || {});
		this._buf = Buffer.alloc(options.size || 1024);
		this._size = this._buf.length;
		this._offset = 0;
		this._options = options;
		this._seq = [];
	}
	Object.defineProperty(Writer.prototype, "buffer", { get: function() {
		if (this._seq.length) throw newInvalidAsn1Error(this._seq.length + " unended sequence(s)");
		return this._buf.slice(0, this._offset);
	} });
	Writer.prototype.writeByte = function(b) {
		if (typeof b !== "number") throw new TypeError("argument must be a Number");
		this._ensure(1);
		this._buf[this._offset++] = b;
	};
	Writer.prototype.writeInt = function(i, tag) {
		if (typeof i !== "number") throw new TypeError("argument must be a Number");
		if (typeof tag !== "number") tag = ASN1.Integer;
		var sz = 4;
		while (((i & 4286578688) === 0 || (i & 4286578688) === -8388608) && sz > 1) {
			sz--;
			i <<= 8;
		}
		if (sz > 4) throw newInvalidAsn1Error("BER ints cannot be > 0xffffffff");
		this._ensure(2 + sz);
		this._buf[this._offset++] = tag;
		this._buf[this._offset++] = sz;
		while (sz-- > 0) {
			this._buf[this._offset++] = (i & 4278190080) >>> 24;
			i <<= 8;
		}
	};
	Writer.prototype.writeNull = function() {
		this.writeByte(ASN1.Null);
		this.writeByte(0);
	};
	Writer.prototype.writeEnumeration = function(i, tag) {
		if (typeof i !== "number") throw new TypeError("argument must be a Number");
		if (typeof tag !== "number") tag = ASN1.Enumeration;
		return this.writeInt(i, tag);
	};
	Writer.prototype.writeBoolean = function(b, tag) {
		if (typeof b !== "boolean") throw new TypeError("argument must be a Boolean");
		if (typeof tag !== "number") tag = ASN1.Boolean;
		this._ensure(3);
		this._buf[this._offset++] = tag;
		this._buf[this._offset++] = 1;
		this._buf[this._offset++] = b ? 255 : 0;
	};
	Writer.prototype.writeString = function(s, tag) {
		if (typeof s !== "string") throw new TypeError("argument must be a string (was: " + typeof s + ")");
		if (typeof tag !== "number") tag = ASN1.OctetString;
		var len = Buffer.byteLength(s);
		this.writeByte(tag);
		this.writeLength(len);
		if (len) {
			this._ensure(len);
			this._buf.write(s, this._offset);
			this._offset += len;
		}
	};
	Writer.prototype.writeBuffer = function(buf, tag) {
		if (typeof tag !== "number") throw new TypeError("tag must be a number");
		if (!Buffer.isBuffer(buf)) throw new TypeError("argument must be a buffer");
		this.writeByte(tag);
		this.writeLength(buf.length);
		this._ensure(buf.length);
		buf.copy(this._buf, this._offset, 0, buf.length);
		this._offset += buf.length;
	};
	Writer.prototype.writeStringArray = function(strings) {
		if (!strings instanceof Array) throw new TypeError("argument must be an Array[String]");
		var self = this;
		strings.forEach(function(s) {
			self.writeString(s);
		});
	};
	Writer.prototype.writeOID = function(s, tag) {
		if (typeof s !== "string") throw new TypeError("argument must be a string");
		if (typeof tag !== "number") tag = ASN1.OID;
		if (!/^([0-9]+\.){3,}[0-9]+$/.test(s)) throw new Error("argument is not a valid OID string");
		function encodeOctet(bytes$1, octet) {
			if (octet < 128) bytes$1.push(octet);
			else if (octet < 16384) {
				bytes$1.push(octet >>> 7 | 128);
				bytes$1.push(octet & 127);
			} else if (octet < 2097152) {
				bytes$1.push(octet >>> 14 | 128);
				bytes$1.push((octet >>> 7 | 128) & 255);
				bytes$1.push(octet & 127);
			} else if (octet < 268435456) {
				bytes$1.push(octet >>> 21 | 128);
				bytes$1.push((octet >>> 14 | 128) & 255);
				bytes$1.push((octet >>> 7 | 128) & 255);
				bytes$1.push(octet & 127);
			} else {
				bytes$1.push((octet >>> 28 | 128) & 255);
				bytes$1.push((octet >>> 21 | 128) & 255);
				bytes$1.push((octet >>> 14 | 128) & 255);
				bytes$1.push((octet >>> 7 | 128) & 255);
				bytes$1.push(octet & 127);
			}
		}
		var tmp = s.split(".");
		var bytes = [];
		bytes.push(parseInt(tmp[0], 10) * 40 + parseInt(tmp[1], 10));
		tmp.slice(2).forEach(function(b) {
			encodeOctet(bytes, parseInt(b, 10));
		});
		var self = this;
		this._ensure(2 + bytes.length);
		this.writeByte(tag);
		this.writeLength(bytes.length);
		bytes.forEach(function(b) {
			self.writeByte(b);
		});
	};
	Writer.prototype.writeLength = function(len) {
		if (typeof len !== "number") throw new TypeError("argument must be a Number");
		this._ensure(4);
		if (len <= 127) this._buf[this._offset++] = len;
		else if (len <= 255) {
			this._buf[this._offset++] = 129;
			this._buf[this._offset++] = len;
		} else if (len <= 65535) {
			this._buf[this._offset++] = 130;
			this._buf[this._offset++] = len >> 8;
			this._buf[this._offset++] = len;
		} else if (len <= 16777215) {
			this._buf[this._offset++] = 131;
			this._buf[this._offset++] = len >> 16;
			this._buf[this._offset++] = len >> 8;
			this._buf[this._offset++] = len;
		} else throw newInvalidAsn1Error("Length too long (> 4 bytes)");
	};
	Writer.prototype.startSequence = function(tag) {
		if (typeof tag !== "number") tag = ASN1.Sequence | ASN1.Constructor;
		this.writeByte(tag);
		this._seq.push(this._offset);
		this._ensure(3);
		this._offset += 3;
	};
	Writer.prototype.endSequence = function() {
		var seq = this._seq.pop();
		var start = seq + 3;
		var len = this._offset - start;
		if (len <= 127) {
			this._shift(start, len, -2);
			this._buf[seq] = len;
		} else if (len <= 255) {
			this._shift(start, len, -1);
			this._buf[seq] = 129;
			this._buf[seq + 1] = len;
		} else if (len <= 65535) {
			this._buf[seq] = 130;
			this._buf[seq + 1] = len >> 8;
			this._buf[seq + 2] = len;
		} else if (len <= 16777215) {
			this._shift(start, len, 1);
			this._buf[seq] = 131;
			this._buf[seq + 1] = len >> 16;
			this._buf[seq + 2] = len >> 8;
			this._buf[seq + 3] = len;
		} else throw newInvalidAsn1Error("Sequence too long");
	};
	Writer.prototype._shift = function(start, len, shift) {
		assert.ok(start !== void 0);
		assert.ok(len !== void 0);
		assert.ok(shift);
		this._buf.copy(this._buf, start + shift, start, start + len);
		this._offset += shift;
	};
	Writer.prototype._ensure = function(len) {
		assert.ok(len);
		if (this._size - this._offset < len) {
			var sz = this._size * this._options.growthFactor;
			if (sz - this._offset < len) sz += len;
			var buf = Buffer.alloc(sz);
			this._buf.copy(buf, 0, 0, this._offset);
			this._buf = buf;
			this._size = sz;
		}
	};
	module.exports = Writer;
}) });

//#endregion
Object.defineProperty(exports, 'default', {
  enumerable: true,
  get: function () {
    return require_writer();
  }
});
//# sourceMappingURL=writer.cjs.map