xref: /PHP-7.4/ext/xmlrpc/libxmlrpc/encodings.c (revision 29eb3561)
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 #include <errno.h>
36 #include <string.h>
37 
38 #ifdef HAVE_GICONV_H
39 #include <giconv.h>
40 #else
41 #include <iconv.h>
42 #endif
43 
44 #include "encodings.h"
45 
46 #ifndef ICONV_CSNMAXLEN
47 #define ICONV_CSNMAXLEN 64
48 #endif
49 
convert(const char * src,int src_len,int * new_len,const char * from_enc,const char * to_enc)50 static char* convert(const char* src, int src_len, int *new_len, const char* from_enc, const char* to_enc) {
51    char* outbuf = 0;
52 
53    if(src && src_len && from_enc && to_enc) {
54       size_t outlenleft = src_len;
55       size_t inlenleft = src_len;
56       int outlen = src_len;
57       iconv_t ic;
58       char* out_ptr = 0;
59 
60       if(strlen(to_enc) >= ICONV_CSNMAXLEN || strlen(from_enc) >= ICONV_CSNMAXLEN) {
61          return NULL;
62       }
63       ic = iconv_open(to_enc, from_enc);
64       if(ic != (iconv_t)-1) {
65          size_t st;
66          outbuf = (char*)emalloc(outlen + 1);
67 
68          out_ptr = (char*)outbuf;
69          while(inlenleft) {
70             st = iconv(ic, (char**)&src, &inlenleft, &out_ptr, &outlenleft);
71             if(st == -1) {
72                if(errno == E2BIG) {
73                   int diff = out_ptr - outbuf;
74                   outlen += inlenleft;
75                   outlenleft += inlenleft;
76                   outbuf = (char*)erealloc(outbuf, outlen + 1);
77                   out_ptr = outbuf + diff;
78                }
79                else {
80                   efree(outbuf);
81                   outbuf = 0;
82                   break;
83                }
84             }
85          }
86          iconv_close(ic);
87       }
88       outlen -= outlenleft;
89 
90       if(new_len) {
91          *new_len = outbuf ? outlen : 0;
92       }
93       if(outbuf) {
94          outbuf[outlen] = 0;
95       }
96    }
97    return outbuf;
98 }
99 
100 /* returns a new string that must be freed */
utf8_encode(const char * s,int len,int * newlen,const char * encoding)101 char* utf8_encode(const char *s, int len, int *newlen, const char* encoding)
102 {
103    return convert(s, len, newlen, encoding, "UTF-8");
104 }
105 
106 /* returns a new string, possibly decoded */
utf8_decode(const char * s,int len,int * newlen,const char * encoding)107 char* utf8_decode(const char *s, int len, int *newlen, const char* encoding)
108 {
109    return convert(s, len, newlen, "UTF-8", encoding);
110 }
111