1 /*-
2 * Copyright (c) 1999,2000
3 * Konstantin Chuguev. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Konstantin Chuguev
16 * and its contributors.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * iconv (Charset Conversion Library) v2.0
31 */
32
33 #include <errno.h> /* errno */
34 #include <fcntl.h> /* O_RDONLY */
35 #include <limits.h> /* PATH_MAX */
36 #include <stdio.h> /* snprintf */
37 #include <stdlib.h> /* free, malloc */
38 #include <string.h> /* bzero, strdup, strsep */
39 #include <unistd.h> /* close, open */
40 #include <sys/mman.h> /* MAP_FAILED, PROT_READ, mmap, munmap */
41 #include <sys/stat.h> /* stat */
42
43 #define ICONV_INTERNAL
44 #include <iconv.h>
45
46 int
iconv_malloc(size_t size,void ** pp)47 iconv_malloc(size_t size, void **pp)
48 {
49 void *p = malloc(size);
50
51 if (p == NULL)
52 return errno;
53 bzero(p, size);
54 *pp = p;
55 return 0;
56 }
57
58 off_t
iconv_filesize(const char * pathlist,const char * filename,char * result)59 iconv_filesize(const char *pathlist, const char *filename, char *result)
60 {
61 off_t size = -1;
62 char *path, *plist = strdup(pathlist);
63
64 if (plist == NULL)
65 return -1;
66 while ((path = strsep(&plist, ":")) != NULL ) {
67 struct stat st;
68
69 snprintf(result, PATH_MAX, "%s/%s", path, filename);
70 if (stat(result, &st) < 0 || !S_ISREG(st.st_mode))
71 continue;
72 size = st.st_size;
73 break;
74 }
75 free(plist);
76 return size;
77 }
78
79 const void *
iconv_mmap(const char * filename,size_t size)80 iconv_mmap(const char *filename, size_t size)
81 {
82 const void *ptr;
83 int fd = open(filename, O_RDONLY);
84
85 if (fd < 0)
86 return NULL;
87 ptr = (const void *)mmap(NULL, size, PROT_READ, 0, fd, 0);
88 close(fd);
89 if (ptr == MAP_FAILED)
90 return NULL;
91 return ptr;
92 }
93
94 int
iconv_munmap(const void * addr,size_t size)95 iconv_munmap(const void *addr, size_t size)
96 {
97 return munmap((void *)addr, size);
98 }
99
100 #ifdef ICONV_DEBUG
101 #include <stdarg.h> /* va_end, va_list, va_start */
102
iconv_debug(const char * file,int line,const char * function,const char * format,...)103 void iconv_debug(const char *file, int line, const char *function,
104 const char *format, ...)
105 {
106 static char buffer[256];
107 va_list ap;
108 va_start(ap, format);
109 vsnprintf(buffer, sizeof(buffer), format, ap);
110 fprintf(stderr, "%-14s[%3d]->%-14s: %s\n", file, line, function, buffer);
111 fflush(stderr);
112 va_end(ap);
113 }
114 #endif
115