4fc566d3308cd30f13a5c30fb3b2d9c5baa255a2
[gnulib.git] / lib / uniconv / u-conv-from-enc.h
1 /* Conversion to UTF-16/UTF-32 from legacy encodings.
2    Copyright (C) 2002, 2006-2007 Free Software Foundation, Inc.
3
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
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 int
18 FUNC (const char *fromcode,
19       enum iconv_ilseq_handler handler,
20       const char *src, size_t srclen,
21       size_t *offsets,
22       UNIT **resultp, size_t *lengthp)
23 {
24 #if HAVE_UTF_NAME
25   size_t length;
26
27   if (mem_iconveha (src, srclen, fromcode, UTF_NAME, true, handler,
28                     offsets, (char **) resultp, &length) < 0)
29     return -1;
30   if (offsets != NULL)
31     {
32       /* Convert 'char *' offsets to 'UNIT *' offsets.  */
33       size_t *offsets_end = offsets + srclen;
34       size_t *o;
35
36       for (o = offsets; o < offsets_end; o++)
37         if (*o != (size_t)(-1))
38           *o = *o / sizeof (UNIT);
39     }
40   if ((length % sizeof (UNIT)) != 0)
41     abort ();
42   *lengthp = length / sizeof (UNIT);
43   return 0;
44 #else
45   uint8_t *utf8_string = NULL;
46   size_t utf8_length = 0;
47   UNIT *result;
48
49   if (u8_conv_from_encoding (fromcode, handler, src, srclen, offsets,
50                              &utf8_string, &utf8_length) < 0)
51     return -1;
52   if (utf8_string == NULL)
53     {
54       *lengthp = 0;
55       return 0;
56     }
57   result = U8_TO_U (utf8_string, utf8_length, *resultp, lengthp);
58   if (result == NULL)
59     {
60       int saved_errno = errno;
61       free (utf8_string);
62       errno = saved_errno;
63       return -1;
64     }
65   if (offsets != NULL)
66     {
67       size_t length = *lengthp;
68       size_t *offsets_end = offsets + srclen;
69       size_t *o;
70       size_t off8 = 0;  /* offset into utf8_string */
71       size_t offunit = 0;       /* offset into result */
72
73       for (o = offsets; o < offsets_end; o++)
74         if (*o != (size_t)(-1))
75           {
76             while (off8 < *o)
77               {
78                 int count8 = u8_mblen (utf8_string + off8, utf8_length - off8);
79                 int countunit = U_MBLEN (result + offunit, length - offunit);
80                 if (count8 < 0 || countunit < 0)
81                   abort ();
82                 off8 += count8;
83                 offunit += countunit;
84               }
85             if (*o != off8)
86               abort ();
87             *o = offunit;
88           }
89     }
90   free (utf8_string);
91   *resultp = result;
92   return 0;
93 #endif
94 }