Debian Codegraph @

terps/tads/tads3/charmap.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/charmap.h,v 1.2 1999/05/17 02:52:29 MJRoberts Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 charmap.h - character-set mapper
12 Function
13 Provides mappings between 16-bit Unicode and single-byte, multi-byte,
14 and double-byte character sets.
15 Notes
16
17 Modified
18 10/17/98 MJRoberts - Creation
19 */
20
21 #ifndef CHARMAP_H
22 #define CHARMAP_H
23
24 #include <stdlib.h>
25 #include <memory.h>
26 #include <string.h>
27
28 #include "utf8.h"
29 #include "os.h"
30 #include "t3std.h"
31
32
33 /* ------------------------------------------------------------------------ */
34 /*
35 * Mapping Types. This enum provides a characterization of a local
36 * character set (as defined in a mapping file).
37 */
38 enum charmap_type_t
39 {
40 /*
41 * Single-byte character set - each character is represented with a
42 * single 8-bit byte.
43 */
44 CHARMAP_TYPE_SB,
45
46 /*
47 * Double-byte character set - each character is represented with
48 * exactly two 8-bit bytes. In each byte pair, the first byte is
49 * taken as the high-order byte, so a text input stream consisting
50 * of the bytes 0x12, 0x34, 0x56, 0x78 would be interpreted as the
51 * two 16-bit code point values 0x1234, 0x5678.
52 */
53 CHARMAP_TYPE_DB,
54
55 /*
56 * Mixed multi-byte - each character is represented by either one or
57 * two 8-bit bytes. Each two-byte character starts with a byte that
58 * is only used in two-byte characters; each one-byte character
59 * consists of a single byte that is not used as the first byte of
60 * any two-byte character. In each two-byte character, the first
61 * byte is taken as the high-order byte.
62 *
63 * For example, assuming that 0x00-0x7F are defined as single-byte
64 * characters, and 0x8000-0xFFFF are defined as double-byte
65 * characters, the byte sequence 0x12, 0x81, 0xAB, 0x82, 0xCD, 0x34
66 * would be taken as the character sequence 0x12, 0x81AB, 0x82CD,
67 * 0x34.
68 */
69 CHARMAP_TYPE_MB
70 };
71
72 /* ------------------------------------------------------------------------ */
73 /*
74 * Basic character mapper class.
75 */
76 class CCharmap
77 {
78 public:
79 /* add a reference */
80 void add_ref() { ++ref_cnt_; }
81
82 /* release a reference; delete on removing the last reference */
83 void release_ref()
84 {
85 /* count the unreference */
86 --ref_cnt_;
87
88 /* if that leaves no references, delete me */
89 if (ref_cnt_ == 0)
90 delete this;
91 }
92
93 protected:
94 CCharmap()
95 {
96 /* start out with one reference, for the initial creator */
97 ref_cnt_ = 1;
98 }
99
100 virtual ~CCharmap() { }
101
102 /*
103 * Open and characterize a mapping file. Returns the osfildef
104 * pointer if the file was successfully opened and parsed, or null
105 * if not. Sets *map_type to indicate the type of mapping contained
106 * in the file.
107 */
108 static osfildef *open_map_file(class CResLoader *res_loader,
109 const char *table_name,
110 charmap_type_t *map_type);
111
112 /*
113 * Open and characterize a map file, checking name synonyms
114 */
115 static osfildef *open_map_file_syn(class CResLoader *res_loader,
116 const char *table_name,
117 charmap_type_t *map_type);
118
119 /* check a name to see if it matches one of the names for ASCII */
120 static int name_is_ascii_synonym(const char *table_name)
121 {
122 /* accept any of the various synonyms for ASCII */
123 return (stricmp(table_name, "us-ascii") == 0
124 || stricmp(table_name, "asc7dflt") == 0
125 || stricmp(table_name, "ascii") == 0
126 || stricmp(table_name, "iso646-us") == 0
127 || stricmp(table_name, "iso-ir-6") == 0
128 || stricmp(table_name, "cp367") == 0
129 || stricmp(table_name, "us") == 0);
130 }
131
132 /* check for a utf-8 synonym */
133 static int name_is_utf8_synonym(const char *table_name)
134 {
135 return (stricmp(table_name, "utf-8") == 0
136 || stricmp(table_name, "utf8") == 0);
137 }
138
139 /* check for ucs2-le synonyms */
140 static int name_is_ucs2le_synonym(const char *table_name)
141 {
142 return (stricmp(table_name, "utf-16le") == 0
143 || stricmp(table_name, "utf16le") == 0
144 || stricmp(table_name, "utf_16le") == 0
145 || stricmp(table_name, "unicodel") == 0
146 || stricmp(table_name, "unicode-l") == 0
147 || stricmp(table_name, "unicode-le") == 0
148 || stricmp(table_name, "ucs-2le") == 0
149 || stricmp(table_name, "ucs2le") == 0);
150 }
151
152 /* check for ucs2-be synonyms */
153 static int name_is_ucs2be_synonym(const char *table_name)
154 {
155 return (stricmp(table_name, "utf-16be") == 0
156 || stricmp(table_name, "utf16be") == 0
157 || stricmp(table_name, "utf_16be") == 0
158 || stricmp(table_name, "unicodeb") == 0
159 || stricmp(table_name, "unicode-b") == 0
160 || stricmp(table_name, "unicode-be") == 0
161 || stricmp(table_name, "ucs-2be") == 0
162 || stricmp(table_name, "ucs2be") == 0);
163 }
164
165 /* check a name to see if it matches one of the names for ISO 8859-1 */
166 static int name_is_8859_1_synonym(const char *table_name)
167 {
168 /* accept any of the various names for ISO 8859-1 */
169 return (stricmp(table_name, "iso-8859-1") == 0
170 || stricmp(table_name, "iso_8859-1") == 0
171 || stricmp(table_name, "iso_8859_1") == 0
172 || stricmp(table_name, "iso8859-1") == 0
173 || stricmp(table_name, "iso8859_1") == 0
174 || stricmp(table_name, "8859-1") == 0
175 || stricmp(table_name, "8859_1") == 0
176 || stricmp(table_name, "iso-ir-100") == 0
177 || stricmp(table_name, "latin1") == 0
178 || stricmp(table_name, "latin-1") == 0
179 || stricmp(table_name, "l1") == 0
180 || stricmp(table_name, "iso1") == 0
181 || stricmp(table_name, "cp819") == 0);
182 }
183
184 /* reference count */
185 unsigned int ref_cnt_;
186 };
187
188 /* ------------------------------------------------------------------------ */
189 /*
190 * Base character mapper class for mapping from a local character set to
191 * UTF-8. This is an abstract interface that must be implemented for
192 * different classes of character sets.
193 */
194 class CCharmapToUni: public CCharmap
195 {
196 public:
197 /* initialize */
198 CCharmapToUni() { }
199
200 /*
201 * Create a mapping object for a given character table. We'll read
202 * enough of the character table to determine the appropriate
203 * concrete subclass to instantiate, then create an object, load the
204 * table into the object, and return the object. The caller is
205 * responsible for deleting the object when finished with it.
206 *
207 * Returns null if the mapping file cannot be loaded.
208 */
209 static CCharmapToUni *load(class CResLoader *res_loader,
210 const char *table_name);
211
212 /*
213 * Validate a UTF-8 buffer. If we find any ill-formed byte sequences,
214 * we'll convert the errant bytes to '?' characters.
215 */
216 static void validate(char *buf, size_t len);
217
218 /*
219 * Determine if the given byte sequence forms a complete character in
220 * the local character set. Returns true if so, false if not. 'len'
221 * must be at least 1.
222 */
223 virtual int is_complete_char(const char *p, size_t len) const = 0;
224
225 /*
226 * Map one character. Advances the input pointer and length past the
227 * character. If there's a complete character at 'p', maps the
228 * character and returns true; if additional bytes are needed to form a
229 * complete character, leaves 'p' unchanged and returns false.
230 */
231 virtual int mapchar(wchar_t &ch, const char *&p, size_t &len) = 0;
232
233 /*
234 * Convert a string from the local character set to Unicode.
235 * Returns the byte length of the output. If the output buffer is
236 * too small to store the result, we will return the size of the
237 * full result, but we won't write past the end of the buffer.
238 *
239 * We'll advance *output_ptr by the number of bytes we write.
240 *
241 * If we store anything, we'll decrement *output_buf_len by the
242 * number of bytes we store; if we don't have enough room, we'll set
243 * *output_buf_len to zero.
244 *
245 * input_ptr is a pointer to the input string; input_len is the
246 * length in bytes of the input string.
247 */
248 virtual size_t map(char **output_ptr, size_t *output_buf_len,
249 const char *input_ptr, size_t input_len) const = 0;
250
251 /*
252 * Convert a string from the local character set to Unicode.
253 *
254 * This works the same way as map(), but additionally provides
255 * information on the consumption of source bytes by filling in
256 * partial_len with the number of bytes at the end of the source
257 * buffer that are not mappable because they do not form complete
258 * characters in the source character set. Since we scan all input
259 * regardless of whether there's space to store the resulting output,
260 * this will reflect the same number of bytes no matter what the
261 * output buffer length.
262 */
263 virtual size_t map2(char **output_ptr, size_t *output_buf_len,
264 const char *input_ptr, size_t input_len,
265 size_t *partial_len) const = 0;
266
267 /*
268 * Map a null-terminated string into a buffer; returns the number of
269 * bytes of the buffer actually needed to store the string. If the
270 * entire string couldn't be mapped, this will return a number
271 * greater than or equal to the output buffer size, but we will not
272 * write beyond the end of the buffer.
273 *
274 * If there's space, the result will be null-terminated; however,
275 * the null terminator byte will not be included in the result
276 * length. If the return value exactly equals outbuflen, it means
277 * that the string exactly fills the buffer, hence there isn't space
278 * for a null terminator.
279 */
280 size_t map_str(char *outbuf, size_t outbuflen, const char *input_str);
281
282 /*
283 * Map a counted-length string into a buffer; returns the number of
284 * bytes actually needed to store the string. If the entire string
285 * couldn't be mapped, returns the number of bytes actually needed, but
286 * will only map up to the available size limit. Does not
287 * null-terminate the result.
288 */
289 size_t map_str(char *outbuf, size_t outbuflen,
290 const char *input_str, size_t input_len);
291
292 /*
293 * Map a string, allocating space
294 *
295 * The caller is responsible for freeing the returned string with
296 * t3free() when done with it.
297 */
298 size_t map_str_alo(char **outbuf, const char *input_str);
299
300 /*
301 * Read characters from a file into a buffer, translating the
302 * characters to UTF-8. Returns the number of bytes copied into the
303 * buffer; returns zero on end of file. The buffer must be at least
304 * three bytes long to ensure that at least one character can be read
305 * from the file (the longest UTF-8 character takes up three bytes),
306 * since it would otherwise not be possible to distinguish reaching
307 * the end of the file from simply being unable to fit even one
308 * character into the buffer.
309 *
310 * The file can be opened in text or binary mode; we don't pay any
311 * attention to newline sequences, so the mode is not relevant to us.
312 *
313 * This routine may read fewer than the desired number of bytes. Upon
314 * return, the file's seek position should be set to the next byte of
315 * the file after the last character copied into the output buffer.
316 *
317 * 'read_limit' is the maximum number of bytes we're allowed to read
318 * from the underlying file. If this is zero, then the read size is
319 * unlimited.
320 */
321 virtual size_t read_file(class CVmDataSource *fp,
322 char *buf, size_t bufl) = 0;
323
324 protected:
325 /* delete the mapping */
326 virtual ~CCharmapToUni() { }
327
328 /* load the mapping table from the file */
329 void load_table(osfildef *fp);
330
331 /*
332 * Set a mapping. uni_code_pt is the unicode code point, and
333 * local_code_pt is the code point in the local character set.
334 */
335 virtual void set_mapping(wchar_t uni_code_pt, wchar_t local_code_pt) = 0;
336 };
337
338 /* ------------------------------------------------------------------------ */
339 /*
340 * Base character mapper class for mapping from Unicode UTF-8 to a local
341 * character set. This is an abstract interface that must be separately
342 * implemented for different classes of character sets.
343 *
344 * Each mapping object maintains a table of mapping tables. The master
345 * table contains an array of up to 256 sub-tables. The top 8 bits of
346 * the unicode character value give the index in the master table. Each
347 * entry in the master table is a pointer to a sub-table, or a null
348 * pointer if there are no mappings for characters in the range for that
349 * sub-table.
350 *
351 * For example, unicode characters 0x0000 through 0x007f are mapped
352 * through the table obtained by getting the pointer at index 0 from the
353 * master table. Unicode characters 0x0200 through 0x02ff are in the
354 * table at master table index 2.
355 *
356 * If a master table index entry is empty (i.e., the pointer in the
357 * master table at that index is null), it means that all of the
358 * characters in the range for that master index map to the default
359 * character. Otherwise, we index into the sub-table using the
360 * low-order 8 bits of the Unicode character code to find the character
361 * mapping giving the local character set code for the Unicode value.
362 *
363 * Each entry in the mapping table is the offset of the translation of
364 * the character within the translation array. The translation array is
365 * an array of bytes. The first byte of each entry is the length in
366 * bytes of the entry (not including the length byte), followed by the
367 * bytes of the entry.
368 *
369 * The first entry in the translation array is always the default
370 * character, which is the mapping we use for characters with no other
371 * valid mapping.
372 */
373 class CCharmapToLocal: public CCharmap
374 {
375 public:
376 /* initialize */
377 CCharmapToLocal();
378
379 /* create a mapper and load the mapping from a file */
380 static CCharmapToLocal *load(class CResLoader *res_loader,
381 const char *table_name);
382
383 /*
384 * Convert a character from Unicode to the local character set.
385 * Stores the character's byte or bytes at the given pointer, and
386 * increments the pointer to point to the next byte after the
387 * character.
388 *
389 * Returns the byte length of the output. If the output buffer is
390 * not long enough to store the result, we simply return the size of
391 * the result without storing anything.
392 *
393 * If we actually store anything, we'll decrement *output_buf_len by
394 * the number of bytes we stored; if we don't have room to store
395 * anything, we'll set *output_buf_len to zero.
396 */
397 virtual size_t map(wchar_t unicode_char, char **output_ptr,
398 size_t *output_buf_len) const = 0;
399
400 /*
401 * Simple single-character mapper - returns the byte length of the
402 * local character equivalent of the unicode character, which is
403 * written into the buffer. If the buffer isn't big enough, we'll
404 * still return the length, but won't write anything to the buffer.
405 */
406 size_t map_char(wchar_t unicode_char, char *buf, size_t buflen)
407 {
408 /* map the character */
409 return map(unicode_char, &buf, &buflen);
410 }
411
412
413 /*
414 * Convert a UTF-8 string with a given byte length to the local
415 * character set.
416 *
417 * Returns the byte length of the result. If the result is too long
418 * to fit in the output buffer, we'll return the number of bytes we
419 * actually were able to store (we'll store as much as we can, and
420 * stop when we run out of space). We'll indicate in
421 * *src_bytes_used how many bytes of the source we were able to map.
422 *
423 * If the output buffer is null, we will store nothing, but simply
424 * determine how much space it would take to store the entire string.
425 *
426 * This base class provides an implementation of this method that is
427 * suitable for all subclasses, but the method is defined as virtual
428 * so that subclasses can override it with a more tailored (and thus
429 * more efficient) implementation. The general-purpose base-class
430 * implementation must call the virtual function map() for each
431 * character mapped.
432 */
433 virtual size_t map_utf8(char *dest, size_t dest_len,
434 utf8_ptr src, size_t src_byte_len,
435 size_t *src_bytes_used) const;
436
437 /*
438 * map to local - alternative interface using character buffers (rather
439 * than UTF8 pointers)
440 */
441 size_t map_utf8(char *dest, size_t dest_len,
442 const char *src, size_t src_byte_len,
443 size_t *src_bytes_used) const;
444
445 /*
446 * Map to local, allocating a new buffer. Fills in 'buf' with a
447 * pointer to the new buffer, which we allocate via t3malloc() with
448 * enough space for the mapped string plus a null terminator. Maps the
449 * string into the buffer and adds a null byte. Returns the byte
450 * length of the mapped string (not including the null byte).
451 *
452 * The caller is responsible for freeing the returned string with
453 * t3free() when done with it.
454 */
455 size_t map_utf8_alo(char **buf, const char *src, size_t srclen) const;
456
457
458 /*
459 * Convert a null-terminated UTF-8 string to the local character set.
460 *
461 * Returns the byte length of the result. If the result is too long
462 * to fit in the output buffer, we'll return the size without storing
463 * the entire string (we'll store as much as we can, and stop when we
464 * run out of space, but continue counting the length needed; call
465 * with a destination buffer length of zero to simply determine how
466 * much space is needed for the result).
467 *
468 * The length returned does NOT include the null terminator. However,
469 * if there's room, we will null-terminate the result string. So, if
470 * the caller wants the result to be null terminated, it should make
471 * sure that the buffer contains one byte more than the space reported
472 * as necessary to store the result.
473 */
474 virtual size_t map_utf8z(char *dest, size_t dest_len, utf8_ptr src)
475 const;
476
477 /*
478 * Convert a null-terminated UTF-8 string to the local character set,
479 * filling in an 'escape' sequence for unknown characters. For each
480 * unknown character, we'll invoke the given callback to get the
481 * 'escaped' representation. Use &CCharmapToLocal::source_esc_cb, for
482 * example, to map using source-code-style escape sequences.
483 *
484 * The callback takes the unmappable character, a pointer to the output
485 * buffer, and a pointer to the length remaining. It should fill in
486 * the buffer with the escaped sequence (up to the remaining length
487 * limit), and adjust the buffer pointer and length for the space
488 * consumed. The return value is the full length required for the
489 * complete escape sequence, even if there's not enough space in the
490 * buffer to hold that many characters.
491 */
492 virtual size_t map_utf8z_esc(char *dest, size_t dest_len, utf8_ptr src,
493 size_t (*esc_fn)(wchar_t, char **, size_t *))
494 const;
495
496 /*
497 * ready-made callback for map_utf8z_esc() - map to unicode 'backslash'
498 * escape sequences ('\u1234'), as we'd use in tads source code
499 */
500 static size_t source_esc_cb(wchar_t ch, char **dest, size_t *len);
501
502 /*
503 * Write data to a file, converting from UTF-8 to the local character
504 * set. Returns zero on success, non-zero if an error occurs writing
505 * the data.
506 */
507 int write_file(class CVmDataSource *fp, const char *buf, size_t bufl);
508
509 /*
510 * determine if the given Unicode character has a mapping to the local
511 * character set
512 */
513 virtual int is_mappable(wchar_t unicode_char) const
514 {
515 /*
516 * By default, it's mappable if it has a non-default mapping in
517 * the translation table. The default mapping is always at offset
518 * zero in the translation table.
519 */
520 return (get_mapping(unicode_char) != 0);
521 }
522
523 /*
524 * Get the display expansion for a unicode character. This returns a
525 * pointer to an array of wchar_t characters, and fills in the length
526 * variable. Returns null if there's no expansion.
527 *
528 * An "expansion" is a list of two or more unicode characters that
529 * should be substituted for the given unicode character when the
530 * character is displayed. Display expansions are normally used for
531 * visual approximations when the local character set doesn't contain
532 * an exact match for the unicode character; for example, an ASCII
533 * mapping might use the expansion "(c)" to represent the copyright
534 * circled-C symbol, or the two-character sequence "AE" to represent
535 * the AE ligature.
536 */
537 const wchar_t *get_expansion(wchar_t unicode_char, size_t *len)
538 {
539 size_t ofs;
540 const wchar_t *map;
541
542 /* get the mapping offset in the expansion array */
543 ofs = get_exp_mapping(unicode_char);
544
545 /* if the mapping offset is zero, it means there's no mapping */
546 if (ofs == 0)
547 {
548 /* indicate that there's no mapping by returning null */
549 *len = 0;
550 return 0;
551 }
552
553 /* get the mapping pointer */
554 map = get_exp_ptr(ofs);
555
556 /* read the length and skip it */
557 *len = (size_t)*map++;
558
559 /* return the pointer to the first character of the expansion */
560 return map;
561 }
562
563 protected:
564 /* delete the mapping */
565 virtual ~CCharmapToLocal();
566
567 /* given a Unicode character, get the mapping for the character */
568 unsigned int get_mapping(wchar_t unicode_char) const
569 {
570 unsigned int *subtable;
571
572 /* get the mapping table */
573 subtable = get_sub_table(unicode_char);
574
575 /*
576 * If there is no subtable, return the default character, which is
577 * always at offset zero in the translation array; otherwise, use
578 * the low-order 8 bits of the character code as the index into
579 * the subtable and return the value we find there
580 */
581 if (subtable == 0)
582 return 0;
583 else
584 return subtable[unicode_char & 0xff];
585 }
586
587 /* given a Unicode character, get the expansion for the character */
588 unsigned int get_exp_mapping(wchar_t unicode_char) const
589 {
590 unsigned int *subtable;
591
592 /* get the mapping table */
593 subtable = get_exp_sub_table(unicode_char);
594
595 /*
596 * if there's no subtable, return zero to indicate there's no
597 * expansion; otherwise, return the entry from the subtable
598 */
599 return (subtable == 0 ? 0 : subtable[unicode_char & 0xff]);
600 }
601
602 /*
603 * Get a pointer to the sequence of bytes in the translation array at
604 * a given offset
605 */
606 const unsigned char *get_xlat_ptr(unsigned int ofs) const
607 {
608 return &xlat_array_[ofs];
609 }
610
611 /*
612 * Get a pointer to the translation of a character and the length in
613 * bytes of the translation
614 */
615 const unsigned char *get_xlation(wchar_t unicode_char, size_t *map_len)
616 const
617 {
618 const unsigned char *map;
619
620 /* get the translation offset */
621 map = get_xlat_ptr(get_mapping(unicode_char));
622
623 /* read the length and skip it in the table */
624 *map_len = (size_t)*map++;
625
626 /* return the mapped byte sequence */
627 return map;
628 }
629
630 /*
631 * get a pointer to the sequence of wchar_t values in the expansion
632 * array at a given offset
633 */
634 const wchar_t *get_exp_ptr(unsigned int ofs) const
635 {
636 return &exp_array_[ofs];
637 }
638
639 /* load the mapping table from a file */
640 void load_table(osfildef *fp);
641
642 /*
643 * Given a Unicode character, get the sub-table for the character,
644 * or null if there is no sub-table for this character.
645 */
646 unsigned int *get_sub_table(wchar_t unicode_char) const
647 {
648 /*
649 * use the high-order 8 bits of the unicode character as the
650 * index into the master table
651 */
652 return map_[(unicode_char >> 8) & 0xff];
653 }
654
655 /*
656 * Given a Unicode character, get the expansion sub-table for the
657 * character. or null if there is no sub-table for the character.
658 */
659 unsigned int *get_exp_sub_table(wchar_t unicode_char) const
660 {
661 /*
662 * use the high-order 8 bits of the unicode character as the index
663 * into the master table
664 */
665 return exp_map_[(unicode_char >> 8) & 0xff];
666 }
667
668 /*
669 * Set a mapping. This allocates a new sub-table if necessary, and
670 * stores the local character mapping in the table.
671 */
672 void set_mapping(wchar_t unicode_char, unsigned int xlat_offset);
673
674 /* set an expansion mapping */
675 void set_exp_mapping(wchar_t unicode_char, unsigned int exp_offset);
676
677 /*
678 * The master mapping table list. Each entry points to the
679 * sub-array that contains the mapping for the 256 characters whose
680 * high-order 8 bits give the index into this table. Each entry of
681 * the subarray is the offset within the xlat_array_ byte array of
682 * the first byte of the translation for the unicode character.
683 */
684 unsigned int *map_[256];
685
686 /*
687 * The master expansion mapping list. This works just like map_, but
688 * points to exp_array_ entries for unicode display expansions.
689 */
690 unsigned int *exp_map_[256];
691
692 /*
693 * The translation array. This is an array of bytes containing the
694 * translations. map_[high_8_bits][low_8_bits] contains the offset
695 * within this array of the translation of the character with the
696 * given code ((high_8_bits << 8) + low_8_bits). The first byte at
697 * this offset is the length in bytes of the translation, not
698 * counting the length byte. The remaining bytes are the bytes of
699 * the translation for the character.
700 */
701 unsigned char *xlat_array_;
702
703 /* size of the translation array */
704 size_t xlat_array_size_;
705
706 /*
707 * The expansion array. This is an array of unicode characters
708 * containing the expansions for displaying unicode characters. This
709 * works just like xlat_array_: each entry in expmap_ is an index into
710 * this array, which gives the starting point in the array of the run
711 * of entries for the expansion of that character. The first character
712 * of a run is a length prefix giving the number of characters in the
713 * expansion.
714 */
715 wchar_t *exp_array_;
716 };
717
718
719 /* ======================================================================== */
720 /*
721 * Local character set - to - Unicode UTF-8 mappers
722 */
723
724 /* ------------------------------------------------------------------------ */
725 /*
726 * Trival UTF8-to-UTF8 mapper - performs no conversions. This can be
727 * used when reading from an external data source that is itself in
728 * UTF-8 format; since this is identical to the format we use
729 * internally, no mapping is required.
730 */
731 class CCharmapToUniUTF8: public CCharmapToUni
732 {
733 public:
734 /* read from a file */
735 virtual size_t read_file(class CVmDataSource *fp, char *buf, size_t bufl);
736
737 /* determine if a byte sequence forms a complete character */
738 virtual int is_complete_char(const char *p, size_t len) const
739 {
740 /*
741 * For UTF-8, we can infer the byte length of a character from the
742 * first byte of the sequence. If the given length is at least the
743 * inferred byte length, we have a complete character.
744 */
745 return (len >= utf8_ptr::s_charsize(*p));
746 }
747
748 /* map one character */
749 virtual int mapchar(wchar_t &ch, const char *&p, size_t &len)
750 {
751 /* make sure we have a complete character */
752 size_t clen = utf8_ptr::s_charsize(*p);
753 if (len >= clen)
754 {
755 /* read the character */
756 ch = utf8_ptr::s_getch(p);
757
758 /* advance the input pointers */
759 p += clen;
760 len -= clen;
761
762 /* success */
763 return TRUE;
764 }
765 else
766 {
767 /* incomplete character */
768 return FALSE;
769 }
770 }
771
772 /* map a string */
773 size_t map(char **output_ptr, size_t *output_buf_len,
774 const char *input_ptr, size_t input_len) const
775 {
776 size_t partial_len;
777
778 /*
779 * do the full mapping, discarding the partial last character byte
780 * length information
781 */
782 return map2(output_ptr, output_buf_len, input_ptr, input_len,
783 &partial_len);
784 }
785
786 /* map a string, providing partial character info */
787 virtual size_t map2(char **output_ptr, size_t *output_buf_len,
788 const char *input_ptr, size_t input_len,
789 size_t *partial_len) const;
790
791 protected:
792 /* we don't need a mapping table - ignore any that is set */
793 virtual void set_mapping(wchar_t, wchar_t) { }
794 };
795
796 /* ------------------------------------------------------------------------ */
797 /*
798 * Character mapper base class for UCS-2 to UTF-8. We will subclass
799 * this mapper for big-endian and little-endian UCS-2 representations,
800 * but both mappers are essentially the same in that only format
801 * translation is required, since UCS-2 and UTF-8 use the same code
802 * point mapping (i.e., Unicode).
803 */
804 class CCharmapToUniUcs2: public CCharmapToUni
805 {
806 public:
807 /* read from a file */
808 virtual size_t read_file(class CVmDataSource *fp, char *buf, size_t bufl);
809
810 /* determine if a byte sequence forms a complete character */
811 virtual int is_complete_char(const char *, size_t len) const
812 {
813 /* every character in UCS-2 requires two bytes */
814 return (len >= 2);
815 }
816
817 /* map a string, providing partial character info */
818 virtual size_t map2(char **output_ptr, size_t *output_buf_len,
819 const char *input_ptr, size_t input_len,
820 size_t *partial_len) const
821 {
822 /*
823 * if the input length is odd, there's one byte of partial
824 * character information at the end of the buffer; otherwise
825 * everything is valid
826 */
827 *partial_len = (input_len & 1);
828
829 /* perform the usual mapping */
830 return map(output_ptr, output_buf_len, input_ptr, input_len);
831 }
832
833 protected:
834 /*
835 * there's no mapping table for UCS-2 translations, so we don't need
836 * to do anything with mappings
837 */
838 virtual void set_mapping(wchar_t, wchar_t) { }
839
840 /* temporary buffer for reading files */
841 char inbuf_[512];
842 };
843
844 /* ------------------------------------------------------------------------ */
845 /*
846 * Character mapper for UCS-2 little-endian to UTF-8
847 */
848 class CCharmapToUniUcs2Little: public CCharmapToUniUcs2
849 {
850 public:
851 /* map a string */
852 size_t map(char **output_ptr, size_t *output_buf_len,
853 const char *input_ptr, size_t input_len) const;
854
855 /* map one character */
856 virtual int mapchar(wchar_t &ch, const char *&p, size_t &len)
857 {
858 if (len >= 2)
859 {
860 /* map the character */
861 ch = (unsigned char)*p++;
862 ch |= ((unsigned char)*p++) << 8;
863
864 /* deduct it from the length */
865 len -= 2;
866
867 /* success */
868 return TRUE;
869 }
870 else
871 return FALSE;
872 }
873 };
874
875 /* ------------------------------------------------------------------------ */
876 /*
877 * Character mapper for UCS-2 big-endian to UTF-8
878 */
879 class CCharmapToUniUcs2Big: public CCharmapToUniUcs2
880 {
881 public:
882 /* map a string */
883 size_t map(char **output_ptr, size_t *output_buf_len,
884 const char *input_ptr, size_t input_len) const;
885
886 /* map one character */
887 virtual int mapchar(wchar_t &ch, const char *&p, size_t &len)
888 {
889 if (len >= 2)
890 {
891 /* map the character */
892 ch = ((unsigned char)*p++) << 8;
893 ch |= (unsigned char)*p++;
894
895 /* deduct it from the length */
896 len -= 2;
897
898 /* success */
899 return TRUE;
900 }
901 else
902 return FALSE;
903 }
904 };
905
906 /* ------------------------------------------------------------------------ */
907 /*
908 * Basic character mapper for single-byte character sets to UTF-8
909 */
910 class CCharmapToUniSB_basic: public CCharmapToUni
911 {
912 public:
913 /* read from a single-byte input file, translating to UTF-8 */
914 virtual size_t read_file(class CVmDataSource *fp, char *buf, size_t bufl);
915
916 /* determine if a byte sequence forms a complete character */
917 virtual int is_complete_char(const char *, size_t) const
918 {
919 /*
920 * every character in a single-byte set requires just one byte;
921 * since 'len' is required to be at least one, there's no way we
922 * can't have a complete character
923 */
924 return TRUE;
925 }
926
927 /* map a string, providing partial character info */
928 virtual size_t map2(char **output_ptr, size_t *output_buf_len,
929 const char *input_ptr, size_t input_len,
930 size_t *partial_len) const
931 {
932 /*
933 * for all single-byte character sets, one byte == one character,
934 * so it's impossible to have partial characters
935 */
936 *partial_len = 0;
937
938 /* perform the normal mapping */
939 return map(output_ptr, output_buf_len, input_ptr, input_len);
940 }
941
942 protected:
943 /* temporary buffer for reading files */
944 char inbuf_[512];
945 };
946
947 /* ------------------------------------------------------------------------ */
948 /*
949 * Character mapper for plain ASCII to UTF-8
950 */
951 class CCharmapToUniASCII: public CCharmapToUniSB_basic
952 {
953 public:
954 /* map a string */
955 size_t map(char **output_ptr, size_t *output_buf_len,
956 const char *input_ptr, size_t input_len) const;
957
958 /* map one character */
959 virtual int mapchar(wchar_t &ch, const char *&p, size_t &len)
960 {
961 if (len >= 1)
962 {
963 /* map the character */
964 ch = (unsigned char)*p++;
965
966 /* substitude U+FFFD for any invalid character */
967 if (ch > 127)
968 ch = 0xFFFD;
969
970 /* deduct it from the length */
971 len -= 1;
972
973 /* success */
974 return TRUE;
975 }
976 else
977 return FALSE;
978 }
979
980 protected:
981 /*
982 * there's no map for the ASCII translation, so we can ignore
983 * mapping calls
984 */
985 void set_mapping(wchar_t, wchar_t) { }
986 };
987
988 /* ------------------------------------------------------------------------ */
989 /*
990 * Character mapper for single-byte character sets to UTF-8.
991 */
992 class CCharmapToUniSB: public CCharmapToUniSB_basic
993 {
994 public:
995 CCharmapToUniSB()
996 {
997 int i;
998
999 /* initialize the mapping table to all U+FFFD */
1000 for (i = 0 ; i < 256 ; ++i)
1001 map_[i] = 0xFFFD;
1002 }
1003
1004 /* map a string */
1005 size_t map(char **output_ptr, size_t *output_buf_len,
1006 const char *input_ptr, size_t input_len) const;
1007
1008 /* map one character */
1009 virtual int mapchar(wchar_t &ch, const char *&p, size_t &len)
1010 {
1011 if (len >= 1)
1012 {
1013 /* map the character */
1014 ch = map_[(unsigned char)*p++];
1015
1016 /* deduct it from the length */
1017 len -= 1;
1018
1019 /* success */
1020 return TRUE;
1021 }
1022 else
1023 return FALSE;
1024 }
1025
1026 protected:
1027 /* set a mapping */
1028 void set_mapping(wchar_t uni_code_pt, wchar_t local_code_pt)
1029 {
1030 /*
1031 * set the mapping, ignoring characters outside of our 8-bit
1032 * range
1033 */
1034 if (((unsigned int)local_code_pt) < 256)
1035 map_[local_code_pt] = uni_code_pt;
1036 }
1037
1038 private:
1039 /*
1040 * our mapping table - since the source character set is
1041 * single-byte, we need only store a wchar_t for each of the
1042 * possible 256 source characters
1043 */
1044 wchar_t map_[256];
1045 };
1046
1047 /* ------------------------------------------------------------------------ */
1048 /*
1049 * Character mapper for mixed multi-byte character sets to UTF-8. This
1050 * maps from local character sets that use a mixture of one-byte and
1051 * two-byte sequences to represent characters.
1052 */
1053
1054 /*
1055 * Primary-byte mapping table entry. This gives us mapping instructions
1056 * for each leading byte of a character sequence.
1057 *
1058 * Each character is represented by a one-byte or two-byte sequence. This
1059 * mapper assumes a context-free mapping, hence for each character
1060 * represented by a single byte, that single byte unambiguously indicates
1061 * that character, and hence is never the first byte of a two-byte
1062 * sequence. For each character represented by a two-byte sequence, the
1063 * first byte of the sequence can only be part of two-byte sequences, hence
1064 * whenever we see that first byte we'll know for sure we have a two-byte
1065 * character.
1066 *
1067 * Each mapping here is for a first byte. If the byte is a single-byte
1068 * character, then the 'sub' pointer is null and the 'ch' entry gives the
1069 * Unicode code point for the character. If the byte is the lead byte of
1070 * one or more two-byte characters, then the 'sub' pointer is non-null and
1071 * 'ch' is ignored.
1072 */
1073 struct cmap_mb_entry
1074 {
1075 /*
1076 * The sub-mapping table. This is a pointer to a table of the Unicode
1077 * code points of the two-byte sequences that start with this byte.
1078 * Each entry in the array is a Unicode code point, and the array is
1079 * indexed by the second byte of the two-byte sequence. If this
1080 * pointer is null, then this lead byte is a single-byte character.
1081 *
1082 * Note that this pointer, if non-null, always points to a 256-element
1083 * array. This array can thus be indexed directly with any unsigned
1084 * 8-bit byte value without any range checking.
1085 */
1086 wchar_t *sub;
1087
1088 /*
1089 * The Unicode code point of this character, if this primary byte is a
1090 * one-byte character.
1091 */
1092 wchar_t ch;
1093 };
1094
1095
1096 /*
1097 * The multi-byte-to-UTF8 mapper
1098 */
1099 class CCharmapToUniMB: public CCharmapToUni
1100 {
1101 public:
1102 CCharmapToUniMB();
1103
1104 /* delete the table */
1105 virtual ~CCharmapToUniMB();
1106
1107 /* determine if a byte sequence forms a complete character */
1108 virtual int is_complete_char(const char *p, size_t len) const
1109 {
1110 /*
1111 * Check the first byte to see if this is a leading byte or a
1112 * stand-alone single byte.
1113 */
1114 if (map_[(unsigned char)*p].sub == 0)
1115 {
1116 /*
1117 * it's a stand-alone byte, so the character length is one;
1118 * 'len' is required to be at least 1, so we definitely have a
1119 * complete character
1120 */
1121 return TRUE;
1122 }
1123 else
1124 {
1125 /* it's a lead byte, so the character length is two */
1126 return (len >= 2);
1127 }
1128 }
1129
1130 /* map one character */
1131 virtual int mapchar(wchar_t &ch, const char *&p, size_t &len)
1132 {
1133 /* get the first byte's translation entry */
1134 cmap_mb_entry e = map_[(unsigned char)*p];
1135
1136 /* check if we have a single-byte or double-byte character */
1137 if (e.sub != 0)
1138 {
1139 /* double-byte character */
1140 if (len >= 2)
1141 {
1142 /* get the second character mapping */
1143 ch = e.sub[(unsigned char)*++p];
1144
1145 /* skip the two bytes */
1146 p += 1;
1147 len -= 2;
1148
1149 /* success */
1150 return TRUE;
1151 }
1152 }
1153 else
1154 {
1155 /* single-byte character */
1156 if (len >= 1)
1157 {
1158 /* get the mapping */
1159 ch = e.ch;
1160
1161 /* skip the character */
1162 p += 1;
1163 len -= 1;
1164
1165 /* success */
1166 return TRUE;
1167 }
1168 }
1169
1170 /* if we made it this far, we don't have a complete character */
1171 return FALSE;
1172 }
1173
1174 /* read from a multi-byte input file, translating to UTF-8 */
1175 virtual size_t read_file(class CVmDataSource *fp, char *buf, size_t bufl);
1176
1177 /* map a string */
1178 size_t map(char **output_ptr, size_t *output_buf_len,
1179 const char *input_ptr, size_t input_len) const
1180 {
1181 size_t partial_len;
1182
1183 /*
1184 * do the full mapping, discarding the partial last character byte
1185 * length information
1186 */
1187 return map2(output_ptr, output_buf_len, input_ptr, input_len,
1188 &partial_len);
1189 }
1190
1191 /* map a string, providing partial character info */
1192 virtual size_t map2(char **output_ptr, size_t *output_buf_len,
1193 const char *input_ptr, size_t input_len,
1194 size_t *partial_len) const;
1195
1196 protected:
1197 /* set a mapping */
1198 void set_mapping(wchar_t uni_code_pt, wchar_t local_code_pt);
1199
1200 private:
1201 /* the primary-byte mapping table */
1202 cmap_mb_entry map_[256];
1203
1204 /* temporary buffer for reading files */
1205 char inbuf_[512];
1206 };
1207
1208 /* ------------------------------------------------------------------------ */
1209 /*
1210 * Character mapper for double-byte character sets to UTF-8. This maps
1211 * from local character sets that use a two-byte sequence to represent each
1212 * local character.
1213 *
1214 * For now, this is a trivial subclass of the multi-byte mapper; that
1215 * mapper handles the more general case where each character can have a
1216 * different byte length, so it readily handles the case where every
1217 * character is two bytes. If there's ever any demand for it, a
1218 * special-case subclass to handle double-byte character sets specifically
1219 * could provide slight efficiency gains, since it wouldn't have to check
1220 * each lead byte to determine the character sequence length. I don't
1221 * expect this will be needed, though: there just aren't many fixed
1222 * double-byte character sets in use (apart from the UCS2 sets, which we
1223 * already have special handling for), and there's little chance of new
1224 * ones arising in the future since Unicode is rapidly making the whole
1225 * idea of vendor character sets obsolete.
1226 */
1227 class CCharmapToUniDB: public CCharmapToUniMB
1228 {
1229 public:
1230 };
1231
1232 /* ------------------------------------------------------------------------ */
1233 /*
1234 * Character mapper for plain ISO-8859-1 to UTF-8
1235 */
1236 class CCharmapToUni8859_1: public CCharmapToUniSB
1237 {
1238 public:
1239 /* creation */
1240 CCharmapToUni8859_1()
1241 {
1242 wchar_t i;
1243
1244 /*
1245 * Initialize our mapping table. Each 8859-1 code point maps to
1246 * the same code point in Unicode, so this is a trivial
1247 * translation.
1248 */
1249 for (i = 0 ; i < 256 ; ++i)
1250 set_mapping(i, i);
1251 }
1252 };
1253
1254 /* ======================================================================== */
1255 /*
1256 * Unicode UTF-8 - to - local character set mappers
1257 */
1258
1259 /* ------------------------------------------------------------------------ */
1260 /*
1261 * Trivial character mapper for UTF8-to-UTF8 conversions. This can be
1262 * used when writing external data in UTF8 format; since this is the
1263 * same format we use internally, no conversion is required.
1264 */
1265 class CCharmapToLocalUTF8: public CCharmapToLocal
1266 {
1267 public:
1268 /* map a string */
1269 virtual size_t map_utf8(char *dest, size_t dest_len,
1270 utf8_ptr src, size_t src_byte_len,
1271 size_t *src_bytes_used) const;
1272
1273 /* map a null-terminated string */
1274 virtual size_t map_utf8z(char *dest, size_t dest_len,
1275 utf8_ptr src) const;
1276
1277 /* map a character */
1278 size_t map(wchar_t unicode_char, char **output_ptr,
1279 size_t *output_len) const;
1280
1281 /*
1282 * determine if the given Unicode character has a mapping to the local
1283 * character set
1284 */
1285 virtual int is_mappable(wchar_t unicode_char) const
1286 {
1287 /* every character can be mapped UTF8-to-UTF8, obviously */
1288 return TRUE;
1289 }
1290 };
1291
1292 /* ------------------------------------------------------------------------ */
1293 /*
1294 * Character mapper for single-byte character sets. Each character in
1295 * the local (output) character set is represented by a single byte.
1296 */
1297 class CCharmapToLocalSB: public CCharmapToLocal
1298 {
1299 public:
1300 /* map a string */
1301 virtual size_t map_utf8(char *dest, size_t dest_len,
1302 utf8_ptr src, size_t src_byte_len,
1303 size_t *src_bytes_used) const;
1304
1305 /* map a null-terminated string */
1306 virtual size_t map_utf8z(char *dest, size_t dest_len,
1307 utf8_ptr src) const;
1308
1309 /* map a character */
1310 size_t map(wchar_t unicode_char, char **output_ptr,
1311 size_t *output_len) const;
1312 };
1313
1314
1315 /* ------------------------------------------------------------------------ */
1316 /*
1317 * Mixed multi-byte mapper. Each local character is represented by a
1318 * sequence of one or more bytes.
1319 *
1320 * This class is a trivial subclass of CCharmapToLocalSB. The single-byte
1321 * base class already does everything we need to do, because it is designed
1322 * to cope with mappings that involve expansions that represent a single
1323 * Unicode character with a sequence of local characters (for example,
1324 * "(c)" for the copyright symbol).
1325 */
1326 class CCharmapToLocalMB: public CCharmapToLocalSB
1327 {
1328 public:
1329 };
1330
1331 /*
1332 * Double-byte mapper. Each local character is represented by exactly two
1333 * bytes. This class is a trivial subclass of CCharmapToLocalMB, because
1334 * the multi-byte mapper already handles the more general case of local
1335 * character representations that use varying byte lengths; there is no
1336 * particular efficiency gain to be had by creating a separate special-case
1337 * class for double-byte character sets.
1338 */
1339 class CCharmapToLocalDB: public CCharmapToLocalMB
1340 {
1341 public:
1342 };
1343
1344
1345 /* ------------------------------------------------------------------------ */
1346 /*
1347 * Character mapper for mapping to local default 7-bit ASCII. This
1348 * mapper is has a built-in character set translation so that we can
1349 * always create one without having to find an external mapping file.
1350 */
1351 class CCharmapToLocalASCII: public CCharmapToLocalSB
1352 {
1353 public:
1354 CCharmapToLocalASCII();
1355 };
1356
1357
1358 /*
1359 * Character mapper for mapping to local ISO-8859-1. This mapper has a
1360 * built-in character set translation so that we can always create one
1361 * even without an external mapping file.
1362 */
1363 class CCharmapToLocal8859_1: public CCharmapToLocalSB
1364 {
1365 public:
1366 CCharmapToLocal8859_1();
1367 };
1368
1369 /* ------------------------------------------------------------------------ */
1370 /*
1371 * Character mapper for 16-bit Wide Unicode local character set. Stores
1372 * characters in the correct local wchar_t representation. Assumes that
1373 * the pointer is wchar_t-aligned.
1374 *
1375 * This is a trival translation. Because we're mapping from Unicode to
1376 * Unicode, the only thing we're changing is the encoding format - the
1377 * character code is simply copied without any translation, since
1378 * Unicode is the same everywhere.
1379 */
1380 class CCharmapToLocalWideUnicode: public CCharmapToLocal
1381 {
1382 public:
1383 /* map a string */
1384 virtual size_t map_utf8(char *dest, size_t dest_len,
1385 utf8_ptr src, size_t src_byte_len,
1386 size_t *src_bytes_used) const;
1387
1388 /* map a null-terminated string */
1389 virtual size_t map_utf8z(char *dest, size_t dest_len,
1390 utf8_ptr src) const;
1391
1392 /* map a character */
1393 size_t map(wchar_t unicode_char, char **output_ptr,
1394 size_t *output_len) const;
1395
1396 /*
1397 * determine if the given Unicode character has a mapping to the local
1398 * character set
1399 */
1400 virtual int is_mappable(wchar_t unicode_char) const
1401 {
1402 /* every character can be mapped UTF8-to-UCS2 */
1403 return TRUE;
1404 }
1405 };
1406
1407 /* ------------------------------------------------------------------------ */
1408 /*
1409 * Character mapper for 16-bit Wide Unicode, big-endian. Stores the
1410 * characters in big-endian UCS-2 representation.
1411 */
1412 class CCharmapToLocalUcs2Big: public CCharmapToLocal
1413 {
1414 public:
1415 /* map a string */
1416 virtual size_t map_utf8(char *dest, size_t dest_len,
1417 utf8_ptr src, size_t src_byte_len,
1418 size_t *src_bytes_used) const;
1419
1420 /* map a null-terminated string */
1421 virtual size_t map_utf8z(char *dest, size_t dest_len,
1422 utf8_ptr src) const;
1423
1424 /* map a character */
1425 size_t map(wchar_t unicode_char, char **output_ptr,
1426 size_t *output_len) const;
1427 };
1428
1429 /* ------------------------------------------------------------------------ */
1430 /*
1431 * Character mapper for 16-bit Wide Unicode, little-endian. Stores the
1432 * characters in little-endian UCS-2 representation.
1433 */
1434 class CCharmapToLocalUcs2Little: public CCharmapToLocal
1435 {
1436 public:
1437 /* map a string */
1438 virtual size_t map_utf8(char *dest, size_t dest_len,
1439 utf8_ptr src, size_t src_byte_len,
1440 size_t *src_bytes_used) const;
1441
1442 /* map a null-terminated string */
1443 virtual size_t map_utf8z(char *dest, size_t dest_len,
1444 utf8_ptr src) const;
1445
1446 /* map a character */
1447 size_t map(wchar_t unicode_char, char **output_ptr,
1448 size_t *output_len) const;
1449 };
1450
1451
1452 #endif /* CHARMAP_H */