Select Git revision
builtin.cmod
PEM.pmod 4.83 KiB
/* PEM.pmod
*
* Support for parsing PEM-style messages.
*/
class encapsulated_message {
import ._PEM;
string boundary;
string body;
mapping(string:string) headers;
object init(string eb, string contents)
{
boundary = eb;
// werror(sprintf("init: contents = '%s\n'", contents));
if (rfc822_start_re->match(contents))
{
array a = MIME.parse_headers(contents);
headers = a[0];
body = a[1];
} else {
headers = 0;
body = contents;
}
return this_object();
}
string decoded_body()
{
return MIME.decode_base64(body);
}
string get_boundary()
{
return extract_boundary(boundary);
}
string canonical_body()
{
/* Replace singular LF with CRLF */
array lines = body / "\n";
/* Make all lines terminated with \r (but the last, which is
* either empty or a "line" that was not terminated). */
for(int i=0; i < sizeof(lines)-1; i++)
if (!strlen(lines[i]) || (lines[i][-1] != '\r'))
lines[i] += "\r";
return lines * "\n";
}
string to_string()
{
string s = (headers
? Array.map(indices(headers),
lambda(string hname, mapping(string:string) h)
{
return hname+": "+h[hname];
}, headers) * "\n"
: "");
return s + "\n\n" + body;
}
}
class rfc934 {
import ._PEM;