1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2005-2025 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <mailutils/mailutils.h>
/* Replace all octal escapes in BUF with the corresponding characters. */
static void
decode_octal (char *buf)
{
char *p;
unsigned i, n;
for (p = buf; *p;)
{
if (*buf == '\\')
{
buf++;
switch (*buf)
{
case 'a':
*p++ = '\a';
buf++;
break;
case 'b':
*p++ = '\b';
buf++;
break;
case 'f':
*p++ = '\f';
buf++;
break;
case 'n':
*p++ = '\n';
buf++;
break;
case 'r':
*p++ = '\r';
buf++;
break;
case 't':
*p++ = '\t';
buf++;
break;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
n = 0;
for (i = 0; i < 3; i++, buf++)
{
unsigned x = *(unsigned char*)buf - '0';
if (x > 7)
break;
n <<= 3;
n += x;
}
if (i != 3)
{
buf -= i;
*p++ = '\\';
}
else
*p++ = n;
break;
default:
*p++ = '\\';
*p++ = *buf++;
break;
}
}
else
*p++ = *buf++;
}
*p = 0;
}
int
main (int argc, char *argv[])
{
int rc;
char *buf = NULL;
size_t size = 0;
size_t n;
char *charset = "iso-8859-1";
char *encoding = "quoted-printable";
int octal = 0;
struct mu_option options[] = {
{ "charset", 'c', "NAME", MU_OPTION_DEFAULT,
"define input character set", mu_c_string, &charset },
{ "encoding", 'e', "NAME", MU_OPTION_DEFAULT,
"define input encoding", mu_c_string, &encoding },
{ "octal", 'o', NULL, MU_OPTION_DEFAULT,
"decode octal escape notations on input", mu_c_bool, &octal },
MU_OPTION_END
};
mu_set_program_name (argv[0]);
mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
mu_cli_simple (argc, argv,
MU_CLI_OPTION_OPTIONS, options,
MU_CLI_OPTION_PROG_DOC, "Test RFC 2047 encoding function",
MU_CLI_OPTION_EXTRA_INFO, "Defaults are: --charset=iso-8859-1 --encoding=quoted-printable",
MU_CLI_OPTION_END);
while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0)
{
char *p;
mu_rtrim_class (buf, MU_CTYPE_ENDLN);
if (octal)
decode_octal (buf);
rc = mu_rfc2047_encode (charset, encoding, buf, &p);
if (rc)
mu_diag_funcall (MU_DIAG_ERROR, "mu_rfc2047_encode", NULL, rc);
else if (p)
mu_printf ("%s\n", p);
free (p);
}
if (rc)
mu_diag_funcall (MU_DIAG_ERROR, "mu_stream_getline", NULL, rc);
return 0;
}
|