1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/netstring.hpp"
21 #include "base/debug.hpp"
24 using namespace icinga;
27 * Reads data from a stream in netstring format.
29 * @param stream The stream to read from.
30 * @param[out] str The String that has been read from the IOQueue.
31 * @returns true if a complete String was read from the IOQueue, false otherwise.
32 * @exception invalid_argument The input stream is invalid.
33 * @see https://github.com/PeterScott/netstring-c/blob/master/netstring.c
35 StreamReadStatus NetString::ReadStringFromStream(const Stream::Ptr& stream, String *str, StreamReadContext& context, bool may_wait)
40 if (context.MustRead) {
41 if (!context.FillFromStream(stream, may_wait)) {
46 context.MustRead = false;
49 size_t header_length = 0;
51 for (size_t i = 0; i < context.Size; i++) {
52 if (context.Buffer[i] == ':') {
55 /* make sure there's a header */
56 if (header_length == 0)
57 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (no length specifier)"));
61 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (missing :)"));
64 if (header_length == 0) {
65 context.MustRead = true;
66 return StatusNeedData;
69 /* no leading zeros allowed */
70 if (context.Buffer[0] == '0' && isdigit(context.Buffer[1]))
71 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (leading zero)"));
76 for (i = 0; i < header_length && isdigit(context.Buffer[i]); i++) {
77 /* length specifier must have at most 9 characters */
79 BOOST_THROW_EXCEPTION(std::invalid_argument("Length specifier must not exceed 9 characters"));
81 len = len * 10 + (context.Buffer[i] - '0');
84 /* read the whole message */
85 size_t data_length = len + 1;
87 char *data = context.Buffer + header_length + 1;
89 if (context.Size < header_length + 1 + data_length) {
90 context.MustRead = true;
91 return StatusNeedData;
95 BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid NetString (missing ,)"));
97 *str = String(&data[0], &data[len]);
99 context.DropData(header_length + 1 + len + 1);
101 return StatusNewItem;
105 * Writes data into a stream using the netstring format and returns bytes written.
107 * @param stream The stream.
108 * @param str The String that is to be written.
110 * @return The amount of bytes written.
112 size_t NetString::WriteStringToStream(const Stream::Ptr& stream, const String& str)
114 std::ostringstream msgbuf;
115 WriteStringToStream(msgbuf, str);
117 String msg = msgbuf.str();
118 stream->Write(msg.CStr(), msg.GetLength());
119 return msg.GetLength();
123 * Writes data into a stream using the netstring format.
125 * @param stream The stream.
126 * @param str The String that is to be written.
128 void NetString::WriteStringToStream(std::ostream& stream, const String& str)
130 stream << str.GetLength() << ":" << str << ",";