Coverage Report

Created: 2025-07-23 06:43

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