Libkleo

hex.cpp
1/* -*- mode: c++; c-basic-offset:4 -*-
2 utils/hex.cpp
3
4 This file is part of libkleopatra
5 SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#include <config-libkleo.h>
11
12#include "hex.h"
13
14#include <libkleo/kleoexception.h>
15
16#include <KLocalizedString>
17
18#include <QByteArray>
19#include <QString>
20
21using namespace Kleo;
22
23static unsigned char unhex(unsigned char ch)
24{
25 if (ch >= '0' && ch <= '9') {
26 return ch - '0';
27 }
28 if (ch >= 'A' && ch <= 'F') {
29 return ch - 'A' + 10;
30 }
31 if (ch >= 'a' && ch <= 'f') {
32 return ch - 'a' + 10;
33 }
34 const char cch = ch;
35 throw Kleo::Exception(gpg_error(GPG_ERR_ASS_SYNTAX), i18n("Invalid hex char '%1' in input stream.", QString::fromLatin1(&cch, 1)));
36}
37
38std::string Kleo::hexdecode(const std::string &in)
39{
40 std::string result;
41 result.reserve(in.size());
42 for (std::string::const_iterator it = in.begin(), end = in.end(); it != end; ++it) {
43 if (*it == '%') {
44 ++it;
45 unsigned char ch = '\0';
46 if (it == end) {
47 throw Exception(gpg_error(GPG_ERR_ASS_SYNTAX), i18n("Premature end of hex-encoded char in input stream"));
48 }
49 ch |= unhex(*it) << 4;
50 ++it;
51 if (it == end) {
52 throw Exception(gpg_error(GPG_ERR_ASS_SYNTAX), i18n("Premature end of hex-encoded char in input stream"));
53 }
54 ch |= unhex(*it);
55 result.push_back(ch);
56 } else if (*it == '+') {
57 result += ' ';
58 } else {
59 result.push_back(*it);
60 }
61 }
62 return result;
63}
64
65std::string Kleo::hexencode(const std::string &in)
66{
67 std::string result;
68 result.reserve(3 * in.size());
69
70 static const char hex[] = "0123456789ABCDEF";
71
72 for (std::string::const_iterator it = in.begin(), end = in.end(); it != end; ++it) {
73 switch (const unsigned char ch = *it) {
74 default:
75 if ((ch >= '!' && ch <= '~') || ch > 0xA0) {
76 result += ch;
77 break;
78 }
79 // else fall through
80 case ' ':
81 result += '+';
82 break;
83 case '"':
84 case '#':
85 case '$':
86 case '%':
87 case '\'':
88 case '+':
89 case '=':
90 result += '%';
91 result += hex[(ch & 0xF0) >> 4];
92 result += hex[(ch & 0x0F)];
93 break;
94 }
95 }
96
97 return result;
98}
99
100std::string Kleo::hexdecode(const char *in)
101{
102 if (!in) {
103 return std::string();
104 }
105 return hexdecode(std::string(in));
106}
107
108std::string Kleo::hexencode(const char *in)
109{
110 if (!in) {
111 return std::string();
112 }
113 return hexencode(std::string(in));
114}
115
116QByteArray Kleo::hexdecode(const QByteArray &in)
117{
118 if (in.isNull()) {
119 return QByteArray();
120 }
121 const std::string result = hexdecode(std::string(in.constData()));
122 return QByteArray(result.data(), result.size());
123}
124
125QByteArray Kleo::hexencode(const QByteArray &in)
126{
127 if (in.isNull()) {
128 return QByteArray();
129 }
130 const std::string result = hexencode(std::string(in.constData()));
131 return QByteArray(result.data(), result.size());
132}
QString i18n(const char *text, const TYPE &arg...)
const char * constData() const const
bool isNull() const const
QString fromLatin1(QByteArrayView str)
QTextStream & hex(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:00:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.