Coverage Report

Created: 2026-01-06 07:18

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
27.8M
#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
9.28M
{
34
9.28M
  const SRC_UNIT *s_end = s + n;
35
36
  /* Output string accumulator.  */
37
9.28M
  DST_UNIT *result;
38
9.28M
  size_t allocated;
39
9.28M
  if (resultbuf != NULL)
40
0
    {
41
0
      result = resultbuf;
42
0
      allocated = *lengthp;
43
0
    }
44
9.28M
  else
45
9.28M
    {
46
9.28M
      result = NULL;
47
9.28M
      allocated = 0;
48
9.28M
    }
49
9.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
26.2M
  while (s < s_end)
55
16.9M
    {
56
      /* Fetch a Unicode character from the input string.  */
57
16.9M
      ucs4_t uc;
58
16.9M
      int count = u8_mbtoucr (&uc, s, s_end - s);
59
16.9M
      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
16.9M
      s += count;
67
68
      /* Store it in the output string.  */
69
16.9M
      if (length + 1 > allocated)
70
9.30M
        {
71
9.30M
          allocated = (allocated > 0 ? 2 * allocated : 12);
72
9.30M
          if (length + 1 > allocated)
73
0
            allocated = length + 1;
74
75
9.30M
          DST_UNIT *memory;
76
9.30M
          if (result == resultbuf || result == NULL)
77
9.28M
            memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
78
22.9k
          else
79
22.9k
            memory =
80
22.9k
              (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
81
82
9.30M
          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
9.30M
          if (result == resultbuf && length > 0)
90
0
            memcpy ((char *) memory, (char *) result,
91
0
                    length * sizeof (DST_UNIT));
92
9.30M
          result = memory;
93
9.30M
        }
94
16.9M
      result[length++] = uc;
95
16.9M
    }
96
97
9.28M
  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
9.28M
  else if (result != resultbuf && length < allocated)
111
9.28M
    {
112
      /* Shrink the allocated memory if possible.  */
113
9.28M
      DST_UNIT *memory =
114
9.28M
        (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
115
9.28M
      if (memory != NULL)
116
9.28M
        result = memory;
117
9.28M
    }
118
119
9.28M
  *lengthp = length;
120
9.28M
  return result;
121
9.28M
}