-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_thrift.c
1544 lines (1417 loc) · 56.6 KB
/
pg_thrift.c
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <postgres.h>
#include <port.h>
#include <catalog/pg_type.h>
#include <utils/builtins.h>
#include <utils/array.h>
#include <utils/lsyscache.h>
#include <utils/jsonb.h>
#include "pg_thrift.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(thrift_binary_get_bool);
PG_FUNCTION_INFO_V1(thrift_binary_get_byte);
PG_FUNCTION_INFO_V1(thrift_binary_get_double);
PG_FUNCTION_INFO_V1(thrift_binary_get_int16);
PG_FUNCTION_INFO_V1(thrift_binary_get_int32);
PG_FUNCTION_INFO_V1(thrift_binary_get_int64);
PG_FUNCTION_INFO_V1(thrift_binary_get_string);
PG_FUNCTION_INFO_V1(thrift_binary_get_struct_bytea);
PG_FUNCTION_INFO_V1(thrift_binary_get_list_bytea);
PG_FUNCTION_INFO_V1(thrift_binary_get_set_bytea);
PG_FUNCTION_INFO_V1(thrift_binary_get_map_bytea);
PG_FUNCTION_INFO_V1(thrift_compact_get_bool);
PG_FUNCTION_INFO_V1(thrift_compact_get_byte);
PG_FUNCTION_INFO_V1(thrift_compact_get_double);
PG_FUNCTION_INFO_V1(thrift_compact_get_int16);
PG_FUNCTION_INFO_V1(thrift_compact_get_int32);
PG_FUNCTION_INFO_V1(thrift_compact_get_int64);
PG_FUNCTION_INFO_V1(thrift_compact_get_string);
PG_FUNCTION_INFO_V1(thrift_compact_get_struct_bytea);
PG_FUNCTION_INFO_V1(thrift_compact_get_list_bytea);
PG_FUNCTION_INFO_V1(thrift_compact_get_set_bytea);
PG_FUNCTION_INFO_V1(thrift_compact_get_map_bytea);
PG_FUNCTION_INFO_V1(parse_thrift_binary_boolean);
PG_FUNCTION_INFO_V1(parse_thrift_binary_string);
PG_FUNCTION_INFO_V1(parse_thrift_binary_bytes);
PG_FUNCTION_INFO_V1(parse_thrift_binary_int16);
PG_FUNCTION_INFO_V1(parse_thrift_binary_int32);
PG_FUNCTION_INFO_V1(parse_thrift_binary_int64);
PG_FUNCTION_INFO_V1(parse_thrift_binary_double);
PG_FUNCTION_INFO_V1(parse_thrift_binary_list_bytea);
PG_FUNCTION_INFO_V1(parse_thrift_binary_map_bytea);
PG_FUNCTION_INFO_V1(thrift_binary_in);
PG_FUNCTION_INFO_V1(thrift_binary_out);
PG_FUNCTION_INFO_V1(get_thrift_binary_type);
PG_FUNCTION_INFO_V1(get_thrift_binary_value);
PG_FUNCTION_INFO_V1(parse_thrift_compact_boolean);
PG_FUNCTION_INFO_V1(parse_thrift_compact_string);
PG_FUNCTION_INFO_V1(parse_thrift_compact_bytes);
PG_FUNCTION_INFO_V1(parse_thrift_compact_int16);
PG_FUNCTION_INFO_V1(parse_thrift_compact_int32);
PG_FUNCTION_INFO_V1(parse_thrift_compact_int64);
PG_FUNCTION_INFO_V1(parse_thrift_compact_double);
PG_FUNCTION_INFO_V1(parse_thrift_compact_list_bytea);
PG_FUNCTION_INFO_V1(parse_thrift_compact_map_bytea);
PG_FUNCTION_INFO_V1(jsonb_to_thrift_binary);
//PG_FUNCTION_INFO_V1(thrift_binary_to_jsonb);
Datum thrift_binary_to_json(int type, uint8* start, uint8* end);
Datum jsonb_to_thrift_binary_helper(char* type, JsonbValue jbv);
Datum thrift_binary_decode(uint8* data, Size size, int16 field_id, int8 type_id);
Datum thrift_compact_decode(uint8* data, Size size, int16 field_id, int8 type_id);
Datum parse_binary_field(uint8* start, uint8* end, int8 type_id);
Datum parse_compact_field(uint8* start, uint8* end, int8 type_id);
uint8* skip_binary_field(uint8* start, uint8* end, int8 type_id);
uint8* skip_compact_field(uint8* start, uint8* end, int8 type_id);
Datum parse_thrift_binary_boolean_internal(uint8* start, uint8* end);
Datum parse_thrift_binary_string_internal(uint8* start, uint8* end);
Datum parse_thrift_binary_bytes_internal(uint8* start, uint8* end);
Datum parse_thrift_binary_int16_internal(uint8* start, uint8* end);
Datum parse_thrift_binary_int32_internal(uint8* start, uint8* end);
Datum parse_thrift_binary_int64_internal(uint8* start, uint8* end);
Datum parse_thrift_binary_double_internal(uint8* start, uint8* end);
Datum parse_thrift_binary_struct_bytea_internal(uint8* start, uint8* end);
Datum parse_thrift_binary_list_bytea_internal(uint8* start, uint8* end);
Datum parse_thrift_binary_map_bytea_internal(uint8* start, uint8* end);
Datum parse_thrift_compact_string_internal(uint8* start, uint8* end);
Datum parse_thrift_compact_bytes_internal(uint8* start, uint8* end);
Datum parse_thrift_compact_int16_internal(uint8* start, uint8* end);
Datum parse_thrift_compact_int32_internal(uint8* start, uint8* end);
Datum parse_thrift_compact_int64_internal(uint8* start, uint8* end);
Datum parse_thrift_compact_struct_bytea_internal(uint8* start, uint8* end);
Datum parse_thrift_compact_list_bytea_internal(uint8* start, uint8* end);
Datum parse_thrift_compact_map_bytea_internal(uint8* start, uint8* end);
uint8* encode_binary_bool(char* value);
uint8* encode_binary_int16(char* value);
uint8* encode_binary_int32(char* value);
uint8* encode_binary_int64(char* value);
uint8* encode_binary_double(char* value);
uint8* encode_binary_string(char* value);
uint8* encode_binary_byte(char* value);
uint8 char_to_int8(char c);
uint8* string_to_bytes(char* value);
char convert_int8_to_char(uint8 value, bool first_half);
char* bytes_to_string(uint8* start, int32 len);
bool is_big_endian(void);
int64 parse_int_helper(uint8* start, uint8* end, int len);
void swap_bytes(char* bytes, int len);
int64 parse_varint_helper(uint8* start, uint8* end, int64* len_description);
uint8 compact_list_type_to_struct_type(uint8 element_type);
bool is_big_endian() {
uint32 i = 1;
char *c = (char*)&i;
return !(*c);
}
void swap_bytes(char* bytes, int len) {
for (int i = 0; i < len/2; i++) {
char tmp = bytes[i];
bytes[i] = bytes[len - 1 - i];
bytes[len - 1 - i] = tmp;
}
}
int64 parse_int_helper(uint8* start, uint8* end, int len) {
if (start + len > end) {
elog(ERROR, "Invalid thrift format for int");
}
int64 val = 0;
for (int i = 0; i < len; i++) {
val = (val << 8) + *(start + i);
}
return val;
}
// returns value from varint encoded zigzag int
int64 parse_varint_helper(uint8* start, uint8* end, int64* len_length) {
uint8* p = start;
int64 val = 0;
while (p < end) {
val = (((*p) & 0x7f) << 7*(p - start)) + val;
if ((*p) & 0x80) p++;
else break;
}
*len_length = p - start + 1;
return (val >> 1) ^ -(val & 1);
}
// skip field is needed in list(set, map) and struct,
// but types are different, this helper does the mapping
uint8 compact_list_type_to_struct_type(uint8 element_type) {
if (element_type == 2) {
return PG_THRIFT_COMPACT_BOOL;
}
if (element_type == 3) {
return PG_THRIFT_COMPACT_BYTE;
}
if (element_type == 4) {
return PG_THRIFT_COMPACT_DOUBLE;
}
if (element_type == 6) {
return PG_THRIFT_COMPACT_INT16;
}
if (element_type == 8) {
return PG_THRIFT_COMPACT_INT32;
}
if (element_type == 10) {
return PG_THRIFT_COMPACT_INT64;
}
if (element_type == 11) {
return PG_THRIFT_COMPACT_STRING;
}
if (element_type == 12) {
return PG_THRIFT_COMPACT_STRUCT;
}
if (element_type == 13) {
return PG_THRIFT_COMPACT_MAP;
}
if (element_type == 14) {
return PG_THRIFT_COMPACT_SET;
}
if (element_type == 15) {
return PG_THRIFT_COMPACT_LIST;
}
elog(ERROR, "Invalid thrift compact element type");
}
Datum parse_thrift_binary_boolean(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_binary_boolean_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_binary_boolean_internal(uint8* start, uint8* end) {
if (start >= end) {
elog(ERROR, "Invalid thrift binary format for bool");
}
PG_RETURN_BOOL(*start);
}
Datum parse_thrift_binary_string(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_binary_bytes_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_binary_bytes(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_binary_bytes_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_binary_bytes_internal(uint8* start, uint8* end) {
int32 len = parse_int_helper(start, end, BYTE_LEN);
if (start + BYTE_LEN + len - 1 >= end) {
elog(ERROR, "Invalid thrift format for bytes");
}
bytea* ret = palloc(len + VARHDRSZ);
memcpy(VARDATA(ret), start + BYTE_LEN, len);
SET_VARSIZE(ret, len + VARHDRSZ);
PG_RETURN_POINTER(ret);
}
Datum parse_thrift_compact_string(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_compact_bytes_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_compact_bytes(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_compact_bytes_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_compact_bytes_internal(uint8* start, uint8* end) {
int64 len_length = 0;
int64 len = parse_varint_helper(start, end, &len_length);
if (start + len_length + len - 1 >= end) {
elog(ERROR, "Invalid thrift compact format for bytes");
}
bytea* ret = palloc(len + VARHDRSZ);
memcpy(VARDATA(ret), start + len_length, len);
SET_VARSIZE(ret, len + VARHDRSZ);
PG_RETURN_POINTER(ret);
}
Datum parse_thrift_binary_string_internal(uint8* start, uint8* end) {
int32 len = parse_int_helper(start, end, BYTE_LEN);
if (start + BYTE_LEN + len - 1 >= end) {
elog(ERROR, "Invalid thrift format for string");
}
char* ret = palloc(len + 1);
memset(ret, 0, len + 1);
memcpy(ret, start + BYTE_LEN, len);
PG_RETURN_CSTRING(ret);
}
Datum parse_thrift_binary_double(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_binary_double_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_compact_double(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
// binary and compact are same for double
return parse_thrift_binary_double_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_binary_double_internal(uint8* start, uint8* end) {
if (start + DOUBLE_LEN - 1 >= end) {
elog(ERROR, "Invalid thrift format for double");
}
float8 ret;
memcpy(&ret, start, DOUBLE_LEN);
if (!is_big_endian()) {
swap_bytes((char*)&ret, DOUBLE_LEN);
}
PG_RETURN_FLOAT8(ret);
}
Datum parse_thrift_binary_int16(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_binary_int16_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_binary_int16_internal(uint8* start, uint8* end) {
PG_RETURN_INT16(parse_int_helper(start, end, INT16_LEN));
}
Datum parse_thrift_compact_int16(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_compact_int16_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_compact_int16_internal(uint8* start, uint8* end) {
int64 len = 0;
PG_RETURN_INT16(parse_varint_helper(start, end, &len));
}
Datum parse_thrift_binary_int32(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_binary_int32_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_binary_int32_internal(uint8* start, uint8* end) {
PG_RETURN_INT32(parse_int_helper(start, end, INT32_LEN));
}
Datum parse_thrift_compact_int32(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_compact_int32_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_compact_int32_internal(uint8* start, uint8* end) {
int64 len = 0;
PG_RETURN_INT32(parse_varint_helper(start, end, &len));
}
Datum parse_thrift_binary_int64(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_binary_int64_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_binary_int64_internal(uint8* start, uint8* end) {
PG_RETURN_INT64(parse_int_helper(start, end, INT64_LEN));
}
Datum parse_thrift_compact_int64(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_compact_int64_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_compact_int64_internal(uint8* start, uint8* end) {
int64 len = 0;
PG_RETURN_INT64(parse_varint_helper(start, end, &len));
}
Datum parse_thrift_binary_struct_bytea_internal(uint8* start, uint8* end) {
uint8* next_start = skip_binary_field(start, end, PG_THRIFT_BINARY_STRUCT);
int32 len = next_start - start;
bytea* ret = palloc(len + VARHDRSZ);
memcpy(VARDATA(ret), start, len);
SET_VARSIZE(ret, len + VARHDRSZ);
PG_RETURN_POINTER(ret);
}
Datum parse_thrift_compact_struct_bytea_internal(uint8* start, uint8* end) {
uint8* next_start = skip_compact_field(start, end, PG_THRIFT_COMPACT_STRUCT);
int32 len = next_start - start;
bytea* ret = palloc(len + VARHDRSZ);
memcpy(VARDATA(ret), start, len);
SET_VARSIZE(ret, len + VARHDRSZ);
PG_RETURN_POINTER(ret);
}
Datum parse_thrift_binary_list_bytea(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_binary_list_bytea_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_binary_list_bytea_internal(uint8* start, uint8* end) {
if (start + PG_THRIFT_TYPE_LEN + LIST_LEN - 1 >= end) {
elog(ERROR, "Invalid thrift binary format for list");
}
int8 element_type = *start;
int32 len = parse_int_helper(start + PG_THRIFT_TYPE_LEN, end, LIST_LEN);
uint8* curr = start + PG_THRIFT_TYPE_LEN + LIST_LEN;
Datum ret[THRIFT_RESULT_MAX_FIELDS];
bool null[THRIFT_RESULT_MAX_FIELDS], typbyval;
int16 typlen;
char typalign;
get_typlenbyvalalign(BYTEAOID, &typlen, &typbyval, &typalign);
for (int i = 0; i < len; i++) {
uint8* p = skip_binary_field(curr, end, element_type);
ret[i] = PointerGetDatum(palloc(p - curr + VARHDRSZ));
null[i] = false;
memcpy(VARDATA(ret[i]), curr, p - curr);
SET_VARSIZE(ret[i], p - curr + VARHDRSZ);
curr = p;
}
int dims[MAXDIM], lbs[MAXDIM], ndims = 1;
dims[0] = len;
lbs[0] = 1;
PG_RETURN_POINTER(
construct_md_array(ret, null, ndims, dims, lbs, BYTEAOID, typlen, typbyval, typalign)
);
}
Datum parse_thrift_compact_list_bytea(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_compact_list_bytea_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_compact_list_bytea_internal(uint8* start, uint8* end) {
if (start + PG_THRIFT_TYPE_LEN >= end) {
elog(ERROR, "Invalid thrift compact format for list");
}
uint8 size_type_id = parse_int_helper(start, end, PG_THRIFT_TYPE_LEN);
uint8 type_id = size_type_id & 0x0f;
uint32 len = (size_type_id & 0xf0) >> 4;
uint8* curr = start;
if (len == 0xf) {
int64 size_len = 0;
len = parse_varint_helper(start + PG_THRIFT_TYPE_LEN, end, &size_len);
curr = start + PG_THRIFT_TYPE_LEN + size_len;
} else {
curr = start + PG_THRIFT_TYPE_LEN;
}
Datum ret[THRIFT_RESULT_MAX_FIELDS];
bool null[THRIFT_RESULT_MAX_FIELDS], typbyval;
int16 typlen;
char typalign;
get_typlenbyvalalign(BYTEAOID, &typlen, &typbyval, &typalign);
for (int i = 0; i < len; i++) {
uint8* p = skip_compact_field(curr, end, compact_list_type_to_struct_type(type_id));
ret[i] = PointerGetDatum(palloc(p - curr + VARHDRSZ));
null[i] = false;
memcpy(VARDATA(ret[i]), curr, p - curr);
SET_VARSIZE(ret[i], p - curr + VARHDRSZ);
curr = p;
}
int dims[MAXDIM], lbs[MAXDIM], ndims = 1;
dims[0] = len;
lbs[0] = 1;
PG_RETURN_POINTER(
construct_md_array(ret, null, ndims, dims, lbs, BYTEAOID, typlen, typbyval, typalign)
);
}
Datum parse_thrift_binary_map_bytea(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_binary_map_bytea_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_binary_map_bytea_internal(uint8* start, uint8* end) {
if (start + 2*PG_THRIFT_TYPE_LEN + INT32_LEN - 1 >= end) {
elog(ERROR, "Invalid thrift binary format for map");
}
int32 len = parse_int_helper(start + 2*PG_THRIFT_TYPE_LEN, end, INT32_LEN);
uint8* curr = start + 2*PG_THRIFT_TYPE_LEN + INT32_LEN;
Datum ret[THRIFT_RESULT_MAX_FIELDS];
bool null[THRIFT_RESULT_MAX_FIELDS], typbyval;
int16 typlen;
char typalign;
get_typlenbyvalalign(BYTEAOID, &typlen, &typbyval, &typalign);
for (int i = 0; i < 2 * len; i++) {
int type_id = (i % 2 == 0? *start : *(start + 1));
uint8* p = skip_binary_field(curr, end, type_id);
ret[i] = PointerGetDatum(palloc(p - curr + VARHDRSZ));
null[i] = false;
memcpy(VARDATA(ret[i]), curr, p - curr);
SET_VARSIZE(ret[i], p - curr + VARHDRSZ);
curr = p;
}
int dims[MAXDIM], lbs[MAXDIM], ndims = 1;
dims[0] = 2*len;
lbs[0] = 1;
PG_RETURN_POINTER(
construct_md_array(ret, null, ndims, dims, lbs, BYTEAOID, typlen, typbyval, typalign)
);
}
Datum parse_thrift_compact_map_bytea(PG_FUNCTION_ARGS) {
bytea* data = PG_GETARG_BYTEA_P(0);
return parse_thrift_compact_map_bytea_internal((uint8*)VARDATA(data), (uint8*)VARDATA(data) + VARSIZE(data));
}
Datum parse_thrift_compact_map_bytea_internal(uint8* start, uint8* end) {
if (start >= end) {
elog(ERROR, "Invalid thrift compact format for map");
}
int64 size_len = 0;
int32 len = parse_varint_helper(start, end, &size_len);
uint8* curr = start + size_len;
Datum ret[THRIFT_RESULT_MAX_FIELDS];
bool null[THRIFT_RESULT_MAX_FIELDS], typbyval;
int16 typlen;
char typalign;
get_typlenbyvalalign(BYTEAOID, &typlen, &typbyval, &typalign);
uint8 type_id = *curr;
uint8 key_type_id = compact_list_type_to_struct_type((type_id & 0xf0) >> 4);
uint8 value_type_id = compact_list_type_to_struct_type(type_id & 0x0f);
curr = curr + PG_THRIFT_TYPE_LEN;
for (int i = 0; i < 2 * len; i++) {
int type_id = (i % 2 == 0? key_type_id : value_type_id);
uint8* p = skip_compact_field(curr, end, type_id);
ret[i] = PointerGetDatum(palloc(p - curr + VARHDRSZ));
null[i] = false;
memcpy(VARDATA(ret[i]), curr, p - curr);
SET_VARSIZE(ret[i], p - curr + VARHDRSZ);
curr = p;
}
int dims[MAXDIM], lbs[MAXDIM], ndims = 1;
dims[0] = 2*len;
lbs[0] = 1;
PG_RETURN_POINTER(
construct_md_array(ret, null, ndims, dims, lbs, BYTEAOID, typlen, typbyval, typalign)
);
}
Datum parse_binary_field(uint8* start, uint8* end, int8 type_id) {
if (type_id == PG_THRIFT_BINARY_BOOL) {
return parse_thrift_binary_boolean_internal(start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN, end);
}
if (type_id == PG_THRIFT_BINARY_BYTE || type_id == PG_THRIFT_BINARY_STRING) {
return parse_thrift_binary_bytes_internal(start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN, end);
}
if (type_id == PG_THRIFT_BINARY_DOUBLE) {
return parse_thrift_binary_double_internal(start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN, end);
}
if (type_id == PG_THRIFT_BINARY_INT16) {
return parse_thrift_binary_int16_internal(start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN, end);
}
if (type_id == PG_THRIFT_BINARY_INT32) {
return parse_thrift_binary_int32_internal(start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN, end);
}
if (type_id == PG_THRIFT_BINARY_INT64) {
return parse_thrift_binary_int64_internal(start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN, end);
}
if (type_id == PG_THRIFT_BINARY_STRUCT) {
return parse_thrift_binary_struct_bytea_internal(start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN, end);
}
if (type_id == PG_THRIFT_BINARY_LIST || type_id == PG_THRIFT_BINARY_SET) {
return parse_thrift_binary_list_bytea_internal(start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN, end);
}
if (type_id == PG_THRIFT_BINARY_MAP) {
return parse_thrift_binary_map_bytea_internal(start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN, end);
}
elog(ERROR, "Unsupported thrift binary type");
}
Datum parse_compact_field(uint8* start, uint8* end, int8 type_id) {
if (type_id == PG_THRIFT_COMPACT_BYTE) {
return parse_thrift_compact_bytes_internal(start, end);
}
if (type_id == PG_THRIFT_COMPACT_INT16) {
return parse_thrift_compact_int16_internal(start, end);
}
if (type_id == PG_THRIFT_COMPACT_INT32) {
return parse_thrift_compact_int32_internal(start, end);
}
if (type_id == PG_THRIFT_COMPACT_INT64) {
return parse_thrift_compact_int64_internal(start, end);
}
if (type_id == PG_THRIFT_COMPACT_DOUBLE) {
// double is same for binary and compact
return parse_thrift_binary_double_internal(start, end);
}
if (type_id == PG_THRIFT_COMPACT_STRING) {
return parse_thrift_compact_bytes_internal(start, end);
}
if (type_id == PG_THRIFT_COMPACT_LIST || type_id == PG_THRIFT_COMPACT_SET) {
return parse_thrift_compact_list_bytea_internal(start, end);
}
if (type_id == PG_THRIFT_COMPACT_MAP) {
return parse_thrift_compact_map_bytea_internal(start, end);
}
if (type_id == PG_THRIFT_COMPACT_STRUCT) {
return parse_thrift_compact_struct_bytea_internal(start, end);
}
elog(ERROR, "Unsupported thrift compact type");
}
// give start of data, end of data and type id,
// return pointer after its end
uint8* skip_binary_field(uint8* start, uint8* end, int8 field_type) {
uint8* ret = 0;
if (field_type == PG_THRIFT_BINARY_BOOL) {
ret = start + BOOL_LEN;
} else if (field_type == PG_THRIFT_BINARY_BYTE || field_type == PG_THRIFT_BINARY_STRING) {
int32 len = parse_int_helper(start, end, INT32_LEN);
ret = start + INT32_LEN + len;
} else if (field_type == PG_THRIFT_BINARY_DOUBLE) {
ret = start + DOUBLE_LEN;
} else if (field_type == PG_THRIFT_BINARY_INT16) {
ret = start + INT16_LEN;
} else if (field_type == PG_THRIFT_BINARY_INT32) {
ret = start + INT32_LEN;
} else if (field_type == PG_THRIFT_BINARY_INT64) {
ret = start + INT64_LEN;
} else if (field_type == PG_THRIFT_BINARY_STRUCT) {
ret = start;
while (true) {
if (*ret == 0) { ret += 1; break; }
int8 field_type = *ret;
ret = skip_binary_field(ret + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN, end, field_type);
}
} else if (field_type == PG_THRIFT_BINARY_MAP) {
int8 key_type = *start;
int8 value_type = *(start + PG_THRIFT_TYPE_LEN);
int32 len = parse_int_helper(start + 2*PG_THRIFT_TYPE_LEN, end, INT32_LEN);
ret = start + 2*PG_THRIFT_TYPE_LEN + INT32_LEN;
for (int i = 0; i < len; i++) {
ret = skip_binary_field(ret, end, key_type);
ret = skip_binary_field(ret, end, value_type);
}
} else if (field_type == PG_THRIFT_BINARY_SET || field_type == PG_THRIFT_BINARY_LIST) {
int32 len = parse_int_helper(start + PG_THRIFT_TYPE_LEN, end, INT32_LEN);
int8 field_type = *start;
ret = start + PG_THRIFT_TYPE_LEN + INT32_LEN;
for (int i = 0; i < len; i++) {
ret = skip_binary_field(ret, end, field_type);
}
}
if (ret > end) {
elog(ERROR, "Invalid thrift format");
}
return ret;
}
uint8* skip_compact_field(uint8* start, uint8* end, int8 field_type) {
uint8* ret = 0;
if (field_type == PG_THRIFT_COMPACT_BOOL) {
ret = start + BOOL_LEN;
} else if (field_type == PG_THRIFT_COMPACT_BYTE || field_type == PG_THRIFT_COMPACT_STRING) {
int64 len_length = 0;
int32 len = parse_varint_helper(start, end, &len_length);
ret = start + len_length + len;
} else if (field_type == PG_THRIFT_COMPACT_DOUBLE) {
ret = start + DOUBLE_LEN;
} else if (
field_type == PG_THRIFT_COMPACT_INT16 ||
field_type == PG_THRIFT_COMPACT_INT32 ||
field_type == PG_THRIFT_COMPACT_INT64
) {
int64 len = 0;
parse_varint_helper(start, end, &len);
ret = start + len;
} else if (
field_type == PG_THRIFT_COMPACT_SET ||
field_type == PG_THRIFT_COMPACT_LIST
) {
uint8 len_type_id = parse_int_helper(start, end, PG_THRIFT_TYPE_LEN);
uint32 len = (len_type_id & 0xf0) >> 4;
uint8 type_id = len_type_id & 0x0f;
if (len == 0x0f) {
int64 len_length = 0;
len = parse_varint_helper(start + PG_THRIFT_TYPE_LEN, end, &len_length);
ret = start + PG_THRIFT_TYPE_LEN + len_length;
} else {
ret = start + PG_THRIFT_TYPE_LEN;
}
for (int i = 0; i < len; i++) {
ret = skip_compact_field(ret, end, compact_list_type_to_struct_type(type_id));
}
} else if (field_type == PG_THRIFT_COMPACT_MAP) {
int64 len_length = 0;
int32 len = parse_varint_helper(start, end, &len_length);
uint8 key_value_type_id = parse_int_helper(start + len_length, end, PG_THRIFT_TYPE_LEN);
uint8 key_type = (key_value_type_id & 0xf0) >> 4;
uint8 value_type = (key_value_type_id & 0x0f);
ret = start + len_length + PG_THRIFT_TYPE_LEN;
for (int i = 0; i < len; i++) {
ret = skip_compact_field(ret, end, compact_list_type_to_struct_type(key_type));
ret = skip_compact_field(ret, end, compact_list_type_to_struct_type(value_type));
}
} else if (field_type == PG_THRIFT_COMPACT_STRUCT) {
ret = start;
while (true) {
if (*ret == 0) {
ret += 1; break;
}
uint8 field_type_id = parse_int_helper(ret, end, PG_THRIFT_TYPE_LEN);
uint8 type_id = field_type_id & 0x0f;
if ((field_type_id & 0xf0) == 0) {
ret += PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN;
} else {
ret += PG_THRIFT_TYPE_LEN;
}
ret = skip_compact_field(ret, end, type_id);
}
} else {
elog(ERROR, "Invalid thrift compact field type");
}
if (ret > end) {
elog(ERROR, "Invalid thrift compact format");
}
return ret;
}
Datum thrift_binary_decode(uint8* data, Size size, int16 field_id, int8 type_id) {
uint8* start = data, *end = data + size;
while (start < end) {
if (start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN - 1 >= end) break;
int16 parsed_field_id = parse_int_helper(start + PG_THRIFT_TYPE_LEN, end, FIELD_LEN);
if (parsed_field_id == field_id) {
if (*start == type_id) {
return parse_binary_field(start, end, type_id);
}
break;
} else {
int8 target_type_id = *start;
start = start + PG_THRIFT_TYPE_LEN + PG_THRIFT_FIELD_LEN;
start = skip_binary_field(start, end, target_type_id);
}
}
elog(ERROR, "Invalid thrift format");
}
Datum thrift_compact_decode(uint8* data, Size size, int16 field_id, int8 type_id) {
uint8* start = data, *end = data + size;
int16 current_field_id = 0;
while (start < end) {
if (start + PG_THRIFT_TYPE_LEN >= end) break;
uint8 field_type_id = parse_int_helper(start, end, PG_THRIFT_TYPE_LEN);
uint8 field_delta = (field_type_id >> 4) & 0x0f;
uint8 parsed_type_id = field_type_id & 0x0f;
if (field_delta != 0) {
current_field_id += field_delta;
} else {
current_field_id = parse_int_helper(start + PG_THRIFT_TYPE_LEN, end, FIELD_LEN);
}
if (current_field_id == field_id) {
if (type_id == PG_THRIFT_COMPACT_BOOL) {
if (parsed_type_id == 1) {
PG_RETURN_BOOL(1);
} else if (parsed_type_id == 2) {
PG_RETURN_BOOL(0);
} else {
elog(ERROR, "Invalid parsed type id for compact bool");
}
}
if (parsed_type_id == type_id) {
return parse_compact_field(start + PG_THRIFT_TYPE_LEN, end, type_id);
}
break;
} else {
start += PG_THRIFT_TYPE_LEN;
if (field_delta == 0) {
start += PG_THRIFT_FIELD_LEN;
}
start = skip_compact_field(start, end, parsed_type_id);
}
}
elog(ERROR, "Invalid thrift compact format");
}
Datum thrift_binary_get_bool(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_BOOL);
}
Datum thrift_compact_get_bool(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_BOOL);
}
Datum thrift_binary_get_byte(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_BYTE);
}
Datum thrift_compact_get_byte(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_BYTE);
}
Datum thrift_binary_get_double(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_DOUBLE);
}
Datum thrift_compact_get_double(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_DOUBLE);
}
Datum thrift_binary_get_int16(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_INT16);
}
Datum thrift_compact_get_int16(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_INT16);
}
Datum thrift_binary_get_int32(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_INT32);
}
Datum thrift_compact_get_int32(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_INT32);
}
Datum thrift_binary_get_int64(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_INT64);
}
Datum thrift_compact_get_int64(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_INT64);
}
Datum thrift_binary_get_string(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_STRING);
}
Datum thrift_compact_get_string(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_STRING);
}
Datum thrift_binary_get_struct_bytea(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_STRUCT);
}
Datum thrift_compact_get_struct_bytea(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_STRUCT);
}
Datum thrift_binary_get_list_bytea(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_LIST);
}
Datum thrift_compact_get_list_bytea(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_LIST);
}
Datum thrift_binary_get_set_bytea(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_SET);
}
Datum thrift_compact_get_set_bytea(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_SET);
}
Datum thrift_binary_get_map_bytea(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_binary_decode(data, size, field_id, PG_THRIFT_BINARY_MAP);
}
Datum thrift_compact_get_map_bytea(PG_FUNCTION_ARGS) {
bytea* thrift_bytea = PG_GETARG_BYTEA_P(0);
int32 field_id = PG_GETARG_INT32(1);
uint8* data = (uint8*)VARDATA(thrift_bytea);
Size size = VARSIZE(thrift_bytea) - VARHDRSZ;
return thrift_compact_decode(data, size, field_id, PG_THRIFT_COMPACT_MAP);
}
uint8* encode_binary_bool(char* value) {
uint8* ret = palloc(PG_THRIFT_TYPE_LEN + BOOL_LEN);
*ret = PG_THRIFT_BINARY_BOOL;
bool v = atoi(value);
memcpy(ret + PG_THRIFT_TYPE_LEN, &v, BOOL_LEN);
return ret;
}
uint8* encode_binary_int16(char* value) {
uint8* ret = palloc(PG_THRIFT_TYPE_LEN + INT16_LEN);
*ret = PG_THRIFT_BINARY_INT16;
int16 v = atoi(value);
memcpy(ret + PG_THRIFT_TYPE_LEN, &v, INT16_LEN);
if (!is_big_endian()) {
swap_bytes((char*)(ret + PG_THRIFT_TYPE_LEN), INT16_LEN);
}
return ret;
}
uint8* encode_binary_int32(char* value) {
uint8* ret = palloc(PG_THRIFT_TYPE_LEN + INT32_LEN);
*ret = PG_THRIFT_BINARY_INT32;
int32 v = atoi(value);
memcpy(ret + PG_THRIFT_TYPE_LEN, &v, INT32_LEN);
if (!is_big_endian()) {
swap_bytes((char*)(ret + PG_THRIFT_TYPE_LEN), INT32_LEN);
}
return ret;
}
uint8* encode_binary_int64(char* value) {
uint8* ret = palloc(PG_THRIFT_TYPE_LEN + INT64_LEN);
*ret = PG_THRIFT_BINARY_INT64;
int64 v = atol(value);
memcpy(ret + PG_THRIFT_TYPE_LEN, &v, INT64_LEN);
if (!is_big_endian()) {
swap_bytes((char*)(ret + PG_THRIFT_TYPE_LEN), INT64_LEN);
}
return ret;
}
uint8* encode_binary_double(char* value) {
uint8* ret = palloc(PG_THRIFT_TYPE_LEN + DOUBLE_LEN);
*ret = PG_THRIFT_BINARY_DOUBLE;
float8 v = atof(value);
memcpy(ret + PG_THRIFT_TYPE_LEN, &v, DOUBLE_LEN);
if (!is_big_endian()) {
swap_bytes((char*)(ret + PG_THRIFT_TYPE_LEN), DOUBLE_LEN);
}
return ret;
}
uint8* encode_binary_string(char* value) {
uint8* ret = palloc(PG_THRIFT_TYPE_LEN + BYTE_LEN + strlen(value));
*ret = PG_THRIFT_BINARY_STRING;
int32 len = strlen(value);
memcpy(ret + PG_THRIFT_TYPE_LEN, &len, INT32_LEN);
if (!is_big_endian()) {
swap_bytes((char*)(ret + PG_THRIFT_TYPE_LEN), INT32_LEN);
}
memcpy(ret + PG_THRIFT_TYPE_LEN + BYTE_LEN, value, strlen(value));