RFC2047 MIME extensions encoding / decoding routines. More...
#include "config.h"
#include <ctype.h>
#include <errno.h>
#include <iconv.h>
#include <stdbool.h>
#include <string.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "rfc2047.h"
#include "envelope.h"
#include "mime.h"
Go to the source code of this file.
Macros | |
#define | ENCWORD_LEN_MAX 75 |
#define | ENCWORD_LEN_MIN 9 /* strlen ("=?.?.?.?=") */ |
#define | HSPACE(ch) (((ch) == '\0') || ((ch) == ' ') || ((ch) == '\t')) |
#define | CONTINUATION_BYTE(ch) (((ch) & 0xc0) == 0x80) |
Typedefs | |
typedef size_t(* | encoder_t) (char *res, const char *buf, size_t buflen, const char *tocode) |
Functions | |
static size_t | b_encoder (char *res, const char *src, size_t srclen, const char *tocode) |
Base64 Encode a string - Implements encoder_t -. | |
static size_t | q_encoder (char *res, const char *src, size_t srclen, const char *tocode) |
Quoted-printable Encode a string - Implements encoder_t -. | |
static char * | parse_encoded_word (char *str, enum ContentEncoding *enc, char **charset, size_t *charsetlen, char **text, size_t *textlen) |
Parse a string and report RFC2047 elements. | |
static size_t | try_block (const char *d, size_t dlen, const char *fromcode, const char *tocode, encoder_t *encoder, size_t *wlen) |
Attempt to convert a block of text. | |
static size_t | encode_block (char *str, char *buf, size_t buflen, const char *fromcode, const char *tocode, encoder_t encoder) |
Encode a block of text using an encoder. | |
static size_t | choose_block (char *d, size_t dlen, int col, const char *fromcode, const char *tocode, encoder_t *encoder, size_t *wlen) |
Calculate how much data can be converted. | |
static void | finalize_chunk (struct Buffer *res, struct Buffer *buf, char *charset, size_t charsetlen) |
Perform charset conversion and filtering. | |
static char * | decode_word (const char *s, size_t len, enum ContentEncoding enc) |
Decode an RFC2047-encoded string. | |
static int | encode (const char *d, size_t dlen, int col, const char *fromcode, const struct Slist *charsets, char **e, size_t *elen, const char *specials) |
RFC2047-encode a string. | |
void | rfc2047_encode (char **pd, const char *specials, int col, const struct Slist *charsets) |
RFC-2047-encode a string. | |
void | rfc2047_decode (char **pd) |
Decode any RFC2047-encoded header fields. | |
void | rfc2047_encode_addrlist (struct AddressList *al, const char *tag) |
Encode any RFC2047 headers, where required, in an Address list. | |
void | rfc2047_decode_addrlist (struct AddressList *al) |
Decode any RFC2047 headers in an Address list. | |
void | rfc2047_decode_envelope (struct Envelope *env) |
Decode the fields of an Envelope. | |
void | rfc2047_encode_envelope (struct Envelope *env) |
Encode the fields of an Envelope. | |
RFC2047 MIME extensions encoding / decoding routines.
This program 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 this program. If not, see http://www.gnu.org/licenses/.
Definition in file rfc2047.c.
#define HSPACE | ( | ch | ) | (((ch) == '\0') || ((ch) == ' ') || ((ch) == '\t')) |
typedef size_t(* encoder_t) (char *res, const char *buf, size_t buflen, const char *tocode) |
|
static |
Parse a string and report RFC2047 elements.
[in] | str | String to parse |
[out] | enc | Content encoding found in the first RFC2047 word |
[out] | charset | Charset found in the first RFC2047 word |
[out] | charsetlen | Length of the charset string found |
[out] | text | Start of the first RFC2047 encoded text |
[out] | textlen | Length of the encoded text found |
ptr | Start of the RFC2047 encoded word |
NULL | None was found |
Definition at line 148 of file rfc2047.c.
|
static |
Attempt to convert a block of text.
d | String to convert |
dlen | Length of string |
fromcode | Original encoding |
tocode | New encoding |
encoder | Encoding function |
wlen | Number of characters converted |
0 | Success, string converted |
>0 | Error, number of bytes that could be converted |
If the data could be converted using encoder, then set *encoder and *wlen. Otherwise return an upper bound on the maximum length of the data which could be converted.
The data is converted from fromcode (which must be stateless) to tocode, unless fromcode is NULL, in which case the data is assumed to be already in tocode, which should be 8-bit and stateless.
Definition at line 191 of file rfc2047.c.
|
static |
Encode a block of text using an encoder.
str | String to convert |
buf | Buffer for result |
buflen | Buffer length |
fromcode | Original encoding |
tocode | New encoding |
encoder | Encoding function |
num | Length of the encoded word |
Encode the data (buf, buflen) into str using the encoder.
Definition at line 274 of file rfc2047.c.
|
static |
Calculate how much data can be converted.
d | String to convert |
dlen | Length of string |
col | Starting column to convert |
fromcode | Original encoding |
tocode | New encoding |
encoder | Encoding function |
wlen | Number of characters converted |
num | Bytes that can be converted |
Discover how much of the data (d, dlen) can be converted into a single encoded word. Return how much data can be converted, and set the length *wlen of the encoded word and *encoder. We start in column col, which limits the length of the word.
Definition at line 311 of file rfc2047.c.
|
static |
Perform charset conversion and filtering.
[out] | res | Buffer where the resulting string is appended |
[in] | buf | Buffer with the input string |
[in] | charset | Charset to use for the conversion |
[in] | charsetlen | Length of the charset parameter |
The buffer buf is reinitialized at the end of this function.
Definition at line 341 of file rfc2047.c.
|
static |
Decode an RFC2047-encoded string.
s | String to decode |
len | Length of the string |
enc | Encoding type |
ptr | Decoded string |
Definition at line 365 of file rfc2047.c.
|
static |
RFC2047-encode a string.
[in] | d | String to convert |
[in] | dlen | Length of string |
[in] | col | Starting column to convert |
[in] | fromcode | Original encoding |
[in] | charsets | List of allowable encodings (colon separated) |
[out] | e | Encoded string |
[out] | elen | Length of encoded string |
[in] | specials | Special characters to be encoded |
0 | Success |
Definition at line 426 of file rfc2047.c.
void rfc2047_encode | ( | char ** | pd, |
const char * | specials, | ||
int | col, | ||
const struct Slist * | charsets | ||
) |
RFC-2047-encode a string.
[in,out] | pd | String to be encoded, and resulting encoded string |
[in] | specials | Special characters to be encoded |
[in] | col | Starting index in string |
[in] | charsets | List of charsets to choose from |
Definition at line 628 of file rfc2047.c.
void rfc2047_decode | ( | char ** | pd | ) |
Decode any RFC2047-encoded header fields.
[in,out] | pd | String to be decoded, and resulting decoded string |
Try to decode anything that looks like a valid RFC2047 encoded header field, ignoring RFC822 parsing rules. If decoding fails, for example due to an invalid base64 string, the original input is left untouched.
Definition at line 661 of file rfc2047.c.
void rfc2047_encode_addrlist | ( | struct AddressList * | al, |
const char * | tag | ||
) |
Encode any RFC2047 headers, where required, in an Address list.
al | AddressList |
tag | Header tag (used for wrapping calculation) |
Definition at line 766 of file rfc2047.c.
void rfc2047_decode_addrlist | ( | struct AddressList * | al | ) |
Decode any RFC2047 headers in an Address list.
al | AddressList |
Definition at line 801 of file rfc2047.c.
void rfc2047_decode_envelope | ( | struct Envelope * | env | ) |
Decode the fields of an Envelope.
env | Envelope |
Definition at line 832 of file rfc2047.c.
void rfc2047_encode_envelope | ( | struct Envelope * | env | ) |
Encode the fields of an Envelope.
env | Envelope |
Definition at line 857 of file rfc2047.c.