1/* Conversion UCS-4 to UTF-8.
2   Copyright (C) 2002 Free Software Foundation, Inc.
3   Written by Bruno Haible <haible@clisp.cons.org>, 2002.
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2, or (at your option)
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19
20#include <stddef.h>
21
22/* Return the length (number of units) of the UTF-8 representation of uc,
23   after storing it at S.  Return -1 upon failure, -2 if the number of
24   available units, N, is too small.  */
25static int
26u8_uctomb_aux (unsigned char *s, unsigned int uc, int n)
27{
28  int count;
29
30  if (uc < 0x80)
31    count = 1;
32  else if (uc < 0x800)
33    count = 2;
34  else if (uc < 0x10000)
35    count = 3;
36#if 0
37  else if (uc < 0x200000)
38    count = 4;
39  else if (uc < 0x4000000)
40    count = 5;
41  else if (uc <= 0x7fffffff)
42    count = 6;
43#else
44  else if (uc < 0x110000)
45    count = 4;
46#endif
47  else
48    return -1;
49
50  if (n < count)
51    return -2;
52
53  switch (count) /* note: code falls through cases! */
54    {
55#if 0
56    case 6: s[5] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x4000000;
57    case 5: s[4] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x200000;
58#endif
59    case 4: s[3] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x10000;
60    case 3: s[2] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x800;
61    case 2: s[1] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0xc0;
62    case 1: s[0] = uc;
63    }
64  return count;
65}
66
67static inline int
68u8_uctomb (unsigned char *s, unsigned int uc, int n)
69{
70  if (uc < 0x80 && n > 0)
71    {
72      s[0] = uc;
73      return 1;
74    }
75  else
76    return u8_uctomb_aux (s, uc, n);
77}
78