1 /*
2 * Copyright (c) 2003-2004, Artem B. Bityuckiy
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25 #define _DEFAULT_SOURCE
26 #include <sys/types.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <ctype.h>
31 #include <sys/iconvnls.h>
32 #include "local.h"
33
34 /*
35 * canonical_form - canonize 'str'.
36 *
37 * PARAMETERS:
38 * const char *str - string to canonize.
39 *
40 * DESCRIPTION:
41 * Converts all letters to small and substitute all '-' characters by '_'
42 * characters.
43 *
44 * RETURN:
45 * Returns canonical form of 'str' if success, NULL if failure.
46 */
47 static const char *
canonical_form(const char * str)48 canonical_form (
49 const char *str)
50 {
51 char *p, *p1;
52
53 if (str == NULL || (p = p1 = strdup (str)) == NULL)
54 return (const char *)NULL;
55
56 for (; *str; str++, p++)
57 {
58 if (*str == '-')
59 *p = '_';
60 else
61 *p = tolower (*str);
62 }
63
64 return (const char *)p1;
65 }
66
67 /*
68 * find_alias - find encoding name name by it's alias.
69 *
70 * PARAMETERS:
71 * const char *alias - alias by which "official" name should be found.
72 * const char *table - aliases table.
73 * int len - aliases table length.
74 *
75 * DESCRIPTION:
76 * 'table' contains the list of encoding names and aliases.
77 * Names go first, e.g.:
78 *
79 * name1 alias11 alias12 alias1N
80 * name2 alias21 alias22 alias2N
81 * nameM aliasM1 aliasM2 aliasMN
82 *
83 * If line begins with backspace it is considered as the continuation of
84 * previous line.
85 *
86 * RETURN:
87 * Returns pointer to name found if success. In case of error returns NULL
88 * and sets current thread's/process's errno.
89 */
90 static char *
find_alias(const char * alias,const char * table,int len)91 find_alias (
92 const char *alias,
93 const char *table,
94 int len)
95 {
96 const char *end;
97 const char *p;
98 int l;
99 const char *ptable = table;
100 const char *table_end = table + len;
101
102 if (table == NULL || alias == NULL || *table == '\0' || *alias == '\0')
103 return NULL;
104
105 l = strlen (alias);
106 search_again:
107 if (len < l || (p = strnstr (ptable, alias, len)) == NULL)
108 return NULL;
109
110 /* Check that substring is segregated by '\n', '\t' or ' ' */
111 if (!((p == table || isspace (*(p-1)) || *(p-1) == '\n')
112 && (p+l == table_end || isspace (*(p+l)) || *(p+l) == '\n')))
113 {
114 ptable = p + l;
115 len = table_end - ptable;
116 goto search_again;
117 }
118
119 while(--p > table && *p != '\n');
120
121 if (*(++p) == '#')
122 return NULL;
123
124 for (end = p + 1; !isspace (*end) && *end != '\n' && *end != '\0'; end++);
125
126 return strndup (p, (size_t)(end - p));
127 }
128
129 /*
130 * _iconv_resolve_encoding_name - resolves encoding's name by given alias.
131 *
132 * PARAMETERS:
133 * const char *ca - encoding alias to resolve.
134 *
135 * DESCRIPTION:
136 * First, tries to find 'ca' among built-in aliases. If not found, tries to
137 * find it external file.
138 *
139 * RETURN:
140 * Encoding name if found. In case of error returns NULL
141 * and sets current thread's/process's errno.
142 */
143 char *
_iconv_resolve_encoding_name(const char * ca)144 _iconv_resolve_encoding_name (
145 const char *ca)
146 {
147 char *p = (char *)ca;
148
149 /* Alias shouldn't contain white spaces, '\n' and '\r' symbols */
150 while (*p)
151 if (*p == ' ' || *p == '\r' || *p++ == '\n')
152 return NULL;
153
154 if ((ca = canonical_form (ca)) == NULL)
155 return NULL;
156
157 p = find_alias (ca, _iconv_aliases, strlen (_iconv_aliases));
158
159 free ((void *)ca);
160 return p;
161 }
162
163