~ubuntu-branches/ubuntu/vivid/mutt/vivid-updates

« back to all changes in this revision

Viewing changes to wcwidth.c

Tags: 1.5.18-6
* Grab from upstream:
  + Guard idata->mailbox read in imap_mailbox_state.
    The bug happens when a user has more than a imap mailbox, when
    he/she browses through them mutt will segfault.
    (Closes: #462266, #513230, #514309. Mutt: #3057)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * This is an implementation of wcwidth() and wcswidth() as defined in
3
 
 * "The Single UNIX Specification, Version 2, The Open Group, 1997"
4
 
 * <http://www.UNIX-systems.org/online.html>
5
 
 *
6
 
 * Markus Kuhn -- 2000-02-08 -- public domain
 
2
 * This is an implementation of wcwidth() and wcswidth() (defined in
 
3
 * IEEE Std 1002.1-2001) for Unicode.
 
4
 *
 
5
 * http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
 
6
 * http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
 
7
 *
 
8
 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
 
9
 *
 
10
 * Permission to use, copy, modify, and distribute this software
 
11
 * for any purpose and without fee is hereby granted. The author
 
12
 * disclaims all warranties with regard to this software.
 
13
 *
 
14
 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
7
15
 */
8
16
 
9
 
/* Adapted for Mutt by Edmund Grimley Evans.
 
17
/* Changes made for mutt:
 
18
 * - Adapted for Mutt by Edmund Grimley Evans.
 
19
 * - Changed 'first'/'last' members of combined[] to wchar_t from
 
20
 *   unsigned short to fix compiler warnings, 2007-11-13, Rocco Rutte
10
21
 */
11
22
 
12
23
#if HAVE_CONFIG_H
20
31
 
21
32
#include <ctype.h>
22
33
 
23
 
/* These functions define the column width of an ISO 10646 character
24
 
 * as follows:
 
34
/* The following two functions define the column width of an ISO 10646
 
35
 * character as follows:
25
36
 *
26
37
 *    - The null character (U+0000) has a column width of 0.
27
38
 *
32
43
 *      category code Mn or Me in the Unicode database) have a
33
44
 *      column width of 0.
34
45
 *
 
46
 *    - SOFT HYPHEN (U+00AD) has a column width of 1.
 
47
 *
 
48
 *    - Other format characters (general category code Cf in the Unicode
 
49
 *      database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
 
50
 *
 
51
 *    - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
 
52
 *      have a column width of 0.
 
53
 *
35
54
 *    - Spacing characters in the East Asian Wide (W) or East Asian
36
 
 *      FullWidth (F) category as defined in Unicode Technical
 
55
 *      Full-width (F) category as defined in Unicode Technical
37
56
 *      Report #11 have a column width of 2.
38
57
 *
39
58
 *    - All remaining characters (including all printable
47
66
int wcwidth_ucs(wchar_t ucs)
48
67
{
49
68
  /* sorted list of non-overlapping intervals of non-spacing characters */
 
69
  /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
50
70
  static const struct interval {
51
 
    unsigned short first;
52
 
    unsigned short last;
 
71
    wchar_t first;
 
72
    wchar_t last;
53
73
  } combining[] = {
54
 
    { 0x0300, 0x034E }, { 0x0360, 0x0362 }, { 0x0483, 0x0486 },
55
 
    { 0x0488, 0x0489 }, { 0x0591, 0x05A1 }, { 0x05A3, 0x05B9 },
56
 
    { 0x05BB, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
57
 
    { 0x05C4, 0x05C4 }, { 0x064B, 0x0655 }, { 0x0670, 0x0670 },
58
 
    { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
59
 
    { 0x0711, 0x0711 }, { 0x0730, 0x074A }, { 0x07A6, 0x07B0 },
60
 
    { 0x0901, 0x0902 }, { 0x093C, 0x093C }, { 0x0941, 0x0948 },
61
 
    { 0x094D, 0x094D }, { 0x0951, 0x0954 }, { 0x0962, 0x0963 },
62
 
    { 0x0981, 0x0981 }, { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 },
63
 
    { 0x09CD, 0x09CD }, { 0x09E2, 0x09E3 }, { 0x0A02, 0x0A02 },
64
 
    { 0x0A3C, 0x0A3C }, { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 },
65
 
    { 0x0A4B, 0x0A4D }, { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 },
66
 
    { 0x0ABC, 0x0ABC }, { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 },
67
 
    { 0x0ACD, 0x0ACD }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C },
68
 
    { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D },
69
 
    { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 },
70
 
    { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 },
71
 
    { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBF, 0x0CBF },
72
 
    { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD }, { 0x0D41, 0x0D43 },
73
 
    { 0x0D4D, 0x0D4D }, { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 },
74
 
    { 0x0DD6, 0x0DD6 }, { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A },
75
 
    { 0x0E47, 0x0E4E }, { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 },
76
 
    { 0x0EBB, 0x0EBC }, { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 },
77
 
    { 0x0F35, 0x0F35 }, { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 },
78
 
    { 0x0F71, 0x0F7E }, { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 },
79
 
    { 0x0F90, 0x0F97 }, { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 },
80
 
    { 0x102D, 0x1030 }, { 0x1032, 0x1032 }, { 0x1036, 0x1037 },
81
 
    { 0x1039, 0x1039 }, { 0x1058, 0x1059 }, { 0x17B7, 0x17BD },
82
 
    { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x18A9, 0x18A9 },
83
 
    { 0x20D0, 0x20E3 }, { 0x302A, 0x302F }, { 0x3099, 0x309A },
84
 
    { 0xFB1E, 0xFB1E }, { 0xFE20, 0xFE23 }
 
74
    { 0x0300, 0x036f }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 },
 
75
    { 0x0591, 0x05bd }, { 0x05bf, 0x05bf }, { 0x05c1, 0x05c2 },
 
76
    { 0x05c4, 0x05c5 }, { 0x05c7, 0x05c7 }, { 0x0600, 0x0603 },
 
77
    { 0x0610, 0x0615 }, { 0x064b, 0x065e }, { 0x0670, 0x0670 },
 
78
    { 0x06d6, 0x06e4 }, { 0x06e7, 0x06e8 }, { 0x06ea, 0x06ed },
 
79
    { 0x070f, 0x070f }, { 0x0711, 0x0711 }, { 0x0730, 0x074a },
 
80
    { 0x07a6, 0x07b0 }, { 0x07eb, 0x07f3 }, { 0x0901, 0x0902 },
 
81
    { 0x093c, 0x093c }, { 0x0941, 0x0948 }, { 0x094d, 0x094d },
 
82
    { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 },
 
83
    { 0x09bc, 0x09bc }, { 0x09c1, 0x09c4 }, { 0x09cd, 0x09cd },
 
84
    { 0x09e2, 0x09e3 }, { 0x0a01, 0x0a02 }, { 0x0a3c, 0x0a3c },
 
85
    { 0x0a41, 0x0a42 }, { 0x0a47, 0x0a48 }, { 0x0a4b, 0x0a4d },
 
86
    { 0x0a70, 0x0a71 }, { 0x0a81, 0x0a82 }, { 0x0abc, 0x0abc },
 
87
    { 0x0ac1, 0x0ac5 }, { 0x0ac7, 0x0ac8 }, { 0x0acd, 0x0acd },
 
88
    { 0x0ae2, 0x0ae3 }, { 0x0b01, 0x0b01 }, { 0x0b3c, 0x0b3c },
 
89
    { 0x0b3f, 0x0b3f }, { 0x0b41, 0x0b43 }, { 0x0b4d, 0x0b4d },
 
90
    { 0x0b56, 0x0b56 }, { 0x0b82, 0x0b82 }, { 0x0bc0, 0x0bc0 },
 
91
    { 0x0bcd, 0x0bcd }, { 0x0c3e, 0x0c40 }, { 0x0c46, 0x0c48 },
 
92
    { 0x0c4a, 0x0c4d }, { 0x0c55, 0x0c56 }, { 0x0cbc, 0x0cbc },
 
93
    { 0x0cbf, 0x0cbf }, { 0x0cc6, 0x0cc6 }, { 0x0ccc, 0x0ccd },
 
94
    { 0x0ce2, 0x0ce3 }, { 0x0d41, 0x0d43 }, { 0x0d4d, 0x0d4d },
 
95
    { 0x0dca, 0x0dca }, { 0x0dd2, 0x0dd4 }, { 0x0dd6, 0x0dd6 },
 
96
    { 0x0e31, 0x0e31 }, { 0x0e34, 0x0e3a }, { 0x0e47, 0x0e4e },
 
97
    { 0x0eb1, 0x0eb1 }, { 0x0eb4, 0x0eb9 }, { 0x0ebb, 0x0ebc },
 
98
    { 0x0ec8, 0x0ecd }, { 0x0f18, 0x0f19 }, { 0x0f35, 0x0f35 },
 
99
    { 0x0f37, 0x0f37 }, { 0x0f39, 0x0f39 }, { 0x0f71, 0x0f7e },
 
100
    { 0x0f80, 0x0f84 }, { 0x0f86, 0x0f87 }, { 0x0f90, 0x0f97 },
 
101
    { 0x0f99, 0x0fbc }, { 0x0fc6, 0x0fc6 }, { 0x102d, 0x1030 },
 
102
    { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 },
 
103
    { 0x1058, 0x1059 }, { 0x1160, 0x11ff }, { 0x135f, 0x135f },
 
104
    { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 },
 
105
    { 0x1772, 0x1773 }, { 0x17b4, 0x17b5 }, { 0x17b7, 0x17bd },
 
106
    { 0x17c6, 0x17c6 }, { 0x17c9, 0x17d3 }, { 0x17dd, 0x17dd },
 
107
    { 0x180b, 0x180d }, { 0x18a9, 0x18a9 }, { 0x1920, 0x1922 },
 
108
    { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193b },
 
109
    { 0x1a17, 0x1a18 }, { 0x1b00, 0x1b03 }, { 0x1b34, 0x1b34 },
 
110
    { 0x1b36, 0x1b3a }, { 0x1b3c, 0x1b3c }, { 0x1b42, 0x1b42 },
 
111
    { 0x1b6b, 0x1b73 }, { 0x1dc0, 0x1dca }, { 0x1dfe, 0x1dff },
 
112
    { 0x200b, 0x200f }, { 0x202a, 0x202e }, { 0x2060, 0x2063 },
 
113
    { 0x206a, 0x206f }, { 0x20d0, 0x20ef }, { 0x302a, 0x302f },
 
114
    { 0x3099, 0x309a }, { 0xa806, 0xa806 }, { 0xa80b, 0xa80b },
 
115
    { 0xa825, 0xa826 }, { 0xfb1e, 0xfb1e }, { 0xfe00, 0xfe0f },
 
116
    { 0xfe20, 0xfe23 }, { 0xfeff, 0xfeff }, { 0xfff9, 0xfffb },
 
117
    { 0x10a01, 0x10a03 }, { 0x10a05, 0x10a06 }, { 0x10a0c, 0x10a0f },
 
118
    { 0x10a38, 0x10a3a }, { 0x10a3f, 0x10a3f }, { 0x1d167, 0x1d169 },
 
119
    { 0x1d173, 0x1d182 }, { 0x1d185, 0x1d18b }, { 0x1d1aa, 0x1d1ad },
 
120
    { 0x1d242, 0x1d244 }, { 0xe0001, 0xe0001 }, { 0xe0020, 0xe007f },
 
121
    { 0xe0100, 0xe01ef }
85
122
  };
86
123
  int min = 0;
87
124
  int max = sizeof(combining) / sizeof(struct interval) - 1;
88
125
  int mid;
89
126
 
 
127
  /* test for 8-bit control characters */
90
128
  if (ucs == 0)
91
129
    return 0;
92
 
 
93
 
  /* test for 8-bit control characters */
94
130
  if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
95
131
    return -1;
96
132
 
116
152
    return 1;
117
153
 
118
154
  return 1 +
119
 
    ((ucs >= 0x1100 && ucs <= 0x115f) || /* Hangul Jamo */
120
 
     (ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
121
 
      ucs != 0x303f) ||                  /* CJK ... Yi */
122
 
     (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
123
 
     (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
124
 
     (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
125
 
     (ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
126
 
     (ucs >= 0xffe0 && ucs <= 0xffe6));
 
155
    (ucs >= 0x1100 &&
 
156
     (ucs <= 0x115f ||                    /* Hangul Jamo init. consonants */
 
157
      ucs == 0x2329 || ucs == 0x232a ||
 
158
      (ucs >= 0x2e80 && ucs <= 0xa4cf &&
 
159
       ucs != 0x303f) ||                  /* CJK ... Yi */
 
160
      (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
 
161
      (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
 
162
      (ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */
 
163
      (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
 
164
      (ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */
 
165
      (ucs >= 0xffe0 && ucs <= 0xffe6) ||
 
166
      (ucs >= 0x20000 && ucs <= 0x2fffd) ||
 
167
      (ucs >= 0x30000 && ucs <= 0x3fffd)));
127
168
}
128
169
 
129
170
#endif /* !HAVE_WC_FUNCS */