src/stdlib/SDL_iconv.c
changeset 2136 5a1b6fe6e90f
parent 2135 0313af081a84
child 2142 ca80c942e69c
equal deleted inserted replaced
2135:0313af081a84 2136:5a1b6fe6e90f
    25 
    25 
    26 #include "SDL_stdinc.h"
    26 #include "SDL_stdinc.h"
    27 #include "SDL_endian.h"
    27 #include "SDL_endian.h"
    28 
    28 
    29 #ifdef HAVE_ICONV
    29 #ifdef HAVE_ICONV
       
    30 
       
    31 /* Depending on which standard the iconv() was implemented with,
       
    32    iconv() may or may not use const char ** for the inbuf param.
       
    33    If we get this wrong, it's just a warning, so no big deal.
       
    34 */
       
    35 #if defined(_XGP6) || \
       
    36     defined(__GLIBC__) && ((__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
       
    37 #define ICONV_INBUF_NONCONST
       
    38 #endif
    30 
    39 
    31 #include <errno.h>
    40 #include <errno.h>
    32 
    41 
    33 size_t
    42 size_t
    34 SDL_iconv(SDL_iconv_t cd,
    43 SDL_iconv(SDL_iconv_t cd,
    35           const char **inbuf, size_t * inbytesleft,
    44           const char **inbuf, size_t * inbytesleft,
    36           char **outbuf, size_t * outbytesleft)
    45           char **outbuf, size_t * outbytesleft)
    37 {
    46 {
    38     size_t retCode;
    47     size_t retCode;
    39 #ifdef ICONV_REALLY_MODIFIES_INBUF
    48 #ifdef ICONV_INBUF_NONCONST
    40     if (inbuf && *inbuf && inbytesleft) {
    49     retCode = iconv(cd, (char **) inbuf, inbytesleft, outbuf, outbytesleft);
    41         char *tmp = SDL_stack_alloc(char, *inbytesleft);
       
    42         char *ptr = tmp;
       
    43         SDL_memcpy(tmp, inbuf, *inbytesleft);
       
    44         retCode = iconv(cd, &ptr, inbytesleft, outbuf, outbytesleft);
       
    45         inbuf += (ptr - tmp);
       
    46         SDL_stack_free(tmp);
       
    47     } else {
       
    48         retCode = iconv(cd, NULL, inbytesleft, outbuf, outbytesleft);
       
    49     }
       
    50 #else
    50 #else
    51     retCode = iconv(cd, (char **) inbuf, inbytesleft, outbuf, outbytesleft);
    51     retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
    52 #endif
    52 #endif
    53     if (retCode == (size_t) - 1) {
    53     if (retCode == (size_t) - 1) {
    54         switch (errno) {
    54         switch (errno) {
    55         case E2BIG:
    55         case E2BIG:
    56             return SDL_ICONV_E2BIG;
    56             return SDL_ICONV_E2BIG;