#include "lint.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/param.h>
#include <alloca.h>
#include "iconv.h"
#include "iconvP.h"
#include "../i18n/_loc_path.h"
static iconv_p iconv_open_all(const char *, const char *, char *);
static iconv_p iconv_open_passthru(void);
static iconv_p iconv_open_private(const char *, const char *);
static iconv_p iconv_search_alias(const char *, const char *, char *);
static size_t passthru_icv_iconv(iconv_t, const char **, size_t *, char **,
size_t *);
static void passthru_icv_close(iconv_t);
#define PASSTHRU_MAGIC_NUMBER (0x53756e)
iconv_t
iconv_open(const char *tocode, const char *fromcode)
{
iconv_t cd;
char *ipath;
if ((cd = malloc(sizeof (struct _iconv_info))) == NULL)
return ((iconv_t)-1);
ipath = malloc(MAXPATHLEN);
if (ipath == NULL) {
free(cd);
return ((iconv_t)-1);
}
cd->_conv = iconv_open_all(tocode, fromcode, ipath);
if (cd->_conv != (iconv_p)-1) {
free(ipath);
return (cd);
}
cd->_conv = iconv_search_alias(tocode, fromcode, ipath);
free(ipath);
if (cd->_conv == (iconv_p)-1) {
free(cd);
return ((iconv_t)-1);
}
return (cd);
}
static size_t
search_alias(char **paddr, size_t size, const char *variant)
{
char *addr = *paddr;
char *p, *sp, *q;
size_t var_len, can_len;
var_len = strlen(variant);
p = addr;
q = addr + size;
while (q > p) {
if (*p == '#') {
p++;
while ((q > p) && (*p++ != '\n'))
;
continue;
}
while ((q > p) &&
((*p == ' ') || (*p == '\t')))
p++;
if (q <= p)
break;
sp = p;
while ((q > p) && (*p != ' ') &&
(*p != '\t') && (*p != '\n'))
p++;
if (q <= p) {
break;
}
if (*p == '\n') {
p++;
continue;
}
if (((p - sp) != var_len) ||
((strncmp(sp, variant, var_len) != 0) &&
(strncasecmp(sp, variant, var_len) != 0))) {
p++;
while ((q > p) && (*p++ != '\n'))
;
continue;
}
while ((q > p) &&
((*p == ' ') || (*p == '\t')))
p++;
if (q <= p)
break;
sp = p;
while ((q > p) && (*p != ' ') &&
(*p != '\t') && (*p != '\n'))
p++;
can_len = p - sp;
if (can_len == 0) {
while ((q > p) && (*p++ != '\n'))
;
continue;
}
*paddr = sp;
return (can_len);
}
return (0);
}
static iconv_p
iconv_open_all(const char *to, const char *from, char *ipath)
{
iconv_p cv;
int len;
len = snprintf(ipath, MAXPATHLEN, _GENICONVTBL_PATH, from, to);
if ((len <= MAXPATHLEN) && (access(ipath, R_OK) == 0)) {
cv = iconv_open_private(_GENICONVTBL_INT_PATH, ipath);
if (cv != (iconv_p)-1) {
return (cv);
}
}
len = snprintf(ipath, MAXPATHLEN, _ICONV_PATH, from, to);
if ((len <= MAXPATHLEN) && (access(ipath, R_OK) == 0)) {
return (iconv_open_private(ipath, NULL));
}
if (strcasecmp(to, from) == 0)
return (iconv_open_passthru());
errno = EINVAL;
return ((iconv_p)-1);
}
static iconv_p
iconv_search_alias(const char *tocode, const char *fromcode, char *ipath)
{
char *p;
char *to_canonical, *from_canonical;
size_t tolen, fromlen;
iconv_p cv;
int fd;
struct stat64 statbuf;
caddr_t addr;
size_t buflen;
fd = open(_ENCODING_ALIAS_PATH, O_RDONLY);
if (fd == -1) {
errno = EINVAL;
return ((iconv_p)-1);
}
if (fstat64(fd, &statbuf) == -1) {
(void) close(fd);
return ((iconv_p)-1);
}
buflen = (size_t)statbuf.st_size;
addr = mmap(NULL, buflen, PROT_READ, MAP_SHARED, fd, 0);
(void) close(fd);
if (addr == MAP_FAILED) {
return ((iconv_p)-1);
}
p = (char *)addr;
tolen = search_alias(&p, buflen, tocode);
if (tolen) {
to_canonical = alloca(tolen + 1);
(void) memcpy(to_canonical, p, tolen);
to_canonical[tolen] = '\0';
} else {
to_canonical = (char *)tocode;
}
p = (char *)addr;
fromlen = search_alias(&p, buflen, fromcode);
if (fromlen) {
from_canonical = alloca(fromlen + 1);
(void) memcpy(from_canonical, p, fromlen);
from_canonical[fromlen] = '\0';
} else {
from_canonical = (char *)fromcode;
}
(void) munmap(addr, buflen);
if (tolen == 0 && fromlen == 0) {
errno = EINVAL;
return ((iconv_p)-1);
}
cv = iconv_open_all(to_canonical, from_canonical, ipath);
return (cv);
}
static iconv_p
iconv_open_passthru(void)
{
iconv_p cdpath;
cdpath = malloc(sizeof (struct _iconv_fields));
if (cdpath == NULL)
return ((iconv_p)-1);
cdpath->_icv_handle = NULL;
cdpath->_icv_iconv = passthru_icv_iconv;
cdpath->_icv_close = passthru_icv_close;
cdpath->_icv_state = (void *)PASSTHRU_MAGIC_NUMBER;
return (cdpath);
}
static iconv_p
iconv_open_private(const char *lib, const char *tbl)
{
iconv_t (*fptr)(const char *);
iconv_p cdpath;
if ((cdpath = malloc(sizeof (struct _iconv_fields))) == NULL)
return ((iconv_p)-1);
if ((cdpath->_icv_handle = dlopen(lib, RTLD_LAZY)) == 0) {
free(cdpath);
errno = EINVAL;
return ((iconv_p)-1);
}
if ((fptr = (iconv_t(*)(const char *))dlsym(cdpath->_icv_handle,
"_icv_open")) == NULL) {
(void) dlclose(cdpath->_icv_handle);
free(cdpath);
errno = EINVAL;
return ((iconv_p)-1);
}
if ((cdpath->_icv_iconv = (size_t(*)(iconv_t, const char **,
size_t *, char **, size_t *))dlsym(cdpath->_icv_handle,
"_icv_iconv")) == NULL) {
(void) dlclose(cdpath->_icv_handle);
free(cdpath);
errno = EINVAL;
return ((iconv_p)-1);
}
if ((cdpath->_icv_close = (void(*)(iconv_t))dlsym(cdpath->_icv_handle,
"_icv_close")) == NULL) {
(void) dlclose(cdpath->_icv_handle);
free(cdpath);
errno = EINVAL;
return ((iconv_p)-1);
}
cdpath->_icv_state = (void *)(*fptr)(tbl);
if (cdpath->_icv_state == (struct _icv_state *)-1) {
(void) dlclose(cdpath->_icv_handle);
free(cdpath);
errno = EINVAL;
return ((iconv_p)-1);
}
return (cdpath);
}
int
iconv_close(iconv_t cd)
{
if (cd == NULL) {
errno = EBADF;
return (-1);
}
(*(cd->_conv)->_icv_close)(cd->_conv->_icv_state);
if (cd->_conv->_icv_handle != NULL)
(void) dlclose(cd->_conv->_icv_handle);
free(cd->_conv);
free(cd);
return (0);
}
static void
passthru_icv_close(iconv_t cd __unused)
{
}
size_t
iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft)
{
if (cd == NULL || cd == (iconv_t)-1) {
errno = EBADF;
return ((size_t)-1);
}
return ((*(cd->_conv)->_icv_iconv)(cd->_conv->_icv_state,
inbuf, inbytesleft, outbuf, outbytesleft));
}
static size_t
passthru_icv_iconv(iconv_t cd, const char **inbuf, size_t *inbufleft,
char **outbuf, size_t *outbufleft)
{
size_t ibl;
size_t obl;
size_t len;
size_t ret_val;
if (cd != (iconv_t)PASSTHRU_MAGIC_NUMBER) {
errno = EBADF;
return ((size_t)-1);
}
if (inbuf == NULL || *inbuf == NULL)
return (0);
ibl = *inbufleft;
obl = *outbufleft;
if (ibl > obl) {
len = obl;
errno = E2BIG;
ret_val = (size_t)-1;
} else {
len = ibl;
ret_val = 0;
}
(void) memmove((void *)*outbuf, (const void *)*inbuf, len);
*inbuf = *inbuf + len;
*outbuf = *outbuf + len;
*inbufleft = ibl - len;
*outbufleft = obl - len;
return (ret_val);
}