Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/BufferStream.h>
28#include <LibIPC/Decoder.h>
29
30namespace IPC {
31
32bool Decoder::decode(bool& value)
33{
34 m_stream >> value;
35 return !m_stream.handle_read_failure();
36}
37
38bool Decoder::decode(u8& value)
39{
40 m_stream >> value;
41 return !m_stream.handle_read_failure();
42}
43
44bool Decoder::decode(u16& value)
45{
46 m_stream >> value;
47 return !m_stream.handle_read_failure();
48}
49
50bool Decoder::decode(u32& value)
51{
52 m_stream >> value;
53 return !m_stream.handle_read_failure();
54}
55
56bool Decoder::decode(u64& value)
57{
58 m_stream >> value;
59 return !m_stream.handle_read_failure();
60}
61
62bool Decoder::decode(i8& value)
63{
64 m_stream >> value;
65 return !m_stream.handle_read_failure();
66}
67
68bool Decoder::decode(i16& value)
69{
70 m_stream >> value;
71 return !m_stream.handle_read_failure();
72}
73
74bool Decoder::decode(i32& value)
75{
76 m_stream >> value;
77 return !m_stream.handle_read_failure();
78}
79
80bool Decoder::decode(i64& value)
81{
82 m_stream >> value;
83 return !m_stream.handle_read_failure();
84}
85
86bool Decoder::decode(float& value)
87{
88 m_stream >> value;
89 return !m_stream.handle_read_failure();
90}
91
92bool Decoder::decode(String& value)
93{
94 i32 length = 0;
95 m_stream >> length;
96 if (m_stream.handle_read_failure())
97 return false;
98 if (length < 0) {
99 value = {};
100 return true;
101 }
102 if (length == 0) {
103 value = String::empty();
104 return true;
105 }
106 char* text_buffer = nullptr;
107 auto text_impl = StringImpl::create_uninitialized(static_cast<size_t>(length), text_buffer);
108 for (size_t i = 0; i < static_cast<size_t>(length); ++i) {
109 m_stream >> text_buffer[i];
110 }
111 value = *text_impl;
112 return !m_stream.handle_read_failure();
113}
114
115}