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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
|
/*
* Copyright 2014-2015 James Geboski <jgeboski@gmail.com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _FACEBOOK_THRIFT_H_
#define _FACEBOOK_THRIFT_H_
/**
* SECTION:thrift
* @section_id: facebook-thrift
* @short_description: <filename>facebook-thrift.h</filename>
* @title: Thrift Reader/Writer
*
* The Thrift reader/writer.
*/
#include <glib.h>
#include <glib-object.h>
#define FB_TYPE_THRIFT (fb_thrift_get_type())
#define FB_THRIFT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), FB_TYPE_THRIFT, FbThrift))
#define FB_THRIFT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), FB_TYPE_THRIFT, FbThriftClass))
#define FB_IS_THRIFT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), FB_TYPE_THRIFT))
#define FB_IS_THRIFT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), FB_TYPE_THRIFT))
#define FB_THRIFT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), FB_TYPE_THRIFT, FbThriftClass))
typedef struct _FbThrift FbThrift;
typedef struct _FbThriftClass FbThriftClass;
typedef struct _FbThriftPrivate FbThriftPrivate;
/**
* FbThriftType:
* @FB_THRIFT_TYPE_STOP: A stopper for certain types.
* @FB_THRIFT_TYPE_VOID: A void or empty value.
* @FB_THRIFT_TYPE_BOOL: A boolean (#TRUE or #FALSE).
* @FB_THRIFT_TYPE_BYTE: A signed 8-bit integer.
* @FB_THRIFT_TYPE_DOUBLE: A 64-bit floating point number.
* @FB_THRIFT_TYPE_I16: A signed 16-bit integer.
* @FB_THRIFT_TYPE_I32: A signed 32-bit integer.
* @FB_THRIFT_TYPE_I64: A signed 64-bit integer.
* @FB_THRIFT_TYPE_STRING: A UTF-8 encoded string.
* @FB_THRIFT_TYPE_STRUCT: A set of typed fields.
* @FB_THRIFT_TYPE_MAP: A map of unique keys to values.
* @FB_THRIFT_TYPE_SET: A unique set of values.
* @FB_THRIFT_TYPE_LIST: A ordered list of values.
* @FB_THRIFT_TYPE_ENUM: A 32-bit enumerated list.
* @FB_THRIFT_TYPE_UNKNOWN: An unknown type.
*
* The Thrift data types.
*/
typedef enum
{
FB_THRIFT_TYPE_STOP = 0,
FB_THRIFT_TYPE_VOID = 1,
FB_THRIFT_TYPE_BOOL = 2,
FB_THRIFT_TYPE_BYTE = 3,
FB_THRIFT_TYPE_DOUBLE = 4,
FB_THRIFT_TYPE_I16 = 6,
FB_THRIFT_TYPE_I32 = 8,
FB_THRIFT_TYPE_I64 = 10,
FB_THRIFT_TYPE_STRING = 11,
FB_THRIFT_TYPE_STRUCT = 12,
FB_THRIFT_TYPE_MAP = 13,
FB_THRIFT_TYPE_SET = 14,
FB_THRIFT_TYPE_LIST = 15,
FB_THRIFT_TYPE_ENUM = 16,
FB_THRIFT_TYPE_UNKNOWN
} FbThriftType;
/**
* FbThrift:
*
* Represents a reader/writer for compact Thrift data.
*/
struct _FbThrift
{
/*< private >*/
GObject parent;
FbThriftPrivate *priv;
};
/**
* FbThriftClass:
*
* The base class for all #FbThrift's.
*/
struct _FbThriftClass
{
/*< private >*/
GObjectClass parent_class;
};
/**
* fb_thrift_get_type:
*
* Returns: The #GType for an #FbThrift.
*/
GType
fb_thrift_get_type(void);
/**
* fb_thrift_new:
* @bytes: The #GByteArray to read or write.
* @offset: The offset in bytes of the data in @bytes.
*
* Creates a new #FbThrift. The returned #FbThrift should be freed with
* #g_object_unref() when no longer needed. This will optionally use a
* #GByteArray at an offset, rather than a newly created and internal
* #GByteArray.
*
* Returns: The new #FbThrift.
*/
FbThrift *
fb_thrift_new(GByteArray *bytes, guint offset);
/**
* fb_thrift_get_bytes:
* @thft: The #FbThrift.
*
* Gets the underlying #GByteArray of an #FbThrift.
*
* Returns: The #GByteArray.
*/
const GByteArray *
fb_thrift_get_bytes(FbThrift *thft);
/**
* fb_thrift_get_pos:
* @thft: The #FbThrift.
*
* Gets the cursor position of an #FbThrift.
*
* Returns: The cursor position.
*/
guint
fb_thrift_get_pos(FbThrift *thft);
/**
* fb_thrift_set_pos:
* @thft: The #FbThrift.
* @pos: The position.
*
* Sets the cursor position of an #FbThrift.
*
* Returns: The #GByteArray.
*/
void
fb_thrift_set_pos(FbThrift *thft, guint pos);
/**
* fb_thrift_reset:
* @thft: The #FbThrift.
*
* Resets the cursor position of an #FbThrift.
*
* Returns: The #GByteArray.
*/
void
fb_thrift_reset(FbThrift *thft);
/**
* fb_thrift_read:
* @thft: The #FbThrift.
* @data: The data buffer.
* @size: The size of @buffer.
*
* Reads data from the #FbThrift into a buffer. If @data is #NULL, this
* will simply advance the cursor position.
*
* Returns: #TRUE if the data was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read(FbThrift *thft, gpointer data, guint size);
/**
* fb_thrift_read_bool:
* @thft: The #FbThrift.
* @value: The return location for the value or #NULL.
*
* Reads a boolean value from the #FbThrift. If @value is #NULL, this
* will simply advance the cursor position.
*
* Returns: #TRUE if the value was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_bool(FbThrift *thft, gboolean *value);
/**
* fb_thrift_read_byte:
* @thft: The #FbThrift.
* @value: The return location for the value or #NULL.
*
* Reads an 8-bit integer value from the #FbThrift. If @value is #NULL,
* this will simply advance the cursor position.
*
* Returns: #TRUE if the value was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_byte(FbThrift *thft, guint8 *value);
/**
* fb_thrift_read_dbl:
* @thft: The #FbThrift.
* @value: The return location for the value or #NULL.
*
* Reads a 64-bit floating point value from the #FbThrift. If @value
* is #NULL, this will simply advance the cursor position.
*
* Returns: #TRUE if the value was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_dbl(FbThrift *thft, gdouble *value);
/**
* fb_thrift_read_i16:
* @thft: The #FbThrift.
* @value: The return location for the value or #NULL.
*
* Reads a signed 16-bit integer value from the #FbThrift. This will
* convert the integer from the zig-zag format. If @value is #NULL,
* this will simply advance the cursor position.
*
* Returns: #TRUE if the value was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_i16(FbThrift *thft, gint16 *value);
/**
* fb_thrift_read_vi16:
* @thft: The #FbThrift.
* @value: The return location for the value or #NULL.
*
* Reads a 16-bit integer value from the #FbThrift. This reads the raw
* integer value without converting it from the zig-zag format. If
* @value is #NULL, this will simply advance the cursor position.
*
* Returns: #TRUE if the value was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_vi16(FbThrift *thft, guint16 *value);
/**
* fb_thrift_read_i32:
* @thft: The #FbThrift.
* @value: The return location for the value or #NULL.
*
* Reads a signed 32-bit integer value from the #FbThrift. This will
* convert the integer from the zig-zag format. If @value is #NULL,
* this will simply advance the cursor position.
*
* Returns: #TRUE if the value was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_i32(FbThrift *thft, gint32 *value);
/**
* fb_thrift_read_vi32:
* @thft: The #FbThrift.
* @value: The return location for the value or #NULL.
*
* Reads a 32-bit integer value from the #FbThrift. This reads the raw
* integer value without converting it from the zig-zag format. If
* @value is #NULL, this will simply advance the cursor position.
*
* Returns: #TRUE if the value was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_vi32(FbThrift *thft, guint32 *value);
/**
* fb_thrift_read_i64:
* @thft: The #FbThrift.
* @value: The return location for the value or #NULL.
*
* Reads a signed 64-bit integer value from the #FbThrift. This will
* convert the integer from the zig-zag format. If @value is #NULL,
* this will simply advance the cursor position.
*
* Returns: #TRUE if the value was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_i64(FbThrift *thft, gint64 *value);
/**
* fb_thrift_read_vi64:
* @thft: The #FbThrift.
* @value: The return location for the value or #NULL.
*
* Reads a 64-bit integer value from the #FbThrift. This reads the raw
* integer value without converting it from the zig-zag format. If
* @value is #NULL, this will simply advance the cursor position.
*
* Returns: #TRUE if the value was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_vi64(FbThrift *thft, guint64 *value);
/**
* fb_thrift_read_str:
* @thft: The #FbThrift.
* @value: The return location for the value or #NULL.
*
* Reads a string value from the #FbThrift. The value returned to
* @value should be freed with #g_free() when no longer needed. If
* @value is #NULL, this will simply advance the cursor position.
*
* Returns: #TRUE if the value was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_str(FbThrift *thft, gchar **value);
/**
* fb_thrift_read_field:
* @thft: The #FbThrift.
* @type: The return location for the #FbThriftType.
* @id: The return location for the identifier or #NULL.
*
* Reads a field header from the #FbThrift.
*
* Returns: #TRUE if the field header was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_field(FbThrift *thft, FbThriftType *type, gint16 *id);
/**
* fb_thrift_read_stop:
* @thft: The #FbThrift.
*
* Reads a field stop from the #FbThrift.
*
* Returns: #TRUE if the field stop was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_stop(FbThrift *thft);
/**
* fb_thrift_read_isstop:
* @thft: The #FbThrift.
*
* Determines if the next byte of the #FbThrift is a field stop.
*
* Returns: #TRUE if the next byte is a field stop, otherwise #FALSE.
*/
gboolean
fb_thrift_read_isstop(FbThrift *thft);
/**
* fb_thrift_read_list:
* @thft: The #FbThrift.
* @type: The return location for the #FbThriftType.
* @size: The return location for the size.
*
* Reads a list header from the #FbThrift.
*
* Returns: #TRUE if the list header was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_list(FbThrift *thft, FbThriftType *type, guint *size);
/**
* fb_thrift_read_map:
* @thft: The #FbThrift.
* @ktype: The return location for the key #FbThriftType.
* @vtype: The return location for the value #FbThriftType.
* @size: The return location for the size.
*
* Reads a map header from the #FbThrift.
*
* Returns: #TRUE if the map header was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_map(FbThrift *thft, FbThriftType *ktype, FbThriftType *vtype,
guint *size);
/**
* fb_thrift_read_set:
* @thft: The #FbThrift.
* @type: The return location for the #FbThriftType.
* @size: The return location for the size.
*
* Reads a set header from the #FbThrift.
*
* Returns: #TRUE if the set header was read, otherwise #FALSE.
*/
gboolean
fb_thrift_read_set(FbThrift *thft, FbThriftType *type, guint *size);
/**
* fb_thrift_write:
* @thft: The #FbThrift.
* @data: The data buffer.
* @size: The size of @buffer.
*
* Writes data to the #FbThrift.
*/
void
fb_thrift_write(FbThrift *thft, gconstpointer data, guint size);
/**
* fb_thrift_write_bool:
* @thft: The #FbThrift.
* @value: The value.
*
* Writes a boolean value to the #FbThrift.
*/
void
fb_thrift_write_bool(FbThrift *thft, gboolean value);
/**
* fb_thrift_write_byte:
* @thft: The #FbThrift.
* @value: The value.
*
* Writes an 8-bit integer value to the #FbThrift.
*/
void
fb_thrift_write_byte(FbThrift *thft, guint8 value);
/**
* fb_thrift_write_dbl:
* @thft: The #FbThrift.
* @value: The value.
*
* Writes a 64-bit floating point value to the #FbThrift.
*/
void
fb_thrift_write_dbl(FbThrift *thft, gdouble value);
/**
* fb_thrift_write_i16:
* @thft: The #FbThrift.
* @value: The value.
*
* Writes a signed 16-bit integer value to the #FbThrift. This will
* convert the integer to the zig-zag format.
*/
void
fb_thrift_write_i16(FbThrift *thft, gint16 value);
/**
* fb_thrift_write_vi16:
* @thft: The #FbThrift.
* @value: The value.
*
* Writes a 16-bit integer value to the #FbThrift. This writes the raw
* integer value without converting it to the zig-zag format.
*/
void
fb_thrift_write_vi16(FbThrift *thft, guint16 value);
/**
* fb_thrift_write_i32:
* @thft: The #FbThrift.
* @value: The value.
*
* Writes a signed 32-bit integer value to the #FbThrift. This will
* convert the integer to the zig-zag format.
*/
void
fb_thrift_write_i32(FbThrift *thft, gint32 value);
/**
* fb_thrift_write_vi32:
* @thft: The #FbThrift.
* @value: The value.
*
* Writes a 32-bit integer value to the #FbThrift. This writes the raw
* integer value without converting it to the zig-zag format.
*/
void
fb_thrift_write_vi32(FbThrift *thft, guint32 value);
/**
* fb_thrift_write_i64:
* @thft: The #FbThrift.
* @value: The value.
*
* Writes a signed 64-bit integer value to the #FbThrift. This will
* convert the integer to the zig-zag format.
*/
void
fb_thrift_write_i64(FbThrift *thft, gint64 value);
/**
* fb_thrift_write_vi64:
* @thft: The #FbThrift.
* @value: The value.
*
* Writes a 64-bit integer value to the #FbThrift. This writes the raw
* integer value without converting it to the zig-zag format.
*/
void
fb_thrift_write_vi64(FbThrift *thft, guint64 value);
/**
* fb_thrift_write_str:
* @thft: The #FbThrift.
* @value: The value.
*
* Writes a string value to the #FbThrift.
*/
void
fb_thrift_write_str(FbThrift *thft, const gchar *value);
/**
* fb_thrift_write_field:
* @thft: The #FbThrift.
* @type: The #FbThriftType.
* @id: The identifier.
*
* Writes a field header to the #FbThrift.
*/
void
fb_thrift_write_field(FbThrift *thft, FbThriftType type, gint16 id);
/**
* fb_thrift_write_stop:
* @thft: The #FbThrift.
*
* Writes a field stop to the #FbThrift.
*/
void
fb_thrift_write_stop(FbThrift *thft);
/**
* fb_thrift_write_list:
* @thft: The #FbThrift.
* @type: The #FbThriftType.
* @size: The size.
*
* Writes a list header to the #FbThrift.
*/
void
fb_thrift_write_list(FbThrift *thft, FbThriftType type, guint size);
/**
* fb_thrift_write_map:
* @thft: The #FbThrift.
* @ktype: The key #FbThriftType.
* @vtype: The value #FbThriftType.
* @size: The size.
*
* Writes a map header to the #FbThrift.
*/
void
fb_thrift_write_map(FbThrift *thft, FbThriftType ktype, FbThriftType vtype,
guint size);
/**
* fb_thrift_write_set:
* @thft: The #FbThrift.
* @type: The #FbThriftType.
* @size: The size.
*
* Writes a set header to the #FbThrift.
*/
void
fb_thrift_write_set(FbThrift *thft, FbThriftType type, guint size);
/**
* fb_thrift_t2ct:
* @type: The #FbThriftType.
*
* Converts a #FbThriftType to a compact type.
*
* Return: The equivalent compact type.
*/
guint8
fb_thrift_t2ct(FbThriftType type);
/**
* fb_thrift_ct2t:
* @type: The compact type.
*
* Converts a compact type to an #FbThriftType.
*
* Return: The equivalent #FbThriftType.
*/
FbThriftType
fb_thrift_ct2t(guint8 type);
#endif /* _FACEBOOK_THRIFT_H_ */
|