| 1 | /* Conversion of files between different charsets and surfaces. |
| 2 | Copyright © 1993-2018 Free Software Foundation, Inc. |
| 3 | Contributed by François Pinard <[email protected]>, 1993. |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public License |
| 7 | as published by the Free Software Foundation; either version 3 of the |
| 8 | License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be |
| 11 | useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 12 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the Recode Library; see the file `COPYING.LIB'. |
| 17 | If not, see <https://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "common.h" |
| 22 | #include "charname.h" |
| 23 | |
| 24 | /*--------------------------------------------------------------------. |
| 25 | | Return a statically allocated full charname associated with a given | |
| 26 | | SYMBOL, or NULL if not found. | |
| 27 | `--------------------------------------------------------------------*/ |
| 28 | |
| 29 | const char * |
| 30 | recode_ucs2_to_charname (int ucs2) |
| 31 | { |
| 32 | static char result[MAX_CHARNAME_LENGTH + 1]; |
| 33 | int first; |
| 34 | int last; |
| 35 | int middle; |
| 36 | int value; |
| 37 | const char *in; |
| 38 | char *out; |
| 39 | const char *cursor; |
| 40 | |
| 41 | /* Find the symbol by binary searching the charname table. */ |
| 42 | |
| 43 | first = 0; |
| 44 | last = NUMBER_OF_CHARNAMES; |
| 45 | while (first < last) |
| 46 | { |
| 47 | middle = (first + last) / 2; |
| 48 | if (charname[middle].code < ucs2) |
| 49 | first = middle + 1; |
| 50 | else if (charname[middle].code > ucs2) |
| 51 | last = middle; |
| 52 | else |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | /* If the UCS value has not been found, return the NULL string. */ |
| 57 | |
| 58 | if (first >= last) |
| 59 | return NULL; |
| 60 | |
| 61 | /* Else, construct the resulting charname. */ |
| 62 | |
| 63 | out = NULL; |
| 64 | for (in = charname[middle].crypted; *in; in++) |
| 65 | { |
| 66 | |
| 67 | /* Decrypt the next word. */ |
| 68 | |
| 69 | value = *(const unsigned char *) in - 1; |
| 70 | if (value >= NUMBER_OF_SINGLES) |
| 71 | value = (NUMBER_OF_SINGLES + 255 * (value - NUMBER_OF_SINGLES) |
| 72 | + *(const unsigned char *) ++in - 1); |
| 73 | |
| 74 | /* Copy it. */ |
| 75 | |
| 76 | if (out) |
| 77 | *out++ = ' '; |
| 78 | else |
| 79 | out = result; |
| 80 | |
| 81 | for (cursor = word[value]; *cursor; cursor++) |
| 82 | *out++ = *cursor; |
| 83 | } |
| 84 | |
| 85 | /* Return the result. */ |
| 86 | |
| 87 | if (out) |
| 88 | *out = NUL; |
| 89 | return result; |
| 90 | } |