summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibIPC/Encoder.cpp
blob: de01f2d99857f8682f97a7952f5c2ecc6863b875 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/ByteBuffer.h>
#include <AK/String.h>
#include <AK/URL.h>
#include <LibCore/AnonymousBuffer.h>
#include <LibCore/DateTime.h>
#include <LibIPC/Dictionary.h>
#include <LibIPC/Encoder.h>
#include <LibIPC/File.h>

namespace IPC {

Encoder& Encoder::operator<<(bool value)
{
    return *this << (u8)value;
}

Encoder& Encoder::operator<<(u8 value)
{
    m_buffer.data.append(value);
    return *this;
}

Encoder& Encoder::operator<<(u16 value)
{
    m_buffer.data.ensure_capacity(m_buffer.data.size() + 2);
    m_buffer.data.unchecked_append((u8)value);
    m_buffer.data.unchecked_append((u8)(value >> 8));
    return *this;
}

Encoder& Encoder::operator<<(u32 value)
{
    m_buffer.data.ensure_capacity(m_buffer.data.size() + 4);
    m_buffer.data.unchecked_append((u8)value);
    m_buffer.data.unchecked_append((u8)(value >> 8));
    m_buffer.data.unchecked_append((u8)(value >> 16));
    m_buffer.data.unchecked_append((u8)(value >> 24));
    return *this;
}

Encoder& Encoder::operator<<(u64 value)
{
    m_buffer.data.ensure_capacity(m_buffer.data.size() + 8);
    m_buffer.data.unchecked_append((u8)value);
    m_buffer.data.unchecked_append((u8)(value >> 8));
    m_buffer.data.unchecked_append((u8)(value >> 16));
    m_buffer.data.unchecked_append((u8)(value >> 24));
    m_buffer.data.unchecked_append((u8)(value >> 32));
    m_buffer.data.unchecked_append((u8)(value >> 40));
    m_buffer.data.unchecked_append((u8)(value >> 48));
    m_buffer.data.unchecked_append((u8)(value >> 56));
    return *this;
}

Encoder& Encoder::operator<<(i8 value)
{
    m_buffer.data.append((u8)value);
    return *this;
}

Encoder& Encoder::operator<<(i16 value)
{
    m_buffer.data.ensure_capacity(m_buffer.data.size() + 2);
    m_buffer.data.unchecked_append((u8)value);
    m_buffer.data.unchecked_append((u8)(value >> 8));
    return *this;
}

Encoder& Encoder::operator<<(i32 value)
{
    m_buffer.data.ensure_capacity(m_buffer.data.size() + 4);
    m_buffer.data.unchecked_append((u8)value);
    m_buffer.data.unchecked_append((u8)(value >> 8));
    m_buffer.data.unchecked_append((u8)(value >> 16));
    m_buffer.data.unchecked_append((u8)(value >> 24));
    return *this;
}

Encoder& Encoder::operator<<(i64 value)
{
    m_buffer.data.ensure_capacity(m_buffer.data.size() + 8);
    m_buffer.data.unchecked_append((u8)value);
    m_buffer.data.unchecked_append((u8)(value >> 8));
    m_buffer.data.unchecked_append((u8)(value >> 16));
    m_buffer.data.unchecked_append((u8)(value >> 24));
    m_buffer.data.unchecked_append((u8)(value >> 32));
    m_buffer.data.unchecked_append((u8)(value >> 40));
    m_buffer.data.unchecked_append((u8)(value >> 48));
    m_buffer.data.unchecked_append((u8)(value >> 56));
    return *this;
}

Encoder& Encoder::operator<<(float value)
{
    union bits {
        float as_float;
        u32 as_u32;
    } u;
    u.as_float = value;
    return *this << u.as_u32;
}

Encoder& Encoder::operator<<(char const* value)
{
    return *this << StringView(value);
}

Encoder& Encoder::operator<<(StringView const& value)
{
    m_buffer.data.append((u8 const*)value.characters_without_null_termination(), value.length());
    return *this;
}

Encoder& Encoder::operator<<(String const& value)
{
    if (value.is_null())
        return *this << (i32)-1;
    *this << static_cast<i32>(value.length());
    return *this << value.view();
}

Encoder& Encoder::operator<<(ByteBuffer const& value)
{
    *this << static_cast<i32>(value.size());
    m_buffer.data.append(value.data(), value.size());
    return *this;
}

Encoder& Encoder::operator<<(URL const& value)
{
    return *this << value.to_string();
}

Encoder& Encoder::operator<<(Dictionary const& dictionary)
{
    *this << (u64)dictionary.size();
    dictionary.for_each_entry([this](auto& key, auto& value) {
        *this << key << value;
    });
    return *this;
}

Encoder& Encoder::operator<<(File const& file)
{
    int fd = file.fd();
    if (fd != -1) {
        auto result = dup(fd);
        if (result < 0) {
            perror("dup");
            VERIFY_NOT_REACHED();
        }
        fd = result;
    }
    m_buffer.fds.append(adopt_ref(*new AutoCloseFileDescriptor(fd)));
    return *this;
}

bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
{
    encoder << buffer.is_valid();
    if (buffer.is_valid()) {
        encoder << (u32)buffer.size();
        encoder << IPC::File(buffer.fd());
    }
    return true;
}

bool encode(Encoder& encoder, Core::DateTime const& datetime)
{
    encoder << static_cast<i64>(datetime.timestamp());
    return true;
}

}