File: ucm_analyse.cc

package info (click to toggle)
libtranscript 0.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,272 kB
  • sloc: ansic: 223,135; cpp: 4,289; sh: 1,095; xml: 172; makefile: 70; lex: 44
file content (545 lines) | stat: -rw-r--r-- 21,596 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/* Copyright (C) 2011 G.P. Halkes
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3, as
   published by the Free Software Foundation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "ucm2ltc.h"
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <cstring>

int Ucm::calculate_depth(Entry *entry) {
  int depth, max_depth = 0;
  size_t i;

  switch (entry->action) {
    case ACTION_FINAL:
    case ACTION_FINAL_PAIR:
    case ACTION_UNASSIGNED:
    case ACTION_SHIFT:
      return 1;
    case ACTION_VALID:
      /* Set action to ACTION_LOOP while recursing, such that we can recognize
         loops in the state machine. The action is reset to ACTION_VALID after
         the recursion is done. */
      entry->action = ACTION_LOOP;
      for (i = 0; i < codepage_states[entry->next_state]->entries.size(); i++) {
        depth = calculate_depth(&codepage_states[entry->next_state]->entries[i]);
        if (depth > max_depth) max_depth = depth;
      }
      entry->action = ACTION_VALID;
      if (max_depth > 0)
        return max_depth + 1;
      else
        return -1;
    case ACTION_ILLEGAL:
      return -1;
    case ACTION_LOOP:
      fatal("%s: State machine contains a loop\n", file_name);
    default:
      PANIC();
  }
  PANIC();
  return 0;
}

void Ucm::validate_states(void) {
  size_t i, j;
  int mb_cur_max = 4, mb_cur_min = 1;

  for (i = 0; i < codepage_states.size(); i++) {
    for (j = 0; j < codepage_states[i]->entries.size(); j++) {
      if (codepage_states[i]->entries[j].next_state >= (int)codepage_states.size())
        fatal("%s: State %zd:%x-%x designates a non-existant state as next state\n", name, i,
              codepage_states[i]->entries[j].low, codepage_states[i]->entries[j].high);

      if (codepage_states[i]->entries[j].action == ACTION_VALID) {
        if (codepage_states[codepage_states[i]->entries[j].next_state]->flags & State::INITIAL)
          fatal(
              "%s: State %d:%x-%x designates an initial state as next state for non-final "
              "transition\n",
              name, i, codepage_states[i]->entries[j].low, codepage_states[i]->entries[j].high);
      } else {
        if (!(codepage_states[codepage_states[i]->entries[j].next_state]->flags & State::INITIAL))
          fatal(
              "%s: State %zd:%x-%x designates a non-initial state as next state for "
              "final/unassigned/illegal/shift transition\n",
              name, i, codepage_states[i]->entries[j].low, codepage_states[i]->entries[j].high);
      }
    }
  }

  mb_cur_max = atoi(tag_values[MB_MAX].str);
  mb_cur_min = atoi(tag_values[MB_MIN].str);

  if (mb_cur_max > 4 || mb_cur_max < 1) fatal("%s: <mb_cur_max> is out of range\n", name);
  if (mb_cur_min > mb_cur_max || mb_cur_min < 1) fatal("%s: <mb_cur_min> is out of range\n", name);

  for (i = 0; i < codepage_states.size(); i++) {
    if (!(codepage_states[i]->flags & State::INITIAL)) continue;

    for (j = 0; j < codepage_states[i]->entries.size(); j++) {
      int depth = calculate_depth(&codepage_states[i]->entries[j]);
      if (depth > 0 && depth > mb_cur_max)
        fatal("%s: State machine specifies byte sequences longer than <mb_cur_max>\n", name);
      if (depth > 0 && depth < mb_cur_min)
        fatal("%s: State machine specifies byte sequences shorter than <mb_cur_min>\n", name);
    }
  }

  if (tag_values[SUBCHAR].str != NULL) {
    vector<uint8_t> bytes;
    int saved_line_number = line_number;

    /* Set line_number such that correct line numbers are displayed in error messages. */
    line_number = tag_values[SUBCHAR].line_number;
    parse_byte_sequence(tag_values[SUBCHAR].str, bytes);
    /* In check_codepage_bytes the line number is always shown -1 because normally
       it is called after a line has been completely parsed. */
    line_number++;
    check_codepage_bytes(bytes);

    if (tag_values[SUBCHAR1].str != NULL) {
      /* Set line_number such that correct line numbers are displayed in error messages. */
      line_number = tag_values[SUBCHAR1].line_number;
      parse_byte_sequence(tag_values[SUBCHAR1].str, bytes);
      /* In check_codepage_bytes the line number is always shown -1 because normally
         it is called after a line has been completely parsed. */
      line_number++;
      check_codepage_bytes(bytes);
    }
    line_number = saved_line_number;
  }
}

static const int reorder_precision[4] = {0, 2, 3, 1};
static int compare_codepage_bytes_simple(Mapping *a, Mapping *b) {
  size_t i;

  for (i = 0; i < a->codepage_bytes.size() && i < b->codepage_bytes.size(); i++) {
    if (a->codepage_bytes[i] < b->codepage_bytes[i]) return -1;
    if (a->codepage_bytes[i] > b->codepage_bytes[i]) return 1;
  }

  if (a->codepage_bytes.size() < b->codepage_bytes.size())
    return -1;
  else if (a->codepage_bytes.size() > b->codepage_bytes.size())
    return 1;
  return 0;
}

bool compare_codepage_bytes(Mapping *a, Mapping *b) {
  int result = compare_codepage_bytes_simple(a, b);

  if (result == 0) return reorder_precision[a->precision] < reorder_precision[b->precision];
  return result < 0;
}

static int compare_codepoints_simple(Mapping *a, Mapping *b) {
  size_t i;

  for (i = 0; i < a->codepoints.size() && i < b->codepoints.size(); i++) {
    if (a->codepoints[i] < b->codepoints[i]) return -1;
    if (a->codepoints[i] > b->codepoints[i]) return 1;
  }

  if (a->codepoints.size() < b->codepoints.size())
    return -1;
  else if (a->codepoints.size() > b->codepoints.size())
    return 1;
  return 0;
}

bool compare_codepoints(Mapping *a, Mapping *b) {
  int result = compare_codepoints_simple(a, b);

  if (result == 0) return a->precision < b->precision;
  return result < 0;
}

void Ucm::check_duplicates(vector<Mapping *> &mappings, const char *variant_name) {
  vector<Mapping *>::const_iterator iter;
  if (mappings.size() != 0) {
    sort(mappings.begin(), mappings.end(), compare_codepoints);
    for (iter = mappings.begin() + 1; iter != mappings.end(); iter++) {
      if (compare_codepoints_simple(*iter, *(iter - 1)) == 0) {
        if ((*iter)->precision > 1 || (*(iter - 1))->precision > 1) continue;
        fatal("%s: Duplicate mapping defined for %s%s%s\n", name,
              sprint_codepoints((*iter)->codepoints), variant_name == NULL ? "" : " in variant ",
              variant_name == NULL ? "" : variant_name);
      }
    }

    sort(mappings.begin(), mappings.end(), compare_codepage_bytes);
    for (iter = mappings.begin() + 1; iter != mappings.end(); iter++) {
      if (compare_codepage_bytes_simple(*iter, *(iter - 1)) == 0) {
        if (reorder_precision[(*iter)->precision] > 1 ||
            reorder_precision[(*(iter - 1))->precision] > 1)
          continue;
        fatal("%s: Duplicate mapping defined for %s%s%s\n", name,
              sprint_sequence((*iter)->codepage_bytes), variant_name == NULL ? "" : " in variant ",
              variant_name == NULL ? "" : variant_name);
      }
    }
  }
}

void Ucm::check_variant_duplicates(vector<Mapping *> &base_mappings,
                                   vector<Mapping *> &variant_mappings, const char *variant_id) {
  /* We use the simple way to check the consistency of the combination: combine and check. */
  vector<Mapping *> combined;

  combined.insert(combined.end(), base_mappings.begin(), base_mappings.end());
  combined.insert(combined.end(), variant_mappings.begin(), variant_mappings.end());
  check_duplicates(combined, variant_id);
}

void Ucm::check_duplicates(void) {
  if (option_verbose) fprintf(stderr, "Checking for duplicate mappings\n");
  check_duplicates(simple_mappings, NULL);
  check_duplicates(multi_mappings, NULL);
  for (deque<Variant *>::const_iterator iter = variants.begin(); iter != variants.end(); iter++) {
    check_variant_duplicates(simple_mappings, (*iter)->simple_mappings, (*iter)->id);
    check_variant_duplicates(multi_mappings, (*iter)->multi_mappings, (*iter)->id);
  }
}

void Ucm::find_used_flags(vector<Mapping *> &mappings, int *length_counts) {
  for (vector<Mapping *>::const_iterator iter = mappings.begin(); iter != mappings.end(); iter++) {
    uint8_t change = from_unicode_flags ^ (*iter)->from_unicode_flags;
    if ((*iter)->from_unicode_flags & Mapping::FROM_UNICODE_SUBCHAR1)
      change &= ~Mapping::FROM_UNICODE_LENGTH_MASK;
    used_from_unicode_flags |= change;

    used_to_unicode_flags |= to_unicode_flags ^ (*iter)->to_unicode_flags;

    if (length_counts != NULL) length_counts[(*iter)->codepage_bytes.size() - 1]++;
  }
}

void Ucm::calculate_item_costs(void) {
  from_unicode_flags = simple_mappings[0]->from_unicode_flags;
  to_unicode_flags = simple_mappings[0]->to_unicode_flags;

  int i, j, best_size;
  double size;
  int length_counts[4] = {0, 0, 0, 0};

  find_used_flags(simple_mappings, length_counts);
  for (deque<Variant *>::const_iterator iter = variants.begin(); iter != variants.end(); iter++) {
    if ((*iter)->simple_mappings.size() > 0) {
      used_from_unicode_flags |= Mapping::FROM_UNICODE_VARIANT;
      used_to_unicode_flags |= Mapping::TO_UNICODE_VARIANT;
    }
    find_used_flags((*iter)->simple_mappings, NULL);
    used_from_unicode_flags |= (*iter)->used_from_unicode_flags;
    used_to_unicode_flags |= (*iter)->used_to_unicode_flags;
  }

  if (option_verbose) fprintf(stderr, "Items to save:\n");

  /* FIXME: when there are no unassigned mappings in the range, this isn't necessary. However, we
          don't know whether there are unassigned mappings, because that will be calculated based on
          the costs calculated here. Chicken, egg, etc. */
  used_from_unicode_flags |= Mapping::FROM_UNICODE_NOT_AVAIL;
  from_flag_costs = 0.25 * popcount(create_mask(used_from_unicode_flags));
  to_flag_costs = 0.25 * popcount(create_mask(used_to_unicode_flags));

  if (option_verbose) {
    fprintf(stderr, "- from unicode not available flags\n");
    if (used_from_unicode_flags & Mapping::FROM_UNICODE_FALLBACK)
      fprintf(stderr, "- from unicode fallback flags\n");
    if (used_from_unicode_flags & Mapping::FROM_UNICODE_MULTI_START)
      fprintf(stderr, "- from unicode M:N mappings flags\n");
    if (used_from_unicode_flags & Mapping::FROM_UNICODE_SUBCHAR1)
      fprintf(stderr, "- from unicode subchar1 flags\n");
    if (used_from_unicode_flags & Mapping::FROM_UNICODE_LENGTH_MASK &
        (Mapping::FROM_UNICODE_LENGTH_MASK >> 1))
      fprintf(stderr, "- from unicode length low bit\n");
    if (used_from_unicode_flags & Mapping::FROM_UNICODE_LENGTH_MASK &
        (Mapping::FROM_UNICODE_LENGTH_MASK << 1))
      fprintf(stderr, "- from unicode length high bit\n");
    if (used_from_unicode_flags & Mapping::FROM_UNICODE_VARIANT)
      fprintf(stderr, "- from unicode variant flags\n");

    if (used_to_unicode_flags & Mapping::TO_UNICODE_FALLBACK)
      fprintf(stderr, "- to unicode fallback flags\n");
    if (used_to_unicode_flags & Mapping::TO_UNICODE_MULTI_START)
      fprintf(stderr, "- to unicode M:N mappings flags\n");
    if (used_to_unicode_flags & Mapping::TO_UNICODE_PRIVATE_USE)
      fprintf(stderr, "- to unicode private use flags\n");
    if (used_to_unicode_flags & Mapping::TO_UNICODE_VARIANT)
      fprintf(stderr, "- to unicode variant flags\n");
  }

  best_size = INT_MAX;
  for (i = 1; i <= 3; i++) {
    size = 0.0;

    if (i == 1 && (length_counts[2] != 0 || length_counts[3] != 0)) continue;

    for (j = 0; j < 4; j++) {
      if (j < i)
        size += (double)length_counts[j] * (i + from_flag_costs);
      else
        size += (double)length_counts[j] * 2 * (i + from_flag_costs);
    }

    if (size + 0.99 < best_size) {
      best_size = (int)(size + 0.99);
      single_bytes = i;
    }
  }

  if (used_from_unicode_flags != 0) flags |= FROM_UNICODE_FLAGS_TABLE_INCLUDED;
  if (used_to_unicode_flags != 0) flags |= TO_UNICODE_FLAGS_TABLE_INCLUDED;
  if (!multi_mappings.empty()) flags |= MULTI_MAPPINGS_AVAILABLE;
  if (variants.size() > 1) flags |= VARIANTS_AVAILABLE;
}

void Ucm::trace_back(size_t idx, shift_sequence_t &shift_sequence) {
  if (codepage_states[idx]->flags & State::INITIAL) {
    shift_sequence.from_state = idx;
    if (shift_sequence.from_state != shift_sequence.to_state)
      shift_sequences.push_back(shift_sequence);
    return;
  }

  for (size_t i = 0; i != codepage_states.size(); i++) {
    for (vector<Entry>::const_iterator entry_iter = codepage_states[i]->entries.begin();
         entry_iter != codepage_states[i]->entries.end(); entry_iter++) {
      if (entry_iter->action == ACTION_VALID && entry_iter->next_state == (int)idx) {
        shift_sequence.bytes.push_front(entry_iter->low);
        trace_back(i, shift_sequence);
        shift_sequence.bytes.pop_front();
      }
    }
  }
}

void Ucm::find_shift_sequences(void) {
  if (!(flags & MULTIBYTE_START_STATE_1)) return;

  for (size_t i = 0; i != codepage_states.size(); i++) {
    for (vector<Entry>::const_iterator entry_iter = codepage_states[i]->entries.begin();
         entry_iter != codepage_states[i]->entries.end(); entry_iter++) {
      if (entry_iter->action == ACTION_SHIFT) {
        shift_sequence_t shift_sequence;
        shift_sequence.bytes.push_front(entry_iter->low);
        shift_sequence.to_state = entry_iter->next_state;
        trace_back(i, shift_sequence);
      }
    }
  }
}

void Ucm::check_state_machine(Ucm *other, int this_state, int other_state) {
  vector<Entry>::const_iterator this_iter = codepage_states[this_state]->entries.begin();
  vector<Entry>::const_iterator other_iter = other->codepage_states[other_state]->entries.begin();

  while (this_iter != codepage_states[this_state]->entries.end() &&
         other_iter != other->codepage_states[other_state]->entries.end()) {
    switch (this_iter->action) {
      case ACTION_VALID:
        if (other_iter->action != ACTION_VALID) goto not_compat;
        check_state_machine(other, this_iter->next_state, other_iter->next_state);
        break;
      case ACTION_FINAL:
      case ACTION_FINAL_PAIR:
      case ACTION_UNASSIGNED:
        if (other_iter->action != ACTION_FINAL && other_iter->action != ACTION_FINAL_PAIR &&
            other_iter->action != ACTION_UNASSIGNED)
          goto not_compat;
        if (this_iter->next_state != other_iter->next_state) goto not_compat;
        break;
      case ACTION_SHIFT:
      case ACTION_ILLEGAL:
        if (other_iter->action != this_iter->action) goto not_compat;

        if (this_iter->next_state != other_iter->next_state) goto not_compat;
        break;
      default:
        PANIC();
    }

    if (this_iter->high < other_iter->high) {
      this_iter++;
    } else if (this_iter->high > other_iter->high) {
      other_iter++;
    } else {
      this_iter++;
      other_iter++;
    }
  }
  return;

not_compat:
  fatal("%s: State machine in %s is not compatible\n", name, other->name);
}

void Ucm::check_compatibility(Ucm *other) {
  if (uconv_class != other->uconv_class)
    fatal("%s: Converter in %s has different uconv_class\n", name, other->name);
  if (strcmp(tag_values[MB_MAX].str, other->tag_values[MB_MAX].str) != 0)
    fatal("%s: Converter in %s has different mb_cur_max\n", name, other->name);
  if (strcmp(tag_values[MB_MIN].str, other->tag_values[MB_MIN].str) != 0)
    fatal("%s: Converter in %s has different mb_cur_min\n", name, other->name);
  if ((flags & ~INTERNAL_TABLE) != (other->flags & ~INTERNAL_TABLE))
    // FIXME: make error message more specific!
    fatal("%s: Converter in %s is incompatible\n", name, other->name);
  if (tag_values[SUBCHAR].str == NULL) {
    if (other->tag_values[SUBCHAR].str != NULL)
      fatal("%s: Converter in %s has different subchar\n", name, other->name);
  } else {
    if (other->tag_values[SUBCHAR].str == NULL)
      fatal("%s: Converter in %s has different subchar\n", name, other->name);
    if (strcmp(tag_values[SUBCHAR].str, other->tag_values[SUBCHAR].str) != 0)
      fatal("%s: Converter in %s has different subchar\n", name, other->name);
  }
  if (tag_values[SUBCHAR1].str == NULL) {
    if (other->tag_values[SUBCHAR1].str != NULL)
      fatal("%s: Converter in %s has different subchar1\n", name, other->name);
  } else {
    if (other->tag_values[SUBCHAR1].str == NULL ||
        strcmp(tag_values[SUBCHAR1].str, other->tag_values[SUBCHAR1].str) != 0)
      fatal("%s: Converter in %s has different subchar1\n", name, other->name);
  }

  check_state_machine(other, 0, 0);
  if (flags & MULTIBYTE_START_STATE_1) check_state_machine(other, 1, 1);
}

static bool compareMapping(Mapping *a, Mapping *b) {
  int result = compare_codepage_bytes_simple(a, b);

  if (result != 0) return result < 0;
  result = compare_codepoints_simple(a, b);
  if (result != 0) return result < 0;

  return a->precision < b->precision;
}

void Ucm::prepare_subtract(void) {
  sort(simple_mappings.begin(), simple_mappings.end(), compareMapping);
}

void Ucm::subtract(vector<Mapping *> &this_mappings, vector<Mapping *> &other_mappings,
                   vector<Mapping *> &this_variant_mappings) {
  int bytes_result, codepoints_result;
  vector<Mapping *>::iterator this_iter = this_mappings.begin();
  vector<Mapping *>::const_iterator other_iter = other_mappings.begin();

  while (this_iter != this_mappings.end() && other_iter != other_mappings.end()) {
    bytes_result = compare_codepage_bytes_simple(*this_iter, *other_iter);
    codepoints_result = compare_codepoints_simple(*this_iter, *other_iter);

    if (bytes_result < 0) {
      this_variant_mappings.push_back(*this_iter);
      this_iter = this_mappings.erase(this_iter);
    } else if (bytes_result > 0) {
      other_iter++;
    } else if (codepoints_result < 0) {
      this_variant_mappings.push_back(*this_iter);
      this_iter = this_mappings.erase(this_iter);
    } else if (codepoints_result > 0) {
      other_iter++;
    } else if ((*this_iter)->precision < (*other_iter)->precision) {
      this_variant_mappings.push_back(*this_iter);
      this_iter = this_mappings.erase(this_iter);
    } else if ((*this_iter)->precision > (*other_iter)->precision) {
      other_iter++;
    } else {
      this_iter++;
      other_iter++;
    }
  }

  if (other_iter == other_mappings.end()) {
    while (this_iter != this_mappings.end()) {
      this_variant_mappings.push_back(*this_iter);
      this_iter = this_mappings.erase(this_iter);
    }
  }
}

void Ucm::subtract(Ucm *other) {
  subtract(simple_mappings, other->simple_mappings, variant.simple_mappings);
  subtract(multi_mappings, other->multi_mappings, variant.multi_mappings);
}

void Ucm::fixup_variants(void) {
  for (deque<Variant *>::const_iterator iter = variants.begin(); iter != variants.end(); iter++) {
    (*iter)->simple_mappings.insert((*iter)->simple_mappings.end(), variant.simple_mappings.begin(),
                                    variant.simple_mappings.end());
    (*iter)->multi_mappings.insert((*iter)->multi_mappings.end(), variant.multi_mappings.begin(),
                                   variant.multi_mappings.end());
  }
}

void Ucm::merge_variants(Ucm *other) {
  if (other->variants.size() == 0) {
    variants.push_back(new Variant(other->variant));
    other->variant.simple_mappings.clear();
    other->variant.multi_mappings.clear();
  } else {
    variants.insert(variants.end(), other->variants.begin(), other->variants.end());
    other->variants.clear();
  }
  /* We used to delete the other Ucm here, but because we may need info that is
     contained in it when checking for name clashes, we just leave it. */
}

void Ucm::variants_done(void) {
  /* Add the local variant, but only if no others are defined. */
  if (variants.size() != 0) return;
  variants.push_back(new Variant(variant));
  variant.simple_mappings.clear();
  variant.multi_mappings.clear();
}

void Ucm::check_base_mul_ranges(vector<State *> &states) {
  for (vector<State *>::const_iterator state_iter = states.begin(); state_iter != states.end();
       state_iter++) {
    if ((*state_iter)->base > 0xffff) fatal("%s: Calculated state table too large\n", name);
    for (vector<Entry>::const_iterator entry_iter = (*state_iter)->entries.begin();
         entry_iter != (*state_iter)->entries.end(); entry_iter++) {
      if (entry_iter->base > 0xffff || entry_iter->mul > 0xffff)
        fatal("%s: Calculated state table too large\n", name);
    }
  }
}

void Ucm::check_base_mul_ranges(void) {
  check_base_mul_ranges(codepage_states);
  check_base_mul_ranges(unicode_states);
}

bool Ucm::is_simple_table(void) {
  /* Restrictions for simple tables:
     - may have no variants
     - must map only to BMP
     - may not include multi-mappings
     - 0 byte must map only to U0000 [ allows checking for absence without checking flags ]
     - state machine may have only 1 state
  */

  if (uconv_class != CLASS_SBCS || !multi_mappings.empty() || !variants.empty() ||
      codepage_states.size() != 1)
    return false;

  for (vector<Mapping *>::const_iterator iter = simple_mappings.begin();
       iter != simple_mappings.end(); iter++) {
    if ((*iter)->codepoints[0] > 0xffff) return false;
    if ((*iter)->codepage_bytes[0] == 0 && (*iter)->codepoints[0] != 0) return false;
  }
  return true;
}