… | |
… | |
36 | char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); |
36 | char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); |
37 | |
37 | |
38 | char *dst = r; |
38 | char *dst = r; |
39 | while (len--) |
39 | while (len--) |
40 | { |
40 | { |
41 | int l = wcrtomb (dst, *str++, mbs); |
41 | ssize_t l = wcrtomb (dst, *str++, mbs); |
42 | if (l < 0) |
42 | if (l < 0) |
43 | *dst++ = '?'; |
43 | *dst++ = '?'; |
44 | else |
44 | else |
45 | dst += l; |
45 | dst += l; |
46 | } |
46 | } |
47 | |
47 | |
48 | *dst++ = 0; |
48 | *dst++ = 0; |
49 | |
49 | |
50 | return r; |
50 | return (char *)rxvt_realloc (r, dst - r); |
51 | } |
51 | } |
52 | |
52 | |
53 | wchar_t * |
53 | wchar_t * |
54 | rxvt_mbstowcs (const char *str, int len) |
54 | rxvt_mbstowcs (const char *str, int len) |
55 | { |
55 | { |
56 | if (len < 0) len = strlen (str); |
56 | if (len < 0) len = strlen (str); |
57 | |
57 | |
58 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); |
58 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); |
59 | |
59 | |
60 | if (mbstowcs (r, str, len + 1) < 0) |
60 | if ((ssize_t)mbstowcs (r, str, len + 1) < 0) |
61 | *r = 0; |
61 | *r = 0; |
62 | |
62 | |
63 | return r; |
63 | return r; |
64 | } |
64 | } |
65 | |
65 | |
… | |
… | |
71 | char *r = (char *)rxvt_malloc (len * 4 + 1); |
71 | char *r = (char *)rxvt_malloc (len * 4 + 1); |
72 | char *p = r; |
72 | char *p = r; |
73 | |
73 | |
74 | while (len--) |
74 | while (len--) |
75 | { |
75 | { |
76 | unicode_t w = *str++; |
76 | unicode_t w = *str++ & UNICODE_MASK; |
77 | |
77 | |
78 | if (w < 0x000080) |
78 | if (w < 0x000080) |
79 | *p++ = w; |
79 | *p++ = w; |
80 | else if (w < 0x000800) |
80 | else if (w < 0x000800) |
81 | *p++ = 0xc0 | ( w >> 6), |
81 | *p++ = 0xc0 | ( w >> 6), |
… | |
… | |
91 | *p++ = 0x80 | ( w & 0x3f); |
91 | *p++ = 0x80 | ( w & 0x3f); |
92 | else |
92 | else |
93 | *p++ = '?'; |
93 | *p++ = '?'; |
94 | } |
94 | } |
95 | |
95 | |
96 | *p = 0; |
96 | *p++ = 0; |
97 | |
97 | |
98 | return r; |
98 | return (char *)rxvt_realloc (r, p - r); |
99 | } |
99 | } |
100 | |
100 | |
101 | wchar_t * |
101 | wchar_t * |
102 | rxvt_utf8towcs (const char *str, int len) |
102 | rxvt_utf8towcs (const char *str, int len) |
103 | { |
103 | { |
104 | if (len < 0) len = strlen (str); |
104 | if (len < 0) len = strlen (str); |
105 | |
105 | |
106 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); |
106 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)), |
107 | wchar_t *p = r; |
107 | *p = r; |
108 | |
108 | |
109 | unsigned char *s = (unsigned char *)str; |
109 | unsigned char *s = (unsigned char *)str, |
|
|
110 | *e = s + len; |
110 | |
111 | |
111 | while (len--) |
112 | for (;;) |
112 | { |
113 | { |
|
|
114 | len = e - s; |
|
|
115 | |
|
|
116 | if (len == 0) |
|
|
117 | break; |
113 | if (s[0] < 0x80) |
118 | else if (s[0] < 0x80) |
114 | *p++ = *s++; |
119 | *p++ = *s++; |
115 | else if (len > 0 |
120 | else if (len >= 2 |
116 | && s[0] >= 0xc2 && s[0] <= 0xdf |
121 | && s[0] >= 0xc2 && s[0] <= 0xdf |
117 | && (s[1] & 0xc0) == 0x80) |
122 | && (s[1] & 0xc0) == 0x80) |
118 | { |
123 | { |
119 | *p++ = ((s[0] & 0x1f) << 6) |
124 | *p++ = ((s[0] & 0x1f) << 6) |
120 | | (s[1] & 0x3f); |
125 | | (s[1] & 0x3f); |
121 | s += 2; |
126 | s += 2; |
122 | } |
127 | } |
123 | else if (len > 1 |
128 | else if (len >= 3 |
124 | && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) |
129 | && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) |
125 | || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf) |
130 | || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf) |
126 | || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) |
131 | || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) |
127 | || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf) |
132 | || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf) |
128 | ) |
133 | ) |
… | |
… | |
131 | *p++ = ((s[0] & 0x0f) << 12) |
136 | *p++ = ((s[0] & 0x0f) << 12) |
132 | | ((s[1] & 0x3f) << 6) |
137 | | ((s[1] & 0x3f) << 6) |
133 | | (s[2] & 0x3f); |
138 | | (s[2] & 0x3f); |
134 | s += 3; |
139 | s += 3; |
135 | } |
140 | } |
136 | else if (len > 2 |
141 | else if (len >= 4 |
137 | && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) |
142 | && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) |
138 | || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf) |
143 | || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf) |
139 | || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) |
144 | || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) |
140 | ) |
145 | ) |
141 | && (s[2] & 0xc0) == 0x80 |
146 | && (s[2] & 0xc0) == 0x80 |