1 /****************************************************************************
2  * Copyright (c) 2013,2014 Free Software Foundation, Inc.                   *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *  Author: Thomas E. Dickey                        2013-on                 *
31  ****************************************************************************/
32 
33 /*
34 **	Support for obsolete/unusual features.
35 */
36 
37 #include <curses.priv.h>
38 
39 MODULE_ID("$Id: obsolete.c,v 1.3 2014/10/11 02:39:35 tom Exp $")
40 
41 /*
42  * Obsolete entrypoint retained for binary compatbility.
43  */
44 NCURSES_EXPORT(void)
45 NCURSES_SP_NAME(_nc_set_buffer) (NCURSES_SP_DCLx FILE *ofp, int buffered)
46 {
47 #if NCURSES_SP_FUNCS
48     (void) SP_PARM;
49 #endif
50     (void) ofp;
51     (void) buffered;
52 }
53 
54 #if NCURSES_SP_FUNCS
55 NCURSES_EXPORT(void)
56 _nc_set_buffer(FILE *ofp, int buffered)
57 {
58     NCURSES_SP_NAME(_nc_set_buffer) (CURRENT_SCREEN, ofp, buffered);
59 }
60 #endif
61 
62 #if !HAVE_STRDUP
63 NCURSES_EXPORT(char *)
64 _nc_strdup(const char *s)
65 {
66     char *result = 0;
67     if (s != 0) {
68 	size_t need = strlen(s);
69 	result = malloc(need + 1);
70 	if (result != 0) {
71 	    strcpy(result, s);
72 	}
73     }
74     return result;
75 }
76 #endif
77 
78 #if USE_MY_MEMMOVE
79 #define DST ((char *)s1)
80 #define SRC ((const char *)s2)
81 NCURSES_EXPORT(void *)
82 _nc_memmove(void *s1, const void *s2, size_t n)
83 {
84     if (n != 0) {
85 	if ((DST + n > SRC) && (SRC + n > DST)) {
86 	    static char *bfr;
87 	    static size_t length;
88 	    register size_t j;
89 	    if (length < n) {
90 		length = (n * 3) / 2;
91 		bfr = typeRealloc(char, length, bfr);
92 	    }
93 	    for (j = 0; j < n; j++)
94 		bfr[j] = SRC[j];
95 	    s2 = bfr;
96 	}
97 	while (n-- != 0)
98 	    DST[n] = SRC[n];
99     }
100     return s1;
101 }
102 #endif /* USE_MY_MEMMOVE */
103 
104 #ifdef EXP_XTERM_1005
105 NCURSES_EXPORT(int)
106 _nc_conv_to_utf8(unsigned char *target, unsigned source, unsigned limit)
107 {
108 #define CH(n) UChar((source) >> ((n) * 8))
109     int rc = 0;
110 
111     if (source <= 0x0000007f)
112 	rc = 1;
113     else if (source <= 0x000007ff)
114 	rc = 2;
115     else if (source <= 0x0000ffff)
116 	rc = 3;
117     else if (source <= 0x001fffff)
118 	rc = 4;
119     else if (source <= 0x03ffffff)
120 	rc = 5;
121     else			/* (source <= 0x7fffffff) */
122 	rc = 6;
123 
124     if ((unsigned) rc > limit) {	/* whatever it is, we cannot decode it */
125 	rc = 0;
126     }
127 
128     if (target != 0) {
129 	switch (rc) {
130 	case 1:
131 	    target[0] = CH(0);
132 	    break;
133 
134 	case 2:
135 	    target[1] = UChar(0x80 | (CH(0) & 0x3f));
136 	    target[0] = UChar(0xc0 | (CH(0) >> 6) | ((CH(1) & 0x07) << 2));
137 	    break;
138 
139 	case 3:
140 	    target[2] = UChar(0x80 | (CH(0) & 0x3f));
141 	    target[1] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
142 	    target[0] = UChar(0xe0 | ((int) (CH(1) & 0xf0) >> 4));
143 	    break;
144 
145 	case 4:
146 	    target[3] = UChar(0x80 | (CH(0) & 0x3f));
147 	    target[2] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
148 	    target[1] = UChar(0x80 |
149 			      ((int) (CH(1) & 0xf0) >> 4) |
150 			      ((int) (CH(2) & 0x03) << 4));
151 	    target[0] = UChar(0xf0 | ((int) (CH(2) & 0x1f) >> 2));
152 	    break;
153 
154 	case 5:
155 	    target[4] = UChar(0x80 | (CH(0) & 0x3f));
156 	    target[3] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
157 	    target[2] = UChar(0x80 |
158 			      ((int) (CH(1) & 0xf0) >> 4) |
159 			      ((int) (CH(2) & 0x03) << 4));
160 	    target[1] = UChar(0x80 | (CH(2) >> 2));
161 	    target[0] = UChar(0xf8 | (CH(3) & 0x03));
162 	    break;
163 
164 	case 6:
165 	    target[5] = UChar(0x80 | (CH(0) & 0x3f));
166 	    target[4] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2));
167 	    target[3] = UChar(0x80 | (CH(1) >> 4) | ((CH(2) & 0x03) << 4));
168 	    target[2] = UChar(0x80 | (CH(2) >> 2));
169 	    target[1] = UChar(0x80 | (CH(3) & 0x3f));
170 	    target[0] = UChar(0xfc | ((int) (CH(3) & 0x40) >> 6));
171 	    break;
172 	}
173     }
174 
175     return rc;			/* number of bytes needed in target */
176 #undef CH
177 }
178 
179 NCURSES_EXPORT(int)
180 _nc_conv_to_utf32(unsigned *target, const char *source, unsigned limit)
181 {
182 #define CH(n) UChar((*target) >> ((n) * 8))
183     int rc = 0;
184     int j;
185     unsigned mask = 0;
186 
187     /*
188      * Find the number of bytes we will need from the source.
189      */
190     if ((*source & 0x80) == 0) {
191 	rc = 1;
192 	mask = (unsigned) *source;
193     } else if ((*source & 0xe0) == 0xc0) {
194 	rc = 2;
195 	mask = (unsigned) (*source & 0x1f);
196     } else if ((*source & 0xf0) == 0xe0) {
197 	rc = 3;
198 	mask = (unsigned) (*source & 0x0f);
199     } else if ((*source & 0xf8) == 0xf0) {
200 	rc = 4;
201 	mask = (unsigned) (*source & 0x07);
202     } else if ((*source & 0xfc) == 0xf8) {
203 	rc = 5;
204 	mask = (unsigned) (*source & 0x03);
205     } else if ((*source & 0xfe) == 0xfc) {
206 	rc = 6;
207 	mask = (unsigned) (*source & 0x01);
208     }
209 
210     if ((unsigned) rc > limit) {	/* whatever it is, we cannot decode it */
211 	rc = 0;
212     }
213 
214     /*
215      * sanity-check.
216      */
217     if (rc > 1) {
218 	for (j = 1; j < rc; j++) {
219 	    if ((source[j] & 0xc0) != 0x80)
220 		break;
221 	}
222 	if (j != rc) {
223 	    rc = 0;
224 	}
225     }
226 
227     if (target != 0) {
228 	int shift = 0;
229 	*target = 0;
230 	for (j = 1; j < rc; j++) {
231 	    *target |= (unsigned) (source[rc - j] & 0x3f) << shift;
232 	    shift += 6;
233 	}
234 	*target |= mask << shift;
235     }
236     return rc;
237 #undef CH
238 }
239 #endif /* EXP_XTERM_1005 */
240