| 1 |
|
| 2 |
. |
| 3 |
. |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "config.h" |
| 21 |
#include "common.h" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
static const char * |
| 30 |
quality_to_string (struct recode_quality quality) |
| 31 |
{ |
| 32 |
static char buffer[100]; |
| 33 |
|
| 34 |
if (quality.reversible) |
| 35 |
return _("reversible"); |
| 36 |
|
| 37 |
sprintf (buffer, _("%s to %s"), |
| 38 |
(quality.in_size == RECODE_1 ? _("byte") |
| 39 |
: quality.in_size == RECODE_2 ? _("ucs2") : _("variable")), |
| 40 |
(quality.out_size == RECODE_1 ? _("byte") |
| 41 |
: quality.out_size == RECODE_2 ? _("ucs2") : _("variable"))); |
| 42 |
return buffer; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static void |
| 51 |
merge_qualities (struct recode_quality *first, |
| 52 |
const struct recode_quality second) |
| 53 |
{ |
| 54 |
first->out_size = second.out_size; |
| 55 |
first->reversible = first->reversible && second.reversible; |
| 56 |
first->slower = first->slower || second.slower; |
| 57 |
first->faster = first->faster && second.faster; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static void |
| 67 |
add_work_character (RECODE_REQUEST request, int character) |
| 68 |
{ |
| 69 |
if (request->work_string_length + 1 >= request->work_string_allocated) |
| 70 |
{ |
| 71 |
char *new_work_string; |
| 72 |
|
| 73 |
request->work_string_allocated += 100; |
| 74 |
new_work_string = (char *) |
| 75 |
realloc (request->work_string, request->work_string_allocated); |
| 76 |
if (new_work_string) |
| 77 |
request->work_string = new_work_string; |
| 78 |
else |
| 79 |
return; |
| 80 |
} |
| 81 |
request->work_string[request->work_string_length++] = character; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
static void |
| 89 |
add_work_string (RECODE_REQUEST request, const char *string) |
| 90 |
{ |
| 91 |
while (*string) |
| 92 |
add_work_character (request, *string++); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
char * |
| 102 |
recode_edit_sequence (RECODE_REQUEST request, bool edit_quality) |
| 103 |
{ |
| 104 |
RECODE_OUTER outer = request->outer; |
| 105 |
|
| 106 |
request->work_string_length = 0; |
| 107 |
|
| 108 |
if (request->sequence_length < 0) |
| 109 |
add_work_string (request, _("*Unachievable*")); |
| 110 |
else if (request->sequence_length == 0) |
| 111 |
add_work_string (request, _("*mere copy*")); |
| 112 |
else |
| 113 |
{ |
| 114 |
RECODE_SYMBOL last_charset_printed = NULL; |
| 115 |
RECODE_STEP step = request->sequence_array; |
| 116 |
|
| 117 |
while (step < request->sequence_array + request->sequence_length) |
| 118 |
{ |
| 119 |
RECODE_STEP unsurfacer_start = step; |
| 120 |
RECODE_STEP unsurfacer_end; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
while (step < request->sequence_array + request->sequence_length |
| 125 |
&& step->after == outer->data_symbol) |
| 126 |
step++; |
| 127 |
unsurfacer_end = step; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
if (step != unsurfacer_start |
| 132 |
|| step == request->sequence_array + request->sequence_length |
| 133 |
|| step->before != last_charset_printed) |
| 134 |
{ |
| 135 |
if (unsurfacer_start != request->sequence_array) |
| 136 |
add_work_character (request, ','); |
| 137 |
if (step < request->sequence_array + request->sequence_length) |
| 138 |
{ |
| 139 |
last_charset_printed = step->before; |
| 140 |
add_work_string (request, last_charset_printed->name); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
for (step = unsurfacer_end - 1; step >= unsurfacer_start; step--) |
| 147 |
{ |
| 148 |
add_work_character (request, '/'); |
| 149 |
add_work_string (request, step->before->name); |
| 150 |
} |
| 151 |
step = unsurfacer_end; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
add_work_string (request, ".."); |
| 156 |
if (step < request->sequence_array + request->sequence_length |
| 157 |
&& step->before != outer->data_symbol) |
| 158 |
{ |
| 159 |
last_charset_printed = step->after; |
| 160 |
add_work_string (request, last_charset_printed->name); |
| 161 |
step++; |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
last_charset_printed = outer->data_symbol; |
| 166 |
add_work_string (request, last_charset_printed->name); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
while (step < request->sequence_array + request->sequence_length |
| 172 |
&& step->before == outer->data_symbol) |
| 173 |
{ |
| 174 |
add_work_character (request, '/'); |
| 175 |
last_charset_printed = NULL; |
| 176 |
add_work_string (request, step->after->name); |
| 177 |
step++; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if (edit_quality) |
| 182 |
{ |
| 183 |
struct recode_quality quality = outer->quality_byte_reversible; |
| 184 |
RECODE_CONST_STEP step2; |
| 185 |
|
| 186 |
for (step2 = request->sequence_array; |
| 187 |
step2 < request->sequence_array + request->sequence_length; |
| 188 |
step2++) |
| 189 |
merge_qualities (&quality, step2->quality); |
| 190 |
add_work_character (request, ' '); |
| 191 |
add_work_character (request, '('); |
| 192 |
add_work_string (request, quality_to_string (quality)); |
| 193 |
add_work_character (request, ')'); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
add_work_character (request, NUL); |
| 198 |
return request->work_string; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
static bool |
| 209 |
add_to_sequence (RECODE_REQUEST request, RECODE_SINGLE single, |
| 210 |
RECODE_CONST_OPTION_LIST before_options, |
| 211 |
RECODE_CONST_OPTION_LIST after_options) |
| 212 |
{ |
| 213 |
RECODE_OUTER outer = request->outer; |
| 214 |
RECODE_STEP step; |
| 215 |
|
| 216 |
if ((unsigned)request->sequence_length == request->sequence_allocated) |
| 217 |
{ |
| 218 |
unsigned old_allocated = request->sequence_allocated; |
| 219 |
|
| 220 |
if (request->sequence_allocated == 0) |
| 221 |
request->sequence_allocated = 16; |
| 222 |
else |
| 223 |
request->sequence_allocated *= 2; |
| 224 |
|
| 225 |
if (!REALLOC (request->sequence_array, request->sequence_allocated, |
| 226 |
struct recode_step)) |
| 227 |
{ |
| 228 |
recode_error (outer, _("Virtual memory exhausted!")); |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
memset (request->sequence_array + old_allocated, 0, |
| 233 |
(request->sequence_allocated - old_allocated) * sizeof (struct recode_step)); |
| 234 |
} |
| 235 |
|
| 236 |
step = request->sequence_array + request->sequence_length++; |
| 237 |
step->before = single->before; |
| 238 |
step->after = single->after; |
| 239 |
step->step_table = single->initial_step_table; |
| 240 |
step->step_table_term_routine = NULL; |
| 241 |
step->step_type |
| 242 |
= step->step_table ? RECODE_COMBINE_EXPLODE : RECODE_NO_STEP_TABLE; |
| 243 |
step->transform_routine = single->transform_routine; |
| 244 |
step->fallback_routine = single->fallback_routine; |
| 245 |
step->term_routine = NULL; |
| 246 |
|
| 247 |
if (single->init_routine) |
| 248 |
{ |
| 249 |
if (!(*single->init_routine) (step, request, |
| 250 |
before_options, after_options)) |
| 251 |
{ |
| 252 |
recode_error (outer, _("Step initialisation failed")); |
| 253 |
return false; |
| 254 |
} |
| 255 |
} |
| 256 |
else if (before_options || after_options) |
| 257 |
{ |
| 258 |
recode_error (outer, |
| 259 |
_("Step initialisation failed (unprocessed options)")); |
| 260 |
return false; |
| 261 |
} |
| 262 |
|
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
#define UNREACHABLE 30000 |
| 274 |
|
| 275 |
static bool |
| 276 |
find_sequence (RECODE_REQUEST request, |
| 277 |
RECODE_CONST_SYMBOL before, |
| 278 |
RECODE_CONST_OPTION_LIST before_options, |
| 279 |
RECODE_CONST_SYMBOL after, |
| 280 |
RECODE_CONST_OPTION_LIST after_options) |
| 281 |
{ |
| 282 |
RECODE_OUTER outer = request->outer; |
| 283 |
struct search |
| 284 |
{ |
| 285 |
RECODE_SINGLE single; |
| 286 |
int cost; |
| 287 |
}; |
| 288 |
struct search *search_array; |
| 289 |
struct search *search; |
| 290 |
RECODE_SINGLE single; |
| 291 |
int cost; |
| 292 |
bool modified; |
| 293 |
RECODE_CONST_SYMBOL charset; |
| 294 |
|
| 295 |
if (!ALLOC (search_array, outer->number_of_symbols, struct search)) |
| 296 |
return false; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
for (search = search_array; |
| 302 |
search < search_array + outer->number_of_symbols; |
| 303 |
search++) |
| 304 |
{ |
| 305 |
search->single = NULL; |
| 306 |
search->cost = UNREACHABLE; |
| 307 |
} |
| 308 |
search_array[after->ordinal].cost = 0; |
| 309 |
|
| 310 |
modified = true; |
| 311 |
while (modified) |
| 312 |
{ |
| 313 |
modified = false; |
| 314 |
for (single = outer->single_list; single; single = single->next) |
| 315 |
if (!single->before->ignore) |
| 316 |
{ |
| 317 |
cost = search_array[single->after->ordinal].cost; |
| 318 |
if (cost != UNREACHABLE) |
| 319 |
{ |
| 320 |
cost += single->conversion_cost; |
| 321 |
search = search_array + single->before->ordinal; |
| 322 |
if (cost < search->cost) |
| 323 |
{ |
| 324 |
search->single = single; |
| 325 |
search->cost = cost; |
| 326 |
modified = true; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
if (search_array[before->ordinal].cost == UNREACHABLE) |
| 333 |
{ |
| 334 |
|
| 335 |
|
| 336 |
free (search_array); |
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
for (charset = before; charset != after; charset = single->after) |
| 343 |
{ |
| 344 |
single = search_array[charset->ordinal].single; |
| 345 |
if (!add_to_sequence (request, single, |
| 346 |
charset == before ? before_options : NULL, |
| 347 |
single->after == after ? after_options : NULL)) |
| 348 |
break; |
| 349 |
} |
| 350 |
|
| 351 |
free (search_array); |
| 352 |
return charset == after; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
static enum recode_step_type |
| 362 |
table_type (RECODE_CONST_REQUEST request, |
| 363 |
RECODE_CONST_STEP step) |
| 364 |
{ |
| 365 |
|
| 366 |
|
| 367 |
if (!request->make_header_flag) |
| 368 |
|
| 369 |
switch (step->step_type) |
| 370 |
{ |
| 371 |
case RECODE_BYTE_TO_BYTE: |
| 372 |
if (step->transform_routine != recode_transform_byte_to_byte) |
| 373 |
return RECODE_NO_STEP_TABLE; |
| 374 |
break; |
| 375 |
|
| 376 |
case RECODE_BYTE_TO_STRING: |
| 377 |
if (step->transform_routine != recode_transform_byte_to_variable) |
| 378 |
return RECODE_NO_STEP_TABLE; |
| 379 |
break; |
| 380 |
|
| 381 |
default: |
| 382 |
return RECODE_NO_STEP_TABLE; |
| 383 |
} |
| 384 |
|
| 385 |
return step->step_type; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
struct item |
| 393 |
{ |
| 394 |
unsigned short code; |
| 395 |
unsigned char byte; |
| 396 |
}; |
| 397 |
|
| 398 |
static int |
| 399 |
compare_struct_item (const void *void_first, const void *void_second) |
| 400 |
{ |
| 401 |
return (((const struct item *) void_first)->code |
| 402 |
- ((const struct item *) void_second)->code); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
static bool |
| 413 |
complete_double_ucs2_step (RECODE_OUTER outer, RECODE_STEP step) |
| 414 |
{ |
| 415 |
struct side |
| 416 |
{ |
| 417 |
RECODE_SYMBOL charset; |
| 418 |
struct item item[256]; |
| 419 |
size_t number_of_items; |
| 420 |
}; |
| 421 |
|
| 422 |
const struct strip_data *data; |
| 423 |
struct side side_array[2]; |
| 424 |
struct side *side; |
| 425 |
bool reversed; |
| 426 |
const recode_ucs2 *pool; |
| 427 |
unsigned offset; |
| 428 |
unsigned byte; |
| 429 |
unsigned row_counter; |
| 430 |
unsigned position_counter; |
| 431 |
struct item *item_cursor; |
| 432 |
struct item *left; |
| 433 |
struct item *left_limit; |
| 434 |
struct item *right; |
| 435 |
struct item *right_limit; |
| 436 |
struct recode_known_pair pair_array[256]; |
| 437 |
struct recode_known_pair *pair_cursor; |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
if (step->before < step->after) |
| 446 |
{ |
| 447 |
side_array[0].charset = step->before; |
| 448 |
side_array[1].charset = step->after; |
| 449 |
reversed = false; |
| 450 |
} |
| 451 |
else |
| 452 |
{ |
| 453 |
side_array[0].charset = step->after; |
| 454 |
side_array[1].charset = step->before; |
| 455 |
reversed = true; |
| 456 |
} |
| 457 |
|
| 458 |
for (side = side_array; side < side_array + 2; side++) |
| 459 |
{ |
| 460 |
|
| 461 |
|
| 462 |
data = (const struct strip_data *) side->charset->data; |
| 463 |
pool = data->pool; |
| 464 |
item_cursor = side->item; |
| 465 |
byte = 0; |
| 466 |
|
| 467 |
for (row_counter = 0; |
| 468 |
row_counter < (256 / STRIP_SIZE); |
| 469 |
row_counter++) |
| 470 |
if (offset = data->offset[row_counter], offset) |
| 471 |
for (position_counter = 0; |
| 472 |
position_counter < STRIP_SIZE; |
| 473 |
position_counter++) |
| 474 |
{ |
| 475 |
unsigned code = pool[offset + position_counter]; |
| 476 |
|
| 477 |
if (code != BIT_MASK (16)) |
| 478 |
{ |
| 479 |
|
| 480 |
item_cursor->byte = byte; |
| 481 |
item_cursor->code = code; |
| 482 |
item_cursor++; |
| 483 |
} |
| 484 |
byte++; |
| 485 |
} |
| 486 |
else |
| 487 |
byte += STRIP_SIZE; |
| 488 |
|
| 489 |
side->number_of_items = item_cursor - side->item; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
qsort (side_array[0].item, side_array[0].number_of_items, |
| 497 |
sizeof (struct item), compare_struct_item); |
| 498 |
qsort (side_array[1].item, side_array[1].number_of_items, |
| 499 |
sizeof (struct item), compare_struct_item); |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
left = side_array[0].item; |
| 505 |
left_limit = left + side_array[0].number_of_items; |
| 506 |
right = side_array[1].item; |
| 507 |
right_limit = right + side_array[1].number_of_items; |
| 508 |
pair_cursor = pair_array; |
| 509 |
|
| 510 |
while (left < left_limit && right < right_limit) |
| 511 |
{ |
| 512 |
int value = left->code - right->code; |
| 513 |
|
| 514 |
if (value < 0) |
| 515 |
left++; |
| 516 |
else if (value > 0) |
| 517 |
right++; |
| 518 |
else |
| 519 |
{ |
| 520 |
pair_cursor->left = (left++)->byte; |
| 521 |
pair_cursor->right = (right++)->byte; |
| 522 |
pair_cursor++; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
return |
| 529 |
recode_complete_pairs (outer, step, |
| 530 |
pair_array, pair_cursor - pair_array, false, reversed); |
| 531 |
} |
| 532 |
|
| 533 |
static bool |
| 534 |
delete_compressed_one_to_many (RECODE_STEP step) |
| 535 |
{ |
| 536 |
free (step->local); |
| 537 |
return true; |
| 538 |
} |
| 539 |
|
| 540 |
static void |
| 541 |
delete_step (RECODE_STEP step) |
| 542 |
{ |
| 543 |
|
| 544 |
if (step->term_routine) |
| 545 |
(*step->term_routine) (step); |
| 546 |
|
| 547 |
if (step->step_table_term_routine != NULL) |
| 548 |
(*step->step_table_term_routine) (step->step_table); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
static bool |
| 557 |
simplify_sequence (RECODE_REQUEST request) |
| 558 |
{ |
| 559 |
RECODE_OUTER outer = request->outer; |
| 560 |
|
| 561 |
unsigned saved_steps; |
| 562 |
RECODE_STEP in; |
| 563 |
RECODE_STEP out; |
| 564 |
RECODE_STEP limit; |
| 565 |
unsigned char *accum; |
| 566 |
const char **string; |
| 567 |
unsigned char temp[256]; |
| 568 |
unsigned counter; |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
if (request->verbose_flag) |
| 573 |
fprintf (stderr, _("Request: %s\n"), recode_edit_sequence (request, 0)); |
| 574 |
|
| 575 |
saved_steps = 0; |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
in = request->sequence_array; |
| 580 |
out = request->sequence_array; |
| 581 |
limit = in + request->sequence_length; |
| 582 |
|
| 583 |
while (in < limit) |
| 584 |
if (in < limit - 1 |
| 585 |
&& in[0].before->data_type == RECODE_STRIP_DATA |
| 586 |
&& in[0].after == outer->ucs2_charset |
| 587 |
&& in[1].before == outer->ucs2_charset |
| 588 |
&& in[1].after->data_type == RECODE_STRIP_DATA) |
| 589 |
{ |
| 590 |
|
| 591 |
delete_step (in); |
| 592 |
delete_step (in + 1); |
| 593 |
|
| 594 |
|
| 595 |
out->before = in[0].before; |
| 596 |
out->after = in[1].after; |
| 597 |
out->quality = in[0].quality; |
| 598 |
merge_qualities (&out->quality, in[1].quality); |
| 599 |
out->transform_routine = recode_transform_byte_to_byte; |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
if (!complete_double_ucs2_step (outer, out)) |
| 604 |
return false; |
| 605 |
|
| 606 |
in += 2; |
| 607 |
saved_steps++; |
| 608 |
out++; |
| 609 |
} |
| 610 |
else if (in < limit - 1 |
| 611 |
&& in[0].after == outer->iconv_pivot |
| 612 |
&& in[1].before == outer->iconv_pivot) |
| 613 |
{ |
| 614 |
|
| 615 |
delete_step (in); |
| 616 |
delete_step (in + 1); |
| 617 |
|
| 618 |
|
| 619 |
out->before = in[0].before; |
| 620 |
out->after = in[1].after; |
| 621 |
out->quality = in[0].quality; |
| 622 |
merge_qualities (&out->quality, in[1].quality); |
| 623 |
out->transform_routine = recode_transform_with_iconv; |
| 624 |
|
| 625 |
in += 2; |
| 626 |
saved_steps++; |
| 627 |
out++; |
| 628 |
} |
| 629 |
else if (out != in) |
| 630 |
*out++ = *in++; |
| 631 |
else |
| 632 |
out++, in++; |
| 633 |
|
| 634 |
request->sequence_length = out - request->sequence_array; |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
in = request->sequence_array; |
| 641 |
out = request->sequence_array; |
| 642 |
limit = in + request->sequence_length; |
| 643 |
|
| 644 |
while (in < limit) |
| 645 |
if (in < limit - 1 |
| 646 |
&& table_type (request, in) == RECODE_BYTE_TO_BYTE |
| 647 |
&& table_type (request, in + 1) != RECODE_NO_STEP_TABLE |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
&& ALLOC (accum, 256, unsigned char)) |
| 653 |
{ |
| 654 |
memcpy (accum, in->step_table, 256); |
| 655 |
out->before = in->before; |
| 656 |
out->after = in->after; |
| 657 |
out->quality = in->quality; |
| 658 |
delete_step (in++); |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
while (in < limit |
| 663 |
&& (table_type (request, in) == RECODE_BYTE_TO_BYTE)) |
| 664 |
{ |
| 665 |
const unsigned char *table = (const unsigned char *) in->step_table; |
| 666 |
|
| 667 |
for (counter = 0; counter < 256; counter++) |
| 668 |
temp[counter] = table[accum[counter]]; |
| 669 |
memcpy (accum, temp, 256); |
| 670 |
|
| 671 |
out->after = in->after; |
| 672 |
merge_qualities (&out->quality, in->quality); |
| 673 |
delete_step (in++); |
| 674 |
saved_steps++; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
if (in < limit && (table_type (request, in) == RECODE_BYTE_TO_STRING) |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
&& (ALLOC (string, 256, const char *))) |
| 685 |
{ |
| 686 |
const char *const *table = (const char *const *) in->step_table; |
| 687 |
|
| 688 |
for (counter = 0; counter < 256; counter++) |
| 689 |
string[counter] = table[accum[counter]]; |
| 690 |
free (accum); |
| 691 |
out->step_type = RECODE_BYTE_TO_STRING; |
| 692 |
out->step_table = string; |
| 693 |
if (in->step_table_term_routine) |
| 694 |
{ |
| 695 |
out->local = (void *) table; |
| 696 |
out->term_routine = delete_compressed_one_to_many; |
| 697 |
} |
| 698 |
out->step_table_term_routine = free; |
| 699 |
out->transform_routine = recode_transform_byte_to_variable; |
| 700 |
out->after = in->after; |
| 701 |
merge_qualities (&out->quality, in->quality); |
| 702 |
in++; |
| 703 |
saved_steps++; |
| 704 |
} |
| 705 |
else |
| 706 |
{ |
| 707 |
|
| 708 |
|
| 709 |
out->step_type = RECODE_BYTE_TO_BYTE; |
| 710 |
out->step_table = accum; |
| 711 |
out->transform_routine = recode_transform_byte_to_byte; |
| 712 |
} |
| 713 |
|
| 714 |
out++; |
| 715 |
} |
| 716 |
else if (out != in) |
| 717 |
*out++ = *in++; |
| 718 |
else |
| 719 |
out++, in++; |
| 720 |
|
| 721 |
request->sequence_length = out - request->sequence_array; |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
in = request->sequence_array; |
| 727 |
|
| 728 |
if (request->sequence_length == 1 |
| 729 |
&& table_type (request, in) == RECODE_BYTE_TO_BYTE |
| 730 |
&& memcmp (in->step_table, outer->one_to_same, 256) == 0) |
| 731 |
{ |
| 732 |
request->sequence_length = 0; |
| 733 |
saved_steps++; |
| 734 |
delete_step (in); |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
if (saved_steps > 0 && request->verbose_flag) |
| 740 |
fprintf (stderr, _("Shrunk to: %s\n"), recode_edit_sequence (request, 0)); |
| 741 |
return true; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
static bool |
| 751 |
scan_identifier (RECODE_REQUEST request) |
| 752 |
{ |
| 753 |
char *cursor = request->scanned_string; |
| 754 |
|
| 755 |
while (*request->scan_cursor && *request->scan_cursor != ',' |
| 756 |
&& (request->scan_cursor[0] != '.' || request->scan_cursor[1] != '.') |
| 757 |
&& *request->scan_cursor != '/' && *request->scan_cursor != '+') |
| 758 |
*cursor++ = *request->scan_cursor++; |
| 759 |
*cursor = NUL; |
| 760 |
|
| 761 |
return true; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
static RECODE_OPTION_LIST |
| 770 |
scan_options (RECODE_REQUEST request) |
| 771 |
{ |
| 772 |
RECODE_OUTER outer = request->outer; |
| 773 |
RECODE_OPTION_LIST list = NULL; |
| 774 |
RECODE_OPTION_LIST last = NULL; |
| 775 |
|
| 776 |
while (*request->scan_cursor == '+') |
| 777 |
{ |
| 778 |
RECODE_OPTION_LIST new_ |
| 779 |
= ALLOC (new_, 1, struct recode_option_list); |
| 780 |
char *copy; |
| 781 |
|
| 782 |
if (!new_) |
| 783 |
break; |
| 784 |
|
| 785 |
request->scan_cursor++; |
| 786 |
scan_identifier (request); |
| 787 |
ALLOC (copy, strlen (request->scanned_string) + 1, char); |
| 788 |
if (!copy) |
| 789 |
{ |
| 790 |
free (new_); |
| 791 |
break; |
| 792 |
} |
| 793 |
strcpy (copy, request->scanned_string); |
| 794 |
|
| 795 |
new_->option = copy; |
| 796 |
if (!list) |
| 797 |
list = new_; |
| 798 |
new_->next = last; |
| 799 |
last = new_; |
| 800 |
} |
| 801 |
return list; |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
static bool |
| 810 |
scan_check_if_last_charset (RECODE_REQUEST request) |
| 811 |
{ |
| 812 |
const char *cursor = request->scan_cursor; |
| 813 |
|
| 814 |
while (*cursor && *cursor != ',' && !(cursor[0] == '.' && cursor[1] != '.')) |
| 815 |
cursor++; |
| 816 |
|
| 817 |
if (*cursor && *cursor != ',') |
| 818 |
{ |
| 819 |
request->scan_cursor = cursor; |
| 820 |
return false; |
| 821 |
} |
| 822 |
|
| 823 |
return true; |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
static bool |
| 832 |
scan_unsurfacers (RECODE_REQUEST request) |
| 833 |
{ |
| 834 |
RECODE_OUTER outer = request->outer; |
| 835 |
RECODE_SYMBOL surface = NULL; |
| 836 |
RECODE_OPTION_LIST surface_options = NULL; |
| 837 |
|
| 838 |
request->scan_cursor++; |
| 839 |
scan_identifier (request); |
| 840 |
if (*request->scanned_string) |
| 841 |
{ |
| 842 |
RECODE_ALIAS alias = recode_find_alias (outer, request->scanned_string, |
| 843 |
ALIAS_FIND_AS_SURFACE); |
| 844 |
|
| 845 |
if (!alias) |
| 846 |
{ |
| 847 |
recode_error (outer, _("Unrecognised surface name `%s'"), |
| 848 |
request->scanned_string); |
| 849 |
return false; |
| 850 |
} |
| 851 |
surface = alias->symbol; |
| 852 |
|
| 853 |
} |
| 854 |
if (*request->scan_cursor == '+') |
| 855 |
surface_options = scan_options (request); |
| 856 |
|
| 857 |
if (*request->scan_cursor == '/') |
| 858 |
if (!scan_unsurfacers (request)) |
| 859 |
return false; |
| 860 |
|
| 861 |
if (surface && surface->unsurfacer) |
| 862 |
return |
| 863 |
add_to_sequence (request, surface->unsurfacer, surface_options, NULL); |
| 864 |
|
| 865 |
return true; |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
static bool |
| 874 |
add_unsurfacers_to_sequence (RECODE_REQUEST request, |
| 875 |
struct recode_surface_list *list) |
| 876 |
{ |
| 877 |
if (list->next) |
| 878 |
if (!add_unsurfacers_to_sequence (request, list->next)) |
| 879 |
return false; |
| 880 |
|
| 881 |
if (list->surface->unsurfacer) |
| 882 |
return |
| 883 |
add_to_sequence (request, list->surface->unsurfacer, NULL, NULL); |
| 884 |
|
| 885 |
return true; |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
static RECODE_SYMBOL |
| 902 |
scan_charset_before (RECODE_REQUEST request, RECODE_OPTION_LIST *options_pointer) |
| 903 |
{ |
| 904 |
RECODE_OUTER outer = request->outer; |
| 905 |
RECODE_ALIAS alias; |
| 906 |
|
| 907 |
*options_pointer = NULL; |
| 908 |
scan_identifier (request); |
| 909 |
alias = recode_find_alias (outer, request->scanned_string, ALIAS_FIND_AS_EITHER); |
| 910 |
if (*request->scan_cursor == '+') |
| 911 |
*options_pointer = scan_options (request); |
| 912 |
if (!alias) |
| 913 |
return NULL; |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
if (*request->scan_cursor == '/') |
| 918 |
{ |
| 919 |
if (!scan_unsurfacers (request)) |
| 920 |
return NULL; |
| 921 |
} |
| 922 |
else if (alias->implied_surfaces && !request->make_header_flag) |
| 923 |
{ |
| 924 |
if (!add_unsurfacers_to_sequence (request, alias->implied_surfaces)) |
| 925 |
return NULL; |
| 926 |
} |
| 927 |
|
| 928 |
return alias->symbol; |
| 929 |
} |
| 930 |
|
| 931 |
static RECODE_SYMBOL |
| 932 |
scan_charset_after (RECODE_REQUEST request, |
| 933 |
RECODE_CONST_SYMBOL before, |
| 934 |
RECODE_CONST_OPTION_LIST before_options) |
| 935 |
{ |
| 936 |
RECODE_OUTER outer = request->outer; |
| 937 |
RECODE_ALIAS alias; |
| 938 |
RECODE_SYMBOL charset; |
| 939 |
RECODE_OPTION_LIST charset_options = NULL; |
| 940 |
|
| 941 |
scan_identifier (request); |
| 942 |
alias = recode_find_alias (outer, request->scanned_string, ALIAS_FIND_AS_EITHER); |
| 943 |
if (*request->scan_cursor == '+') |
| 944 |
charset_options = scan_options (request); |
| 945 |
if (!alias) |
| 946 |
return NULL; |
| 947 |
charset = alias->symbol; |
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
if (!find_sequence (request, before, before_options, |
| 952 |
charset, charset_options)) |
| 953 |
{ |
| 954 |
recode_error (outer, _("No way to recode from `%s' to `%s'"), |
| 955 |
before->name, charset->name); |
| 956 |
return NULL; |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
if (scan_check_if_last_charset (request)) |
| 964 |
{ |
| 965 |
if (*request->scan_cursor == '/') |
| 966 |
{ |
| 967 |
while (*request->scan_cursor == '/') |
| 968 |
{ |
| 969 |
RECODE_SYMBOL surface = NULL; |
| 970 |
RECODE_OPTION_LIST surface_options = NULL; |
| 971 |
|
| 972 |
request->scan_cursor++; |
| 973 |
scan_identifier (request); |
| 974 |
if (*request->scanned_string) |
| 975 |
{ |
| 976 |
RECODE_ALIAS alias2 |
| 977 |
= recode_find_alias (outer, request->scanned_string, |
| 978 |
ALIAS_FIND_AS_SURFACE); |
| 979 |
|
| 980 |
if (!alias2) |
| 981 |
{ |
| 982 |
recode_error (outer, |
| 983 |
_("Unrecognised surface name `%s'"), |
| 984 |
request->scanned_string); |
| 985 |
return NULL; |
| 986 |
} |
| 987 |
surface = alias2->symbol; |
| 988 |
|
| 989 |
|
| 990 |
} |
| 991 |
if (*request->scan_cursor == '+') |
| 992 |
surface_options = scan_options (request); |
| 993 |
|
| 994 |
if (surface && surface->resurfacer) |
| 995 |
if (!add_to_sequence (request, surface->resurfacer, |
| 996 |
NULL, surface_options)) |
| 997 |
return NULL; |
| 998 |
} |
| 999 |
} |
| 1000 |
else if (alias->implied_surfaces && !request->make_header_flag) |
| 1001 |
{ |
| 1002 |
struct recode_surface_list *list; |
| 1003 |
|
| 1004 |
for (list = alias->implied_surfaces; list; list = list->next) |
| 1005 |
if (list->surface->resurfacer) |
| 1006 |
if (!add_to_sequence (request, list->surface->resurfacer, |
| 1007 |
NULL, NULL)) |
| 1008 |
return NULL; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
return charset; |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
static bool |
| 1020 |
scan_request (RECODE_REQUEST request) |
| 1021 |
{ |
| 1022 |
RECODE_OUTER outer = request->outer; |
| 1023 |
RECODE_OPTION_LIST before_options; |
| 1024 |
RECODE_SYMBOL charset = scan_charset_before (request, &before_options); |
| 1025 |
|
| 1026 |
if (!charset) |
| 1027 |
return false; |
| 1028 |
|
| 1029 |
if (request->scan_cursor[0] == '.' && request->scan_cursor[1] == '.') |
| 1030 |
while (request->scan_cursor[0] == '.' && request->scan_cursor[1] == '.') |
| 1031 |
{ |
| 1032 |
request->scan_cursor += 2; |
| 1033 |
charset = scan_charset_after (request, charset, before_options); |
| 1034 |
if (!charset) |
| 1035 |
return false; |
| 1036 |
} |
| 1037 |
else if (*request->scan_cursor == NUL) |
| 1038 |
{ |
| 1039 |
|
| 1040 |
charset = scan_charset_after (request, charset, before_options); |
| 1041 |
if (!charset) |
| 1042 |
return false; |
| 1043 |
} |
| 1044 |
else |
| 1045 |
{ |
| 1046 |
recode_error (outer, _("Expecting `..' in request")); |
| 1047 |
return false; |
| 1048 |
} |
| 1049 |
|
| 1050 |
return true; |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
static bool |
| 1059 |
decode_request (RECODE_REQUEST request, const char *string) |
| 1060 |
{ |
| 1061 |
RECODE_OUTER outer = request->outer; |
| 1062 |
|
| 1063 |
request->scan_cursor = string; |
| 1064 |
if (!ALLOC (request->scanned_string, strlen (string) + 1, char)) |
| 1065 |
return false; |
| 1066 |
request->sequence_length = 0; |
| 1067 |
|
| 1068 |
if (*request->scan_cursor) |
| 1069 |
{ |
| 1070 |
if (!scan_request (request)) |
| 1071 |
{ |
| 1072 |
free (request->scanned_string); |
| 1073 |
return false; |
| 1074 |
} |
| 1075 |
while (*request->scan_cursor == ',') |
| 1076 |
{ |
| 1077 |
request->scan_cursor++; |
| 1078 |
if (!scan_request (request)) |
| 1079 |
{ |
| 1080 |
free (request->scanned_string); |
| 1081 |
return false; |
| 1082 |
} |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
free (request->scanned_string); |
| 1087 |
return true; |
| 1088 |
} |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
static bool |
| 1103 |
guarantee_nul_terminator (RECODE_TASK task) |
| 1104 |
{ |
| 1105 |
if (task->output.cursor + 4 >= task->output.limit) |
| 1106 |
{ |
| 1107 |
RECODE_OUTER outer = task->request->outer; |
| 1108 |
size_t size = task->output.cursor - task->output.buffer; |
| 1109 |
|
| 1110 |
if (REALLOC (task->output.buffer, size + 4, char)) |
| 1111 |
{ |
| 1112 |
task->output.cursor = task->output.buffer + size; |
| 1113 |
task->output.limit = task->output.buffer + size + 4; |
| 1114 |
} |
| 1115 |
else |
| 1116 |
return false; |
| 1117 |
} |
| 1118 |
task->output.cursor[0] = NUL; |
| 1119 |
task->output.cursor[1] = NUL; |
| 1120 |
task->output.cursor[2] = NUL; |
| 1121 |
task->output.cursor[3] = NUL; |
| 1122 |
|
| 1123 |
return true; |
| 1124 |
} |
| 1125 |
|
| 1126 |
RECODE_REQUEST |
| 1127 |
recode_new_request (RECODE_OUTER outer) |
| 1128 |
{ |
| 1129 |
RECODE_REQUEST request; |
| 1130 |
|
| 1131 |
if (!ALLOC (request, 1, struct recode_request)) |
| 1132 |
return NULL; |
| 1133 |
|
| 1134 |
request->outer = outer; |
| 1135 |
request->diaeresis_char = '"'; |
| 1136 |
|
| 1137 |
request->work_string_allocated = 0; |
| 1138 |
return request; |
| 1139 |
} |
| 1140 |
|
| 1141 |
bool |
| 1142 |
recode_delete_request (RECODE_REQUEST request) |
| 1143 |
{ |
| 1144 |
for (RECODE_STEP step = request->sequence_array; |
| 1145 |
step < request->sequence_array + request->sequence_length; |
| 1146 |
step++) |
| 1147 |
delete_step (step); |
| 1148 |
free (request->sequence_array); |
| 1149 |
free (request->work_string); |
| 1150 |
free (request); |
| 1151 |
return true; |
| 1152 |
} |
| 1153 |
|
| 1154 |
bool |
| 1155 |
recode_scan_request (RECODE_REQUEST request, const char *string) |
| 1156 |
{ |
| 1157 |
return |
| 1158 |
decode_request (request, string) |
| 1159 |
&& simplify_sequence (request); |
| 1160 |
} |
| 1161 |
|
| 1162 |
char * |
| 1163 |
recode_string (RECODE_CONST_REQUEST request, const char *input_string) |
| 1164 |
{ |
| 1165 |
char *output_buffer = NULL; |
| 1166 |
size_t output_length = 0; |
| 1167 |
size_t output_allocated = 0; |
| 1168 |
|
| 1169 |
recode_buffer_to_buffer (request, input_string, strlen (input_string), |
| 1170 |
&output_buffer, &output_length, &output_allocated); |
| 1171 |
return output_buffer; |
| 1172 |
} |
| 1173 |
|
| 1174 |
bool |
| 1175 |
recode_string_to_buffer (RECODE_CONST_REQUEST request, |
| 1176 |
const char *input_string, |
| 1177 |
char **output_buffer_pointer, |
| 1178 |
size_t *output_length_pointer, |
| 1179 |
size_t *output_allocated_pointer) |
| 1180 |
{ |
| 1181 |
return |
| 1182 |
recode_buffer_to_buffer (request, input_string, strlen (input_string), |
| 1183 |
output_buffer_pointer, output_length_pointer, |
| 1184 |
output_allocated_pointer); |
| 1185 |
} |
| 1186 |
|
| 1187 |
bool |
| 1188 |
recode_string_to_file (RECODE_CONST_REQUEST request, |
| 1189 |
const char *input_string, |
| 1190 |
FILE *output_file) |
| 1191 |
{ |
| 1192 |
return |
| 1193 |
recode_buffer_to_file (request, input_string, strlen (input_string), |
| 1194 |
output_file); |
| 1195 |
} |
| 1196 |
|
| 1197 |
bool |
| 1198 |
recode_buffer_to_buffer (RECODE_CONST_REQUEST request, |
| 1199 |
const char *input_buffer, |
| 1200 |
size_t input_length, |
| 1201 |
char **output_buffer_pointer, |
| 1202 |
size_t *output_length_pointer, |
| 1203 |
size_t *output_allocated_pointer) |
| 1204 |
{ |
| 1205 |
RECODE_TASK task = recode_new_task (request); |
| 1206 |
bool success; |
| 1207 |
|
| 1208 |
if (!task) |
| 1209 |
return false; |
| 1210 |
|
| 1211 |
task->input.buffer = input_buffer; |
| 1212 |
task->input.cursor = input_buffer; |
| 1213 |
task->input.limit = input_buffer + input_length; |
| 1214 |
task->output.buffer = *output_buffer_pointer; |
| 1215 |
task->output.cursor = *output_buffer_pointer; |
| 1216 |
task->output.limit = *output_buffer_pointer + *output_allocated_pointer; |
| 1217 |
|
| 1218 |
success = recode_perform_task (task) && guarantee_nul_terminator (task); |
| 1219 |
*output_buffer_pointer = task->output.buffer; |
| 1220 |
*output_length_pointer = task->output.cursor - task->output.buffer; |
| 1221 |
*output_allocated_pointer = task->output.limit - task->output.buffer; |
| 1222 |
|
| 1223 |
recode_delete_task (task); |
| 1224 |
return success; |
| 1225 |
} |
| 1226 |
|
| 1227 |
bool |
| 1228 |
recode_buffer_to_file (RECODE_CONST_REQUEST request, |
| 1229 |
const char *input_buffer, |
| 1230 |
size_t input_length, |
| 1231 |
FILE *output_file) |
| 1232 |
{ |
| 1233 |
RECODE_TASK task = recode_new_task (request); |
| 1234 |
bool success; |
| 1235 |
|
| 1236 |
if (!task) |
| 1237 |
return false; |
| 1238 |
|
| 1239 |
task->input.buffer = input_buffer; |
| 1240 |
task->input.cursor = input_buffer; |
| 1241 |
task->input.limit = input_buffer + input_length; |
| 1242 |
task->output.file = output_file; |
| 1243 |
|
| 1244 |
success = recode_perform_task (task); |
| 1245 |
|
| 1246 |
recode_delete_task (task); |
| 1247 |
return success; |
| 1248 |
} |
| 1249 |
|
| 1250 |
bool |
| 1251 |
recode_file_to_buffer (RECODE_CONST_REQUEST request, |
| 1252 |
FILE *input_file, |
| 1253 |
char **output_buffer_pointer, |
| 1254 |
size_t *output_length_pointer, |
| 1255 |
size_t *output_allocated_pointer) |
| 1256 |
{ |
| 1257 |
RECODE_TASK task = recode_new_task (request); |
| 1258 |
bool success; |
| 1259 |
|
| 1260 |
if (!task) |
| 1261 |
return false; |
| 1262 |
|
| 1263 |
task->input.file = input_file; |
| 1264 |
task->output.buffer = *output_buffer_pointer; |
| 1265 |
task->output.cursor = *output_buffer_pointer; |
| 1266 |
task->output.limit = *output_buffer_pointer + *output_allocated_pointer; |
| 1267 |
|
| 1268 |
success = recode_perform_task (task) && guarantee_nul_terminator (task); |
| 1269 |
*output_buffer_pointer = task->output.buffer; |
| 1270 |
*output_length_pointer = task->output.cursor - task->output.buffer; |
| 1271 |
*output_allocated_pointer = task->output.limit - task->output.buffer; |
| 1272 |
|
| 1273 |
recode_delete_task (task); |
| 1274 |
return success; |
| 1275 |
} |
| 1276 |
|
| 1277 |
bool |
| 1278 |
recode_file_to_file (RECODE_CONST_REQUEST request, |
| 1279 |
FILE *input_file, |
| 1280 |
FILE *output_file) |
| 1281 |
{ |
| 1282 |
RECODE_TASK task = recode_new_task (request); |
| 1283 |
bool success; |
| 1284 |
|
| 1285 |
if (!task) |
| 1286 |
return false; |
| 1287 |
|
| 1288 |
task->input.file = input_file; |
| 1289 |
task->output.file = output_file; |
| 1290 |
|
| 1291 |
success = recode_perform_task (task); |
| 1292 |
|
| 1293 |
recode_delete_task (task); |
| 1294 |
return success; |
| 1295 |
} |