Subject: iconv_open() / UCS-2-INTERNAL question
To: None <current-users@netbsd.org>
From: Chuck Cranor <chuck@ece.cmu.edu>
List: current-users
Date: 01/28/2007 22:12:53
hi-

    i updated my version of gphoto2 to libgphoto2-2.3.1 and the
PTP2 driver stopped working due to iconv_open() failing.

     if you look at:

http://gphoto.svn.sourceforge.net/viewvc/gphoto/trunk/libgphoto2/camlibs/ptp2/library.c?view=markup


     you will see that they are doing code that basically boils 
down to calling iconv_open(3) with "UCS-2-INTERNAL" ... but we
don't appear to have a "UCS-2-INTERNAL" so clearly the new gphoto 2.3.1
code is wrong.  What should they be doing here?   they appear to
be trying to address some sort of endian problem (see comment I copied
from them in the test code below).

     the test program below is based on their gphoto2 code (with
all the extra stuff thrown out).  it fails with:

xxxcdc> ./a.out 
curloc=646, UCS_2_INTERNAL=UCS-2-INTERNAL
iconv_open A: Invalid argument
iconv_open B: Invalid argument
xxxcdc> 


chuck


#include <stdio.h>
#include <langinfo.h>
#include <iconv.h>

/*
 * On MacOS (Darwin) and *BSD we're not using glibc, but libiconv.
 * glibc knows that UCS-2 is to be in the local machine endianness,
 * whereas libiconv does not. So we construct this macro to get
 * things right. Reportedly, glibc 2.1.3 has a bug so that UCS-2
 * is always bigendian though, we would need to work around that
 * too...
 */
#ifndef __GLIBC__
#define UCS_2_INTERNAL "UCS-2-INTERNAL"
#else
#if (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1 )
#error "Too old glibc. This versions iconv() implementation cannot be trusted."
#endif
#define UCS_2_INTERNAL "UCS-2"
#endif

main() {

     char *curloc;
     iconv_t a, b;

     curloc = nl_langinfo (CODESET);

     printf("curloc=%s, UCS_2_INTERNAL=%s\n", curloc, UCS_2_INTERNAL);

     a = iconv_open(curloc, UCS_2_INTERNAL);
     if (a == (iconv_t)-1) perror("iconv_open A");
     b = iconv_open(UCS_2_INTERNAL, curloc);
     if (b == (iconv_t)-1) perror("iconv_open B");
}