Coverage Report

Created: 2026-03-31 07:20

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