Coverage Report

Created: 2026-09-28 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libunistring/lib/unistr/u16-to-u8.c
Line
Count
Source
1
/* Convert UTF-16 string to UTF-8 string.
2
   Copyright (C) 2002, 2006-2007, 2009-2026 Free Software Foundation, Inc.
3
   Written by Bruno Haible <bruno@clisp.org>, 2002.
4
5
   This file is free software.
6
   It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
7
   You can redistribute it and/or modify it under either
8
     - the terms of the GNU Lesser General Public License as published
9
       by the Free Software Foundation, either version 3, or (at your
10
       option) any later version, or
11
     - the terms of the GNU General Public License as published by the
12
       Free Software Foundation; either version 2, or (at your option)
13
       any later version, or
14
     - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
15
16
   This file is distributed in the hope that it will be useful,
17
   but WITHOUT ANY WARRANTY; without even the implied warranty of
18
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19
   Lesser General Public License and the GNU General Public License
20
   for more details.
21
22
   You should have received a copy of the GNU Lesser General Public
23
   License and of the GNU General Public License along with this
24
   program.  If not, see <https://www.gnu.org/licenses/>.  */
25
26
#include <config.h>
27
28
/* Specification.  */
29
#include "unistr.h"
30
31
#define FUNC u16_to_u8
32
#define SRC_UNIT uint16_t
33
77.3k
#define DST_UNIT uint8_t
34
35
#include <errno.h>
36
#include <stdlib.h>
37
#include <string.h>
38
39
DST_UNIT *
40
FUNC (const SRC_UNIT *s, size_t n, DST_UNIT *resultbuf, size_t *lengthp)
41
28.8k
{
42
28.8k
  const SRC_UNIT *s_end = s + n;
43
44
  /* Output string accumulator.  */
45
28.8k
  DST_UNIT *result;
46
28.8k
  size_t allocated;
47
28.8k
  if (resultbuf != NULL)
48
0
    {
49
0
      result = resultbuf;
50
0
      allocated = *lengthp;
51
0
    }
52
28.8k
  else
53
28.8k
    {
54
28.8k
      result = NULL;
55
28.8k
      allocated = 0;
56
28.8k
    }
57
28.8k
  size_t length = 0;
58
  /* Invariants:
59
     result is either == resultbuf or == NULL or malloc-allocated.
60
     If length > 0, then result != NULL.  */
61
62
142k
  while (s < s_end)
63
123k
    {
64
      /* Fetch a Unicode character from the input string.  */
65
123k
      ucs4_t uc;
66
123k
      int count = u16_mbtoucr (&uc, s, s_end - s);
67
123k
      if (count < 0)
68
9.57k
        {
69
9.57k
          if (!(result == resultbuf || result == NULL))
70
9.57k
            free (result);
71
9.57k
          errno = EILSEQ;
72
9.57k
          return NULL;
73
9.57k
        }
74
113k
      s += count;
75
76
      /* Store it in the output string.  */
77
113k
      count = u8_uctomb (result + length, uc, allocated - length);
78
113k
      if (count == -1)
79
0
        {
80
0
          if (!(result == resultbuf || result == NULL))
81
0
            free (result);
82
0
          errno = EILSEQ;
83
0
          return NULL;
84
0
        }
85
113k
      if (count == -2)
86
31.9k
        {
87
31.9k
          allocated = (allocated > 0 ? 2 * allocated : 12);
88
31.9k
          if (length + 6 > allocated)
89
0
            allocated = length + 6;
90
91
31.9k
          DST_UNIT *memory;
92
31.9k
          if (result == resultbuf || result == NULL)
93
23.0k
            memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
94
8.93k
          else
95
8.93k
            memory =
96
8.93k
              (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
97
98
31.9k
          if (memory == NULL)
99
0
            {
100
0
              if (!(result == resultbuf || result == NULL))
101
0
                free (result);
102
0
              errno = ENOMEM;
103
0
              return NULL;
104
0
            }
105
31.9k
          if (result == resultbuf && length > 0)
106
0
            memcpy ((char *) memory, (char *) result,
107
0
                    length * sizeof (DST_UNIT));
108
31.9k
          result = memory;
109
31.9k
          count = u8_uctomb (result + length, uc, allocated - length);
110
31.9k
          if (count < 0)
111
0
            abort ();
112
31.9k
        }
113
113k
      length += count;
114
113k
    }
115
116
19.2k
  if (length == 0)
117
1.08k
    {
118
1.08k
      if (result == NULL)
119
1.08k
        {
120
          /* Return a non-NULL value.  NULL means error.  */
121
1.08k
          result = (DST_UNIT *) malloc (1);
122
1.08k
          if (result == NULL)
123
0
            {
124
0
              errno = ENOMEM;
125
0
              return NULL;
126
0
            }
127
1.08k
        }
128
1.08k
    }
129
18.1k
  else if (result != resultbuf && length < allocated)
130
16.5k
    {
131
      /* Shrink the allocated memory if possible.  */
132
16.5k
      DST_UNIT *memory =
133
16.5k
        (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
134
16.5k
      if (memory != NULL)
135
16.5k
        result = memory;
136
16.5k
    }
137
138
19.2k
  *lengthp = length;
139
19.2k
  return result;
140
19.2k
}