This source file includes following definitions.
- convert
- utf8_encode
- utf8_decode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #ifndef PHP_WIN32
38 #include <php_config.h>
39 #else
40 #include <config.w32.h>
41 #include <stdlib.h>
42 #endif
43
44 static const char rcsid[] = "#(@) $Id$";
45
46 #include <errno.h>
47 #include <string.h>
48
49 #ifdef HAVE_GICONV_H
50 #include <giconv.h>
51 #else
52 #include <iconv.h>
53 #endif
54
55 #include "encodings.h"
56
57 #ifndef ICONV_CSNMAXLEN
58 #define ICONV_CSNMAXLEN 64
59 #endif
60
61 static char* convert(const char* src, int src_len, int *new_len, const char* from_enc, const char* to_enc) {
62 char* outbuf = 0;
63
64 if(src && src_len && from_enc && to_enc) {
65 size_t outlenleft = src_len;
66 size_t inlenleft = src_len;
67 int outlen = src_len;
68 iconv_t ic;
69 char* out_ptr = 0;
70
71 if(strlen(to_enc) >= ICONV_CSNMAXLEN || strlen(from_enc) >= ICONV_CSNMAXLEN) {
72 return NULL;
73 }
74 ic = iconv_open(to_enc, from_enc);
75 if(ic != (iconv_t)-1) {
76 size_t st;
77 outbuf = (char*)malloc(outlen + 1);
78
79 if(outbuf) {
80 out_ptr = (char*)outbuf;
81 while(inlenleft) {
82 st = iconv(ic, (char**)&src, &inlenleft, &out_ptr, &outlenleft);
83 if(st == -1) {
84 if(errno == E2BIG) {
85 int diff = out_ptr - outbuf;
86 outlen += inlenleft;
87 outlenleft += inlenleft;
88 outbuf = (char*)realloc(outbuf, outlen + 1);
89 if(!outbuf) {
90 break;
91 }
92 out_ptr = outbuf + diff;
93 }
94 else {
95 free(outbuf);
96 outbuf = 0;
97 break;
98 }
99 }
100 }
101 }
102 iconv_close(ic);
103 }
104 outlen -= outlenleft;
105
106 if(new_len) {
107 *new_len = outbuf ? outlen : 0;
108 }
109 if(outbuf) {
110 outbuf[outlen] = 0;
111 }
112 }
113 return outbuf;
114 }
115
116
117 char* utf8_encode(const char *s, int len, int *newlen, const char* encoding)
118 {
119 return convert(s, len, newlen, encoding, "UTF-8");
120 }
121
122
123 char* utf8_decode(const char *s, int len, int *newlen, const char* encoding)
124 {
125 return convert(s, len, newlen, "UTF-8", encoding);
126 }
127