percent_encoding.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include "src/core/lib/slice/percent_encoding.h"
  19. #include <grpc/support/log.h>
  20. #include "src/core/lib/slice/slice_internal.h"
  21. const uint8_t grpc_url_percent_encoding_unreserved_bytes[256 / 8] = {
  22. 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xff, 0x03, 0xfe, 0xff, 0xff,
  23. 0x87, 0xfe, 0xff, 0xff, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  24. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  25. const uint8_t grpc_compatible_percent_encoding_unreserved_bytes[256 / 8] = {
  26. 0x00, 0x00, 0x00, 0x00, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  27. 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  28. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  29. static bool is_unreserved_character(uint8_t c,
  30. const uint8_t* unreserved_bytes) {
  31. return ((unreserved_bytes[c / 8] >> (c % 8)) & 1) != 0;
  32. }
  33. grpc_slice grpc_percent_encode_slice(grpc_slice slice,
  34. const uint8_t* unreserved_bytes) {
  35. static const uint8_t hex[] = "0123456789ABCDEF";
  36. // first pass: count the number of bytes needed to output this string
  37. size_t output_length = 0;
  38. const uint8_t* slice_start = GRPC_SLICE_START_PTR(slice);
  39. const uint8_t* slice_end = GRPC_SLICE_END_PTR(slice);
  40. const uint8_t* p;
  41. bool any_reserved_bytes = false;
  42. for (p = slice_start; p < slice_end; p++) {
  43. bool unres = is_unreserved_character(*p, unreserved_bytes);
  44. output_length += unres ? 1 : 3;
  45. any_reserved_bytes |= !unres;
  46. }
  47. // no unreserved bytes: return the string unmodified
  48. if (!any_reserved_bytes) {
  49. return grpc_slice_ref_internal(slice);
  50. }
  51. // second pass: actually encode
  52. grpc_slice out = GRPC_SLICE_MALLOC(output_length);
  53. uint8_t* q = GRPC_SLICE_START_PTR(out);
  54. for (p = slice_start; p < slice_end; p++) {
  55. if (is_unreserved_character(*p, unreserved_bytes)) {
  56. *q++ = *p;
  57. } else {
  58. *q++ = '%';
  59. *q++ = hex[*p >> 4];
  60. *q++ = hex[*p & 15];
  61. }
  62. }
  63. GPR_ASSERT(q == GRPC_SLICE_END_PTR(out));
  64. return out;
  65. }
  66. static bool valid_hex(const uint8_t* p, const uint8_t* end) {
  67. if (p >= end) return false;
  68. return (*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'f') ||
  69. (*p >= 'A' && *p <= 'F');
  70. }
  71. static uint8_t dehex(uint8_t c) {
  72. if (c >= '0' && c <= '9') return (uint8_t)(c - '0');
  73. if (c >= 'A' && c <= 'F') return (uint8_t)(c - 'A' + 10);
  74. if (c >= 'a' && c <= 'f') return (uint8_t)(c - 'a' + 10);
  75. GPR_UNREACHABLE_CODE(return 255);
  76. }
  77. bool grpc_strict_percent_decode_slice(grpc_slice slice_in,
  78. const uint8_t* unreserved_bytes,
  79. grpc_slice* slice_out) {
  80. const uint8_t* p = GRPC_SLICE_START_PTR(slice_in);
  81. const uint8_t* in_end = GRPC_SLICE_END_PTR(slice_in);
  82. size_t out_length = 0;
  83. bool any_percent_encoded_stuff = false;
  84. while (p != in_end) {
  85. if (*p == '%') {
  86. if (!valid_hex(++p, in_end)) return false;
  87. if (!valid_hex(++p, in_end)) return false;
  88. p++;
  89. out_length++;
  90. any_percent_encoded_stuff = true;
  91. } else if (is_unreserved_character(*p, unreserved_bytes)) {
  92. p++;
  93. out_length++;
  94. } else {
  95. return false;
  96. }
  97. }
  98. if (!any_percent_encoded_stuff) {
  99. *slice_out = grpc_slice_ref_internal(slice_in);
  100. return true;
  101. }
  102. p = GRPC_SLICE_START_PTR(slice_in);
  103. *slice_out = GRPC_SLICE_MALLOC(out_length);
  104. uint8_t* q = GRPC_SLICE_START_PTR(*slice_out);
  105. while (p != in_end) {
  106. if (*p == '%') {
  107. *q++ = (uint8_t)(dehex(p[1]) << 4) | (dehex(p[2]));
  108. p += 3;
  109. } else {
  110. *q++ = *p++;
  111. }
  112. }
  113. GPR_ASSERT(q == GRPC_SLICE_END_PTR(*slice_out));
  114. return true;
  115. }
  116. grpc_slice grpc_permissive_percent_decode_slice(grpc_slice slice_in) {
  117. const uint8_t* p = GRPC_SLICE_START_PTR(slice_in);
  118. const uint8_t* in_end = GRPC_SLICE_END_PTR(slice_in);
  119. size_t out_length = 0;
  120. bool any_percent_encoded_stuff = false;
  121. while (p != in_end) {
  122. if (*p == '%') {
  123. if (!valid_hex(p + 1, in_end) || !valid_hex(p + 2, in_end)) {
  124. p++;
  125. out_length++;
  126. } else {
  127. p += 3;
  128. out_length++;
  129. any_percent_encoded_stuff = true;
  130. }
  131. } else {
  132. p++;
  133. out_length++;
  134. }
  135. }
  136. if (!any_percent_encoded_stuff) {
  137. return grpc_slice_ref_internal(slice_in);
  138. }
  139. p = GRPC_SLICE_START_PTR(slice_in);
  140. grpc_slice out = GRPC_SLICE_MALLOC(out_length);
  141. uint8_t* q = GRPC_SLICE_START_PTR(out);
  142. while (p != in_end) {
  143. if (*p == '%') {
  144. if (!valid_hex(p + 1, in_end) || !valid_hex(p + 2, in_end)) {
  145. *q++ = *p++;
  146. } else {
  147. *q++ = (uint8_t)(dehex(p[1]) << 4) | (dehex(p[2]));
  148. p += 3;
  149. }
  150. } else {
  151. *q++ = *p++;
  152. }
  153. }
  154. GPR_ASSERT(q == GRPC_SLICE_END_PTR(out));
  155. return out;
  156. }