Coverage Report

Created: 2026-02-05 06:23

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
6.86M
#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
2.28M
{
34
2.28M
  const SRC_UNIT *s_end = s + n;
35
36
  /* Output string accumulator.  */
37
2.28M
  DST_UNIT *result;
38
2.28M
  size_t allocated;
39
2.28M
  if (resultbuf != NULL)
40
0
    {
41
0
      result = resultbuf;
42
0
      allocated = *lengthp;
43
0
    }
44
2.28M
  else
45
2.28M
    {
46
2.28M
      result = NULL;
47
2.28M
      allocated = 0;
48
2.28M
    }
49
2.28M
  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
6.44M
  while (s < s_end)
55
4.15M
    {
56
      /* Fetch a Unicode character from the input string.  */
57
4.15M
      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
4.15M
      int count = u8_uctomb (result + length, uc, allocated - length);
63
4.15M
      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
4.15M
      if (count == -2)
71
2.29M
        {
72
2.29M
          allocated = (allocated > 0 ? 2 * allocated : 12);
73
2.29M
          if (length + 6 > allocated)
74
0
            allocated = length + 6;
75
76
2.29M
          DST_UNIT *memory;
77
2.29M
          if (result == resultbuf || result == NULL)
78
2.28M
            memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
79
16.3k
          else
80
16.3k
            memory =
81
16.3k
              (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
82
83
2.29M
          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
2.29M
          if (result == resultbuf && length > 0)
91
0
            memcpy ((char *) memory, (char *) result,
92
0
                    length * sizeof (DST_UNIT));
93
2.29M
          result = memory;
94
2.29M
          count = u8_uctomb (result + length, uc, allocated - length);
95
2.29M
          if (count < 0)
96
0
            abort ();
97
2.29M
        }
98
4.15M
      length += count;
99
4.15M
    }
100
101
2.28M
  if (length == 0)
102
207
    {
103
207
      if (result == NULL)
104
207
        {
105
          /* Return a non-NULL value.  NULL means error.  */
106
207
          result = (DST_UNIT *) malloc (1);
107
207
          if (result == NULL)
108
0
            {
109
0
              errno = ENOMEM;
110
0
              return NULL;
111
0
            }
112
207
        }
113
207
    }
114
2.28M
  else if (result != resultbuf && length < allocated)
115
2.28M
    {
116
      /* Shrink the allocated memory if possible.  */
117
2.28M
      DST_UNIT *memory =
118
2.28M
        (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
119
2.28M
      if (memory != NULL)
120
2.28M
        result = memory;
121
2.28M
    }
122
123
2.28M
  *lengthp = length;
124
2.28M
  return result;
125
2.28M
}