diff --git a/lib/remote/base64.cpp b/lib/remote/base64.cpp new file mode 100644 index 000000000..84303b3e4 --- /dev/null +++ b/lib/remote/base64.cpp @@ -0,0 +1,61 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software Foundation * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#include "remote/base64.hpp" +#include +#include +#include +#include +#include +#include + +using namespace icinga; + +const String base64_padding[] = {"", "==", "="}; + +String Base64::Encode(const String& data) +{ + typedef boost::archive::iterators::base64_from_binary > base64_encode; + + std::ostringstream msgbuf; + std::copy(base64_encode(data.CStr()), base64_encode(data.CStr() + data.GetLength()), std::ostream_iterator(msgbuf)); + msgbuf << base64_padding[data.GetLength() % 3]; + return msgbuf.str(); +} + +String Base64::Decode(const String& data) +{ + typedef boost::archive::iterators::transform_width, 8, 6> base64_decode; + + String::SizeType size = data.GetLength(); + + if (size && data[size - 1] == '=') { + --size; + + if (size && data[size - 1] == '=') + --size; + } + + if (size == 0) + return String(); + + std::ostringstream msgbuf; + std::copy(base64_decode(data.CStr()), base64_decode(data.CStr() + size), std::ostream_iterator(msgbuf)); + return msgbuf.str(); +} diff --git a/lib/remote/base64.hpp b/lib/remote/base64.hpp new file mode 100644 index 000000000..dacbf1285 --- /dev/null +++ b/lib/remote/base64.hpp @@ -0,0 +1,42 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software Foundation * + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ******************************************************************************/ + +#ifndef BASE64_H +#define BASE64_H + +#include "remote/i2-remote.hpp" +#include "base/string.hpp" + +namespace icinga +{ + +/** + * Base64 + * + * @ingroup remote + */ +struct I2_REMOTE_API Base64 +{ + static String Decode(const String& data); + static String Encode(const String& data); +}; + +} + +#endif /* BASE64_H */