Home | History | Annotate | Download | only in slice
      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 #ifndef GRPC_CORE_LIB_SLICE_PERCENT_ENCODING_H
     20 #define GRPC_CORE_LIB_SLICE_PERCENT_ENCODING_H
     21 
     22 /* Percent encoding and decoding of slices.
     23    Transforms arbitrary strings into safe-for-transmission strings by using
     24    variants of percent encoding (RFC 3986).
     25    Two major variants are supplied: one that strictly matches URL encoding,
     26      and another which applies percent encoding only to non-http2 header
     27      bytes (the 'compatible' variant) */
     28 
     29 #include <grpc/support/port_platform.h>
     30 
     31 #include <stdbool.h>
     32 
     33 #include <grpc/slice.h>
     34 
     35 /* URL percent encoding spec bitfield (usabel as 'unreserved_bytes' in
     36    grpc_percent_encode_slice, grpc_strict_percent_decode_slice).
     37    Flags [A-Za-z0-9-_.~] as unreserved bytes for the percent encoding routines
     38    */
     39 extern const uint8_t grpc_url_percent_encoding_unreserved_bytes[256 / 8];
     40 /* URL percent encoding spec bitfield (usabel as 'unreserved_bytes' in
     41    grpc_percent_encode_slice, grpc_strict_percent_decode_slice).
     42    Flags ascii7 non-control characters excluding '%' as unreserved bytes for the
     43    percent encoding routines */
     44 extern const uint8_t grpc_compatible_percent_encoding_unreserved_bytes[256 / 8];
     45 
     46 /* Percent-encode a slice, returning the new slice (this cannot fail):
     47    unreserved_bytes is a bitfield indicating which bytes are considered
     48    unreserved and thus do not need percent encoding */
     49 grpc_slice grpc_percent_encode_slice(grpc_slice slice,
     50                                      const uint8_t* unreserved_bytes);
     51 /* Percent-decode a slice, strictly.
     52    If the input is legal (contains no unreserved bytes, and legal % encodings),
     53    returns true and sets *slice_out to the decoded slice.
     54    If the input is not legal, returns false and leaves *slice_out untouched.
     55    unreserved_bytes is a bitfield indicating which bytes are considered
     56    unreserved and thus do not need percent encoding */
     57 bool grpc_strict_percent_decode_slice(grpc_slice slice_in,
     58                                       const uint8_t* unreserved_bytes,
     59                                       grpc_slice* slice_out);
     60 /* Percent-decode a slice, permissively.
     61    If a % triplet can not be decoded, pass it through verbatim.
     62    This cannot fail. */
     63 grpc_slice grpc_permissive_percent_decode_slice(grpc_slice slice_in);
     64 
     65 #endif /* GRPC_CORE_LIB_SLICE_PERCENT_ENCODING_H */
     66