Coverage Report

Created: 2026-03-18 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/util-linux/lib/encode.c
Line
Count
Source
1
/*
2
 * Based on code from libblkid,
3
 *
4
 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5
 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
6
 * Copyright (C) 2020 Pali Rohár <pali.rohar@gmail.com>
7
 *
8
 * This file may be redistributed under the terms of the
9
 * GNU Lesser General Public License.
10
 */
11
#include "c.h"
12
#include "encode.h"
13
14
size_t ul_encode_to_utf8(int enc, unsigned char *dest, size_t len,
15
      const unsigned char *src, size_t count)
16
1.30k
{
17
1.30k
  size_t i, j;
18
1.30k
  uint32_t c;
19
1.30k
  uint16_t c2;
20
21
28.4k
  for (j = i = 0; i < count; i++) {
22
27.5k
    if (enc == UL_ENCODE_UTF16LE) {
23
13.9k
      if (i+2 > count)
24
15
        break;
25
13.9k
      c = (src[i+1] << 8) | src[i];
26
13.9k
      i++;
27
13.9k
    } else if (enc == UL_ENCODE_UTF16BE) {
28
2.72k
      if (i+2 > count)
29
15
        break;
30
2.71k
      c = (src[i] << 8) | src[i+1];
31
2.71k
      i++;
32
10.8k
    } else if (enc == UL_ENCODE_LATIN1) {
33
10.8k
      c = src[i];
34
10.8k
    } else {
35
0
      return 0;
36
0
    }
37
27.5k
    if ((enc == UL_ENCODE_UTF16LE || enc == UL_ENCODE_UTF16BE) &&
38
16.6k
        c >= 0xD800 && c <= 0xDBFF && i+2 < count) {
39
2.64k
      if (enc == UL_ENCODE_UTF16LE)
40
2.39k
        c2 = (src[i+2] << 8) | src[i+1];
41
244
      else
42
244
        c2 = (src[i+1] << 8) | src[i+2];
43
2.64k
      if (c2 >= 0xDC00 && c2 <= 0xDFFF) {
44
765
        c = 0x10000 + ((c - 0xD800) << 10) + (c2 - 0xDC00);
45
765
        i += 2;
46
765
      }
47
2.64k
    }
48
27.5k
    if (c == 0) {
49
255
      dest[j] = '\0';
50
255
      break;
51
255
    }
52
53
27.2k
    if (c < 0x80) {
54
11.5k
      if (j+1 >= len)
55
108
        break;
56
11.4k
      dest[j++] = (uint8_t) c;
57
15.7k
    } else if (c < 0x800) {
58
1.51k
      if (j+2 >= len)
59
7
        break;
60
1.50k
      dest[j++] = (uint8_t) (0xc0 | (c >> 6));
61
1.50k
      dest[j++] = (uint8_t) (0x80 | (c & 0x3f));
62
14.2k
    } else if (c < 0x10000) {
63
13.4k
      if (j+3 >= len)
64
72
        break;
65
13.3k
      dest[j++] = (uint8_t) (0xe0 | (c >> 12));
66
13.3k
      dest[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
67
13.3k
      dest[j++] = (uint8_t) (0x80 | (c & 0x3f));
68
13.3k
    } else {
69
765
      if (j+4 >= len)
70
0
        break;
71
765
      dest[j++] = (uint8_t) (0xf0 | (c >> 18));
72
765
      dest[j++] = (uint8_t) (0x80 | ((c >> 12) & 0x3f));
73
765
      dest[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
74
765
      dest[j++] = (uint8_t) (0x80 | (c & 0x3f));
75
765
    }
76
27.2k
  }
77
1.30k
  dest[j] = '\0';
78
1.30k
  return j;
79
1.30k
}