1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
/**
* Copyright (C) 2021 Saturneric <[email protected]>
*
* This file is part of GpgFrontend.
*
* GpgFrontend is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GpgFrontend is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <[email protected]> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "core/function/CharsetOperator.h"
#include <spdlog/spdlog.h>
#include <unicode/ucnv.h>
#include <unicode/ucsdet.h>
#include <unicode/ustring.h>
#include <unicode/utypes.h>
#include <cstddef>
#include <memory>
#include <string>
GpgFrontend::CharsetOperator::CharsetInfo GpgFrontend::CharsetOperator::Detect(
const std::string &buffer) {
const UCharsetMatch *ucm;
UErrorCode status = U_ZERO_ERROR;
UCharsetDetector *csd = ucsdet_open(&status);
status = U_ZERO_ERROR;
if (U_FAILURE(status)) {
SPDLOG_ERROR("failed to open charset detector: {}", u_errorName(status));
return {"unknown", "unknown", 0};
}
SPDLOG_DEBUG("detecting charset buffer: {} bytes", buffer.size());
status = U_ZERO_ERROR;
ucsdet_setText(csd, buffer.data(), buffer.size(), &status);
if (U_FAILURE(status)) {
SPDLOG_ERROR("failed to set text to charset detector: {}",
u_errorName(status));
return {"unknown", "unknown", 0};
}
status = U_ZERO_ERROR;
ucm = ucsdet_detect(csd, &status);
if (U_FAILURE(status)) return {"unknown", "unknown", 0};
status = U_ZERO_ERROR;
const char *name = ucsdet_getName(ucm, &status);
if (U_FAILURE(status)) return {"unknown", "unknown", 0};
status = U_ZERO_ERROR;
int confidence = ucsdet_getConfidence(ucm, &status);
if (U_FAILURE(status)) return {name, "unknown", 0};
status = U_ZERO_ERROR;
const char *language = ucsdet_getLanguage(ucm, &status);
if (U_FAILURE(status)) return {name, "unknown", confidence};
SPDLOG_DEBUG("Detected charset: {} {} {}", name, language, confidence);
return {name, language, confidence};
}
bool GpgFrontend::CharsetOperator::Convert2Utf8(const std::string &buffer,
std::string &out_buffer,
std::string from_charset_name) {
UErrorCode status = U_ZERO_ERROR;
const auto from_encode = std::string("utf-8");
const auto to_encode = from_charset_name;
SPDLOG_DEBUG("Converting buffer: {}", buffer.size());
// test if the charset is supported
UConverter *conv = ucnv_open(from_encode.c_str(), &status);
ucnv_close(conv);
if (U_FAILURE(status)) {
SPDLOG_ERROR("failed to open converter: {}, from encode: {}",
u_errorName(status), from_encode);
return false;
}
// test if the charset is supported
conv = ucnv_open(to_encode.c_str(), &status);
ucnv_close(conv);
if (U_FAILURE(status)) {
SPDLOG_ERROR("failed to open converter: {}, to encode: {}",
u_errorName(status), to_encode);
return false;
}
status = U_ZERO_ERROR;
int32_t target_limit = 0, target_capacity = 0;
target_capacity =
ucnv_convert(from_encode.c_str(), to_encode.c_str(), nullptr,
target_limit, buffer.data(), buffer.size(), &status);
if (status == U_BUFFER_OVERFLOW_ERROR) {
status = U_ZERO_ERROR;
out_buffer.clear();
out_buffer.resize(target_capacity);
ucnv_convert(from_encode.c_str(), to_encode.c_str(), out_buffer.data(),
out_buffer.size(), buffer.data(), buffer.size(), &status);
}
if (U_FAILURE(status)) {
SPDLOG_ERROR("failed to convert to utf-8: {}", u_errorName(status));
return false;
}
SPDLOG_DEBUG("converted buffer: {} bytes", out_buffer.size());
return true;
}
|