Coverage Report

Created: 2026-02-05 06:23

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
10.9M
#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
3.65M
{
34
3.65M
  const SRC_UNIT *s_end = s + n;
35
36
  /* Output string accumulator.  */
37
3.65M
  DST_UNIT *result;
38
3.65M
  size_t allocated;
39
3.65M
  if (resultbuf != NULL)
40
0
    {
41
0
      result = resultbuf;
42
0
      allocated = *lengthp;
43
0
    }
44
3.65M
  else
45
3.65M
    {
46
3.65M
      result = NULL;
47
3.65M
      allocated = 0;
48
3.65M
    }
49
3.65M
  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
10.7M
  while (s < s_end)
55
7.09M
    {
56
      /* Fetch a Unicode character from the input string.  */
57
7.09M
      ucs4_t uc;
58
7.09M
      int count = u8_mbtoucr (&uc, s, s_end - s);
59
7.09M
      if (count < 0)
60
0
        {
61
0
          if (!(result == resultbuf || result == NULL))
62
0
            free (result);
63
0
          errno = EILSEQ;
64
0
          return NULL;
65
0
        }
66
7.09M
      s += count;
67
68
      /* Store it in the output string.  */
69
7.09M
      if (length + 1 > allocated)
70
3.66M
        {
71
3.66M
          allocated = (allocated > 0 ? 2 * allocated : 12);
72
3.66M
          if (length + 1 > allocated)
73
0
            allocated = length + 1;
74
75
3.66M
          DST_UNIT *memory;
76
3.66M
          if (result == resultbuf || result == NULL)
77
3.65M
            memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
78
19.9k
          else
79
19.9k
            memory =
80
19.9k
              (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
81
82
3.66M
          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
3.66M
          if (result == resultbuf && length > 0)
90
0
            memcpy ((char *) memory, (char *) result,
91
0
                    length * sizeof (DST_UNIT));
92
3.66M
          result = memory;
93
3.66M
        }
94
7.09M
      result[length++] = uc;
95
7.09M
    }
96
97
3.65M
  if (length == 0)
98
0
    {
99
0
      if (result == NULL)
100
0
        {
101
          /* Return a non-NULL value.  NULL means error.  */
102
0
          result = (DST_UNIT *) malloc (1);
103
0
          if (result == NULL)
104
0
            {
105
0
              errno = ENOMEM;
106
0
              return NULL;
107
0
            }
108
0
        }
109
0
    }
110
3.65M
  else if (result != resultbuf && length < allocated)
111
3.64M
    {
112
      /* Shrink the allocated memory if possible.  */
113
3.64M
      DST_UNIT *memory =
114
3.64M
        (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
115
3.64M
      if (memory != NULL)
116
3.64M
        result = memory;
117
3.64M
    }
118
119
3.65M
  *lengthp = length;
120
3.65M
  return result;
121
3.65M
}