import { __commonJS } from "../../../_virtual/rolldown_runtime.mjs";
import { require_forge } from "./forge.mjs";
import { require_util } from "./util.mjs";

//#region ../../node_modules/node-forge/lib/pem.js
var require_pem = /* @__PURE__ */ __commonJS({ "../../node_modules/node-forge/lib/pem.js": ((exports, module) => {
	/**
	* Javascript implementation of basic PEM (Privacy Enhanced Mail) algorithms.
	*
	* See: RFC 1421.
	*
	* @author Dave Longley
	*
	* Copyright (c) 2013-2014 Digital Bazaar, Inc.
	*
	* A Forge PEM object has the following fields:
	*
	* type: identifies the type of message (eg: "RSA PRIVATE KEY").
	*
	* procType: identifies the type of processing performed on the message,
	*   it has two subfields: version and type, eg: 4,ENCRYPTED.
	*
	* contentDomain: identifies the type of content in the message, typically
	*   only uses the value: "RFC822".
	*
	* dekInfo: identifies the message encryption algorithm and mode and includes
	*   any parameters for the algorithm, it has two subfields: algorithm and
	*   parameters, eg: DES-CBC,F8143EDE5960C597.
	*
	* headers: contains all other PEM encapsulated headers -- where order is
	*   significant (for pairing data like recipient ID + key info).
	*
	* body: the binary-encoded body.
	*/
	var forge = require_forge();
	require_util();
	var pem = module.exports = forge.pem = forge.pem || {};
	/**
	* Encodes (serializes) the given PEM object.
	*
	* @param msg the PEM message object to encode.
	* @param options the options to use:
	*          maxline the maximum characters per line for the body, (default: 64).
	*
	* @return the PEM-formatted string.
	*/
	pem.encode = function(msg, options) {
		options = options || {};
		var rval = "-----BEGIN " + msg.type + "-----\r\n";
		var header;
		if (msg.procType) {
			header = {
				name: "Proc-Type",
				values: [String(msg.procType.version), msg.procType.type]
			};
			rval += foldHeader(header);
		}
		if (msg.contentDomain) {
			header = {
				name: "Content-Domain",
				values: [msg.contentDomain]
			};
			rval += foldHeader(header);
		}
		if (msg.dekInfo) {
			header = {
				name: "DEK-Info",
				values: [msg.dekInfo.algorithm]
			};
			if (msg.dekInfo.parameters) header.values.push(msg.dekInfo.parameters);
			rval += foldHeader(header);
		}
		if (msg.headers) for (var i = 0; i < msg.headers.length; ++i) rval += foldHeader(msg.headers[i]);
		if (msg.procType) rval += "\r\n";
		rval += forge.util.encode64(msg.body, options.maxline || 64) + "\r\n";
		rval += "-----END " + msg.type + "-----\r\n";
		return rval;
	};
	/**
	* Decodes (deserializes) all PEM messages found in the given string.
	*
	* @param str the PEM-formatted string to decode.
	*
	* @return the PEM message objects in an array.
	*/
	pem.decode = function(str) {
		var rval = [];
		var rMessage = /\s*-----BEGIN ([A-Z0-9- ]+)-----\r?\n?([\x21-\x7e\s]+?(?:\r?\n\r?\n))?([:A-Za-z0-9+\/=\s]+?)-----END \1-----/g;
		var rHeader = /([\x21-\x7e]+):\s*([\x21-\x7e\s^:]+)/;
		var rCRLF = /\r?\n/;
		var match;
		while (true) {
			match = rMessage.exec(str);
			if (!match) break;
			var type = match[1];
			if (type === "NEW CERTIFICATE REQUEST") type = "CERTIFICATE REQUEST";
			var msg = {
				type,
				procType: null,
				contentDomain: null,
				dekInfo: null,
				headers: [],
				body: forge.util.decode64(match[3])
			};
			rval.push(msg);
			if (!match[2]) continue;
			var lines = match[2].split(rCRLF);
			var li = 0;
			while (match && li < lines.length) {
				var line = lines[li].replace(/\s+$/, "");
				for (var nl = li + 1; nl < lines.length; ++nl) {
					var next = lines[nl];
					if (!/\s/.test(next[0])) break;
					line += next;
					li = nl;
				}
				match = line.match(rHeader);
				if (match) {
					var header = {
						name: match[1],
						values: []
					};
					var values = match[2].split(",");
					for (var vi = 0; vi < values.length; ++vi) header.values.push(ltrim(values[vi]));
					if (!msg.procType) {
						if (header.name !== "Proc-Type") throw new Error("Invalid PEM formatted message. The first encapsulated header must be \"Proc-Type\".");
						else if (header.values.length !== 2) throw new Error("Invalid PEM formatted message. The \"Proc-Type\" header must have two subfields.");
						msg.procType = {
							version: values[0],
							type: values[1]
						};
					} else if (!msg.contentDomain && header.name === "Content-Domain") msg.contentDomain = values[0] || "";
					else if (!msg.dekInfo && header.name === "DEK-Info") {
						if (header.values.length === 0) throw new Error("Invalid PEM formatted message. The \"DEK-Info\" header must have at least one subfield.");
						msg.dekInfo = {
							algorithm: values[0],
							parameters: values[1] || null
						};
					} else msg.headers.push(header);
				}
				++li;
			}
			if (msg.procType === "ENCRYPTED" && !msg.dekInfo) throw new Error("Invalid PEM formatted message. The \"DEK-Info\" header must be present if \"Proc-Type\" is \"ENCRYPTED\".");
		}
		if (rval.length === 0) throw new Error("Invalid PEM formatted message.");
		return rval;
	};
	function foldHeader(header) {
		var rval = header.name + ": ";
		var values = [];
		var insertSpace = function(match, $1) {
			return " " + $1;
		};
		for (var i = 0; i < header.values.length; ++i) values.push(header.values[i].replace(/^(\S+\r\n)/, insertSpace));
		rval += values.join(",") + "\r\n";
		var length = 0;
		var candidate = -1;
		for (var i = 0; i < rval.length; ++i, ++length) if (length > 65 && candidate !== -1) {
			var insert = rval[candidate];
			if (insert === ",") {
				++candidate;
				rval = rval.substr(0, candidate) + "\r\n " + rval.substr(candidate);
			} else rval = rval.substr(0, candidate) + "\r\n" + insert + rval.substr(candidate + 1);
			length = i - candidate - 1;
			candidate = -1;
			++i;
		} else if (rval[i] === " " || rval[i] === "	" || rval[i] === ",") candidate = i;
		return rval;
	}
	function ltrim(str) {
		return str.replace(/^\s+/, "");
	}
}) });

//#endregion
export default require_pem();

export { require_pem };
//# sourceMappingURL=pem.mjs.map