1 /*
2 This file is part of libXMLRPC - a C library for xml-encoded function calls.
3
4 Author: Dan Libby (dan@libby.com)
5 Epinions.com may be contacted at feedback@epinions-inc.com
6 */
7
8 /*
9 Copyright 2000 Epinions, Inc.
10
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.
15
16 1) The above copyright notice and this permission notice shall be included
17 without modification in all copies or substantial portions of the
18 Software.
19
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.
24
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
29 DAMAGES.
30
31 */
32
33 #include <php.h>
34
35 static const char rcsid[] = "#(@) $Id$";
36
37 #include <errno.h>
38 #include <string.h>
39
40 #ifdef HAVE_GICONV_H
41 #include <giconv.h>
42 #else
43 #include <iconv.h>
44 #endif
45
46 #include "encodings.h"
47
48 #ifndef ICONV_CSNMAXLEN
49 #define ICONV_CSNMAXLEN 64
50 #endif
51
convert(const char * src,int src_len,int * new_len,const char * from_enc,const char * to_enc)52 static char* convert(const char* src, int src_len, int *new_len, const char* from_enc, const char* to_enc) {
53 char* outbuf = 0;
54
55 if(src && src_len && from_enc && to_enc) {
56 size_t outlenleft = src_len;
57 size_t inlenleft = src_len;
58 int outlen = src_len;
59 iconv_t ic;
60 char* out_ptr = 0;
61
62 if(strlen(to_enc) >= ICONV_CSNMAXLEN || strlen(from_enc) >= ICONV_CSNMAXLEN) {
63 return NULL;
64 }
65 ic = iconv_open(to_enc, from_enc);
66 if(ic != (iconv_t)-1) {
67 size_t st;
68 outbuf = (char*)emalloc(outlen + 1);
69
70 if(outbuf) {
71 out_ptr = (char*)outbuf;
72 while(inlenleft) {
73 st = iconv(ic, (char**)&src, &inlenleft, &out_ptr, &outlenleft);
74 if(st == -1) {
75 if(errno == E2BIG) {
76 int diff = out_ptr - outbuf;
77 outlen += inlenleft;
78 outlenleft += inlenleft;
79 outbuf = (char*)erealloc(outbuf, outlen + 1);
80 if(!outbuf) {
81 break;
82 }
83 out_ptr = outbuf + diff;
84 }
85 else {
86 efree(outbuf);
87 outbuf = 0;
88 break;
89 }
90 }
91 }
92 }
93 iconv_close(ic);
94 }
95 outlen -= outlenleft;
96
97 if(new_len) {
98 *new_len = outbuf ? outlen : 0;
99 }
100 if(outbuf) {
101 outbuf[outlen] = 0;
102 }
103 }
104 return outbuf;
105 }
106
107 /* returns a new string that must be freed */
utf8_encode(const char * s,int len,int * newlen,const char * encoding)108 char* utf8_encode(const char *s, int len, int *newlen, const char* encoding)
109 {
110 return convert(s, len, newlen, encoding, "UTF-8");
111 }
112
113 /* returns a new string, possibly decoded */
utf8_decode(const char * s,int len,int * newlen,const char * encoding)114 char* utf8_decode(const char *s, int len, int *newlen, const char* encoding)
115 {
116 return convert(s, len, newlen, "UTF-8", encoding);
117 }
118