1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
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 ******************************************************************************/
22 using namespace icinga;
25 * Reads data from a stream in netString format.
27 * @param stream The stream to read from.
28 * @param[out] str The String that has been read from the IOQueue.
29 * @returns true if a complete String was read from the IOQueue, false otherwise.
30 * @exception invalid_argument The input stream is invalid.
31 * @see https://github.com/PeterScott/netString-c/blob/master/netString.c
33 bool NetString::ReadStringFromStream(const Stream::Ptr& stream, String *str)
35 /* 16 bytes are enough for the header */
36 size_t peek_length, buffer_length = 16;
37 char *buffer = static_cast<char *>(malloc(buffer_length));
40 BOOST_THROW_EXCEPTION(bad_alloc());
42 peek_length = stream->Peek(buffer, buffer_length);
44 /* minimum netString length is 3 */
45 if (peek_length < 3) {
50 /* no leading zeros allowed */
51 if (buffer[0] == '0' && isdigit(buffer[1])) {
53 BOOST_THROW_EXCEPTION(invalid_argument("Invalid netString (leading zero)"));
59 for (i = 0; i < peek_length && isdigit(buffer[i]); i++) {
60 /* length specifier must have at most 9 characters */
63 BOOST_THROW_EXCEPTION(invalid_argument("Length specifier must not exceed 9 characters"));
66 len = len * 10 + (buffer[i] - '0');
69 /* read the whole message */
70 buffer_length = i + 1 + len + 1;
72 char *new_buffer = static_cast<char *>(realloc(buffer, buffer_length));
74 if (new_buffer == NULL) {
76 BOOST_THROW_EXCEPTION(bad_alloc());
81 peek_length = stream->Peek(buffer, buffer_length);
83 if (peek_length < buffer_length)
86 /* check for the colon delimiter */
87 if (buffer[i] != ':') {
89 BOOST_THROW_EXCEPTION(invalid_argument("Invalid NetString (missing :)"));
92 /* check for the comma delimiter after the String */
93 if (buffer[i + 1 + len] != ',') {
95 BOOST_THROW_EXCEPTION(invalid_argument("Invalid NetString (missing ,)"));
98 *str = String(&buffer[i + 1], &buffer[i + 1 + len]);
102 /* remove the data from the stream */
103 stream->Read(NULL, peek_length);
109 * Writes data into a stream using the netString format.
111 * @param stream The stream.
112 * @param str The String that is to be written.
114 void NetString::WriteStringToStream(const Stream::Ptr& stream, const String& str)
116 stringstream prefixbuf;
117 prefixbuf << str.GetLength() << ":";
119 String prefix = prefixbuf.str();
120 stream->Write(prefix.CStr(), prefix.GetLength());
121 stream->Write(str.CStr(), str.GetLength());
122 stream->Write(",", 1);