u8-uctomb-aux.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Conversion UCS-4 to UTF-8.
  2. Copyright (C) 2002, 2006-2007, 2009-2012 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2002.
  4. This program is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include "unistr.h"
  17. int
  18. u8_uctomb_aux (uint8_t *s, ucs4_t uc, int n)
  19. {
  20. int count;
  21. if (uc < 0x80)
  22. /* The case n >= 1 is already handled by the caller. */
  23. return -2;
  24. else if (uc < 0x800)
  25. count = 2;
  26. else if (uc < 0x10000)
  27. {
  28. if (uc < 0xd800 || uc >= 0xe000)
  29. count = 3;
  30. else
  31. return -1;
  32. }
  33. #if 0
  34. else if (uc < 0x200000)
  35. count = 4;
  36. else if (uc < 0x4000000)
  37. count = 5;
  38. else if (uc <= 0x7fffffff)
  39. count = 6;
  40. #else
  41. else if (uc < 0x110000)
  42. count = 4;
  43. #endif
  44. else
  45. return -1;
  46. if (n < count)
  47. return -2;
  48. switch (count) /* note: code falls through cases! */
  49. {
  50. #if 0
  51. case 6: s[5] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x4000000;
  52. case 5: s[4] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x200000;
  53. #endif
  54. case 4: s[3] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x10000;
  55. case 3: s[2] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x800;
  56. case 2: s[1] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0xc0;
  57. /*case 1:*/ s[0] = uc;
  58. }
  59. return count;
  60. }