Debian Codegraph @

Unicode/ucharmap.cpp

raw
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29 #include <assert.h>
30 #include <errno.h>
31
32 #include "gwwiconv.h"
33 #include "ustring.h"
34 #include "utype.h"
35
36 static iconv_t to_unicode=(iconv_t) (-1), from_unicode=(iconv_t) (-1);
37 static iconv_t to_utf8=(iconv_t) (-1), from_utf8=(iconv_t) (-1);
38 static iconv_t from_utf8_safe=(iconv_t) (-1);
39 static bool is_local_encoding_utf8 = true;
40
41 bool SetupUCharMap(const char* unichar_name, const char* local_name, bool is_local_utf8) {
42 if (to_unicode != (iconv_t)-1)
43 iconv_close(to_unicode);
44 if (from_unicode != (iconv_t)-1)
45 iconv_close(from_unicode);
46 if (to_utf8 != (iconv_t)-1)
47 iconv_close(to_utf8);
48 if (from_utf8 != (iconv_t)-1)
49 iconv_close(from_utf8);
50 if (from_utf8_safe != (iconv_t)-1)
51 iconv_close(from_utf8_safe);
52
53 is_local_encoding_utf8 = is_local_utf8;
54 if (is_local_encoding_utf8) {
55 return true;
56 }
57
58 if ((to_unicode = iconv_open(unichar_name, local_name)) == (iconv_t)-1)
59 return false;
60 if ((from_unicode = iconv_open(local_name, unichar_name)) == (iconv_t)-1)
61 return false;
62 if ((to_utf8 = iconv_open("UTF-8", local_name)) == (iconv_t)-1)
63 return false;
64 if ((from_utf8 = iconv_open(local_name, "UTF-8")) == (iconv_t)-1)
65 return false;
66 if ((from_utf8_safe = iconv_open("ASCII//TRANSLIT", "UTF-8")) == (iconv_t)-1)
67 return false;
68
69 return true;
70 }
71
72 unichar_t *def2u_strncpy(unichar_t *uto, const char *from, size_t n) {
73 if (from == NULL || uto == NULL || n == 0) {
74 return uto;
75 } else if (is_local_encoding_utf8) {
76 return utf82u_strncpy(uto, from, n);
77 } else {
78 size_t in_left = sizeof(from[0]) * strlen(from), out_left = sizeof(uto[0])*(n-1);
79 char *cto = (char *) uto;
80 iconv(to_unicode, (iconv_arg2_t) &from, &in_left, &cto, &out_left);
81 uto[n - (out_left/sizeof(uto[0])) - 1] = '\0';
82 }
83 return uto;
84 }
85
86 char *u2def_strncpy(char *to, const unichar_t *ufrom, size_t n) {
87 if (ufrom == NULL || to == NULL || n == 0) {
88 return to;
89 } else if (is_local_encoding_utf8) {
90 return u2utf8_strncpy(to, ufrom, n);
91 } else {
92 size_t in_left = sizeof(ufrom[0]) * u_strlen(ufrom), out_left = sizeof(to[0])*(n-1);
93 char *cto = (char *) to;
94 iconv(from_unicode, (iconv_arg2_t) &ufrom, &in_left, &cto, &out_left);
95 to[n - (out_left/sizeof(to[0])) - 1] = '\0';
96 }
97 return to;
98 }
99
100 static void* do_iconv(iconv_t cd, const void* inbuf, size_t incount, size_t inunitsize, size_t outunitsize) {
101 size_t outbytes = (incount + 1) * outunitsize, outremain = outbytes;
102 incount *= inunitsize;
103 char *buf = (char*) malloc(outbytes), *dst = buf;
104
105 while (incount > 0 && buf) {
106 if (iconv(cd, (iconv_arg2_t)&inbuf, &incount, &dst, &outremain) == (size_t)-1) {
107 if (errno == E2BIG) {
108 buf = (char*) realloc(buf, outbytes*2);
109 if (buf) {
110 dst = buf + outbytes;
111 outremain += outbytes;
112 outbytes += outbytes;
113 }
114 } else {
115 free(buf);
116 return NULL;
117 }
118 }
119 }
120
121 if (buf) {
122 if (outremain < outunitsize) {
123 outbytes += outunitsize - outremain;
124 outremain = outunitsize;
125 buf = (char*) realloc(buf, outbytes);
126 }
127 if (buf) {
128 memset(buf + (outbytes - outremain), 0, outremain);
129 }
130 }
131
132 return buf;
133 }
134
135 unichar_t *def2u_copy(const char *from) {
136 if (from == NULL)
137 return NULL;
138 else if (is_local_encoding_utf8)
139 return utf82u_copy(from);
140 return (unichar_t*) do_iconv(to_unicode, from, strlen(from), sizeof(from[0]), sizeof(unichar_t));
141 }
142
143 char *u2def_copy(const unichar_t *ufrom) {
144 if (ufrom == NULL)
145 return NULL;
146 else if (is_local_encoding_utf8)
147 return u2utf8_copy(ufrom);
148 return (char*) do_iconv(from_unicode, ufrom, u_strlen(ufrom), sizeof(ufrom[0]), sizeof(char));
149 }
150
151 char *def2utf8_copy(const char *from) {
152 if (from == NULL)
153 return NULL;
154 else if (is_local_encoding_utf8)
155 return copy(from);
156 return (char*) do_iconv(to_utf8, from, strlen(from), sizeof(from[0]), sizeof(char));
157 }
158
159 char *utf82def_copy(const char *ufrom) {
160 if (ufrom == NULL)
161 return NULL;
162 else if (is_local_encoding_utf8)
163 return copy(ufrom);
164 return (char*) do_iconv(from_utf8, ufrom, strlen(ufrom), sizeof(ufrom[0]), sizeof(char));
165 }
166
167 char *utf82def_copy_safe(const char *ufrom) {
168 char* ret_val = NULL;
169 if (ufrom == NULL)
170 return copy("");
171
172 ret_val = utf82def_copy(ufrom);
173 if (ret_val == NULL) {
174 // This should never fail, unless iconv() is severely broken.
175 ret_val = (char*) do_iconv(from_utf8_safe, ufrom, strlen(ufrom), sizeof(ufrom[0]), sizeof(char));
176 }
177 if (ret_val == NULL) {
178 // We should never get here.
179 ret_val = copy("utf82def_copy failure!\n");
180 }
181 return ret_val;
182 }