16ac937c8SXin LI /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
46ac937c8SXin LI * Copyright (c) 2005 Ryuichiro Imura
56ac937c8SXin LI * All rights reserved.
66ac937c8SXin LI *
76ac937c8SXin LI * Redistribution and use in source and binary forms, with or without
86ac937c8SXin LI * modification, are permitted provided that the following conditions
96ac937c8SXin LI * are met:
106ac937c8SXin LI * 1. Redistributions of source code must retain the above copyright
116ac937c8SXin LI * notice, this list of conditions and the following disclaimer.
126ac937c8SXin LI * 2. Redistributions in binary form must reproduce the above copyright
136ac937c8SXin LI * notice, this list of conditions and the following disclaimer in the
146ac937c8SXin LI * documentation and/or other materials provided with the distribution.
156ac937c8SXin LI *
166ac937c8SXin LI * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
176ac937c8SXin LI * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
186ac937c8SXin LI * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
196ac937c8SXin LI * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
206ac937c8SXin LI * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
216ac937c8SXin LI * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
226ac937c8SXin LI * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
236ac937c8SXin LI * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
246ac937c8SXin LI * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
256ac937c8SXin LI * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
266ac937c8SXin LI * SUCH DAMAGE.
276ac937c8SXin LI */
286ac937c8SXin LI
296ac937c8SXin LI #include <sys/types.h>
306ac937c8SXin LI #include <sys/iconv.h>
316ac937c8SXin LI #include <sys/sysctl.h>
326ac937c8SXin LI
336ac937c8SXin LI #include <ctype.h>
346ac937c8SXin LI #include <errno.h>
356ac937c8SXin LI #include <stdlib.h>
366ac937c8SXin LI #include <string.h>
376ac937c8SXin LI
386ac937c8SXin LI int
kiconv_lookupconv(const char * drvname)396ac937c8SXin LI kiconv_lookupconv(const char *drvname)
406ac937c8SXin LI {
416ac937c8SXin LI size_t size;
426ac937c8SXin LI
436ac937c8SXin LI if (sysctlbyname("kern.iconv.drvlist", NULL, &size, NULL, 0) == -1)
446ac937c8SXin LI return (errno);
456ac937c8SXin LI if (size > 0) {
466ac937c8SXin LI char *drivers, *drvp;
476ac937c8SXin LI
486ac937c8SXin LI drivers = malloc(size);
496ac937c8SXin LI if (drivers == NULL)
506ac937c8SXin LI return (ENOMEM);
516ac937c8SXin LI if (sysctlbyname("kern.iconv.drvlist", drivers, &size, NULL, 0) == -1) {
526ac937c8SXin LI free(drivers);
536ac937c8SXin LI return (errno);
546ac937c8SXin LI }
556ac937c8SXin LI for (drvp = drivers; *drvp != '\0'; drvp += strlen(drvp) + 1)
566ac937c8SXin LI if (strcmp(drvp, drvname) == 0) {
576ac937c8SXin LI free(drivers);
586ac937c8SXin LI return (0);
596ac937c8SXin LI }
603cfd08c1SEd Maste free(drivers);
616ac937c8SXin LI }
626ac937c8SXin LI return (ENOENT);
636ac937c8SXin LI }
646ac937c8SXin LI
656ac937c8SXin LI int
kiconv_lookupcs(const char * tocode,const char * fromcode)666ac937c8SXin LI kiconv_lookupcs(const char *tocode, const char *fromcode)
676ac937c8SXin LI {
686ac937c8SXin LI size_t i, size;
696ac937c8SXin LI struct iconv_cspair_info *csi, *csip;
706ac937c8SXin LI
716ac937c8SXin LI if (sysctlbyname("kern.iconv.cslist", NULL, &size, NULL, 0) == -1)
726ac937c8SXin LI return (errno);
736ac937c8SXin LI if (size > 0) {
746ac937c8SXin LI csi = malloc(size);
756ac937c8SXin LI if (csi == NULL)
766ac937c8SXin LI return (ENOMEM);
776ac937c8SXin LI if (sysctlbyname("kern.iconv.cslist", csi, &size, NULL, 0) == -1) {
786ac937c8SXin LI free(csi);
79c9f1e38aSXin LI return (errno);
806ac937c8SXin LI }
816ac937c8SXin LI for (i = 0, csip = csi; i < (size/sizeof(*csi)); i++, csip++){
826ac937c8SXin LI if (strcmp(csip->cs_to, tocode) == 0 &&
836ac937c8SXin LI strcmp(csip->cs_from, fromcode) == 0) {
846ac937c8SXin LI free(csi);
856ac937c8SXin LI return (0);
866ac937c8SXin LI }
876ac937c8SXin LI }
883cfd08c1SEd Maste free(csi);
896ac937c8SXin LI }
906ac937c8SXin LI return (ENOENT);
916ac937c8SXin LI }
92