blob: c34b8177e0c31e88077b12405a8f2ca9430d7908 (
plain)
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
|
/**
* @file PercentEncodedCharacterDecoder.cpp
*
* This module contains the implementation of the
* Uri::PercentEncodedCharacterDecoder class.
*
* © 2018 by Richard Walters
*/
#include "CharacterSet.hpp"
#include "PercentEncodedCharacterDecoder.hpp"
namespace {
/**
* This is the character set containing just numbers.
*/
const Uri::CharacterSet DIGIT('0', '9');
/**
* This is the character set containing just the upper-case
* letters 'A' through 'F', used in upper-case hexadecimal.
*/
const Uri::CharacterSet HEX_UPPER('A', 'F');
/**
* This is the character set containing just the lower-case
* letters 'a' through 'f', used in lower-case hexadecimal.
*/
const Uri::CharacterSet HEX_LOWER('a', 'f');
}
namespace Uri {
struct PercentEncodedCharacterDecoder::Impl {
// Properties
/**
* This is the decoded character.
*/
int decodedCharacter = 0;
/**
* This is the current state of the decoder's state machine.
* - 0: we haven't yet received the first hex digit.
* - 1: we received the first hex digit but not the second.
* - 2: we received both hex digits
*/
size_t decoderState = 0;
// Methods
/**
* This method shifts in the given hex digit as part of
* building the decoded character.
*
* @param[in] c
* This is the hex digit to shift into the decoded character.
*
* @return
* An indication of whether or not the given hex digit
* was valid is returned.
*/
bool ShiftInHexDigit(char c) {
decodedCharacter <<= 4;
if (DIGIT.Contains(c)) {
decodedCharacter += (int)(c - '0');
} else if (HEX_UPPER.Contains(c)) {
decodedCharacter += (int)(c - 'A') + 10;
} else if (HEX_LOWER.Contains(c)) {
decodedCharacter += (int)(c - 'a') + 10;
} else {
return false;
}
return true;
}
};
PercentEncodedCharacterDecoder::~PercentEncodedCharacterDecoder() = default;
PercentEncodedCharacterDecoder::PercentEncodedCharacterDecoder(PercentEncodedCharacterDecoder&&) = default;
PercentEncodedCharacterDecoder& PercentEncodedCharacterDecoder::operator=(PercentEncodedCharacterDecoder&&) = default;
PercentEncodedCharacterDecoder::PercentEncodedCharacterDecoder()
: impl_(new Impl)
{
}
bool PercentEncodedCharacterDecoder::NextEncodedCharacter(char c) {
switch(impl_->decoderState) {
case 0: { // % ...
impl_->decoderState = 1;
if (!impl_->ShiftInHexDigit(c)) {
return false;
}
} break;
case 1: { // %[0-9A-F] ...
impl_->decoderState = 2;
if (!impl_->ShiftInHexDigit(c)) {
return false;
}
} break;
default: break;
}
return true;
}
bool PercentEncodedCharacterDecoder::Done() const {
return (impl_->decoderState == 2);
}
char PercentEncodedCharacterDecoder::GetDecodedCharacter() const {
return (char)impl_->decodedCharacter;
}
}
|