]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/c_glib/src/thrift/c_glib/protocol/thrift_protocol.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / c_glib / src / thrift / c_glib / protocol / thrift_protocol.c
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include <thrift/c_glib/thrift.h>
21 #include <thrift/c_glib/protocol/thrift_protocol.h>
22 #include <thrift/c_glib/transport/thrift_transport.h>
23
24 /* define the GError domain string */
25 #define THRIFT_PROTOCOL_ERROR_DOMAIN "thrift-protocol-error-quark"
26
27 /* object properties */
28 enum _ThriftProtocolProperties
29 {
30 PROP_0,
31 PROP_THRIFT_PROTOCOL_TRANSPORT
32 };
33
34 G_DEFINE_ABSTRACT_TYPE(ThriftProtocol, thrift_protocol, G_TYPE_OBJECT)
35
36 void
37 thrift_protocol_get_property (GObject *object, guint property_id,
38 GValue *value, GParamSpec *pspec)
39 {
40 ThriftProtocol *protocol = THRIFT_PROTOCOL (object);
41
42 THRIFT_UNUSED_VAR (pspec);
43
44 switch (property_id)
45 {
46 case PROP_THRIFT_PROTOCOL_TRANSPORT:
47 g_value_set_object (value, protocol->transport);
48 break;
49 }
50 }
51
52 void
53 thrift_protocol_set_property (GObject *object, guint property_id,
54 const GValue *value, GParamSpec *pspec)
55 {
56
57 ThriftProtocol *protocol = THRIFT_PROTOCOL (object);
58
59 THRIFT_UNUSED_VAR (pspec);
60
61 switch (property_id)
62 {
63 case PROP_THRIFT_PROTOCOL_TRANSPORT:
64 protocol->transport = g_value_dup_object (value);
65 break;
66 }
67 }
68
69
70 gint32
71 thrift_protocol_write_message_begin (ThriftProtocol *protocol,
72 const gchar *name,
73 const ThriftMessageType message_type,
74 const gint32 seqid, GError **error)
75 {
76 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_message_begin
77 (protocol, name,
78 message_type, seqid,
79 error);
80 }
81
82 gint32
83 thrift_protocol_write_message_end (ThriftProtocol *protocol, GError **error)
84 {
85 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_message_end (protocol,
86 error);
87 }
88
89 gint32
90 thrift_protocol_write_struct_begin (ThriftProtocol *protocol, const gchar *name,
91 GError **error)
92 {
93 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_struct_begin (protocol,
94 name, error);
95 }
96
97 gint32
98 thrift_protocol_write_struct_end (ThriftProtocol *protocol, GError **error)
99 {
100 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_struct_end (protocol,
101 error);
102 }
103
104 gint32
105 thrift_protocol_write_field_begin (ThriftProtocol *protocol,
106 const gchar *name,
107 const ThriftType field_type,
108 const gint16 field_id,
109 GError **error)
110 {
111 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_field_begin (protocol,
112 name, field_type,
113 field_id, error);
114 }
115
116 gint32
117 thrift_protocol_write_field_end (ThriftProtocol *protocol, GError **error)
118 {
119 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_field_end (protocol,
120 error);
121 }
122
123 gint32
124 thrift_protocol_write_field_stop (ThriftProtocol *protocol, GError **error)
125 {
126 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_field_stop (protocol,
127 error);
128 }
129
130 gint32
131 thrift_protocol_write_map_begin (ThriftProtocol *protocol,
132 const ThriftType key_type,
133 const ThriftType value_type,
134 const guint32 size, GError **error)
135 {
136 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_map_begin (protocol,
137 key_type, value_type,
138 size, error);
139 }
140
141 gint32
142 thrift_protocol_write_map_end (ThriftProtocol *protocol, GError **error)
143 {
144 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_map_end (protocol,
145 error);
146 }
147
148 gint32
149 thrift_protocol_write_list_begin (ThriftProtocol *protocol,
150 const ThriftType element_type,
151 const guint32 size, GError **error)
152 {
153 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_list_begin (protocol,
154 element_type, size,
155 error);
156 }
157
158 gint32
159 thrift_protocol_write_list_end (ThriftProtocol *protocol, GError **error)
160 {
161 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_list_end (protocol,
162 error);
163 }
164
165 gint32
166 thrift_protocol_write_set_begin (ThriftProtocol *protocol,
167 const ThriftType element_type,
168 const guint32 size, GError **error)
169 {
170 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_set_begin (protocol,
171 element_type, size,
172 error);
173 }
174
175 gint32
176 thrift_protocol_write_set_end (ThriftProtocol *protocol, GError **error)
177 {
178 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_set_end (protocol,
179 error);
180 }
181
182 gint32
183 thrift_protocol_write_bool (ThriftProtocol *protocol,
184 const gboolean value, GError **error)
185 {
186 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_bool (protocol, value,
187 error);
188 }
189
190 gint32
191 thrift_protocol_write_byte (ThriftProtocol *protocol, const gint8 value,
192 GError **error)
193 {
194 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_byte (protocol, value,
195 error);
196 }
197
198 gint32
199 thrift_protocol_write_i16 (ThriftProtocol *protocol, const gint16 value,
200 GError **error)
201 {
202 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_i16 (protocol, value,
203 error);
204 }
205
206 gint32
207 thrift_protocol_write_i32 (ThriftProtocol *protocol, const gint32 value,
208 GError **error)
209 {
210 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_i32 (protocol, value,
211 error);
212 }
213
214 gint32
215 thrift_protocol_write_i64 (ThriftProtocol *protocol, const gint64 value,
216 GError **error)
217 {
218 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_i64 (protocol, value,
219 error);
220 }
221
222 gint32
223 thrift_protocol_write_double (ThriftProtocol *protocol,
224 const gdouble value, GError **error)
225 {
226 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_double (protocol,
227 value, error);
228 }
229
230 gint32
231 thrift_protocol_write_string (ThriftProtocol *protocol,
232 const gchar *str, GError **error)
233 {
234 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_string (protocol, str,
235 error);
236 }
237
238 gint32
239 thrift_protocol_write_binary (ThriftProtocol *protocol, const gpointer buf,
240 const guint32 len, GError **error)
241 {
242 return THRIFT_PROTOCOL_GET_CLASS (protocol)->write_binary (protocol, buf,
243 len, error);
244 }
245
246 gint32
247 thrift_protocol_read_message_begin (ThriftProtocol *protocol,
248 gchar **name,
249 ThriftMessageType *message_type,
250 gint32 *seqid, GError **error)
251 {
252 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_message_begin (protocol,
253 name, message_type,
254 seqid, error);
255 }
256
257 gint32
258 thrift_protocol_read_message_end (ThriftProtocol *protocol,
259 GError **error)
260 {
261 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_message_end (protocol,
262 error);
263 }
264
265 gint32
266 thrift_protocol_read_struct_begin (ThriftProtocol *protocol,
267 gchar **name,
268 GError **error)
269 {
270 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_struct_begin (protocol,
271 name,
272 error);
273 }
274
275 gint32
276 thrift_protocol_read_struct_end (ThriftProtocol *protocol, GError **error)
277 {
278 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_struct_end (protocol,
279 error);
280 }
281
282 gint32
283 thrift_protocol_read_field_begin (ThriftProtocol *protocol,
284 gchar **name,
285 ThriftType *field_type,
286 gint16 *field_id,
287 GError **error)
288 {
289 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_field_begin (protocol,
290 name,
291 field_type,
292 field_id,
293 error);
294 }
295
296 gint32
297 thrift_protocol_read_field_end (ThriftProtocol *protocol,
298 GError **error)
299 {
300 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_field_end (protocol,
301 error);
302 }
303
304 gint32
305 thrift_protocol_read_map_begin (ThriftProtocol *protocol,
306 ThriftType *key_type,
307 ThriftType *value_type, guint32 *size,
308 GError **error)
309 {
310 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_map_begin (protocol,
311 key_type,
312 value_type,
313 size,
314 error);
315 }
316
317 gint32
318 thrift_protocol_read_map_end (ThriftProtocol *protocol, GError **error)
319 {
320 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_map_end (protocol,
321 error);
322 }
323
324 gint32
325 thrift_protocol_read_list_begin (ThriftProtocol *protocol,
326 ThriftType *element_type,
327 guint32 *size, GError **error)
328 {
329 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_list_begin (protocol,
330 element_type,
331 size, error);
332 }
333
334 gint32
335 thrift_protocol_read_list_end (ThriftProtocol *protocol, GError **error)
336 {
337 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_list_end (protocol,
338 error);
339 }
340
341 gint32
342 thrift_protocol_read_set_begin (ThriftProtocol *protocol,
343 ThriftType *element_type,
344 guint32 *size, GError **error)
345 {
346 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_set_begin (protocol,
347 element_type,
348 size, error);
349 }
350
351 gint32
352 thrift_protocol_read_set_end (ThriftProtocol *protocol, GError **error)
353 {
354 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_set_end (protocol,
355 error);
356 }
357
358 gint32
359 thrift_protocol_read_bool (ThriftProtocol *protocol, gboolean *value,
360 GError **error)
361 {
362 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_bool (protocol, value,
363 error);
364 }
365
366 gint32
367 thrift_protocol_read_byte (ThriftProtocol *protocol, gint8 *value,
368 GError **error)
369 {
370 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_byte (protocol, value,
371 error);
372 }
373
374 gint32
375 thrift_protocol_read_i16 (ThriftProtocol *protocol, gint16 *value,
376 GError **error)
377 {
378 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_i16 (protocol, value,
379 error);
380 }
381
382 gint32
383 thrift_protocol_read_i32 (ThriftProtocol *protocol, gint32 *value,
384 GError **error)
385 {
386 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_i32 (protocol, value,
387 error);
388 }
389
390 gint32
391 thrift_protocol_read_i64 (ThriftProtocol *protocol, gint64 *value,
392 GError **error)
393 {
394 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_i64 (protocol, value,
395 error);
396 }
397
398 gint32
399 thrift_protocol_read_double (ThriftProtocol *protocol,
400 gdouble *value, GError **error)
401 {
402 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_double (protocol, value,
403 error);
404 }
405
406 gint32
407 thrift_protocol_read_string (ThriftProtocol *protocol,
408 gchar **str, GError **error)
409 {
410 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_string (protocol, str,
411 error);
412 }
413
414 gint32
415 thrift_protocol_read_binary (ThriftProtocol *protocol, gpointer *buf,
416 guint32 *len, GError **error)
417 {
418 return THRIFT_PROTOCOL_GET_CLASS (protocol)->read_binary (protocol, buf,
419 len, error);
420 }
421
422 #define THRIFT_SKIP_RESULT_OR_RETURN(_RES, _CALL) \
423 { \
424 gint32 _x = (_CALL); \
425 if (_x < 0) { return _x; } \
426 (_RES) += _x; \
427 }
428
429 gint32
430 thrift_protocol_skip (ThriftProtocol *protocol, ThriftType type, GError **error)
431 {
432 switch (type)
433 {
434 case T_BOOL:
435 {
436 gboolean boolv;
437 return thrift_protocol_read_bool (protocol, &boolv, error);
438 }
439 case T_BYTE:
440 {
441 gint8 bytev;
442 return thrift_protocol_read_byte (protocol, &bytev, error);
443 }
444
445 case T_I16:
446 {
447 gint16 i16;
448 return thrift_protocol_read_i16 (protocol, &i16, error);
449 }
450 case T_I32:
451 {
452 gint32 i32;
453 return thrift_protocol_read_i32 (protocol, &i32, error);
454 }
455 case T_I64:
456 {
457 gint64 i64;
458 return thrift_protocol_read_i64 (protocol, &i64, error);
459 }
460 case T_DOUBLE:
461 {
462 gdouble dub;
463 return thrift_protocol_read_double (protocol, &dub, error);
464 }
465 case T_STRING:
466 {
467 gpointer data;
468 guint32 len;
469 gint32 ret = thrift_protocol_read_binary (protocol, &data, &len, error);
470 g_free (data);
471 return ret;
472 }
473 case T_STRUCT:
474 {
475 gint32 result = 0;
476 gchar *name;
477 gint16 fid;
478 ThriftType ftype;
479 THRIFT_SKIP_RESULT_OR_RETURN(result,
480 thrift_protocol_read_struct_begin (protocol, &name, error))
481 while (1)
482 {
483 THRIFT_SKIP_RESULT_OR_RETURN(result,
484 thrift_protocol_read_field_begin (protocol, &name, &ftype,
485 &fid, error))
486 if (ftype == T_STOP)
487 {
488 break;
489 }
490 THRIFT_SKIP_RESULT_OR_RETURN(result,
491 thrift_protocol_skip (protocol, ftype, error))
492 THRIFT_SKIP_RESULT_OR_RETURN(result,
493 thrift_protocol_read_field_end (protocol, error))
494 }
495 THRIFT_SKIP_RESULT_OR_RETURN(result,
496 thrift_protocol_read_struct_end (protocol, error))
497 return result;
498 }
499 case T_SET:
500 {
501 gint32 result = 0;
502 ThriftType elem_type;
503 guint32 i, size;
504 THRIFT_SKIP_RESULT_OR_RETURN(result,
505 thrift_protocol_read_set_begin (protocol, &elem_type, &size,
506 error))
507 for (i = 0; i < size; i++)
508 {
509 THRIFT_SKIP_RESULT_OR_RETURN(result,
510 thrift_protocol_skip (protocol, elem_type, error))
511 }
512 THRIFT_SKIP_RESULT_OR_RETURN(result,
513 thrift_protocol_read_set_end (protocol, error))
514 return result;
515 }
516 case T_MAP:
517 {
518 gint32 result = 0;
519 ThriftType elem_type;
520 ThriftType key_type;
521 guint32 i, size;
522 THRIFT_SKIP_RESULT_OR_RETURN(result,
523 thrift_protocol_read_map_begin (protocol, &key_type, &elem_type, &size,
524 error))
525 for (i = 0; i < size; i++)
526 {
527 THRIFT_SKIP_RESULT_OR_RETURN(result,
528 thrift_protocol_skip (protocol, key_type, error))
529 THRIFT_SKIP_RESULT_OR_RETURN(result,
530 thrift_protocol_skip (protocol, elem_type, error))
531 }
532 THRIFT_SKIP_RESULT_OR_RETURN(result,
533 thrift_protocol_read_map_end (protocol, error))
534 return result;
535 }
536 case T_LIST:
537 {
538 gint32 result = 0;
539 ThriftType elem_type;
540 guint32 i, size;
541 THRIFT_SKIP_RESULT_OR_RETURN(result,
542 thrift_protocol_read_list_begin (protocol, &elem_type, &size,
543 error))
544 for (i = 0; i < size; i++)
545 {
546 THRIFT_SKIP_RESULT_OR_RETURN(result,
547 thrift_protocol_skip (protocol, elem_type, error))
548 }
549 THRIFT_SKIP_RESULT_OR_RETURN(result,
550 thrift_protocol_read_list_end (protocol, error))
551 return result;
552 }
553 default:
554 break;
555 }
556
557 g_set_error (error, THRIFT_PROTOCOL_ERROR,
558 THRIFT_PROTOCOL_ERROR_INVALID_DATA,
559 "unrecognized type");
560 return -1;
561 }
562
563 /* define the GError domain for Thrift protocols */
564 GQuark
565 thrift_protocol_error_quark (void)
566 {
567 return g_quark_from_static_string (THRIFT_PROTOCOL_ERROR_DOMAIN);
568 }
569
570
571 static void
572 thrift_protocol_init (ThriftProtocol *protocol)
573 {
574 protocol->transport = NULL;
575 }
576
577 static void
578 thrift_protocol_dispose (GObject *gobject)
579 {
580 ThriftProtocol *self = THRIFT_PROTOCOL (gobject);
581
582 g_clear_object(&self->transport);
583
584 /* Always chain up to the parent class; there is no need to check if
585 * the parent class implements the dispose() virtual function: it is
586 * always guaranteed to do so
587 */
588 G_OBJECT_CLASS (thrift_protocol_parent_class)->dispose(gobject);
589 }
590
591 static void
592 thrift_protocol_class_init (ThriftProtocolClass *cls)
593 {
594 GObjectClass *gobject_class = G_OBJECT_CLASS (cls);
595
596 gobject_class->get_property = thrift_protocol_get_property;
597 gobject_class->set_property = thrift_protocol_set_property;
598 gobject_class->dispose = thrift_protocol_dispose;
599
600 g_object_class_install_property (gobject_class,
601 PROP_THRIFT_PROTOCOL_TRANSPORT,
602 g_param_spec_object ("transport", "Transport", "Thrift Transport",
603 THRIFT_TYPE_TRANSPORT,
604 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
605
606 cls->write_message_begin = thrift_protocol_write_message_begin;
607 cls->write_message_end = thrift_protocol_write_message_end;
608 cls->write_struct_begin = thrift_protocol_write_struct_begin;
609 cls->write_struct_end = thrift_protocol_write_struct_end;
610 cls->write_field_begin = thrift_protocol_write_field_begin;
611 cls->write_field_end = thrift_protocol_write_field_end;
612 cls->write_field_stop = thrift_protocol_write_field_stop;
613 cls->write_map_begin = thrift_protocol_write_map_begin;
614 cls->write_map_end = thrift_protocol_write_map_end;
615 cls->write_list_begin = thrift_protocol_write_list_begin;
616 cls->write_list_end = thrift_protocol_write_list_end;
617 cls->write_set_begin = thrift_protocol_write_set_begin;
618 cls->write_set_end = thrift_protocol_write_set_end;
619 cls->write_bool = thrift_protocol_write_bool;
620 cls->write_byte = thrift_protocol_write_byte;
621 cls->write_i16 = thrift_protocol_write_i16;
622 cls->write_i32 = thrift_protocol_write_i32;
623 cls->write_i64 = thrift_protocol_write_i64;
624 cls->write_double = thrift_protocol_write_double;
625 cls->write_string = thrift_protocol_write_string;
626 cls->write_binary = thrift_protocol_write_binary;
627 cls->read_message_begin = thrift_protocol_read_message_begin;
628 cls->read_message_end = thrift_protocol_read_message_end;
629 cls->read_struct_begin = thrift_protocol_read_struct_begin;
630 cls->read_struct_end = thrift_protocol_read_struct_end;
631 cls->read_field_begin = thrift_protocol_read_field_begin;
632 cls->read_field_end = thrift_protocol_read_field_end;
633 cls->read_map_begin = thrift_protocol_read_map_begin;
634 cls->read_map_end = thrift_protocol_read_map_end;
635 cls->read_list_begin = thrift_protocol_read_list_begin;
636 cls->read_set_begin = thrift_protocol_read_set_begin;
637 cls->read_set_end = thrift_protocol_read_set_end;
638 cls->read_bool = thrift_protocol_read_bool;
639 cls->read_byte = thrift_protocol_read_byte;
640 cls->read_i16 = thrift_protocol_read_i16;
641 cls->read_i32 = thrift_protocol_read_i32;
642 cls->read_i64 = thrift_protocol_read_i64;
643 cls->read_double = thrift_protocol_read_double;
644 cls->read_string = thrift_protocol_read_string;
645 cls->read_binary = thrift_protocol_read_binary;
646 }