1 /* $NetBSD: iconv.c,v 1.11 2009/03/03 16:22:33 explorer Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2003 Citrus Project, 7 * Copyright (c) 2009, 2010 Gabor Kovesdan <gabor@FreeBSD.org>, 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #include <sys/queue.h> 34 #include <sys/types.h> 35 36 #include <assert.h> 37 #include <errno.h> 38 #include <iconv.h> 39 #include <limits.h> 40 #include <paths.h> 41 #include <stdbool.h> 42 #include <stdlib.h> 43 #include <string.h> 44 45 #include "citrus_types.h" 46 #include "citrus_module.h" 47 #include "citrus_esdb.h" 48 #include "citrus_hash.h" 49 #include "citrus_iconv.h" 50 51 #include "iconv-internal.h" 52 53 #define ISBADF(_h_) (!(_h_) || (_h_) == (iconv_t)-1) 54 55 static iconv_t 56 __bsd___iconv_open(const char *out, const char *in, struct _citrus_iconv *handle) 57 { 58 int ret; 59 60 /* 61 * Remove anything following a //, as these are options (like 62 * //ignore, //translate, etc) and we just don't handle them. 63 * This is for compatibility with software that uses these 64 * blindly. 65 */ 66 ret = _citrus_iconv_open(&handle, in, out); 67 if (ret) { 68 errno = ret == ENOENT ? EINVAL : ret; 69 return ((iconv_t)-1); 70 } 71 72 handle->cv_shared->ci_discard_ilseq = strcasestr(out, "//IGNORE"); 73 handle->cv_shared->ci_ilseq_invalid = false; 74 handle->cv_shared->ci_hooks = NULL; 75 76 return ((iconv_t)(void *)handle); 77 } 78 79 iconv_t 80 __bsd_iconv_open(const char *out, const char *in) 81 { 82 83 return (__bsd___iconv_open(out, in, NULL)); 84 } 85 86 int 87 __bsd_iconv_open_into(const char *out, const char *in, iconv_allocation_t *ptr) 88 { 89 struct _citrus_iconv *handle; 90 91 handle = (struct _citrus_iconv *)ptr; 92 return ((__bsd___iconv_open(out, in, handle) == (iconv_t)-1) ? -1 : 0); 93 } 94 95 int 96 __bsd_iconv_close(iconv_t handle) 97 { 98 99 if (ISBADF(handle)) { 100 errno = EBADF; 101 return (-1); 102 } 103 104 _citrus_iconv_close((struct _citrus_iconv *)(void *)handle); 105 106 return (0); 107 } 108 109 size_t 110 __bsd_iconv(iconv_t handle, char **in, size_t *szin, char **out, size_t *szout) 111 { 112 size_t ret; 113 int err; 114 115 if (ISBADF(handle)) { 116 errno = EBADF; 117 return ((size_t)-1); 118 } 119 120 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle, 121 in, szin, out, szout, 0, &ret); 122 if (err) { 123 errno = err; 124 ret = (size_t)-1; 125 } 126 127 return (ret); 128 } 129 130 size_t 131 __bsd___iconv(iconv_t handle, char **in, size_t *szin, char **out, 132 size_t *szout, uint32_t flags, size_t *invalids) 133 { 134 size_t ret; 135 int err; 136 137 if (ISBADF(handle)) { 138 errno = EBADF; 139 return ((size_t)-1); 140 } 141 142 err = _citrus_iconv_convert((struct _citrus_iconv *)(void *)handle, 143 in, szin, out, szout, flags, &ret); 144 if (invalids) 145 *invalids = ret; 146 if (err) { 147 errno = err; 148 ret = (size_t)-1; 149 } 150 151 return (ret); 152 } 153 154 int 155 __bsd___iconv_get_list(char ***rlist, size_t *rsz, bool sorted) 156 { 157 int ret; 158 159 ret = _citrus_esdb_get_list(rlist, rsz, sorted); 160 if (ret) { 161 errno = ret; 162 return (-1); 163 } 164 165 return (0); 166 } 167 168 void 169 __bsd___iconv_free_list(char **list, size_t sz) 170 { 171 172 _citrus_esdb_free_list(list, sz); 173 } 174 175 /* 176 * GNU-compatibile non-standard interfaces. 177 */ 178 static int 179 qsort_helper(const void *first, const void *second) 180 { 181 const char * const *s1; 182 const char * const *s2; 183 184 s1 = first; 185 s2 = second; 186 return (strcmp(*s1, *s2)); 187 } 188 189 void 190 __bsd_iconvlist(int (*do_one) (unsigned int, const char * const *, 191 void *), void *data) 192 { 193 char **list, **names; 194 const char * const *np; 195 char *curitem, *curkey, *slashpos; 196 size_t sz; 197 unsigned int i, j, n; 198 199 i = 0; 200 names = NULL; 201 202 if (__bsd___iconv_get_list(&list, &sz, true)) { 203 list = NULL; 204 goto out; 205 } 206 qsort((void *)list, sz, sizeof(char *), qsort_helper); 207 while (i < sz) { 208 j = 0; 209 slashpos = strchr(list[i], '/'); 210 names = malloc(sz * sizeof(char *)); 211 if (names == NULL) 212 goto out; 213 curkey = strndup(list[i], slashpos - list[i]); 214 if (curkey == NULL) 215 goto out; 216 names[j++] = curkey; 217 for (; (i < sz) && (memcmp(curkey, list[i], strlen(curkey)) == 0); i++) { 218 slashpos = strchr(list[i], '/'); 219 if (strcmp(curkey, &slashpos[1]) == 0) 220 continue; 221 curitem = strdup(&slashpos[1]); 222 if (curitem == NULL) 223 goto out; 224 names[j++] = curitem; 225 } 226 np = (const char * const *)names; 227 do_one(j, np, data); 228 for (n = 0; n < j; n++) 229 free(names[n]); 230 free(names); 231 names = NULL; 232 } 233 234 out: 235 if (names != NULL) { 236 for (n = 0; n < j; n++) 237 free(names[n]); 238 free(names); 239 } 240 if (list != NULL) 241 __bsd___iconv_free_list(list, sz); 242 } 243 244 __inline const char * 245 __bsd_iconv_canonicalize(const char *name) 246 { 247 248 return (_citrus_iconv_canonicalize(name)); 249 } 250 251 int 252 __bsd_iconvctl(iconv_t cd, int request, void *argument) 253 { 254 struct _citrus_iconv *cv; 255 struct iconv_hooks *hooks; 256 const char *convname; 257 char *dst; 258 int *i; 259 size_t srclen; 260 261 cv = (struct _citrus_iconv *)(void *)cd; 262 hooks = (struct iconv_hooks *)argument; 263 i = (int *)argument; 264 265 if (ISBADF(cd)) { 266 errno = EBADF; 267 return (-1); 268 } 269 270 switch (request) { 271 case ICONV_TRIVIALP: 272 convname = cv->cv_shared->ci_convname; 273 dst = strchr(convname, '/'); 274 srclen = dst - convname; 275 dst++; 276 *i = (srclen == strlen(dst)) && !memcmp(convname, dst, srclen); 277 return (0); 278 case ICONV_GET_TRANSLITERATE: 279 *i = 1; 280 return (0); 281 case ICONV_SET_TRANSLITERATE: 282 return ((*i == 1) ? 0 : -1); 283 case ICONV_GET_DISCARD_ILSEQ: 284 *i = cv->cv_shared->ci_discard_ilseq ? 1 : 0; 285 return (0); 286 case ICONV_SET_DISCARD_ILSEQ: 287 cv->cv_shared->ci_discard_ilseq = *i; 288 return (0); 289 case ICONV_SET_HOOKS: 290 cv->cv_shared->ci_hooks = hooks; 291 return (0); 292 case ICONV_SET_FALLBACKS: 293 errno = EOPNOTSUPP; 294 return (-1); 295 case ICONV_GET_ILSEQ_INVALID: 296 *i = cv->cv_shared->ci_ilseq_invalid ? 1 : 0; 297 return (0); 298 case ICONV_SET_ILSEQ_INVALID: 299 cv->cv_shared->ci_ilseq_invalid = *i; 300 return (0); 301 default: 302 errno = EINVAL; 303 return (-1); 304 } 305 } 306 307 void 308 __bsd_iconv_set_relocation_prefix(const char *orig_prefix __unused, 309 const char *curr_prefix __unused) 310 { 311 312 } 313