• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef LIBBRILLO_BRILLO_DATA_ENCODING_H_
6 #define LIBBRILLO_BRILLO_DATA_ENCODING_H_
7 
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 #include <brillo/brillo_export.h>
13 #include <brillo/secure_blob.h>
14 
15 namespace brillo {
16 namespace data_encoding {
17 
18 using WebParamList = std::vector<std::pair<std::string, std::string>>;
19 
20 // Encode/escape string to be used in the query portion of a URL.
21 // If |encodeSpaceAsPlus| is set to true, spaces are encoded as '+' instead
22 // of "%20"
23 BRILLO_EXPORT std::string UrlEncode(const char* data, bool encodeSpaceAsPlus);
24 
UrlEncode(const char * data)25 inline std::string UrlEncode(const char* data) {
26   return UrlEncode(data, true);
27 }
28 
29 // Decodes/unescapes a URL. Replaces all %XX sequences with actual characters.
30 // Also replaces '+' with spaces.
31 BRILLO_EXPORT std::string UrlDecode(const char* data);
32 
33 // Converts a list of key-value pairs into a string compatible with
34 // 'application/x-www-form-urlencoded' content encoding.
35 BRILLO_EXPORT std::string WebParamsEncode(const WebParamList& params,
36                                           bool encodeSpaceAsPlus);
37 
WebParamsEncode(const WebParamList & params)38 inline std::string WebParamsEncode(const WebParamList& params) {
39   return WebParamsEncode(params, true);
40 }
41 
42 // Parses a string of '&'-delimited key-value pairs (separated by '=') and
43 // encoded in a way compatible with 'application/x-www-form-urlencoded'
44 // content encoding.
45 BRILLO_EXPORT WebParamList WebParamsDecode(const std::string& data);
46 
47 // Encodes binary data using base64-encoding.
48 BRILLO_EXPORT std::string Base64Encode(const void* data, size_t size);
49 
50 // Encodes binary data using base64-encoding and wraps lines at 64 character
51 // boundary using LF as required by PEM (RFC 1421) specification.
52 BRILLO_EXPORT std::string Base64EncodeWrapLines(const void* data, size_t size);
53 
54 // Decodes the input string from Base64.
55 BRILLO_EXPORT bool Base64Decode(const std::string& input, brillo::Blob* output);
56 
57 // Helper wrappers to use std::string and brillo::Blob as binary data
58 // containers.
Base64Encode(const brillo::Blob & input)59 inline std::string Base64Encode(const brillo::Blob& input) {
60   return Base64Encode(input.data(), input.size());
61 }
Base64EncodeWrapLines(const brillo::Blob & input)62 inline std::string Base64EncodeWrapLines(const brillo::Blob& input) {
63   return Base64EncodeWrapLines(input.data(), input.size());
64 }
Base64Encode(const std::string & input)65 inline std::string Base64Encode(const std::string& input) {
66   return Base64Encode(input.data(), input.size());
67 }
Base64EncodeWrapLines(const std::string & input)68 inline std::string Base64EncodeWrapLines(const std::string& input) {
69   return Base64EncodeWrapLines(input.data(), input.size());
70 }
Base64Decode(const std::string & input,std::string * output)71 inline bool Base64Decode(const std::string& input, std::string* output) {
72   brillo::Blob blob;
73   if (!Base64Decode(input, &blob))
74     return false;
75   *output = std::string{blob.begin(), blob.end()};
76   return true;
77 }
78 
79 }  // namespace data_encoding
80 }  // namespace brillo
81 
82 #endif  // LIBBRILLO_BRILLO_DATA_ENCODING_H_
83