Coverage Report

Created: 2026-09-28 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libunistring/lib/unistr/u8-to-u32.c
Line
Count
Source
1
/* Convert UTF-8 string to UTF-32 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 u8_to_u32
24
#define SRC_UNIT uint8_t
25
86.8k
#define DST_UNIT uint32_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
28.3k
{
34
28.3k
  const SRC_UNIT *s_end = s + n;
35
36
  /* Output string accumulator.  */
37
28.3k
  DST_UNIT *result;
38
28.3k
  size_t allocated;
39
28.3k
  if (resultbuf != NULL)
40
0
    {
41
0
      result = resultbuf;
42
0
      allocated = *lengthp;
43
0
    }
44
28.3k
  else
45
28.3k
    {
46
28.3k
      result = NULL;
47
28.3k
      allocated = 0;
48
28.3k
    }
49
28.3k
  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
23.1M
  while (s < s_end)
55
23.1M
    {
56
      /* Fetch a Unicode character from the input string.  */
57
23.1M
      ucs4_t uc;
58
23.1M
      int count = u8_mbtoucr (&uc, s, s_end - s);
59
23.1M
      if (count < 0)
60
434
        {
61
434
          if (!(result == resultbuf || result == NULL))
62
434
            free (result);
63
434
          errno = EILSEQ;
64
434
          return NULL;
65
434
        }
66
23.1M
      s += count;
67
68
      /* Store it in the output string.  */
69
23.1M
      if (length + 1 > allocated)
70
32.6k
        {
71
32.6k
          allocated = (allocated > 0 ? 2 * allocated : 12);
72
32.6k
          if (length + 1 > allocated)
73
0
            allocated = length + 1;
74
75
32.6k
          DST_UNIT *memory;
76
32.6k
          if (result == resultbuf || result == NULL)
77
26.4k
            memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
78
6.16k
          else
79
6.16k
            memory =
80
6.16k
              (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
81
82
32.6k
          if (memory == NULL)
83
0
            {
84
0
              if (!(result == resultbuf || result == NULL))
85
0
                free (result);
86
0
              errno = ENOMEM;
87
0
              return NULL;
88
0
            }
89
32.6k
          if (result == resultbuf && length > 0)
90
0
            memcpy ((char *) memory, (char *) result,
91
0
                    length * sizeof (DST_UNIT));
92
32.6k
          result = memory;
93
32.6k
        }
94
23.1M
      result[length++] = uc;
95
23.1M
    }
96
97
27.8k
  if (length == 0)
98
1.60k
    {
99
1.60k
      if (result == NULL)
100
1.60k
        {
101
          /* Return a non-NULL value.  NULL means error.  */
102
1.60k
          result = (DST_UNIT *) malloc (1);
103
1.60k
          if (result == NULL)
104
0
            {
105
0
              errno = ENOMEM;
106
0
              return NULL;
107
0
            }
108
1.60k
        }
109
1.60k
    }
110
26.2k
  else if (result != resultbuf && length < allocated)
111
25.8k
    {
112
      /* Shrink the allocated memory if possible.  */
113
25.8k
      DST_UNIT *memory =
114
25.8k
        (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
115
25.8k
      if (memory != NULL)
116
25.8k
        result = memory;
117
25.8k
    }
118
119
27.8k
  *lengthp = length;
120
27.8k
  return result;
121
27.8k
}