-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcbor--1.0.sql
More file actions
809 lines (797 loc) · 25.4 KB
/
cbor--1.0.sql
File metadata and controls
809 lines (797 loc) · 25.4 KB
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
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION cbor" to load this file. \quit
CREATE TYPE cbor.next_state AS (remainder bytea, item jsonb);
CREATE OR REPLACE FUNCTION cbor.raise(message text, debug json, dummy_return_value anyelement)
RETURNS anyelement
LANGUAGE plpgsql
AS $$
BEGIN
IF debug IS NOT NULL THEN
RAISE '% %', message, debug;
ELSE
RAISE '%', message;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.decode(text,text)
RETURNS bytea
IMMUTABLE
STRICT
LANGUAGE sql AS $$
-- Wrapper-function to add base64url support to decode()
SELECT
CASE $1
WHEN 'base64url' THEN pg_catalog.decode(rpad(translate($1,'-_','+/'),length($1) + (4 - length($1) % 4) % 4, '='),'base64')
ELSE pg_catalog.decode($1,$2)
END
$$;
CREATE OR REPLACE FUNCTION cbor.encode(bytea,text)
RETURNS text
IMMUTABLE
STRICT
LANGUAGE sql AS $$
-- Wrapper-function to add base64url support to encode()
SELECT
CASE $1
WHEN 'base64url' THEN translate(trim(trailing '=' from replace(pg_catalog.encode($1,'base64'),E'\n','')),'+/','-_')
ELSE pg_catalog.encode($1,$2)
END
$$;
--
-- This function is meant to be replaced by the user if necessary,
-- to allow user-defined handling of CBOR types with no direct analogs in JSON.
--
-- https://tools.ietf.org/html/rfc8949#section-6.1
--
CREATE OR REPLACE FUNCTION cbor.infinity_value(sign boolean)
RETURNS jsonb
LANGUAGE plpgsql
AS $$
BEGIN
RAISE EXCEPTION '%Infinity value has no direct analog in JSON.', CASE sign WHEN TRUE THEN '-' ELSE '' END
USING HINT = 'Replace cbor.infinity_value() with user-defined function returning a substitue value, e.g. JSON null, if Infinity values are expected and needs to be handled.',
DETAIL = 'See: https://github.com/truthly/pg-cbor/blob/master/FUNCTIONS/infinity_value.sql for examples on such user-defined functions.';
END;
$$;
--
-- For inspiration, below are some alternative handlers for Infinity values.
--
-- WARNING:
-- Please understand that returning a substitute value will introduce
-- a new class of possible bugs due to ambiguity, which might be OK
-- or dangerous, depending on the situation.
--
/*
CREATE OR REPLACE FUNCTION cbor.infinity_value(sign boolean)
RETURNS jsonb
LANGUAGE sql
AS $$
SELECT 'null'::jsonb;
$$;
*/
/*
CREATE OR REPLACE FUNCTION cbor.infinity_value(sign boolean)
RETURNS jsonb
LANGUAGE sql
AS $$
SELECT CASE WHEN sign THEN '"-Infinity"'::jsonb ELSE '"Infinity"'::jsonb END
$$;
*/--
-- This function is meant to be replaced by the user if necessary,
-- to allow user-defined handling of CBOR types with no direct analogs in JSON.
--
-- https://tools.ietf.org/html/rfc8949#section-6.1
--
CREATE OR REPLACE FUNCTION cbor.nan_value()
RETURNS jsonb
LANGUAGE plpgsql
AS $$
BEGIN
RAISE EXCEPTION 'NaN value has no direct analog in JSON.'
USING HINT = 'Replace cbor.nan_value() with user-defined function returning a substitue value, e.g. JSON null, if NaN values are expected and needs to be handled.',
DETAIL = 'See: https://github.com/truthly/pg-cbor/blob/master/FUNCTIONS/nan_value.sql for examples on such user-defined functions.';
END;
$$;
--
-- For inspiration, below are some alternative handlers for NaN values.
--
-- WARNING:
-- Please understand that returning a substitute value will introduce
-- a new class of possible bugs due to ambiguity, which might be OK
-- or dangerous, depending on the situation.
--
/*
CREATE OR REPLACE FUNCTION cbor.nan_value()
RETURNS jsonb
LANGUAGE sql
AS $$
SELECT 'null'::jsonb;
$$;
*/
/*
CREATE OR REPLACE FUNCTION cbor.nan_value()
RETURNS jsonb
LANGUAGE sql
AS $$
SELECT '"NaN"'::jsonb;
$$;
*/
--
-- This function is meant to be replaced by the user if necessary,
-- to allow user-defined handling of CBOR types with no direct analogs in JSON.
--
-- https://tools.ietf.org/html/rfc8949#section-6.1
--
CREATE OR REPLACE FUNCTION cbor.undefined_value()
RETURNS jsonb
LANGUAGE plpgsql
AS $$
BEGIN
RAISE EXCEPTION 'Undefined value has no direct analog in JSON.'
USING HINT = 'Replace cbor.undefined_value() with user-defined function returning a substitue value, e.g. JSON null, if Undefined values are expected and needs to be handled.',
DETAIL = 'See: https://github.com/truthly/pg-cbor/blob/master/FUNCTIONS/undefined_value.sql for examples on such user-defined functions.';
END;
$$;
--
-- For inspiration, below are some alternative handlers for Undefined values.
--
-- WARNING:
-- Please understand that returning a substitute value will introduce
-- a new class of possible bugs due to ambiguity, which might be OK
-- or dangerous, depending on the situation.
--
/*
CREATE OR REPLACE FUNCTION cbor.undefined_value()
RETURNS jsonb
LANGUAGE sql
AS $$
SELECT 'null'::jsonb;
$$;
*/
/*
CREATE OR REPLACE FUNCTION cbor.undefined_value()
RETURNS jsonb
LANGUAGE sql
AS $$
SELECT '"Undefined"'::jsonb;
$$;
*/--
-- This function is meant to be replaced by the user,
-- to allow special handling of CBOR types with no direct analogs in JSON.
--
-- https://tools.ietf.org/html/rfc8949#section-6.1
--
CREATE OR REPLACE FUNCTION cbor.substitute_value(cbor bytea, major_type integer, additional_type integer)
RETURNS jsonb
LANGUAGE plpgsql
AS $$
BEGIN;
RAISE EXCEPTION 'major_type % additional_type % has no direct analog in JSON.', major_type, additional_type
USING HINT = 'Replace cbor.substitute_value() with user-defined function returning a substitue value, e.g. JSON null, if such values are expected and needs to be handled.',
DETAIL = 'See: https://github.com/truthly/pg-cbor/blob/master/FUNCTIONS/substitute_value.sql for examples on such user-defined functions.';
END;
$$;
--
-- For inspiration, below are some alternative handlers for Undefined values.
--
-- WARNING:
-- Please understand that returning a substitute value will introduce
-- a new class of possible bugs due to ambiguity, which might be OK
-- or dangerous, depending on the situation.
--
/*
CREATE OR REPLACE FUNCTION cbor.substitute_value(cbor bytea, major_type integer, additional_type integer)
RETURNS jsonb
LANGUAGE sql
AS $$
SELECT 'null'::jsonb;
$$;
*/
/*
CREATE OR REPLACE FUNCTION cbor.substitute_value(cbor bytea, major_type integer, additional_type integer)
RETURNS jsonb
LANGUAGE sql
AS $$
SELECT jsonb_build_object('major_type',major_type,'additional_type',additional_type);
$$;
*/CREATE OR REPLACE FUNCTION cbor.bytea_to_numeric(val bytea)
RETURNS numeric
IMMUTABLE STRICT
LANGUAGE plpgsql
AS $$
DECLARE
n numeric := 0;
BEGIN
FOR i IN 0 .. length(val)-1 LOOP
n := n*256 + get_byte(val,i);
END LOOP;
RETURN n;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.numeric_to_bytea(n numeric)
RETURNS bytea
IMMUTABLE STRICT
LANGUAGE plpgsql
AS $$
DECLARE
b BYTEA := '\x';
v INTEGER;
BEGIN
WHILE n > 0 LOOP
v := n % 256;
b := set_byte(('\x00' || b),0,v);
n := (n-v)/256;
END LOOP;
RETURN b;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.next_float_half(cbor bytea)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
-- https://tools.ietf.org/html/rfc7049#appendix-D
DECLARE
sign boolean;
half int := (get_byte(cbor,0) << 8) + get_byte(cbor,1);
exp int := (half >> 10) & x'1f'::int;
mant int := half & x'3ff'::int;
val float8;
BEGIN
sign := (half & x'8000'::int) != 0;
IF exp = 0 THEN
val := mant * 2^(-24);
ELSIF exp != 31 THEN
val := (mant + 1024) * 2^(exp-25);
ELSIF mant = 0 THEN
RETURN ROW(substring(cbor,3), cbor.infinity_value(sign))::cbor.next_state;
ELSE
RETURN ROW(substring(cbor,3), cbor.nan_value())::cbor.next_state;
END IF;
IF sign THEN
val := -val;
END IF;
RETURN ROW(substring(cbor,3), pg_catalog.to_jsonb(val))::cbor.next_state;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.next_float_single(cbor bytea)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
DECLARE
single bit(32) := get_byte(cbor,0)::bit(8) ||
get_byte(cbor,1)::bit(8) ||
get_byte(cbor,2)::bit(8) ||
get_byte(cbor,3)::bit(8);
/*
00000000011111111112222222222333
12345678901234567890123456789012
seeeeeeeefffffffffffffffffffffff
^-sign (1 bit)
^-exponent (8 bits)
^-fraction (23 bits)
*/
sign boolean := get_bit(single, 0)::integer::boolean;
exponent integer := substring(single from 2 for 8)::integer;
fraction integer := substring(single from 10 for 23)::integer;
frac float8 := (fraction | (1::integer << 23))::float8 / (2::integer << 23)::float8;
value float8;
BEGIN
IF exponent = b'11111111'::integer THEN
IF fraction = b'00000000000000000000000'::integer THEN
RETURN ROW(substring(cbor,5), cbor.infinity_value(sign))::cbor.next_state;
ELSE
RETURN ROW(substring(cbor,5), cbor.nan_value())::cbor.next_state;
END IF;
END IF;
value := frac * 2::float8^(exponent-126);
IF sign THEN
value := -value;
END IF;
RETURN ROW(substring(cbor,5), pg_catalog.to_jsonb(value))::cbor.next_state;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.next_float_double(cbor bytea)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
DECLARE
double bit(64) := get_byte(cbor,0)::bit(8) ||
get_byte(cbor,1)::bit(8) ||
get_byte(cbor,2)::bit(8) ||
get_byte(cbor,3)::bit(8) ||
get_byte(cbor,4)::bit(8) ||
get_byte(cbor,5)::bit(8) ||
get_byte(cbor,6)::bit(8) ||
get_byte(cbor,7)::bit(8);
/*
0000000001111111111222222222233333333334444444444555555555566666
1234567890123456789012345678901234567890123456789012345678901234
seeeeeeeeeeeffffffffffffffffffffffffffffffffffffffffffffffffffff
^-sign (1 bit)
^-exponent (11 bits)
^-fraction (52 bits)
*/
sign boolean := get_bit(double, 0)::integer::boolean;
exponent integer := substring(double from 2 for 11)::integer;
fraction bigint := substring(double from 13 for 52)::bigint;
frac float8 := (fraction | (1::bigint << 52))::float8 / (2::bigint << 52)::float8;
value float8;
BEGIN
IF exponent = b'11111111111'::integer THEN
IF fraction = b'0000000000000000000000000000000000000000000000000000'::bigint THEN
RETURN ROW(substring(cbor,9), cbor.infinity_value(sign))::cbor.next_state;
ELSE
RETURN ROW(substring(cbor,9), cbor.nan_value())::cbor.next_state;
END IF;
END IF;
value := frac * 2::float8^(exponent-1022);
IF sign THEN
value := -value;
END IF;
RETURN ROW(substring(cbor,9), pg_catalog.to_jsonb(value))::cbor.next_state;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.major_type_0(
cbor bytea,
encode_binary_format text,
additional_type integer,
length_bytes integer,
data_value numeric
)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
BEGIN
IF additional_type <= 27 THEN
RETURN ROW(substring(cbor,2+length_bytes), pg_catalog.to_jsonb(data_value));
ELSIF additional_type >= 28 AND additional_type <= 30 THEN
RAISE EXCEPTION 'a reserved value is used for additional information(%)', additional_type;
ELSIF additional_type = 31 THEN
RAISE EXCEPTION 'additional information 31 used with major type 0';
ELSE
RAISE EXCEPTION 'not implemented, major_type %, additional_type %', 0, additional_type;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.major_type_1(
cbor bytea,
encode_binary_format text,
additional_type integer,
length_bytes integer,
data_value numeric
)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
BEGIN
IF additional_type <= 27 THEN
RETURN ROW(substring(cbor,2+length_bytes), pg_catalog.to_jsonb(-1-data_value));
ELSIF additional_type >= 28 AND additional_type <= 30 THEN
RAISE EXCEPTION 'a reserved value is used for additional information(%)', additional_type;
ELSIF additional_type = 31 THEN
RAISE EXCEPTION 'additional information 31 used with major type 1';
ELSE
RAISE EXCEPTION 'not implemented, major_type %, additional_type %', 1, additional_type;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.major_type_2(
cbor bytea,
encode_binary_format text,
additional_type integer,
length_bytes integer,
data_value numeric
)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
BEGIN
IF additional_type <= 27 THEN
RETURN ROW(substring(cbor,2+length_bytes+data_value::integer), pg_catalog.to_jsonb(cbor.encode(substring(cbor,2+length_bytes,data_value::integer),encode_binary_format)));
ELSIF additional_type = 31 THEN
RETURN cbor.next_indefinite_byte_string(substring(cbor,2), encode_binary_format);
ELSIF additional_type >= 28 AND additional_type <= 30 THEN
RAISE EXCEPTION 'a reserved value is used for additional information(%)', additional_type;
ELSE
RAISE EXCEPTION 'not implemented, major_type %, additional_type %', 2, additional_type;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.major_type_3(
cbor bytea,
encode_binary_format text,
additional_type integer,
length_bytes integer,
data_value numeric
)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
BEGIN
IF additional_type <= 27 THEN
RETURN ROW(substring(cbor,2+length_bytes+data_value::integer), pg_catalog.to_jsonb(convert_from(substring(cbor,2+length_bytes,data_value::integer),'utf8')));
ELSIF additional_type = 31 THEN
RETURN cbor.next_indefinite_text_string(substring(cbor,2), encode_binary_format);
ELSIF additional_type >= 28 AND additional_type <= 30 THEN
RAISE EXCEPTION 'a reserved value is used for additional information(%)', additional_type;
ELSE
RAISE EXCEPTION 'not implemented, major_type %, additional_type %', 3, additional_type;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.major_type_4(
cbor bytea,
encode_binary_format text,
additional_type integer,
length_bytes integer,
data_value numeric
)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
BEGIN
IF additional_type <= 27 THEN
RETURN cbor.next_array(substring(cbor,2+length_bytes), data_value::integer, encode_binary_format);
ELSIF additional_type = 31 THEN
RETURN cbor.next_indefinite_array(substring(cbor,2), encode_binary_format);
ELSIF additional_type >= 28 AND additional_type <= 30 THEN
RAISE EXCEPTION 'a reserved value is used for additional information(%)', additional_type;
ELSE
RAISE EXCEPTION 'not implemented, major_type %, additional_type %', 4, additional_type;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.major_type_5(
cbor bytea,
encode_binary_format text,
additional_type integer,
length_bytes integer,
data_value numeric
)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
BEGIN
IF additional_type <= 27 THEN
RETURN cbor.next_map(substring(cbor,2+length_bytes), data_value::integer, encode_binary_format);
ELSIF additional_type = 31 THEN
RETURN cbor.next_indefinite_map(substring(cbor,2), encode_binary_format);
ELSIF additional_type >= 28 AND additional_type <= 30 THEN
RAISE EXCEPTION 'a reserved value is used for additional information(%)', additional_type;
ELSE
RAISE EXCEPTION 'not implemented, major_type %, additional_type %', 5, additional_type;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.major_type_6(
cbor bytea,
encode_binary_format text,
additional_type integer,
length_bytes integer,
data_value numeric
)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
BEGIN
IF additional_type = 2 THEN
RETURN (
SELECT
ROW(tag_item.remainder, pg_catalog.to_jsonb(cbor.bytea_to_numeric(decode(tag_item.item#>>'{}','hex'))))
FROM cbor.next_item(substring(cbor,2), encode_binary_format) AS tag_item
);
ELSIF additional_type = 3 THEN
RETURN (
SELECT ROW(tag_item.remainder, pg_catalog.to_jsonb(-1-cbor.bytea_to_numeric(decode(tag_item.item#>>'{}','hex'))))
FROM cbor.next_item(substring(cbor,2), encode_binary_format) AS tag_item
);
ELSIF additional_type = 21 THEN
RETURN cbor.next_item(substring(cbor,2), 'base64url');
ELSIF additional_type = 22 THEN
RETURN cbor.next_item(substring(cbor,2), 'base64');
ELSIF additional_type = 23 THEN
RETURN cbor.next_item(substring(cbor,2), 'hex');
ELSIF additional_type = ANY(ARRAY[0,1,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,24,25,26,27]) THEN
RETURN cbor.next_tag(substring(cbor,2+length_bytes), data_value, encode_binary_format);
ELSIF additional_type >= 28 AND additional_type <= 30 THEN
RAISE EXCEPTION 'a reserved value is used for additional information(%)', additional_type;
ELSIF additional_type = 31 THEN
RAISE EXCEPTION 'additional information 31 used with major type 6';
ELSE
RAISE EXCEPTION 'not implemented, major_type %, additional_type %', 6, additional_type;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.major_type_7(
cbor bytea,
encode_binary_format text,
additional_type integer,
length_bytes integer,
data_value numeric
)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
BEGIN
IF additional_type = 20 THEN
RETURN ROW(substring(cbor,2), pg_catalog.to_jsonb(false));
ELSIF additional_type = 21 THEN
RETURN ROW(substring(cbor,2), pg_catalog.to_jsonb(true));
ELSIF additional_type = 22 THEN
RETURN ROW(substring(cbor,2), 'null'::jsonb);
ELSIF additional_type = 25 THEN
RETURN cbor.next_float_half(substring(cbor,2));
ELSIF additional_type = 26 THEN
RETURN cbor.next_float_single(substring(cbor,2));
ELSIF additional_type = 27 THEN
RETURN cbor.next_float_double(substring(cbor,2));
ELSIF additional_type = 23 THEN
RETURN ROW(substring(cbor,2), cbor.undefined_value());
ELSIF additional_type = 24 AND data_value >= 32 THEN
RETURN ROW(substring(cbor,2+length_bytes), pg_catalog.to_jsonb(data_value));
ELSIF additional_type <= 19 THEN
RETURN ROW(substring(cbor,2+length_bytes), pg_catalog.to_jsonb(data_value));
ELSIF additional_type > 27 AND additional_type < 31 THEN
RETURN ROW(substring(cbor,2), cbor.substitute_value(substring(cbor,2), 7, additional_type));
ELSIF additional_type = 31 THEN
RAISE EXCEPTION '"break" stop code appeared where a data item is expected, the enclosing item is not well-formed';
ELSIF additional_type = 24 AND data_value < 32 THEN
RAISE EXCEPTION 'major type 7, additional information 24, data_value(%) < 32 (incorrect)', data_value;
ELSIF additional_type >= 28 AND additional_type <= 30 THEN
RAISE EXCEPTION 'a reserved value is used for additional information(%)', additional_type;
ELSE
RAISE EXCEPTION 'not implemented, major_type %, additional_type %', 7, additional_type;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.next_item(cbor bytea, encode_binary_format text)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE plpgsql
AS $$
DECLARE
major_type constant integer := (get_byte(cbor,0) >> 5) & '111'::bit(3)::integer;
additional_type constant integer := get_byte(cbor,0) & '11111'::bit(5)::integer;
length_bytes constant integer := NULLIF(LEAST(floor(2 ^ (additional_type - 24))::integer,16),16);
data_value numeric := 0;
BEGIN
IF additional_type <= 23 THEN
data_value := additional_type::numeric;
ELSIF additional_type BETWEEN 24 AND 27 THEN
/*
FOR byte_pos IN 1..length_bytes LOOP
data_value := data_value + get_byte(cbor,byte_pos) * 2::numeric^(8*(length_bytes-byte_pos));
END LOOP;
data_value := floor(data_value);
*/
SELECT
floor(SUM(get_byte(cbor,byte_pos) * 2::numeric^(8*(length_bytes-byte_pos))))
INTO data_value
FROM generate_series(1,length_bytes) AS byte_pos;
END IF;
--
-- Sorted by observed frequency from real-life WebAuthn examples
-- to hit the matching case as early as possible.
--
IF major_type = 3 THEN RETURN cbor.major_type_3(cbor,encode_binary_format,additional_type,length_bytes,data_value);
ELSIF major_type = 5 THEN RETURN cbor.major_type_5(cbor,encode_binary_format,additional_type,length_bytes,data_value);
ELSIF major_type = 1 THEN RETURN cbor.major_type_1(cbor,encode_binary_format,additional_type,length_bytes,data_value);
ELSIF major_type = 2 THEN RETURN cbor.major_type_2(cbor,encode_binary_format,additional_type,length_bytes,data_value);
ELSIF major_type = 4 THEN RETURN cbor.major_type_4(cbor,encode_binary_format,additional_type,length_bytes,data_value);
ELSIF major_type = 0 THEN RETURN cbor.major_type_0(cbor,encode_binary_format,additional_type,length_bytes,data_value);
ELSIF major_type = 6 THEN RETURN cbor.major_type_6(cbor,encode_binary_format,additional_type,length_bytes,data_value);
ELSIF major_type = 7 THEN RETURN cbor.major_type_7(cbor,encode_binary_format,additional_type,length_bytes,data_value);
ELSE
RAISE EXCEPTION 'not implemented, major_type %, additional_type %', major_type, additional_type;
END IF;
END;
$$;
CREATE OR REPLACE FUNCTION cbor.next_array(cbor bytea, item_count integer, encode_binary_format text)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE sql
AS $$
WITH RECURSIVE x AS (
SELECT
next_array.cbor AS remainder,
next_array.item_count,
jsonb_build_array() AS jsonb_array
UNION ALL
SELECT
next_item.remainder,
x.item_count-1,
x.jsonb_array || jsonb_build_array(next_item.item)
FROM x
JOIN LATERAL cbor.next_item(x.remainder, encode_binary_format) ON TRUE
WHERE x.item_count > 0
)
SELECT ROW(x.remainder, x.jsonb_array)::cbor.next_state FROM x WHERE x.item_count = 0
$$;
CREATE OR REPLACE FUNCTION cbor.next_map(cbor bytea, item_count integer, encode_binary_format text)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE sql
AS $$
WITH RECURSIVE x AS (
SELECT
next_map.cbor AS remainder,
next_map.item_count,
jsonb_build_object() AS map
UNION ALL
SELECT
map_value.remainder,
x.item_count-1,
x.map || jsonb_build_object(map_key.item#>>'{}', map_value.item)
FROM x
JOIN LATERAL cbor.next_item(x.remainder, encode_binary_format) AS map_key ON TRUE
JOIN LATERAL cbor.next_item(map_key.remainder, encode_binary_format) AS map_value ON TRUE
WHERE x.item_count > 0
)
SELECT ROW(x.remainder, x.map)::cbor.next_state FROM x WHERE x.item_count = 0
$$;
CREATE OR REPLACE FUNCTION cbor.next_indefinite_array(cbor bytea, encode_binary_format text)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE sql
AS $$
WITH RECURSIVE x AS (
SELECT
0 AS i,
next_indefinite_array.cbor AS remainder,
jsonb_build_array() AS jsonb_array
UNION ALL
SELECT
x.i + 1,
next_item.remainder,
x.jsonb_array || jsonb_build_array(next_item.item)
FROM x
JOIN LATERAL cbor.next_item(x.remainder, encode_binary_format) ON TRUE
WHERE get_byte(x.remainder,0) <> 255
)
SELECT ROW(substring(x.remainder,2), x.jsonb_array)::cbor.next_state FROM x ORDER BY i DESC LIMIT 1
$$;
CREATE OR REPLACE FUNCTION cbor.next_indefinite_map(cbor bytea, encode_binary_format text)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE sql
AS $$
WITH RECURSIVE x AS (
SELECT
0 AS i,
next_indefinite_map.cbor AS remainder,
jsonb_build_object() AS map
UNION ALL
SELECT
x.i + 1,
map_value.remainder,
x.map || jsonb_build_object(map_key.item#>>'{}', map_value.item)
FROM x
JOIN LATERAL cbor.next_item(x.remainder, encode_binary_format) AS map_key ON TRUE
JOIN LATERAL cbor.next_item(map_key.remainder, encode_binary_format) AS map_value ON TRUE
WHERE get_byte(x.remainder,0) <> 255
)
SELECT ROW(substring(x.remainder,2), x.map)::cbor.next_state FROM x ORDER BY i DESC LIMIT 1
$$;
CREATE OR REPLACE FUNCTION cbor.next_indefinite_byte_string(cbor bytea, encode_binary_format text)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE sql
AS $$
WITH RECURSIVE x AS (
SELECT
0 AS i,
next_indefinite_byte_string.cbor AS remainder,
'\x'::bytea AS bytes
UNION ALL
SELECT
x.i + 1,
next_item.remainder,
x.bytes || CASE
WHEN (get_byte(x.remainder,0)>>5)&'111'::bit(3)::integer = 2
THEN cbor.decode((next_item.item#>>'{}'),encode_binary_format)
ELSE cbor.raise('incorrect substructure of indefinite-length byte string (may only contain definite-length strings of the same major type)',NULL,NULL::bytea)
END
FROM x
JOIN LATERAL cbor.next_item(x.remainder, encode_binary_format) ON TRUE
WHERE get_byte(x.remainder,0) <> 255
)
SELECT ROW(substring(x.remainder,2), to_jsonb(cbor.encode(x.bytes,encode_binary_format)))::cbor.next_state FROM x ORDER BY i DESC LIMIT 1
$$;
CREATE OR REPLACE FUNCTION cbor.next_indefinite_text_string(cbor bytea, encode_binary_format text)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE sql
AS $$
WITH RECURSIVE x AS (
SELECT
0 AS i,
next_indefinite_text_string.cbor AS remainder,
''::text AS text_string
UNION ALL
SELECT
x.i + 1,
next_item.remainder,
x.text_string || CASE
WHEN (get_byte(x.remainder,0)>>5)&'111'::bit(3)::integer = 3
THEN (next_item.item#>>'{}')
ELSE cbor.raise('incorrect substructure of indefinite-length text string (may only contain definite-length strings of the same major type)',NULL,NULL::text)
END
FROM x
JOIN LATERAL cbor.next_item(x.remainder, encode_binary_format) ON TRUE
WHERE get_byte(x.remainder,0) <> 255
)
SELECT ROW(substring(x.remainder,2), to_jsonb(x.text_string))::cbor.next_state FROM x ORDER BY i DESC LIMIT 1
$$;
CREATE OR REPLACE FUNCTION cbor.next_tag(cbor bytea, tag_number numeric, encode_binary_format text)
RETURNS cbor.next_state
IMMUTABLE
LANGUAGE sql
AS $$
SELECT
CASE
WHEN tag_number = 0 AND (next_item.item#>>'{}')::timestamptz IS NOT NULL THEN next_item
ELSE next_item
END
FROM cbor.next_item(cbor, encode_binary_format)
$$;
CREATE OR REPLACE FUNCTION cbor.to_jsonb(
cbor bytea,
encode_binary_format text DEFAULT 'hex'
)
RETURNS jsonb
STRICT
IMMUTABLE
LANGUAGE sql
AS $$
SELECT
CASE
WHEN length(remainder) = 0
THEN item
ELSE cbor.raise('Multiple root level CBOR items. Use to_jsonb_array() instead if this is expected.',NULL,NULL::jsonb)
END
FROM cbor.next_item(to_jsonb.cbor, encode_binary_format)
$$;
CREATE OR REPLACE FUNCTION cbor.to_jsonb_array(
cbor bytea,
encode_binary_format text DEFAULT 'hex'
)
RETURNS jsonb
STRICT
IMMUTABLE
LANGUAGE sql
AS $$
WITH RECURSIVE x AS (
SELECT
0 AS i,
next_item.remainder,
next_item.item
FROM cbor.next_item(to_jsonb_array.cbor, encode_binary_format)
UNION ALL
SELECT
x.i + 1,
next_item.remainder,
next_item.item
FROM x
JOIN LATERAL cbor.next_item(x.remainder, encode_binary_format) ON TRUE
WHERE length(x.remainder) > 0
)
SELECT
CASE
WHEN length(cbor) = 0
THEN jsonb_build_array()
ELSE (SELECT jsonb_agg(x.item ORDER BY i) FROM x)
END
$$;
COMMENT ON COLUMN cbor.next_state.remainder IS 'The remainder after decoding a CBOR item';
COMMENT ON COLUMN cbor.next_state.item IS 'A single decoded CBOR item';