1 //
2 //
3 // Copyright 2016 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/slice/percent_encoding.h"
22
23 #include <stdlib.h>
24
25 #include <cstdint>
26 #include <utility>
27
28 #include <grpc/support/log.h>
29
30 #include "src/core/lib/gprpp/bitset.h"
31
32 namespace grpc_core {
33
34 namespace {
35 class UrlTable : public BitSet<256> {
36 public:
UrlTable()37 constexpr UrlTable() {
38 for (int i = 'a'; i <= 'z'; i++) set(i);
39 for (int i = 'A'; i <= 'Z'; i++) set(i);
40 for (int i = '0'; i <= '9'; i++) set(i);
41 set('-');
42 set('_');
43 set('.');
44 set('~');
45 }
46 };
47
48 constexpr UrlTable g_url_table;
49
50 class CompatibleTable : public BitSet<256> {
51 public:
CompatibleTable()52 constexpr CompatibleTable() {
53 for (int i = 32; i <= 126; i++) {
54 if (i == '%') continue;
55 set(i);
56 }
57 }
58 };
59
60 constexpr CompatibleTable g_compatible_table;
61
62 // Map PercentEncodingType to a lookup table of legal symbols for that encoding.
LookupTableForPercentEncodingType(PercentEncodingType type)63 const BitSet<256>& LookupTableForPercentEncodingType(PercentEncodingType type) {
64 switch (type) {
65 case PercentEncodingType::URL:
66 return g_url_table;
67 case PercentEncodingType::Compatible:
68 return g_compatible_table;
69 }
70 // Crash if a bad PercentEncodingType was passed in.
71 GPR_UNREACHABLE_CODE(abort());
72 }
73 } // namespace
74
PercentEncodeSlice(Slice slice,PercentEncodingType type)75 Slice PercentEncodeSlice(Slice slice, PercentEncodingType type) {
76 static const uint8_t hex[] = "0123456789ABCDEF";
77
78 const BitSet<256>& lut = LookupTableForPercentEncodingType(type);
79
80 // first pass: count the number of bytes needed to output this string
81 size_t output_length = 0;
82 bool any_reserved_bytes = false;
83 for (uint8_t c : slice) {
84 bool unres = lut.is_set(c);
85 output_length += unres ? 1 : 3;
86 any_reserved_bytes |= !unres;
87 }
88 // no unreserved bytes: return the string unmodified
89 if (!any_reserved_bytes) {
90 return slice;
91 }
92 // second pass: actually encode
93 auto out = MutableSlice::CreateUninitialized(output_length);
94 uint8_t* q = out.begin();
95 for (uint8_t c : slice) {
96 if (lut.is_set(c)) {
97 *q++ = c;
98 } else {
99 *q++ = '%';
100 *q++ = hex[c >> 4];
101 *q++ = hex[c & 15];
102 }
103 }
104 GPR_ASSERT(q == out.end());
105 return Slice(std::move(out));
106 }
107
ValidHex(const uint8_t * p,const uint8_t * end)108 static bool ValidHex(const uint8_t* p, const uint8_t* end) {
109 if (p >= end) return false;
110 return (*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f') ||
111 (*p >= 'A' && *p <= 'F');
112 }
113
DeHex(uint8_t c)114 static uint8_t DeHex(uint8_t c) {
115 if (c >= '0' && c <= '9') return static_cast<uint8_t>(c - '0');
116 if (c >= 'A' && c <= 'F') return static_cast<uint8_t>(c - 'A' + 10);
117 if (c >= 'a' && c <= 'f') return static_cast<uint8_t>(c - 'a' + 10);
118 GPR_UNREACHABLE_CODE(return 255);
119 }
120
PermissivePercentDecodeSlice(Slice slice_in)121 Slice PermissivePercentDecodeSlice(Slice slice_in) {
122 bool any_percent_encoded_stuff = false;
123 for (uint8_t c : slice_in) {
124 if (c == '%') {
125 any_percent_encoded_stuff = true;
126 break;
127 }
128 }
129 if (!any_percent_encoded_stuff) return slice_in;
130
131 MutableSlice out = slice_in.TakeMutable();
132 uint8_t* q = out.begin();
133 const uint8_t* p = out.begin();
134 const uint8_t* end = out.end();
135 while (p != end) {
136 if (*p == '%') {
137 if (!ValidHex(p + 1, end) || !ValidHex(p + 2, end)) {
138 *q++ = *p++;
139 } else {
140 *q++ = static_cast<uint8_t>(DeHex(p[1]) << 4) | (DeHex(p[2]));
141 p += 3;
142 }
143 } else {
144 *q++ = *p++;
145 }
146 }
147 return Slice(out.TakeSubSlice(0, q - out.begin()));
148 }
149
150 } // namespace grpc_core
151