| 1 |
|
| 2 |
. |
| 3 |
, |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include "common.h" |
| 23 |
#include "decsteps.h" |
| 24 |
#include <iconv.h> |
| 25 |
#include "iconvdecl.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
static void |
| 32 |
do_iconv (RECODE_OUTER outer, |
| 33 |
iconv_t conversion, |
| 34 |
char **input, size_t *input_left, |
| 35 |
char **output, size_t *output_left, |
| 36 |
int *saved_errno) |
| 37 |
{ |
| 38 |
size_t converted = iconv (conversion, input, input_left, output, output_left); |
| 39 |
if (converted == (size_t) -1 && !(errno == EILSEQ && outer->force)) |
| 40 |
*saved_errno = errno; |
| 41 |
} |
| 42 |
|
| 43 |
#define BUFFER_SIZE 2048 |
| 44 |
|
| 45 |
static bool |
| 46 |
wrapped_transform (iconv_t conversion, RECODE_SUBTASK subtask) |
| 47 |
{ |
| 48 |
char output_buffer[BUFFER_SIZE]; |
| 49 |
char input_buffer[BUFFER_SIZE]; |
| 50 |
int input_char = recode_get_byte (subtask); |
| 51 |
char *cursor = input_buffer; |
| 52 |
bool drain_first = false; |
| 53 |
|
| 54 |
while (true) |
| 55 |
{ |
| 56 |
|
| 57 |
|
| 58 |
char *input = input_buffer; |
| 59 |
char *output = output_buffer; |
| 60 |
size_t input_left = 0; |
| 61 |
size_t output_left = BUFFER_SIZE; |
| 62 |
int saved_errno = 0; |
| 63 |
|
| 64 |
if (drain_first) |
| 65 |
{ |
| 66 |
|
| 67 |
|
| 68 |
do_iconv (subtask->task->request->outer, |
| 69 |
conversion, |
| 70 |
NULL, NULL, |
| 71 |
&output, &output_left, |
| 72 |
&saved_errno); |
| 73 |
} |
| 74 |
|
| 75 |
if (saved_errno == 0) |
| 76 |
{ |
| 77 |
|
| 78 |
while (input_char != EOF && cursor < input_buffer + BUFFER_SIZE) |
| 79 |
{ |
| 80 |
*cursor++ = input_char; |
| 81 |
input_char = recode_get_byte (subtask); |
| 82 |
} |
| 83 |
|
| 84 |
if (cursor == input_buffer) |
| 85 |
{ |
| 86 |
if (output == output_buffer) |
| 87 |
{ |
| 88 |
|
| 89 |
if (drain_first) |
| 90 |
break; |
| 91 |
drain_first = true; |
| 92 |
continue; |
| 93 |
} |
| 94 |
} |
| 95 |
else |
| 96 |
{ |
| 97 |
|
| 98 |
input = input_buffer; |
| 99 |
input_left = cursor - input_buffer; |
| 100 |
do_iconv (subtask->task->request->outer, |
| 101 |
conversion, |
| 102 |
&input, &input_left, |
| 103 |
&output, &output_left, |
| 104 |
&saved_errno); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
for (cursor = output_buffer; cursor < output; cursor++) |
| 110 |
recode_put_byte (*cursor, subtask); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
drain_first = false; |
| 115 |
if (saved_errno != 0 && saved_errno != E2BIG) |
| 116 |
{ |
| 117 |
if (saved_errno == EILSEQ) |
| 118 |
{ |
| 119 |
|
| 120 |
enum recode_error recode_error = RECODE_INVALID_INPUT; |
| 121 |
RECODE_CONST_STEP step = subtask->step; |
| 122 |
iconv_t check_conversion = iconv_open (step->before->iconv_name, |
| 123 |
step->before->iconv_name); |
| 124 |
|
| 125 |
|
| 126 |
if (input_left > 0 && check_conversion != (iconv_t) -1) |
| 127 |
{ |
| 128 |
|
| 129 |
char *check_input = input; |
| 130 |
size_t check_input_left = input_left; |
| 131 |
size_t check_output_left = input_left; |
| 132 |
char *check_output_buffer, *check_output; |
| 133 |
RECODE_OUTER outer = subtask->task->request->outer; |
| 134 |
|
| 135 |
if ((check_output = ALLOC (check_output_buffer, input_left, char)) != NULL) |
| 136 |
{ |
| 137 |
size_t check_converted = iconv (check_conversion, |
| 138 |
&check_input, &check_input_left, |
| 139 |
&check_output, &check_output_left); |
| 140 |
|
| 141 |
if (check_converted != (size_t) -1) |
| 142 |
recode_error = RECODE_UNTRANSLATABLE; |
| 143 |
|
| 144 |
free (check_output_buffer); |
| 145 |
} |
| 146 |
|
| 147 |
iconv_close (check_conversion); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
RETURN_IF_NOGO (recode_error, subtask); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
assert (input_left > 0); |
| 157 |
input++; |
| 158 |
input_left--; |
| 159 |
} |
| 160 |
else if (saved_errno == EINVAL) |
| 161 |
{ |
| 162 |
if (input + input_left < input_buffer + BUFFER_SIZE |
| 163 |
&& input_char == EOF) |
| 164 |
|
| 165 |
RETURN_IF_NOGO (RECODE_INVALID_INPUT, subtask); |
| 166 |
} |
| 167 |
else |
| 168 |
{ |
| 169 |
recode_perror (subtask->task->request->outer, "iconv ()"); |
| 170 |
RETURN_IF_NOGO (RECODE_SYSTEM_ERROR, subtask); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
for (cursor = input_buffer; input_left != 0; input_left--) |
| 176 |
*cursor++ = *input++; |
| 177 |
} |
| 178 |
|
| 179 |
SUBTASK_RETURN (subtask); |
| 180 |
} |
| 181 |
|
| 182 |
static bool |
| 183 |
ends_with (const char *s, size_t s_len, const char *suff, size_t suff_len) |
| 184 |
{ |
| 185 |
return suff_len <= s_len && !memcmp (s + s_len - suff_len, suff, suff_len); |
| 186 |
} |
| 187 |
|
| 188 |
static char * |
| 189 |
iconv_fix_options (RECODE_OUTER outer, const char *charset) |
| 190 |
{ |
| 191 |
size_t charset_len = strlen (charset); |
| 192 |
bool translit = false; |
| 193 |
|
| 194 |
if (ends_with (charset, charset_len, "-translit", strlen ("-translit"))) |
| 195 |
{ |
| 196 |
translit = true; |
| 197 |
charset_len -= strlen ("-translit"); |
| 198 |
} |
| 199 |
|
| 200 |
char *result; |
| 201 |
if (asprintf (&result, "%.*s%s%s", (int) charset_len, charset, |
| 202 |
translit ? "//TRANSLIT" : "", |
| 203 |
outer->strict_mapping ? "//IGNORE": "") |
| 204 |
== -1) |
| 205 |
return NULL; |
| 206 |
return result; |
| 207 |
} |
| 208 |
|
| 209 |
bool |
| 210 |
recode_transform_with_iconv (RECODE_SUBTASK subtask) |
| 211 |
{ |
| 212 |
RECODE_OUTER outer = subtask->task->request->outer; |
| 213 |
RECODE_CONST_STEP step = subtask->step; |
| 214 |
char *tocode = iconv_fix_options (outer, step->after->iconv_name); |
| 215 |
const char *fromcode = step->before->iconv_name; |
| 216 |
iconv_t conversion = (iconv_t) -1; |
| 217 |
|
| 218 |
if (tocode) |
| 219 |
conversion = iconv_open (tocode, fromcode); |
| 220 |
if (conversion == (iconv_t) -1) |
| 221 |
{ |
| 222 |
recode_if_nogo (RECODE_SYSTEM_ERROR, subtask); |
| 223 |
free (tocode); |
| 224 |
SUBTASK_RETURN (subtask); |
| 225 |
} |
| 226 |
|
| 227 |
bool status = wrapped_transform (conversion, subtask); |
| 228 |
iconv_close (conversion); |
| 229 |
free (tocode); |
| 230 |
return status; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
bool |
| 238 |
module_iconv (RECODE_OUTER outer) |
| 239 |
{ |
| 240 |
const char **cursor; |
| 241 |
|
| 242 |
for (cursor = iconv_name_list; *cursor; cursor++) |
| 243 |
{ |
| 244 |
const char **aliases = cursor; |
| 245 |
const char *charset_name = *cursor; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
while (*cursor) |
| 251 |
{ |
| 252 |
RECODE_ALIAS alias |
| 253 |
= recode_find_alias (outer, *cursor, ALIAS_FIND_AS_CHARSET); |
| 254 |
|
| 255 |
if (alias) |
| 256 |
{ |
| 257 |
charset_name = alias->symbol->name; |
| 258 |
break; |
| 259 |
} |
| 260 |
cursor++; |
| 261 |
} |
| 262 |
|
| 263 |
if (!recode_declare_iconv (outer, charset_name, *aliases)) |
| 264 |
return false; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
for (cursor = aliases; *cursor; cursor++) |
| 271 |
{ |
| 272 |
RECODE_ALIAS alias |
| 273 |
= recode_find_alias (outer, *cursor, ALIAS_FIND_AS_CHARSET); |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
if (!alias || alias->symbol->name != charset_name) |
| 278 |
if (!recode_declare_alias (outer, *cursor, charset_name)) |
| 279 |
return false; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
void |
| 287 |
delmodule_iconv (_GL_UNUSED RECODE_OUTER outer) |
| 288 |
{ |
| 289 |
} |