Coverage Report

Created: 2025-11-16 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libidn2/unistring/unistr/u32-to-u8.c
Line
Count
Source
1
/* Convert UTF-32 string to UTF-8 string.
2
   Copyright (C) 2002, 2006-2007, 2009-2025 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
1.94k
#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
1.94k
{
34
1.94k
  const SRC_UNIT *s_end = s + n;
35
  /* Output string accumulator.  */
36
1.94k
  DST_UNIT *result;
37
1.94k
  size_t allocated;
38
1.94k
  size_t length;
39
40
1.94k
  if (resultbuf != NULL)
41
1.94k
    {
42
1.94k
      result = resultbuf;
43
1.94k
      allocated = *lengthp;
44
1.94k
    }
45
0
  else
46
0
    {
47
0
      result = NULL;
48
0
      allocated = 0;
49
0
    }
50
1.94k
  length = 0;
51
  /* Invariants:
52
     result is either == resultbuf or == NULL or malloc-allocated.
53
     If length > 0, then result != NULL.  */
54
55
16.0k
  while (s < s_end)
56
14.1k
    {
57
14.1k
      ucs4_t uc;
58
14.1k
      int count;
59
60
      /* Fetch a Unicode character from the input string.  */
61
14.1k
      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
14.1k
      count = u8_uctomb (result ? result + length : NULL,
67
14.1k
                        uc, allocated - length);
68
14.1k
      if (count == -1)
69
12
        {
70
12
          if (!(result == resultbuf || result == NULL))
71
12
            free (result);
72
12
          errno = EILSEQ;
73
12
          return NULL;
74
12
        }
75
14.1k
      if (count == -2)
76
0
        {
77
0
          DST_UNIT *memory;
78
79
0
          allocated = (allocated > 0 ? 2 * allocated : 12);
80
0
          if (length + 6 > allocated)
81
0
            allocated = length + 6;
82
0
          if (result == resultbuf || result == NULL)
83
0
            memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
84
0
          else
85
0
            memory =
86
0
              (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
87
88
0
          if (memory == NULL)
89
0
            {
90
0
              if (!(result == resultbuf || result == NULL))
91
0
                free (result);
92
0
              errno = ENOMEM;
93
0
              return NULL;
94
0
            }
95
0
          if (result == resultbuf && length > 0)
96
0
            memcpy ((char *) memory, (char *) result,
97
0
                    length * sizeof (DST_UNIT));
98
0
          result = memory;
99
0
          count = u8_uctomb (result + length, uc, allocated - length);
100
0
          if (count < 0)
101
0
            abort ();
102
0
        }
103
14.1k
      length += count;
104
14.1k
    }
105
106
1.93k
  if (length == 0)
107
0
    {
108
0
      if (result == NULL)
109
0
        {
110
          /* Return a non-NULL value.  NULL means error.  */
111
0
          result = (DST_UNIT *) malloc (1);
112
0
          if (result == NULL)
113
0
            {
114
0
              errno = ENOMEM;
115
0
              return NULL;
116
0
            }
117
0
        }
118
0
    }
119
1.93k
  else if (result != resultbuf && length < allocated)
120
0
    {
121
      /* Shrink the allocated memory if possible.  */
122
0
      DST_UNIT *memory;
123
124
0
      memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
125
0
      if (memory != NULL)
126
0
        result = memory;
127
0
    }
128
129
1.93k
  *lengthp = length;
130
1.93k
  return result;
131
1.93k
}