Back to index

php5  5.3.10
Defines | Functions | Variables
encodings.c File Reference
#include <php_config.h>
#include <errno.h>
#include <iconv.h>
#include "encodings.h"

Go to the source code of this file.

Defines

#define ICONV_CSNMAXLEN   64

Functions

static char * convert (const char *src, int src_len, int *new_len, const char *from_enc, const char *to_enc)
char * utf8_encode (const char *s, int len, int *newlen, const char *encoding)
char * utf8_decode (const char *s, int len, int *newlen, const char *encoding)

Variables

static const char rcsid [] = "#(@) $Id: encodings.c 242949 2007-09-26 15:44:16Z cvs2svn $"

Define Documentation

#define ICONV_CSNMAXLEN   64

Definition at line 57 of file encodings.c.


Function Documentation

static char* convert ( const char *  src,
int  src_len,
int new_len,
const char *  from_enc,
const char *  to_enc 
) [static]

Definition at line 60 of file encodings.c.

                                                                                                           {
   char* outbuf = 0;

   if(src && src_len && from_enc && to_enc) {
      size_t outlenleft = src_len;
      size_t inlenleft = src_len;
      int outlen = src_len;
      iconv_t ic;
      char* out_ptr = 0;

      if(strlen(to_enc) >= ICONV_CSNMAXLEN || strlen(from_enc) >= ICONV_CSNMAXLEN) {
         return NULL;
      }
      ic = iconv_open(to_enc, from_enc);
      if(ic != (iconv_t)-1) {
         size_t st;
         outbuf = (char*)malloc(outlen + 1);

         if(outbuf) {
            out_ptr = (char*)outbuf;
            while(inlenleft) {
               st = iconv(ic, (char**)&src, &inlenleft, &out_ptr, &outlenleft);
               if(st == -1) {
                  if(errno == E2BIG) {
                     int diff = out_ptr - outbuf;
                     outlen += inlenleft;
                     outlenleft += inlenleft;
                     outbuf = (char*)realloc(outbuf, outlen + 1);
                     if(!outbuf) {
                        break;
                     }
                     out_ptr = outbuf + diff;
                  }
                  else {
                     free(outbuf);
                     outbuf = 0;
                     break;
                  }
               }
            }
         }
         iconv_close(ic);
      }
      outlen -= outlenleft;

      if(new_len) {
         *new_len = outbuf ? outlen : 0;
      }
      if(outbuf) {
         outbuf[outlen] = 0;
      }
   }
   return outbuf;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* utf8_decode ( const char *  s,
int  len,
int newlen,
const char *  encoding 
)

Definition at line 122 of file encodings.c.

{
   return convert(s, len, newlen, "UTF-8", encoding);
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* utf8_encode ( const char *  s,
int  len,
int newlen,
const char *  encoding 
)

Definition at line 116 of file encodings.c.

{
   return convert(s, len, newlen, encoding, "UTF-8");
}

Here is the call graph for this function:


Variable Documentation

const char rcsid[] = "#(@) $Id: encodings.c 242949 2007-09-26 15:44:16Z cvs2svn $" [static]

Definition at line 44 of file encodings.c.