Coverage Report

Created: 2025-12-31 06:37

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-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 u8_to_u32
24
#define SRC_UNIT uint8_t
25
93.5k
#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
30.9k
{
34
30.9k
  const SRC_UNIT *s_end = s + n;
35
  /* Output string accumulator.  */
36
30.9k
  DST_UNIT *result;
37
30.9k
  size_t allocated;
38
30.9k
  size_t length;
39
40
30.9k
  if (resultbuf != NULL)
41
0
    {
42
0
      result = resultbuf;
43
0
      allocated = *lengthp;
44
0
    }
45
30.9k
  else
46
30.9k
    {
47
30.9k
      result = NULL;
48
30.9k
      allocated = 0;
49
30.9k
    }
50
30.9k
  length = 0;
51
  /* Invariants:
52
     result is either == resultbuf or == NULL or malloc-allocated.
53
     If length > 0, then result != NULL.  */
54
55
21.7M
  while (s < s_end)
56
21.7M
    {
57
21.7M
      ucs4_t uc;
58
21.7M
      int count;
59
60
      /* Fetch a Unicode character from the input string.  */
61
21.7M
      count = u8_mbtoucr (&uc, s, s_end - s);
62
21.7M
      if (count < 0)
63
441
        {
64
441
          if (!(result == resultbuf || result == NULL))
65
441
            free (result);
66
441
          errno = EILSEQ;
67
441
          return NULL;
68
441
        }
69
21.7M
      s += count;
70
71
      /* Store it in the output string.  */
72
21.7M
      if (length + 1 > allocated)
73
34.5k
        {
74
34.5k
          DST_UNIT *memory;
75
76
34.5k
          allocated = (allocated > 0 ? 2 * allocated : 12);
77
34.5k
          if (length + 1 > allocated)
78
0
            allocated = length + 1;
79
34.5k
          if (result == resultbuf || result == NULL)
80
28.6k
            memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT));
81
5.87k
          else
82
5.87k
            memory =
83
5.87k
              (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT));
84
85
34.5k
          if (memory == NULL)
86
0
            {
87
0
              if (!(result == resultbuf || result == NULL))
88
0
                free (result);
89
0
              errno = ENOMEM;
90
0
              return NULL;
91
0
            }
92
34.5k
          if (result == resultbuf && length > 0)
93
0
            memcpy ((char *) memory, (char *) result,
94
0
                    length * sizeof (DST_UNIT));
95
34.5k
          result = memory;
96
34.5k
        }
97
21.7M
      result[length++] = uc;
98
21.7M
    }
99
100
30.5k
  if (length == 0)
101
2.05k
    {
102
2.05k
      if (result == NULL)
103
2.05k
        {
104
          /* Return a non-NULL value.  NULL means error.  */
105
2.05k
          result = (DST_UNIT *) malloc (1);
106
2.05k
          if (result == NULL)
107
0
            {
108
0
              errno = ENOMEM;
109
0
              return NULL;
110
0
            }
111
2.05k
        }
112
2.05k
    }
113
28.4k
  else if (result != resultbuf && length < allocated)
114
28.0k
    {
115
      /* Shrink the allocated memory if possible.  */
116
28.0k
      DST_UNIT *memory;
117
118
28.0k
      memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT));
119
28.0k
      if (memory != NULL)
120
28.0k
        result = memory;
121
28.0k
    }
122
123
30.5k
  *lengthp = length;
124
30.5k
  return result;
125
30.5k
}