gmime-iconv

## gmime-iconv gmime-iconv — Low-level routines for converting text from one charset to another |

## Synopsis

    void                g_mime_iconv_init                   (void);
    void                g_mime_iconv_shutdown               (void);
    iconv_t             g_mime_iconv_open                   (const char *to,
                                                             const char *from);
    #define             g_mime_iconv                        (cd,inbuf,inleft,outbuf,outleft)
    int                 g_mime_iconv_close                  (iconv_t cd);

## Description

These functions are wrappers around the system iconv(3) routines. The purpose of these wrappers are two-fold:

1. Cache iconv_t descriptors for you in order to optimize opening/closing many descriptors frequently

and

2. To use the appropriate system charset alias for the MIME charset names given as arguments.

## Details

### g_mime_iconv_init ()

    void                g_mime_iconv_init                   (void);

Initialize GMime's iconv cache. This *MUST* be called before any gmime-iconv interfaces will work correctly.

Note: this function is called for you by `g_mime_init()`.

### g_mime_iconv_shutdown ()

    void                g_mime_iconv_shutdown               (void);

Frees internal iconv caches created in `g_mime_iconv_init()`.

Note: this function is called for you by `g_mime_shutdown()`.

### g_mime_iconv_open ()

    iconv_t             g_mime_iconv_open                   (const char *to,
                                                             const char *from);

Allocates a coversion descriptor suitable for converting byte sequences from charset `from` to charset `to`. The resulting descriptor can be used with `iconv()` (or the `g_mime_iconv()` wrapper) any number of times until closed using `g_mime_iconv_close()`.

See the manual page for iconv_open(3) for further details.

`to` : | charset to convert to
`from` : | charset to convert from
Returns : | a new conversion descriptor for use with `g_mime_iconv()` on success or (iconv_t) `-1` on fail as well as setting an appropriate errno value.

### g_mime_iconv()

    #define             g_mime_iconv(cd,inbuf,inleft,outbuf,outleft)

The argument `cd` must be a conversion descriptor created using the function g_mime_iconv_open.

The main case is when `inbuf` is not `NULL` and *inbuf is not `NULL`. In this case, the g_mime_iconv function converts the multibyte sequence starting at *inbuf to a multibyte sequence starting at *outbuf. At most *inleft bytes, starting at *inbuf, will be read. At most *outleft bytes, starting at *outbuf, will be written.

The g_mime_iconv function converts one multibyte character at a time, and for each character conversion it increments *inbuf and decrements *inleft by the number of converted input bytes, it increments *outbuf and decrements *outleft by the number of converted output bytes, and it updates the conversion state contained in `cd`. The conversion can stop for four reasons:

1. An invalid multibyte sequence is encountered in the input. In this case it sets errno to `EILSEQ` and returns (size_t)(-1). *inbuf is left pointing to the beginning of the invalid multibyte sequence.

2. The input byte sequence has been entirely converted, i.e. *inleft has gone down to `0`. In this case g_mime_iconv returns the number of non-reversible conversions performed during this call.

3. An incomplete multibyte sequence is encountered in the input, and the input byte sequence terminates after it. In this case it sets errno to `EINVAL` and returns (size_t)(-1). *inbuf is left pointing to the beginning of the incomplete multibyte sequence.

4. The output buffer has no more room for the next converted character. In this case it sets errno to `E2BIG` and returns (size_t)(-1).

A different case is when `inbuf` is `NULL` or *inbuf is `NULL`, but `outbuf` is not `NULL` and *outbuf is not `NULL`. In this case, the g_mime_iconv function attempts to set `cd`'s conversion state to the initial state and store a corresponding shift sequence at *outbuf. At most *outleft bytes, starting at *outbuf, will be written. If the output buffer has no more room for this reset sequence, it sets errno to `E2BIG` and returns (size_t)(-1). Otherwise it increments *outbuf and decrements *outleft by the number of bytes written.

A third case is when `inbuf` is `NULL` or *inbuf is `NULL`, and `outbuf` is `NULL` or *outbuf is `NULL`. In this case, the g_mime_iconv function sets `cd`'s conversion state to the initial state.

`cd` : | iconv_t conversion descriptor
`inbuf` : | input buffer
`inleft` : | number of bytes left in `inbuf`
`outbuf` : | output buffer
`outleft` : | number of bytes left in `outbuf`

### g_mime_iconv_close ()

    int                 g_mime_iconv_close                  (iconv_t cd);

Closes the iconv descriptor `cd`.

See the manual page for iconv_close(3) for further details.

`cd` : | iconv conversion descriptor
Returns : | `0` on success or `-1` on fail as well as setting an appropriate errno value.