X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Ftinfo%2Fobsolete.c;h=2724fe53486a502492e710faa5536ab9d5046629;hp=9b62917d7ccf8e9e6b804dc46d18fc871ec4cdd7;hb=fe43d506ce08d1deef9e9e5a6d5fd4f9c9c32e1d;hpb=2403eaecbe5b488358485a3deb351ab552af8791;ds=inline diff --git a/ncurses/tinfo/obsolete.c b/ncurses/tinfo/obsolete.c index 9b62917d..2724fe53 100644 --- a/ncurses/tinfo/obsolete.c +++ b/ncurses/tinfo/obsolete.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2013 Free Software Foundation, Inc. * + * Copyright (c) 2013-2014,2016 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -27,16 +27,16 @@ ****************************************************************************/ /**************************************************************************** - * Author: Thomas E. Dickey 2013 * + * Author: Thomas E. Dickey 2013-on * ****************************************************************************/ /* -** Support for obsolete features. +** Support for obsolete/unusual features. */ #include -MODULE_ID("$Id: obsolete.c,v 1.1 2013/01/26 22:07:51 tom Exp $") +MODULE_ID("$Id: obsolete.c,v 1.4 2016/09/10 18:39:51 tom Exp $") /* * Obsolete entrypoint retained for binary compatbility. @@ -68,7 +68,7 @@ _nc_strdup(const char *s) size_t need = strlen(s); result = malloc(need + 1); if (result != 0) { - strcpy(result, s); + _nc_STRCPY(result, s, need); } } return result; @@ -100,3 +100,140 @@ _nc_memmove(void *s1, const void *s2, size_t n) return s1; } #endif /* USE_MY_MEMMOVE */ + +#ifdef EXP_XTERM_1005 +NCURSES_EXPORT(int) +_nc_conv_to_utf8(unsigned char *target, unsigned source, unsigned limit) +{ +#define CH(n) UChar((source) >> ((n) * 8)) + int rc = 0; + + if (source <= 0x0000007f) + rc = 1; + else if (source <= 0x000007ff) + rc = 2; + else if (source <= 0x0000ffff) + rc = 3; + else if (source <= 0x001fffff) + rc = 4; + else if (source <= 0x03ffffff) + rc = 5; + else /* (source <= 0x7fffffff) */ + rc = 6; + + if ((unsigned) rc > limit) { /* whatever it is, we cannot decode it */ + rc = 0; + } + + if (target != 0) { + switch (rc) { + case 1: + target[0] = CH(0); + break; + + case 2: + target[1] = UChar(0x80 | (CH(0) & 0x3f)); + target[0] = UChar(0xc0 | (CH(0) >> 6) | ((CH(1) & 0x07) << 2)); + break; + + case 3: + target[2] = UChar(0x80 | (CH(0) & 0x3f)); + target[1] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2)); + target[0] = UChar(0xe0 | ((int) (CH(1) & 0xf0) >> 4)); + break; + + case 4: + target[3] = UChar(0x80 | (CH(0) & 0x3f)); + target[2] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2)); + target[1] = UChar(0x80 | + ((int) (CH(1) & 0xf0) >> 4) | + ((int) (CH(2) & 0x03) << 4)); + target[0] = UChar(0xf0 | ((int) (CH(2) & 0x1f) >> 2)); + break; + + case 5: + target[4] = UChar(0x80 | (CH(0) & 0x3f)); + target[3] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2)); + target[2] = UChar(0x80 | + ((int) (CH(1) & 0xf0) >> 4) | + ((int) (CH(2) & 0x03) << 4)); + target[1] = UChar(0x80 | (CH(2) >> 2)); + target[0] = UChar(0xf8 | (CH(3) & 0x03)); + break; + + case 6: + target[5] = UChar(0x80 | (CH(0) & 0x3f)); + target[4] = UChar(0x80 | (CH(0) >> 6) | ((CH(1) & 0x0f) << 2)); + target[3] = UChar(0x80 | (CH(1) >> 4) | ((CH(2) & 0x03) << 4)); + target[2] = UChar(0x80 | (CH(2) >> 2)); + target[1] = UChar(0x80 | (CH(3) & 0x3f)); + target[0] = UChar(0xfc | ((int) (CH(3) & 0x40) >> 6)); + break; + } + } + + return rc; /* number of bytes needed in target */ +#undef CH +} + +NCURSES_EXPORT(int) +_nc_conv_to_utf32(unsigned *target, const char *source, unsigned limit) +{ +#define CH(n) UChar((*target) >> ((n) * 8)) + int rc = 0; + int j; + unsigned mask = 0; + + /* + * Find the number of bytes we will need from the source. + */ + if ((*source & 0x80) == 0) { + rc = 1; + mask = (unsigned) *source; + } else if ((*source & 0xe0) == 0xc0) { + rc = 2; + mask = (unsigned) (*source & 0x1f); + } else if ((*source & 0xf0) == 0xe0) { + rc = 3; + mask = (unsigned) (*source & 0x0f); + } else if ((*source & 0xf8) == 0xf0) { + rc = 4; + mask = (unsigned) (*source & 0x07); + } else if ((*source & 0xfc) == 0xf8) { + rc = 5; + mask = (unsigned) (*source & 0x03); + } else if ((*source & 0xfe) == 0xfc) { + rc = 6; + mask = (unsigned) (*source & 0x01); + } + + if ((unsigned) rc > limit) { /* whatever it is, we cannot decode it */ + rc = 0; + } + + /* + * sanity-check. + */ + if (rc > 1) { + for (j = 1; j < rc; j++) { + if ((source[j] & 0xc0) != 0x80) + break; + } + if (j != rc) { + rc = 0; + } + } + + if (target != 0) { + int shift = 0; + *target = 0; + for (j = 1; j < rc; j++) { + *target |= (unsigned) (source[rc - j] & 0x3f) << shift; + shift += 6; + } + *target |= mask << shift; + } + return rc; +#undef CH +} +#endif /* EXP_XTERM_1005 */