2014-12-18 18:50:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
2015-05-12 06:31:13 +00:00
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2014-09-12 04:44:10 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2015-11-01 09:12:48 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
2014-09-12 04:44:10 +00:00
|
|
|
#include <memory>
|
2015-11-01 09:12:48 +00:00
|
|
|
#include <string>
|
2014-09-12 04:44:10 +00:00
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
2015-11-01 09:12:48 +00:00
|
|
|
#include <osquery/status.h>
|
|
|
|
|
2014-12-10 09:17:24 +00:00
|
|
|
#ifdef DARWIN
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
#endif
|
|
|
|
|
2014-09-12 04:44:10 +00:00
|
|
|
namespace osquery {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void do_release_boost(typename boost::shared_ptr<T> const&, T*) {}
|
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief Convert a boost::shared_ptr to a std::shared_ptr
|
2014-09-16 05:56:11 +00:00
|
|
|
*/
|
2014-09-12 04:44:10 +00:00
|
|
|
template <typename T>
|
|
|
|
typename std::shared_ptr<T> boost_to_std_shared_ptr(
|
|
|
|
typename boost::shared_ptr<T> const& p) {
|
|
|
|
return std::shared_ptr<T>(p.get(), boost::bind(&do_release_boost<T>, p, _1));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void do_release_std(typename std::shared_ptr<T> const&, T*) {}
|
|
|
|
|
2014-09-16 07:28:23 +00:00
|
|
|
/**
|
|
|
|
* @brief Convert a std::shared_ptr to a boost::shared_ptr
|
2014-09-16 05:56:11 +00:00
|
|
|
*/
|
2014-09-12 04:44:10 +00:00
|
|
|
template <typename T>
|
|
|
|
typename boost::shared_ptr<T> std_to_boost_shared_ptr(
|
|
|
|
typename std::shared_ptr<T> const& p) {
|
|
|
|
return boost::shared_ptr<T>(p.get(), boost::bind(&do_release_std<T>, p, _1));
|
|
|
|
}
|
2014-12-10 09:17:24 +00:00
|
|
|
|
2014-12-17 22:03:52 +00:00
|
|
|
/**
|
|
|
|
* @brief Decode a base64 encoded string.
|
|
|
|
*
|
|
|
|
* @param encoded The encode base64 string.
|
|
|
|
* @return Decoded string.
|
|
|
|
*/
|
|
|
|
std::string base64Decode(const std::string& encoded);
|
|
|
|
|
2015-01-21 02:02:45 +00:00
|
|
|
/**
|
|
|
|
* @brief Encode a string.
|
|
|
|
*
|
|
|
|
* @param A string to encode.
|
|
|
|
* @return Encoded string.
|
|
|
|
*/
|
|
|
|
std::string base64Encode(const std::string& unencoded);
|
|
|
|
|
2015-04-09 00:41:54 +00:00
|
|
|
/**
|
|
|
|
* @brief Check if a string is ASCII printable
|
|
|
|
*
|
|
|
|
* @param A string to check.
|
|
|
|
* @return If the string is printable.
|
|
|
|
*/
|
|
|
|
bool isPrintable(const std::string& check);
|
|
|
|
|
2015-11-01 09:12:48 +00:00
|
|
|
/// Safely convert a string representation of an integer base.
|
|
|
|
inline Status safeStrtol(const std::string& rep, size_t base, long int& out) {
|
|
|
|
char* end{nullptr};
|
|
|
|
out = strtol(rep.c_str(), &end, base);
|
|
|
|
if (end == nullptr || end == rep.c_str() || *end != '\0' ||
|
|
|
|
((out == LONG_MIN || out == LONG_MAX) && errno == ERANGE)) {
|
|
|
|
return Status(1);
|
|
|
|
}
|
|
|
|
return Status(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Safely convert a string representation of an integer base.
|
|
|
|
inline Status safeStrtoll(const std::string& rep, size_t base, long long& out) {
|
|
|
|
char* end{nullptr};
|
|
|
|
out = strtoll(rep.c_str(), &end, base);
|
|
|
|
if (end == nullptr || end == rep.c_str() || *end != '\0' ||
|
|
|
|
((out == LLONG_MIN || out == LLONG_MAX) && errno == ERANGE)) {
|
|
|
|
return Status(1);
|
|
|
|
}
|
|
|
|
return Status(0);
|
|
|
|
}
|
|
|
|
|
2015-11-24 22:43:38 +00:00
|
|
|
/// Safely convert unicode escaped ASCII.
|
|
|
|
inline std::string unescapeUnicode(const std::string& escaped) {
|
|
|
|
if (escaped.size() < 6) {
|
|
|
|
return escaped;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string unescaped;
|
|
|
|
unescaped.reserve(escaped.size());
|
|
|
|
for (size_t i = 0; i < escaped.size(); ++i) {
|
|
|
|
if (i < escaped.size() - 5 && '\\' == escaped[i] && 'u' == escaped[i + 1]) {
|
|
|
|
// Assume 2-byte wide unicode.
|
|
|
|
long value{0};
|
|
|
|
safeStrtol(escaped.substr(i + 2, i + 6), 16, value);
|
|
|
|
if (value < 255) {
|
|
|
|
unescaped += static_cast<char>(value);
|
|
|
|
i += 5;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unescaped += escaped[i];
|
|
|
|
}
|
|
|
|
return unescaped;
|
|
|
|
}
|
|
|
|
|
2014-12-10 09:17:24 +00:00
|
|
|
#ifdef DARWIN
|
2014-12-10 17:12:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Convert a CFStringRef to a std::string.
|
|
|
|
*/
|
2014-12-10 09:52:02 +00:00
|
|
|
std::string stringFromCFString(const CFStringRef& cf_string);
|
2014-12-10 17:12:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convert a CFNumberRef to a std::string.
|
|
|
|
*/
|
2014-12-10 09:17:24 +00:00
|
|
|
std::string stringFromCFNumber(const CFDataRef& cf_number);
|
2015-03-09 06:05:09 +00:00
|
|
|
std::string stringFromCFNumber(const CFDataRef& cf_number, CFNumberType type);
|
|
|
|
|
2015-05-27 22:21:13 +00:00
|
|
|
/**
|
|
|
|
* @brief Convert a CFAbsoluteTime to a std::string.
|
|
|
|
*/
|
|
|
|
std::string stringFromCFAbsoluteTime(const CFDataRef& cf_abstime);
|
|
|
|
|
2014-12-10 22:51:43 +00:00
|
|
|
std::string stringFromCFData(const CFDataRef& cf_data);
|
2014-12-10 09:17:24 +00:00
|
|
|
#endif
|
|
|
|
|
2014-09-12 04:44:10 +00:00
|
|
|
}
|