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
181
182
183
184
185
186
187
188
189
190
191
192
|
/* Marshalling and unmarshalling.
Copyright (C) 2014-2024 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include <cc1plugin-config.h>
#include <new>
#include <string.h>
#include "marshall.hh"
#include "connection.hh"
#include "rpc.hh"
cc1_plugin::status
cc1_plugin::unmarshall_check (connection *conn, unsigned long long check)
{
unsigned long long r;
if (!unmarshall (conn, &r))
return FAIL;
return check == r ? OK : FAIL;
}
cc1_plugin::status
cc1_plugin::marshall_intlike (connection *conn, unsigned long long val)
{
if (!conn->send ('i'))
return FAIL;
return conn->send (&val, sizeof (val));
}
cc1_plugin::status
cc1_plugin::unmarshall_intlike (connection *conn, unsigned long long *result)
{
if (!conn->require ('i'))
return FAIL;
return conn->get (result, sizeof (*result));
}
cc1_plugin::status
cc1_plugin::marshall (connection *conn, const char *str)
{
if (!conn->send ('s'))
return FAIL;
unsigned long long len = str == NULL ? -1ULL : strlen (str);
if (!conn->send (&len, sizeof (len)))
return FAIL;
if (str == NULL)
return OK;
return conn->send (str, len);
}
cc1_plugin::status
cc1_plugin::unmarshall (connection *conn, char **result)
{
unsigned long long len;
if (!conn->require ('s'))
return FAIL;
if (!conn->get (&len, sizeof (len)))
return FAIL;
if (len == -1ULL)
{
*result = NULL;
return OK;
}
char *str = new (std::nothrow) char[len + 1];
if (str == NULL)
return FAIL;
if (!conn->get (str, len))
{
delete[] str;
return FAIL;
}
str[len] = '\0';
*result = str;
return OK;
}
cc1_plugin::status
cc1_plugin::marshall_array_start (connection *conn, char id,
size_t n_elements)
{
if (!conn->send (id))
return FAIL;
unsigned long long r = n_elements;
if (!conn->send (&r, sizeof (r)))
return FAIL;
return OK;
}
cc1_plugin::status
cc1_plugin::marshall_array_elmts (connection *conn, size_t n_bytes,
void *elements)
{
return conn->send (elements, n_bytes);
}
cc1_plugin::status
cc1_plugin::unmarshall_array_start (connection *conn, char id,
size_t *n_elements)
{
unsigned long long len;
if (!conn->require (id))
return FAIL;
if (!conn->get (&len, sizeof (len)))
return FAIL;
*n_elements = len;
return OK;
}
cc1_plugin::status
cc1_plugin::unmarshall_array_elmts (connection *conn, size_t n_bytes,
void *elements)
{
return conn->get (elements, n_bytes);
}
cc1_plugin::status
cc1_plugin::marshall (connection *conn, const gcc_type_array *a)
{
size_t len;
if (a)
len = a->n_elements;
else
len = (size_t)-1;
if (!marshall_array_start (conn, 'a', len))
return FAIL;
if (!a)
return OK;
return marshall_array_elmts (conn, len * sizeof (a->elements[0]),
a->elements);
}
cc1_plugin::status
cc1_plugin::unmarshall (connection *conn, gcc_type_array **result)
{
size_t len;
if (!unmarshall_array_start (conn, 'a', &len))
return FAIL;
if (len == (size_t)-1)
{
*result = NULL;
return OK;
}
cc1_plugin::unique_ptr<gcc_type_array> gta (new gcc_type_array {});
gta->n_elements = len;
gta->elements = new gcc_type[len];
if (!unmarshall_array_elmts (conn,
len * sizeof (gta->elements[0]),
gta->elements))
return FAIL;
*result = gta.release ();
return OK;
}
|