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 "src/core/lib/slice/percent_encoding.h"
20
21 #include <grpc/support/port_platform.h>
22 #include <stdlib.h>
23
24 #include <cstdint>
25 #include <utility>
26
27 #include "absl/log/check.h"
28 #include "src/core/util/bitset.h"
29
30 namespace grpc_core {
31
32 namespace {
33 class UrlTable : public BitSet<256> {
34 public:
UrlTable()35 constexpr UrlTable() {
36 for (int i = 'a'; i <= 'z'; i++) set(i);
37 for (int i = 'A'; i <= 'Z'; i++) set(i);
38 for (int i = '0'; i <= '9'; i++) set(i);
39 set('-');
40 set('_');
41 set('.');
42 set('~');
43 }
44 };
45
46 constexpr UrlTable g_url_table;
47
48 class CompatibleTable : public BitSet<256> {
49 public:
CompatibleTable()50 constexpr CompatibleTable() {
51 for (int i = 32; i <= 126; i++) {
52 if (i == '%') continue;
53 set(i);
54 }
55 }
56 };
57
58 constexpr CompatibleTable g_compatible_table;
59
60 // Map PercentEncodingType to a lookup table of legal symbols for that encoding.
LookupTableForPercentEncodingType(PercentEncodingType type)61 const BitSet<256>& LookupTableForPercentEncodingType(PercentEncodingType type) {
62 switch (type) {
63 case PercentEncodingType::URL:
64 return g_url_table;
65 case PercentEncodingType::Compatible:
66 return g_compatible_table;
67 }
68 // Crash if a bad PercentEncodingType was passed in.
69 GPR_UNREACHABLE_CODE(abort());
70 }
71 } // namespace
72
PercentEncodeSlice(Slice slice,PercentEncodingType type)73 Slice PercentEncodeSlice(Slice slice, PercentEncodingType type) {
74 static const uint8_t hex[] = "0123456789ABCDEF";
75
76 const BitSet<256>& lut = LookupTableForPercentEncodingType(type);
77
78 // first pass: count the number of bytes needed to output this string
79 size_t output_length = 0;
80 bool any_reserved_bytes = false;
81 for (uint8_t c : slice) {
82 bool unres = lut.is_set(c);
83 output_length += unres ? 1 : 3;
84 any_reserved_bytes |= !unres;
85 }
86 // no unreserved bytes: return the string unmodified
87 if (!any_reserved_bytes) {
88 return slice;
89 }
90 // second pass: actually encode
91 auto out = MutableSlice::CreateUninitialized(output_length);
92 uint8_t* q = out.begin();
93 for (uint8_t c : slice) {
94 if (lut.is_set(c)) {
95 *q++ = c;
96 } else {
97 *q++ = '%';
98 *q++ = hex[c >> 4];
99 *q++ = hex[c & 15];
100 }
101 }
102 CHECK(q == out.end());
103 return Slice(std::move(out));
104 }
105
ValidHex(const uint8_t * p,const uint8_t * end)106 static bool ValidHex(const uint8_t* p, const uint8_t* end) {
107 if (p >= end) return false;
108 return (*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f') ||
109 (*p >= 'A' && *p <= 'F');
110 }
111
DeHex(uint8_t c)112 static uint8_t DeHex(uint8_t c) {
113 if (c >= '0' && c <= '9') return static_cast<uint8_t>(c - '0');
114 if (c >= 'A' && c <= 'F') return static_cast<uint8_t>(c - 'A' + 10);
115 if (c >= 'a' && c <= 'f') return static_cast<uint8_t>(c - 'a' + 10);
116 GPR_UNREACHABLE_CODE(return 255);
117 }
118
PermissivePercentDecodeSlice(Slice slice_in)119 Slice PermissivePercentDecodeSlice(Slice slice_in) {
120 bool any_percent_encoded_stuff = false;
121 for (uint8_t c : slice_in) {
122 if (c == '%') {
123 any_percent_encoded_stuff = true;
124 break;
125 }
126 }
127 if (!any_percent_encoded_stuff) return slice_in;
128
129 MutableSlice out = slice_in.TakeMutable();
130 uint8_t* q = out.begin();
131 const uint8_t* p = out.begin();
132 const uint8_t* end = out.end();
133 while (p != end) {
134 if (*p == '%') {
135 if (!ValidHex(p + 1, end) || !ValidHex(p + 2, end)) {
136 *q++ = *p++;
137 } else {
138 *q++ = static_cast<uint8_t>(DeHex(p[1]) << 4) | (DeHex(p[2]));
139 p += 3;
140 }
141 } else {
142 *q++ = *p++;
143 }
144 }
145 return Slice(out.TakeSubSlice(0, q - out.begin()));
146 }
147
148 } // namespace grpc_core
149