Search code examples
clibiconv

C convert iso ISO−8859-1 to UTF-8 with libconv


I need to encode Latin ISO−8859-1 chars into to UTF-8 (and the reverse operation too).

I first used this answer Is there a way to convert from UTF8 to iso-8859-1? to perform the operation and it works;

Now I want to use the libiconv which provides all the conversion mechanisms and should help me to keep my code simpler.

I have followed the example provided here: https://www.lemoda.net/c/iconv-example/iconv-example.html

and I wrote a method that looks like this:

char *iconvISO2UTF8(char *iso) {
    iconv_t iconvDesc = iconv_open ("ISO−8859-1", "UTF-8//TRANSLIT//IGNORE");

    if (iconvDesc == (iconv_t) - 1) {
        /* Something went wrong.  */
        if (errno == EINVAL)
            fprintf(stderr, "conversion from '%s' to '%s' not available", "ISO−8859−1", "UTF-8");           
        else
            fprintf(stderr, "LibIcon initialization failure");          

        return NULL;
    }

    size_t iconv_value;
    char * utf8;
    size_t len;
    size_t utf8len; 
    char * utf8start;

    int len_start;


    len = strlen (iso);
    if (! len) {        
        fprintf(stderr, "iconvISO2UTF8: input String is empty.");           
        return NULL;
    }

    /* Assign enough space to put the UTF-8. */
    utf8len = 2 * len;
    utf8 = calloc (utf8len, sizeof (char));
    if (! utf8) {
        fprintf(stderr, "iconvISO2UTF8: Calloc failed.");           
        return NULL;
    }
    /* Keep track of the variables. */
    utf8start = utf8;
    len_start = len;

    iconv_value = iconv (iconvDesc, & iso, & len, & utf8, & utf8len);
    /* Handle failures. */
    if (iconv_value == (size_t) - 1) {      
        switch (errno) {
                /* See "man 3 iconv" for an explanation. */
            case EILSEQ:
                fprintf(stderr, "iconv failed: Invalid multibyte sequence, in string '%s', length %d, out string '%s', length %d\n", iso, (int) len, utf8start, (int) utf8len);             
                break;
            case EINVAL:
                fprintf(stderr, "iconv failed: Incomplete multibyte sequence, in string '%s', length %d, out string '%s', length %d\n", iso, (int) len, utf8start, (int) utf8len);              
                break;
            case E2BIG:
                fprintf(stderr, "iconv failed: No more room, in string '%s', length %d, out string '%s', length %d\n", iso, (int)  len, utf8start, (int) utf8len);                              
                break;
            default:
                fprintf(stderr, "iconv failed, in string '%s', length %d, out string '%s', length %d\n", iso, (int) len, utf8start, (int) utf8len);                             
        }
        return NULL;
    }


    if(iconv_close (iconvDesc) != 0) {
        fprintf(stderr, "libicon close failed: %s", strerror (errno));          
    }

    return utf8start;

}

When I call this fonction with plain old ascii-like characters, like "abracadabra", iconv works. But as soon as I send accentuated chars to it, like "éàèüöä' then the iconv() call fails with a EILSEQ code:

iconv failed: Invalid multibyte sequence, in string 'éàèüöä', length 6, out string '', length 12

Here is a sample main program that crash when stored in a source file encoded with ISO−8859-1 and compiled on a linux system with ISO−8859-1 as default charset:

int main(int argc, char **argv) {
    char *iso1 = "abracadabra";
    char *utf = iconvISO2UTF8(iso1);
    puts(utf);
    free(utf);

    char *iso2 = "éàèüöä";
    utf = iconvISO2UTF8(iso2);
    puts(utf);
    free(utf);
}

Is it possible to run this kind of conversion with iconv ? If yes what's wrong in this code ?


Solution

  • Please read the iconv_open(3) manual page carefully:

    iconv_t iconv_open(const char *tocode, const char *fromcode);
    

    If you're converting to UTF-8 from ISO 8859-1 then this is at odds:

    iconv_t iconvDesc = iconv_open ("ISO−8859-1", "UTF-8//TRANSLIT//IGNORE");
    

    It should say

    iconv_t iconvDesc = iconv_open ("UTF-8//TRANSLIT//IGNORE", "ISO−8859-1");