Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 133 lines (105 sloc) 3.527 kb
cc03640 @toddtreece replaced utf8 encoding functions
authored
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 "common.h"
34 #include <errno.h>
35
36 #ifdef HAVE_GICONV_H
37 #include <giconv.h>
38 #else
39 #include <iconv.h>
40 #endif
41
42 #include "encodings.h"
43
44 #ifndef ICONV_CSNMAXLEN
45 #define ICONV_CSNMAXLEN 64
46 #endif
47
48 static char* convert(const char* src, int src_len, int *new_len, const char* from_enc, const char* to_enc) {
49 char* outbuf = 0;
50
51 if(src && src_len && from_enc && to_enc) {
52 size_t outlenleft = src_len;
53 size_t inlenleft = src_len;
54 int outlen = src_len;
55 iconv_t ic;
56 char* out_ptr = 0;
57
58 if(strlen(to_enc) >= ICONV_CSNMAXLEN || strlen(from_enc) >= ICONV_CSNMAXLEN) {
59 return NULL;
60 }
61 ic = iconv_open(to_enc, from_enc);
62 if(ic != (iconv_t)-1) {
63 size_t st;
64 outbuf = (char*)malloc(outlen + 1);
65
66 if(outbuf) {
67 out_ptr = (char*)outbuf;
68 while(inlenleft) {
69 st = iconv(ic, (char**)&src, &inlenleft, &out_ptr, &outlenleft);
70 if(st == -1) {
71 if(errno == E2BIG) {
72 int diff = out_ptr - outbuf;
73 outlen += inlenleft;
74 outlenleft += inlenleft;
75 outbuf = (char*)realloc(outbuf, outlen + 1);
76 if(!outbuf) {
77 break;
78 }
79 out_ptr = outbuf + diff;
80 }
81 else {
82 free(outbuf);
83 outbuf = 0;
84 break;
85 }
86 }
87 }
88 }
89 iconv_close(ic);
90 }
91 outlen -= outlenleft;
92
93 if(new_len) {
94 *new_len = outbuf ? outlen : 0;
95 }
96 if(outbuf) {
97 outbuf[outlen] = 0;
98 }
99 }
100 return outbuf;
101 }
102
103 /* returns a new string that must be freed */
104 char* utf8_encode(const char *str) {
105
08be2e6 @toddtreece chars default to NULL
authored
106 char *out = NULL;
cc03640 @toddtreece replaced utf8 encoding functions
authored
107
108 if(strlen(str)) {
109 int alen, len;
110 alen = strlen(str);
111 out = convert(str, alen, &len, "ISO-8859-1", "UTF-8");
112 }
113
114 return out;
115
116 }
117
118 /* returns a new string, possibly decoded */
119 char* utf8_decode(const char *str) {
120
08be2e6 @toddtreece chars default to NULL
authored
121 char *out = NULL;
cc03640 @toddtreece replaced utf8 encoding functions
authored
122
123 if (strlen(str)) {
124 int alen, len;
125 alen = strlen(str);
126 out = convert(str, alen, &len, "UTF-8", "ISO-8859-1");
127 }
128
129 return out;
130
131 }
132
Something went wrong with that request. Please try again.