Coverage Report

Created: 2026-01-06 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libunistring/lib/unistr/u32-to-u8.c
Line
Count
Source
1
/* Convert UTF-32 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: 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
17.1M
#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
5.71M
{
34
5.71M
  const SRC_UNIT *s_end = s + n;
35
36
  /* Output string accumulator.  */
37
5.71M
  DST_UNIT *result;
38
5.71M
  size_t allocated;
39
5.71M
  if (resultbuf != NULL)
40
0
    {
41
0
      result = resultbuf;
42
0
      allocated = *lengthp;
43
0
    }
44
5.71M
  else
45
5.71M
    {
46
5.71M
      result = NULL;
47
5.71M
      allocated = 0;
48
5.71M
    }
49
5.71M
  size_t length = 0;
50
  /* Invariants:
51
     result is either == resultbuf or == NULL or malloc-allocated.
52
     If length > 0, then result != NULL.  */
53
54
15.5M
  while (s < s_end)
55
9.86M
    {
56
      /* Fetch a Unicode character from the input string.  */
57
9.86M
      ucs4_t uc = *s++;
58
      /* No need to call the safe variant u32_mbtouc, because
59
         u8_uctomb will verify uc anyway.  */
60
61
      /* Store it in the output string.  */
62
9.86M
      int count = u8_uctomb (result + length, uc, allocated - length);
63
9.86M
      if (count == -1)
64
0
        {
65
0
          if (!(result == resultbuf || result == NULL))
66
0
            free (result);
67
0
          errno = EILSEQ;
68
0
          return NULL;
69
0
        }
70
9.86M
      if (count == -2)
71
5.73M
        {
72
5.73M
          allocated = (allocated > 0 ? 2 * allocated : 12);
73
5.73M
          if (length + 6 > allocated)
74
0
            allocated = length + 6;
75
76
5.73M
          DST_UNIT *memory;
77
5.73M
          if (result == resultbuf || result == NULL)
78
5.71M
            memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
79
19.1k
          else
80
19.1k
            memory =
81
19.1k
              (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
82
83
5.73M
          if (memory == NULL)
84
0
            {
85
0
              if (!(result == resultbuf || result == NULL))
86
0
                free (result);
87
0
              errno = ENOMEM;
88
0
              return NULL;
89
0
            }
90
5.73M
          if (result == resultbuf && length > 0)
91
0
            memcpy ((char *) memory, (char *) result,
92
0
                    length * sizeof (DST_UNIT));
93
5.73M
          result = memory;
94
5.73M
          count = u8_uctomb (result + length, uc, allocated - length);
95
5.73M
          if (count < 0)
96
0
            abort ();
97
5.73M
        }
98
9.86M
      length += count;
99
9.86M
    }
100
101
5.71M
  if (length == 0)
102
219
    {
103
219
      if (result == NULL)
104
219
        {
105
          /* Return a non-NULL value.  NULL means error.  */
106
219
          result = (DST_UNIT *) malloc (1);
107
219
          if (result == NULL)
108
0
            {
109
0
              errno = ENOMEM;
110
0
              return NULL;
111
0
            }
112
219
        }
113
219
    }
114
5.71M
  else if (result != resultbuf && length < allocated)
115
5.71M
    {
116
      /* Shrink the allocated memory if possible.  */
117
5.71M
      DST_UNIT *memory =
118
5.71M
        (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
119
5.71M
      if (memory != NULL)
120
5.71M
        result = memory;
121
5.71M
    }
122
123
5.71M
  *lengthp = length;
124
5.71M
  return result;
125
5.71M
}