Coverage Report

Created: 2026-07-30 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poppler/poppler/UTF.h
Line
Count
Source
1
//========================================================================
2
//
3
// UTF.h
4
//
5
// This file is licensed under the GPLv2 or later
6
//
7
// Copyright (C) 2012, 2017, 2021, 2023, 2024 Adrian Johnson <ajohnson@redneon.com>
8
// Copyright (C) 2016 Jason Crain <jason@aquaticape.us>
9
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
10
// Copyright (C) 2018 Nelson Benítez León <nbenitezl@gmail.com>
11
// Copyright (C) 2019-2022, 2024, 2025 Albert Astals Cid <aacid@kde.org>
12
// Copyright (C) 2021 Georgiy Sgibnev <georgiy@sgibnev.com>. Work sponsored by lab50.net.
13
// Copyright (C) 2023-2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk>
14
// Copyright (C) 2023 Even Rouault <even.rouault@spatialys.com>
15
// Copyright (C) 2023, 2024 Oliver Sander <oliver.sander@tu-dresden.de>
16
// Copyright (C) 2025 Jonathan Hähne <jonathan.haehne@hotmail.com>
17
//
18
//========================================================================
19
20
#ifndef UTF_H
21
#define UTF_H
22
23
#include <cstdint>
24
#include <climits>
25
#include <string>
26
#include <vector>
27
#include <span>
28
29
#include "CharTypes.h"
30
#include "poppler_private_export.h"
31
32
// Magic bytes that mark the byte order in a UTF-16 unicode string (big-endian case)
33
constexpr std::string_view unicodeByteOrderMark = "\xFE\xFF";
34
35
// Magic bytes that mark the byte order in a UTF-16 unicode string (little-endian case)
36
constexpr std::string_view unicodeByteOrderMarkLE = "\xFF\xFE";
37
38
// Convert a UTF-16 string to a UCS-4
39
//   utf16      - utf16 bytes
40
//   utf16_len  - number of UTF-16 characters
41
//   returns number of UCS-4 characters
42
std::vector<Unicode> UTF16toUCS4(std::span<const Unicode> utf16);
43
44
// Convert a PDF Text String to UCS-4
45
//   s          - PDF text string
46
//   returns UCS-4 characters
47
// Convert a PDF text string to UCS-4
48
std::vector<Unicode> POPPLER_PRIVATE_EXPORT TextStringToUCS4(std::string_view textStr);
49
50
// check if UCS-4 character is valid
51
inline bool UnicodeIsValid(Unicode ucs4)
52
59.7k
{
53
59.7k
    return (ucs4 < 0x110000) && ((ucs4 & 0xfffff800) != 0xd800) && (ucs4 < 0xfdd0 || ucs4 > 0xfdef) && ((ucs4 & 0xfffe) != 0xfffe);
54
59.7k
}
55
56
// check whether string starts with Big-Endian byte order mark
57
inline bool hasUnicodeByteOrderMark(std::string_view s)
58
21.4k
{
59
21.4k
    return s.starts_with(unicodeByteOrderMark);
60
21.4k
}
61
62
// check whether string starts with Little-Endian byte order mark
63
inline bool hasUnicodeByteOrderMarkLE(std::string_view s)
64
10.5k
{
65
10.5k
    return s.starts_with(unicodeByteOrderMarkLE);
66
10.5k
}
67
68
// put big-endian unicode byte order mark at the beginning of a string
69
inline void prependUnicodeByteOrderMark(std::string &s)
70
0
{
71
0
    s.insert(0, unicodeByteOrderMark);
72
0
}
73
74
// check whether string starts with Big-Endian byte order mark and string length is even
75
inline bool hasUnicodeByteOrderMarkAndLengthIsEven(std::string_view s)
76
0
{
77
0
    return s.starts_with(unicodeByteOrderMark) && s.length() % 2 == 0;
78
0
}
79
80
// is a unicode whitespace character
81
bool UnicodeIsWhitespace(Unicode ucs4);
82
83
// Convert a UTF-8 string to a UCS-4
84
//   utf8      - utf8 bytes
85
//   ucs4_out   - if not NULL, allocates and returns UCS-4 string. Free with gfree.
86
//   returns number of UCS-4 characters
87
std::vector<Unicode> POPPLER_PRIVATE_EXPORT utf8ToUCS4(std::string_view utf8);
88
89
// Count number of UTF-16 code units required to convert a UTF-8 string
90
// (excluding terminating NULL). Each invalid byte is counted as a
91
// code point since the UTF-8 conversion functions will replace it with
92
// REPLACEMENT_CHAR.
93
int POPPLER_PRIVATE_EXPORT utf8CountUtf16CodeUnits(std::string_view utf8);
94
95
// Convert UTF-8 to UTF-16
96
//  utf8     - UTF-8 string to convert. If not null terminated, ensure
97
//             maxUtf8 is set the the exact number of bytes to convert.
98
// Returns utf16 string
99
std::u16string POPPLER_PRIVATE_EXPORT utf8ToUtf16(std::string_view utf8);
100
101
inline bool isUtf8WithBom(std::string_view str)
102
873k
{
103
873k
    if (str.size() < 4) {
104
139k
        return false;
105
139k
    }
106
734k
    if (str[0] == '\xef' && str[1] == '\xbb' && str[2] == '\xbf') {
107
124
        return true;
108
124
    }
109
733k
    return false;
110
734k
}
111
112
// Converts a UTF-8 string to a big endian UTF-16 string with BOM.
113
// The caller owns the returned pointer.
114
//  utf8 - UTF-8 string to convert. An empty string is acceptable.
115
// Returns a big endian UTF-16 string with BOM or an empty string without BOM.
116
std::string POPPLER_PRIVATE_EXPORT utf8ToUtf16WithBom(std::string_view utf8);
117
118
// Count number of UTF-8 bytes required to convert a UTF-16 string to
119
// UTF-8 (excluding terminating NULL).
120
int POPPLER_PRIVATE_EXPORT utf16CountUtf8Bytes(const uint16_t *utf16);
121
122
// Convert UTF-16 to UTF-8
123
//  utf16    - UTF-16 string to convert. If not null terminated, ensure
124
//             maxUtf16 is set the the exact number of code units to convert.
125
//  maxUtf16 - Maximum number of UTF-16 code units to convert. Conversion stops
126
//             when either this count is reached or a null is encountered.
127
// Returns utf8 string.
128
std::string POPPLER_PRIVATE_EXPORT utf16ToUtf8(const uint16_t *utf16, int maxUtf16 = INT_MAX);
129
130
// Convert a UCS-4 string to pure ASCII (7bit)
131
//   in       - UCS-4 string bytes
132
//   len      - number of UCS-4 characters
133
//   ucs4_out - if not NULL, allocates and returns UCS-4 string. Free with gfree.
134
//   out_len  - number of UCS-4 characters in ucs4_out.
135
//   in_idx   - if not NULL, the int array returned by the out fourth parameter of
136
//              unicodeNormalizeNFKC() function. Optional, needed for @indices out parameter.
137
//   indices  - if not NULL, @indices is assigned the location of a newly-allocated array
138
//              of length @out_len + 1, for each character in the ascii string giving the index
139
//              of the corresponding character in the text of the line (thanks to this info
140
//              being passed in @in_idx parameter).
141
void POPPLER_PRIVATE_EXPORT unicodeToAscii7(std::span<const Unicode> in, Unicode **ucs4_out, int *out_len, const int *in_idx, int **indices);
142
143
// Convert a PDF Text String to UTF-8
144
//   textStr    - PDF text string
145
//   returns UTF-8 string.
146
std::string POPPLER_PRIVATE_EXPORT TextStringToUtf8(std::string_view textStr);
147
148
#endif