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
15namespace brillo {
16namespace data_encoding {
17
18using 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"
23BRILLO_EXPORT std::string UrlEncode(const char* data, bool encodeSpaceAsPlus);
24
25inline 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.
31BRILLO_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.
35BRILLO_EXPORT std::string WebParamsEncode(const WebParamList& params,
36                                          bool encodeSpaceAsPlus);
37
38inline 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.
45BRILLO_EXPORT WebParamList WebParamsDecode(const std::string& data);
46
47// Encodes binary data using base64-encoding.
48BRILLO_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.
52BRILLO_EXPORT std::string Base64EncodeWrapLines(const void* data, size_t size);
53
54// Decodes the input string from Base64.
55BRILLO_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.
59inline std::string Base64Encode(const brillo::Blob& input) {
60  return Base64Encode(input.data(), input.size());
61}
62inline std::string Base64EncodeWrapLines(const brillo::Blob& input) {
63  return Base64EncodeWrapLines(input.data(), input.size());
64}
65inline std::string Base64Encode(const std::string& input) {
66  return Base64Encode(input.data(), input.size());
67}
68inline std::string Base64EncodeWrapLines(const std::string& input) {
69  return Base64EncodeWrapLines(input.data(), input.size());
70}
71inline 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