]> granicus.if.org Git - icinga2/blob - lib/base/netstring.cpp
Use BOOST_THROW_EXCEPTION instead of boost::throw_exception()
[icinga2] / lib / base / netstring.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "i2-base.h"
21
22 using namespace icinga;
23
24 /**
25  * Reads data from a stream in netString format.
26  *
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
32  */
33 bool NetString::ReadStringFromStream(const Stream::Ptr& stream, String *str)
34 {
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));
38
39         if (buffer == NULL)
40                 BOOST_THROW_EXCEPTION(bad_alloc());
41
42         peek_length = stream->Peek(buffer, buffer_length);
43
44         /* minimum netString length is 3 */
45         if (peek_length < 3) {
46                 free(buffer);
47                 return false;
48         }
49
50         /* no leading zeros allowed */
51         if (buffer[0] == '0' && isdigit(buffer[1])) {
52                 free(buffer);
53                 BOOST_THROW_EXCEPTION(invalid_argument("Invalid netString (leading zero)"));
54         }
55
56         size_t len, i;
57
58         len = 0;
59         for (i = 0; i < peek_length && isdigit(buffer[i]); i++) {
60                 /* length specifier must have at most 9 characters */
61                 if (i >= 9) {
62                         free(buffer);
63                         BOOST_THROW_EXCEPTION(invalid_argument("Length specifier must not exceed 9 characters"));
64                 }
65
66                 len = len * 10 + (buffer[i] - '0');
67         }
68
69         /* read the whole message */
70         buffer_length = i + 1 + len + 1;
71
72         char *new_buffer = static_cast<char *>(realloc(buffer, buffer_length));
73
74         if (new_buffer == NULL) {
75                 free(buffer);
76                 BOOST_THROW_EXCEPTION(bad_alloc());
77         }
78
79         buffer = new_buffer;
80
81         peek_length = stream->Peek(buffer, buffer_length);
82
83         if (peek_length < buffer_length)
84                 return false;
85
86         /* check for the colon delimiter */
87         if (buffer[i] != ':') {
88                 free(buffer);
89                 BOOST_THROW_EXCEPTION(invalid_argument("Invalid NetString (missing :)"));
90         }
91
92         /* check for the comma delimiter after the String */
93         if (buffer[i + 1 + len] != ',') {
94                 free(buffer);
95                 BOOST_THROW_EXCEPTION(invalid_argument("Invalid NetString (missing ,)"));
96         }
97
98         *str = String(&buffer[i + 1], &buffer[i + 1 + len]);
99
100         free(buffer);
101
102         /* remove the data from the stream */
103         stream->Read(NULL, peek_length);
104
105         return true;
106 }
107
108 /**
109  * Writes data into a stream using the netString format.
110  *
111  * @param stream The stream.
112  * @param str The String that is to be written.
113  */
114 void NetString::WriteStringToStream(const Stream::Ptr& stream, const String& str)
115 {
116         stringstream prefixbuf;
117         prefixbuf << str.GetLength() << ":";
118
119         String prefix = prefixbuf.str();
120         stream->Write(prefix.CStr(), prefix.GetLength());
121         stream->Write(str.CStr(), str.GetLength());
122         stream->Write(",", 1);
123 }