2 This file is part of libXMLRPC - a C library for xml-encoded function calls.
4 Author: Dan Libby (dan@libby.com)
5 Epinions.com may be contacted at feedback@epinions-inc.com
9 Copyright 2000 Epinions, Inc.
11 Subject to the following 3 conditions, Epinions, Inc. permits you, free
12 of charge, to (a) use, copy, distribute, modify, perform and display this
13 software and associated documentation files (the "Software"), and (b)
14 permit others to whom the Software is furnished to do so as well.
16 1) The above copyright notice and this permission notice shall be included
17 without modification in all copies or substantial portions of the
20 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
21 ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
22 IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
23 PURPOSE OR NONINFRINGEMENT.
25 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
26 SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
27 OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
28 NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH
38 #include <php_config.h>
40 #include <config.w32.h>
44 static const char rcsid[] = "#(@) $Id$";
54 #include "encodings.h"
56 static char* convert(const char* src, int src_len, int *new_len, const char* from_enc, const char* to_enc) {
59 if(src && src_len && from_enc && to_enc) {
60 size_t outlenleft = src_len;
61 size_t inlenleft = src_len;
63 iconv_t ic = iconv_open(to_enc, from_enc);
66 if(ic != (iconv_t)-1) {
68 outbuf = (char*)malloc(outlen + 1);
71 out_ptr = (char*)outbuf;
73 st = iconv(ic, (char**)&src, &inlenleft, &out_ptr, &outlenleft);
76 int diff = out_ptr - outbuf;
78 outlenleft += inlenleft;
79 outbuf = (char*)realloc(outbuf, outlen + 1);
83 out_ptr = outbuf + diff;
98 *new_len = outbuf ? outlen : 0;
107 /* returns a new string that must be freed */
108 char* utf8_encode(const char *s, int len, int *newlen, const char* encoding)
110 return convert(s, len, newlen, encoding, "UTF-8");
113 /* returns a new string, possibly decoded */
114 char* utf8_decode(const char *s, int len, int *newlen, const char* encoding)
116 return convert(s, len, newlen, "UTF-8", encoding);