-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherlang.ml
1946 lines (1846 loc) · 66.1 KB
/
erlang.ml
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
(*-*-Mode:ocaml;coding:utf-8;tab-width:2;c-basic-offset:2;indent-tabs-mode:()-*-
ex: set ft=ocaml fenc=utf-8 sts=2 ts=2 sw=2 et nomod: *)
(*
MIT License
Copyright (c) 2017-2023 Michael Truog <mjtruog at protonmail dot com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*)
(* tag values here http://www.erlang.org/doc/apps/erts/erl_ext_dist.html *)
let tag_version = 131
let tag_compressed_zlib = 80
let tag_new_float_ext = 70
let tag_bit_binary_ext = 77
let tag_atom_cache_ref = 78
let tag_new_pid_ext = 88
let tag_new_port_ext = 89
let tag_newer_reference_ext = 90
let tag_small_integer_ext = 97
let tag_integer_ext = 98
let tag_float_ext = 99
let tag_atom_ext = 100
let tag_reference_ext = 101
let tag_port_ext = 102
let tag_pid_ext = 103
let tag_small_tuple_ext = 104
let tag_large_tuple_ext = 105
let tag_nil_ext = 106
let tag_string_ext = 107
let tag_list_ext = 108
let tag_binary_ext = 109
let tag_small_big_ext = 110
let tag_large_big_ext = 111
let tag_new_fun_ext = 112
let tag_export_ext = 113
let tag_new_reference_ext = 114
let tag_small_atom_ext = 115
let tag_map_ext = 116
let tag_fun_ext = 117
let tag_atom_utf8_ext = 118
let tag_small_atom_utf8_ext = 119
let tag_v4_port_ext = 120
let tag_local_ext = 121
let buffer_size = 65536
module Pid = struct
type t = {
node_tag : int;
node : string;
id : string;
serial : string;
creation : string;
}
let make ~node_tag ~node ~id ~serial ~creation =
{node_tag; node; id; serial; creation}
end
module Port = struct
type t = {
node_tag : int;
node : string;
id : string;
creation : string;
}
let make ~node_tag ~node ~id ~creation =
{node_tag; node; id; creation}
end
module Reference = struct
type t = {
node_tag : int;
node : string;
id : string;
creation : string;
}
let make ~node_tag ~node ~id ~creation =
{node_tag; node; id; creation}
end
module Function = struct
type t = {
tag : int;
value : string;
}
let make ~tag ~value =
{tag; value}
end
type t =
OtpErlangInteger of int
| OtpErlangIntegerBig of Big_int_Z.big_int
| OtpErlangFloat of float
| OtpErlangAtom of string
| OtpErlangAtomUTF8 of string
| OtpErlangAtomCacheRef of int
| OtpErlangAtomBool of bool
| OtpErlangString of string
| OtpErlangBinary of string
| OtpErlangBinaryBits of string * int
| OtpErlangList of t list
| OtpErlangListImproper of t list
| OtpErlangTuple of t list
| OtpErlangMap of (t, t) Hashtbl.t
| OtpErlangPid of Pid.t
| OtpErlangPort of Port.t
| OtpErlangReference of Reference.t
| OtpErlangFunction of Function.t
let output_error (s : string) : string =
"output_error: " ^ s
let parse_error (s : string) : string =
"parse_error: " ^ s
type ('a,'b,'c) result2 = Ok2 of 'a * 'b | Error2 of 'c
type ('a,'b,'c,'d) result3 = Ok3 of 'a * 'b * 'c | Error3 of 'd
let list_append l1 l2 = List.rev_append (List.rev l1) l2
exception HashtblExit of string
let valid_uint32_positive (value : int) : bool =
(Int64.of_int value) <= (Int64.of_string "4294967295")
let valid_int32 (value : int) : bool =
let value64 = Int64.of_int value in
value64 >= (Int64.of_string "-2147483648") &&
value64 <= (Int64.of_string "2147483647")
let unpack_uint16 i binary : int =
let byte0 = int_of_char binary.[i]
and byte1 = int_of_char binary.[i + 1] in
((byte0 lsl 8) lor byte1)
let unpack_uint32 i binary : (int, int, string) result2 =
let byte0 = int_of_char binary.[i]
and byte1 = int_of_char binary.[i + 1]
and byte2 = int_of_char binary.[i + 2]
and byte3 = int_of_char binary.[i + 3] in
if byte0 > max_int lsr 24 then
(* 32 bit system *)
Error2 (parse_error "ocaml int overflow")
else
Ok2 (i + 4,
(byte0 lsl 24) lor (
(byte1 lsl 16) lor (
(byte2 lsl 8) lor byte3
)
)
)
let unpack_integer i binary : t =
let byte0 = int_of_char binary.[i]
and byte1 = int_of_char binary.[i + 1]
and byte2 = int_of_char binary.[i + 2]
and byte3 = int_of_char binary.[i + 3] in
let value =
Int32.logor (
Int32.shift_left (Int32.of_int byte0) 24
) (
Int32.logor (
Int32.shift_left (Int32.of_int byte1) 16
) (
Int32.logor (
Int32.shift_left (Int32.of_int byte2) 8
) (
Int32.of_int byte3
)
)
)
in
if byte0 > min_int lsr 24 then
OtpErlangIntegerBig (Big_int_Z.big_int_of_int32 value)
else
OtpErlangInteger (Int32.to_int value)
let unpack_double i binary : float =
let byte0 = Int64.of_int (int_of_char binary.[i])
and byte1 = Int64.of_int (int_of_char binary.[i + 1])
and byte2 = Int64.of_int (int_of_char binary.[i + 2])
and byte3 = Int64.of_int (int_of_char binary.[i + 3])
and byte4 = Int64.of_int (int_of_char binary.[i + 4])
and byte5 = Int64.of_int (int_of_char binary.[i + 5])
and byte6 = Int64.of_int (int_of_char binary.[i + 6])
and byte7 = Int64.of_int (int_of_char binary.[i + 7]) in
Int64.float_of_bits (
Int64.logor (
Int64.logor (
Int64.logor (
Int64.logor (
Int64.logor (
Int64.logor (
Int64.logor (
Int64.shift_left byte0 56
) (
Int64.shift_left byte1 48
)
) (
Int64.shift_left byte2 40
)
) (
Int64.shift_left byte3 32
)
) (
Int64.shift_left byte4 24
)
) (
Int64.shift_left byte5 16
)
) (
Int64.shift_left byte6 8
)
) byte7
)
let pack_uint16 (value : int) buffer : unit =
let byte0 = (value asr 8) land 0xff
and byte1 = value land 0xff in
Buffer.add_char buffer (char_of_int byte0) ;
Buffer.add_char buffer (char_of_int byte1)
let pack_uint32 (value : int) buffer : unit =
let byte0 = (value asr 24) land 0xff
and byte1 = (value lsr 16) land 0xff
and byte2 = (value lsr 8) land 0xff
and byte3 = value land 0xff in
Buffer.add_char buffer (char_of_int byte0) ;
Buffer.add_char buffer (char_of_int byte1) ;
Buffer.add_char buffer (char_of_int byte2) ;
Buffer.add_char buffer (char_of_int byte3)
let pack_double (value : float) buffer : unit =
let bits8 = Int64.of_int 0xff
and value64 = Int64.bits_of_float value in
let byte0 = Int64.logand (Int64.shift_right_logical value64 56) bits8
and byte1 = Int64.logand (Int64.shift_right_logical value64 48) bits8
and byte2 = Int64.logand (Int64.shift_right_logical value64 40) bits8
and byte3 = Int64.logand (Int64.shift_right_logical value64 32) bits8
and byte4 = Int64.logand (Int64.shift_right_logical value64 24) bits8
and byte5 = Int64.logand (Int64.shift_right_logical value64 16) bits8
and byte6 = Int64.logand (Int64.shift_right_logical value64 8) bits8
and byte7 = Int64.logand value64 bits8 in
Buffer.add_char buffer (char_of_int (Int64.to_int byte0)) ;
Buffer.add_char buffer (char_of_int (Int64.to_int byte1)) ;
Buffer.add_char buffer (char_of_int (Int64.to_int byte2)) ;
Buffer.add_char buffer (char_of_int (Int64.to_int byte3)) ;
Buffer.add_char buffer (char_of_int (Int64.to_int byte4)) ;
Buffer.add_char buffer (char_of_int (Int64.to_int byte5)) ;
Buffer.add_char buffer (char_of_int (Int64.to_int byte6)) ;
Buffer.add_char buffer (char_of_int (Int64.to_int byte7))
let rec binary_to_term_ i binary : (int, t, string) result2 =
let tag = int_of_char binary.[i]
and i0 = i + 1 in
if tag = tag_new_float_ext then
Ok2 (i0 + 8, OtpErlangFloat (unpack_double i0 binary))
else if tag = tag_bit_binary_ext then
match unpack_uint32 i0 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (_, j) when j > Sys.max_string_length ->
Error2 (parse_error "ocaml string overflow")
| Ok2 (i1, j) ->
let bits = int_of_char binary.[i1]
and i2 = i1 + 1 in
let data = String.sub binary i2 j in
let term : t = if bits = 8 then
OtpErlangBinary (data)
else
OtpErlangBinaryBits ((data, bits))
in
Ok2(i2 + j, term)
else if tag = tag_atom_cache_ref then
Ok2 (i0 + 1, OtpErlangAtomCacheRef (int_of_char binary.[i0]))
else if tag = tag_small_integer_ext then
Ok2 (i0 + 1, OtpErlangInteger (int_of_char binary.[i0]))
else if tag = tag_integer_ext then
Ok2 (i0 + 4, unpack_integer i0 binary)
else if tag = tag_float_ext then
let data = String.sub binary i0 31 in
let float_data = try (String.sub data 0 (String.index data '\000'))
with Not_found -> data in
Ok2 (i0 + 31, OtpErlangFloat (float_of_string float_data))
else if tag = tag_v4_port_ext || tag = tag_new_port_ext ||
tag = tag_reference_ext || tag = tag_port_ext then
match binary_to_atom i0 binary with
| Error3 (error) ->
Error2 (error)
| Ok3(i1, node_tag, node) ->
let id_size = if tag = tag_v4_port_ext then 8 else 4 in
let id = String.sub binary i1 id_size
and i2 = i1 + id_size
and creation_size = if tag = tag_v4_port_ext ||
tag = tag_new_port_ext then 4 else 1 in
let creation = String.sub binary i2 creation_size
and i3 = i2 + creation_size in
if tag = tag_reference_ext then
Ok2 (i3, OtpErlangReference (
Reference.make
~node_tag:node_tag ~node:node ~id:id ~creation:creation))
else
(* tag = tag_v4_port_ext || tag = tag_new_port_ext ||
tag = tag_port_ext *)
Ok2 (i3, OtpErlangPort (
Port.make
~node_tag:node_tag ~node:node ~id:id ~creation:creation))
else if tag = tag_new_pid_ext || tag = tag_pid_ext then
match binary_to_atom i0 binary with
| Error3 (error) ->
Error2 (error)
| Ok3(i1, node_tag, node) ->
let id = String.sub binary i1 4
and i2 = i1 + 4 in
let serial = String.sub binary i2 4
and i3 = i2 + 4
and creation_size = if tag = tag_new_pid_ext then 4 else 1 in
let creation = String.sub binary i3 creation_size
and i4 = i3 + creation_size in
Ok2 (i4, OtpErlangPid (
Pid.make
~node_tag:node_tag ~node:node
~id:id ~serial:serial ~creation:creation))
else if tag = tag_small_tuple_ext then
let length = int_of_char binary.[i0]
and i1 = i0 + 1 in
match binary_to_term_sequence i1 length binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i2, tmp) ->
Ok2 (i2, OtpErlangTuple (tmp))
else if tag = tag_large_tuple_ext then
match unpack_uint32 i0 binary with
| Error2 (error) ->
Error2 error
| Ok2 (_, length) when length > Sys.max_string_length ->
Error2 (parse_error "ocaml string overflow")
| Ok2 (i1, length) ->
match binary_to_term_sequence i1 length binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i2, tmp) ->
Ok2 (i2, OtpErlangTuple (tmp))
else if tag = tag_nil_ext then
Ok2 (i0, OtpErlangList ([]))
else if tag = tag_string_ext then
let j = unpack_uint16 i0 binary
and i1 = i0 + 2 in
Ok2 (i1 + j, OtpErlangString (String.sub binary i1 j))
else if tag = tag_list_ext then
match unpack_uint32 i0 binary with
| Error2 (error) ->
Error2 error
| Ok2 (i1, length) ->
match binary_to_term_sequence i1 length binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i2, tmp) ->
match binary_to_term_ i2 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i3, tail) when tail = OtpErlangList([]) ->
Ok2 (i3, OtpErlangList (tmp))
| Ok2 (i3, tail) ->
Ok2 (i3, OtpErlangListImproper (list_append tmp [tail]))
else if tag = tag_binary_ext then
match unpack_uint32 i0 binary with
| Error2 (error) ->
Error2 error
| Ok2 (_, j) when j > Sys.max_string_length ->
Error2 (parse_error "ocaml string overflow")
| Ok2 (i1, j) ->
Ok2 (i1 + j, OtpErlangBinary (String.sub binary i1 j))
else if tag = tag_small_big_ext || tag = tag_large_big_ext then
let length_f () =
if tag = tag_small_big_ext then
Ok2 (i0 + 1, int_of_char binary.[i0])
else (* tag = tag_large_big_ext *)
unpack_uint32 i0 binary
in
match length_f () with
| Error2 (error) ->
Error2 (error)
| Ok2 (_, j) when j > Sys.max_string_length ->
Error2 (parse_error "ocaml string overflow")
| Ok2 (i1, j) ->
let sign = int_of_char binary.[i1] in
let rec loop bignum_index bignum_value =
if bignum_index = j then
bignum_value
else
loop
(bignum_index + 1)
(Big_int_Z.add_int_big_int
(int_of_char binary.[i1 + j - bignum_index])
(Big_int_Z.mult_int_big_int 256 bignum_value))
in
let bignum = loop 0 Big_int_Z.zero_big_int in
let bignum_result = if sign = 1 then
Big_int_Z.minus_big_int bignum
else
bignum
and i2 = i1 + 1 in
Ok2 (i2 + j, OtpErlangIntegerBig (bignum_result))
else if tag = tag_new_fun_ext then
match unpack_uint32 i0 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i1, length) ->
let value = String.sub binary i1 length in
Ok2 (i1 + length, OtpErlangFunction (
Function.make
~tag:tag ~value:value))
else if tag = tag_export_ext then
match binary_to_atom i0 binary with
| Error3 (error) ->
Error2 (error)
| Ok3(i1, _, _) ->
match binary_to_atom i1 binary with
| Error3 (error) ->
Error2 (error)
| Ok3 (i2, _, _) when int_of_char binary.[i2] <> tag_small_integer_ext ->
Error2 (parse_error "invalid small integer tag")
| Ok3 (i2, _, _) ->
let i3 = i2 + 2 in
let value = String.sub binary i0 (i3 - i0) in
Ok2 (i3, OtpErlangFunction (
Function.make
~tag:tag ~value:value))
else if tag = tag_newer_reference_ext || tag = tag_new_reference_ext then
let j = (unpack_uint16 i0 binary) * 4
and i1 = i0 + 2 in
match binary_to_atom i1 binary with
| Error3 (error) ->
Error2 (error)
| Ok3 (i2, node_tag, node) ->
let creation_size = if tag = tag_newer_reference_ext then 4 else 1 in
let creation = String.sub binary i2 creation_size
and i3 = i2 + creation_size in
let id = String.sub binary i3 j in
Ok2 (i3 + j, OtpErlangReference(
Reference.make
~node_tag:node_tag ~node:node ~id:id ~creation:creation))
else if tag = tag_map_ext then
match unpack_uint32 i0 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i1, length) ->
let pairs = Hashtbl.create length in
let rec loop length_index i2 =
if length_index = length then
Ok2 (i2, OtpErlangMap (pairs))
else
match binary_to_term_ i2 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i3, key) ->
match binary_to_term_ i3 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i4, value) ->
Hashtbl.add pairs key value ;
loop (length_index + 1) i4
in
loop 0 i1
else if tag = tag_fun_ext then
match unpack_uint32 i0 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i1, numfree) ->
match binary_to_pid i1 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i2, _) -> (* pid *)
match binary_to_atom i2 binary with
| Error3 (error) ->
Error2 (error)
| Ok3 (i3, _, _) -> (* module *)
match binary_to_integer i3 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i4, _) -> (* index *)
match binary_to_integer i4 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i5, _) -> (* uniq *)
match binary_to_term_sequence i5 numfree binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i6, _) -> (* free *)
let value = String.sub binary i0 (i6 - i0) in
Ok2 (i6, OtpErlangFunction (
Function.make
~tag:tag ~value:value))
else if tag = tag_atom_utf8_ext || tag = tag_atom_ext then
let j = unpack_uint16 i0 binary
and i1 = i0 + 2 in
let atom_name = String.sub binary i1 j
and i2 = i1 + j in
if atom_name = "true" then
Ok2 (i2, OtpErlangAtomBool (true))
else if atom_name = "false" then
Ok2 (i2, OtpErlangAtomBool (false))
else if tag = tag_atom_utf8_ext then
Ok2 (i2, OtpErlangAtomUTF8 (atom_name))
else
Ok2 (i2, OtpErlangAtom (atom_name))
else if tag = tag_small_atom_utf8_ext || tag = tag_small_atom_ext then
let j = int_of_char binary.[i0]
and i1 = i0 + 1 in
let atom_name = String.sub binary i1 j
and i2 = i1 + j in
if atom_name = "true" then
Ok2 (i2, OtpErlangAtomBool (true))
else if atom_name = "false" then
Ok2 (i2, OtpErlangAtomBool (false))
else if tag = tag_small_atom_utf8_ext then
Ok2 (i2, OtpErlangAtomUTF8 (atom_name))
else
Ok2 (i2, OtpErlangAtom (atom_name))
else if tag = tag_compressed_zlib then
Error2 (parse_error "ocaml doesn't provide zlib")
else if tag = tag_local_ext then
Error2 (parse_error "LOCAL_EXT is opaque")
else
Error2 (parse_error "invalid tag")
and binary_to_term_sequence i length binary : (int, t list, string) result2 =
let rec loop length_index i0 sequence =
if length_index = length then
Ok2 (i0, List.rev sequence)
else
match binary_to_term_ i0 binary with
| Error2 (error) ->
Error2 (error)
| Ok2 (i1, element) ->
loop (length_index + 1) i1 ([element] @ sequence)
in
loop 0 i []
and binary_to_integer i binary : (int, t, string) result2 =
let tag = int_of_char binary.[i]
and i0 = i + 1 in
if tag = tag_small_integer_ext then
Ok2 (i0 + 1, OtpErlangInteger (int_of_char binary.[i0]))
else if tag = tag_integer_ext then
Ok2 (i0 + 4, unpack_integer i0 binary)
else
Error2 (parse_error "invalid integer tag")
and binary_to_pid i binary : (int, t, string) result2 =
let tag = int_of_char binary.[i]
and i0 = i + 1 in
if tag = tag_new_pid_ext then
match binary_to_atom i0 binary with
| Error3 (error) ->
Error2 (error)
| Ok3(i1, node_tag, node) ->
let id = String.sub binary i1 4
and i2 = i1 + 4 in
let serial = String.sub binary i2 4
and i3 = i2 + 4 in
let creation = String.sub binary i3 4
and i4 = i3 + 4 in
Ok2 (i4, OtpErlangPid (
Pid.make
~node_tag:node_tag ~node:node
~id:id ~serial:serial ~creation:creation))
else if tag = tag_pid_ext then
match binary_to_atom i0 binary with
| Error3 (error) ->
Error2 (error)
| Ok3(i1, node_tag, node) ->
let id = String.sub binary i1 4
and i2 = i1 + 4 in
let serial = String.sub binary i2 4
and i3 = i2 + 4 in
let creation = String.sub binary i3 1
and i4 = i3 + 1 in
Ok2 (i4, OtpErlangPid (
Pid.make
~node_tag:node_tag ~node:node
~id:id ~serial:serial ~creation:creation))
else
Error2 (parse_error "invalid pid tag")
and binary_to_atom i binary : (int, int, string, string) result3 =
let tag = int_of_char binary.[i]
and i0 = i + 1 in
if tag = tag_atom_ext then
let j = unpack_uint16 i0 binary
and i1 = i0 + 2 in
Ok3 (i1 + j, tag, String.sub binary i0 (2 + j))
else if tag = tag_atom_cache_ref then
Ok3 (i0 + 1, tag, String.make 1 binary.[i0])
else if tag = tag_small_atom_ext then
let j = int_of_char binary.[i0]
and i1 = i0 + 1 in
Ok3 (i1 + j, tag, String.sub binary i0 (1 + j))
else if tag = tag_atom_utf8_ext then
let j = unpack_uint16 i0 binary
and i1 = i0 + 2 in
Ok3 (i1 + j, tag, String.sub binary i0 (2 + j))
else if tag = tag_small_atom_utf8_ext then
let j = int_of_char binary.[i0]
and i1 = i0 + 1 in
Ok3 (i1 + j, tag, String.sub binary i0 (1 + j))
else
Error3 (parse_error "invalid atom tag")
let rec term_to_binary_ term buffer : (Buffer.t, string) result =
match term with
| OtpErlangInteger (value) ->
integer_to_binary value buffer
| OtpErlangIntegerBig (value) ->
bignum_to_binary value buffer
| OtpErlangFloat (value) ->
float_to_binary value buffer
| OtpErlangAtom (value) ->
atom_to_binary value buffer
| OtpErlangAtomUTF8 (value) ->
atom_utf8_to_binary value buffer
| OtpErlangAtomCacheRef (value) ->
Buffer.add_char buffer (char_of_int tag_atom_cache_ref) ;
Buffer.add_char buffer (char_of_int value) ;
Ok (buffer)
| OtpErlangAtomBool (value) ->
if value then
atom_utf8_to_binary "true" buffer
else
atom_utf8_to_binary "false" buffer
| OtpErlangString (value) ->
string_to_binary value buffer
| OtpErlangBinary (value) ->
binary_bits_to_binary value 8 buffer
| OtpErlangBinaryBits (value, bits) ->
binary_bits_to_binary value bits buffer
| OtpErlangList (value) ->
list_to_binary value false buffer
| OtpErlangListImproper (value) ->
list_to_binary value true buffer
| OtpErlangTuple (value) ->
tuple_to_binary value buffer
| OtpErlangMap (value) ->
hashtbl_to_binary value buffer
| OtpErlangPid ({Pid.node_tag; node; id; serial; creation}) ->
pid_to_binary node_tag node id serial creation buffer
| OtpErlangPort ({Port.node_tag; node; id; creation}) ->
port_to_binary node_tag node id creation buffer
| OtpErlangReference ({Reference.node_tag; node; id; creation}) ->
reference_to_binary node_tag node id creation buffer
| OtpErlangFunction ({Function.tag; value}) ->
function_to_binary tag value buffer
and string_to_binary value buffer =
let length = String.length value in
if length = 0 then (
Buffer.add_char buffer (char_of_int tag_nil_ext) ;
Ok (buffer))
else if length <= 65535 then (
Buffer.add_char buffer (char_of_int tag_string_ext) ;
pack_uint16 length buffer ;
Buffer.add_string buffer value ;
Ok (buffer))
else if valid_uint32_positive length then (
Buffer.add_char buffer (char_of_int tag_list_ext) ;
pack_uint32 length buffer ;
String.iter (fun c ->
Buffer.add_char buffer (char_of_int tag_small_integer_ext) ;
Buffer.add_char buffer c
) value ;
Buffer.add_char buffer (char_of_int tag_nil_ext) ;
Ok (buffer))
else
Error (output_error "uint32 overflow")
and tuple_to_binary value buffer =
let rec loop = function
| [] ->
Ok (buffer)
| h::t ->
match term_to_binary_ h buffer with
| Error (error) ->
Error (error)
| Ok _ ->
loop t
and length = List.length value in
if length <= 255 then (
Buffer.add_char buffer (char_of_int tag_small_tuple_ext) ;
Buffer.add_char buffer (char_of_int length) ;
loop value)
else if valid_uint32_positive length then (
Buffer.add_char buffer (char_of_int tag_large_tuple_ext) ;
pack_uint32 length buffer ;
loop value)
else
Error (output_error "uint32 overflow")
and hashtbl_to_binary value buffer =
Buffer.add_char buffer (char_of_int tag_map_ext) ;
let length = Hashtbl.length value in
if valid_uint32_positive length then (
pack_uint32 length buffer ;
try (Hashtbl.iter (fun key value ->
match term_to_binary_ key buffer with
| Error (error) ->
raise (HashtblExit error)
| Ok _ ->
match term_to_binary_ value buffer with
| Error (error) ->
raise (HashtblExit error)
| Ok _ ->
()
) value ; Ok (buffer))
with
HashtblExit (error) ->
Error (error))
else
Error (output_error "uint32 overflow")
and list_to_binary value improper buffer =
let rec loop = function
| [] ->
Ok (buffer)
| h::t ->
match term_to_binary_ h buffer with
| Ok _ ->
loop t
| Error (error) ->
Error (error)
and length = List.length value in
if length = 0 then (
Buffer.add_char buffer (char_of_int tag_nil_ext) ;
Ok (buffer))
else if valid_uint32_positive length then (
Buffer.add_char buffer (char_of_int tag_list_ext) ; (
if improper then
pack_uint32 (length - 1) buffer
else
pack_uint32 length buffer) ;
match loop value with
| Error (error) ->
Error (error)
| Ok (_) when not improper ->
Buffer.add_char buffer (char_of_int tag_nil_ext) ;
Ok (buffer)
| Ok (_) ->
Ok (buffer))
else
Error (output_error "uint32 overflow")
and integer_to_binary value buffer =
if value >= 0 && value <= 255 then (
Buffer.add_char buffer (char_of_int tag_small_integer_ext) ;
Buffer.add_char buffer (char_of_int value) ;
Ok (buffer))
else if valid_int32 value then (
Buffer.add_char buffer (char_of_int tag_integer_ext) ;
pack_uint32 value buffer ;
Ok (buffer))
else
bignum_to_binary (Big_int_Z.big_int_of_int value) buffer
and bignum_to_binary value buffer =
let bits8 = Big_int_Z.big_int_of_int 0xff
and sign = if (Big_int_Z.sign_big_int value) = -1 then
char_of_int 1
else
char_of_int 0
in
let rec loop bignum l =
if Big_int_Z.gt_big_int bignum Big_int_Z.zero_big_int then (
Buffer.add_char l (char_of_int
(Big_int_Z.int_of_big_int (Big_int_Z.and_big_int bignum bits8))) ;
loop (Big_int_Z.shift_right_towards_zero_big_int bignum 8) l)
else
l
in
let l_result = loop (Big_int_Z.abs_big_int value) (Buffer.create 255) in
let l_length = Buffer.length l_result in
if l_length <= 255 then (
Buffer.add_char buffer (char_of_int tag_small_big_ext) ;
Buffer.add_char buffer (char_of_int l_length) ;
Buffer.add_char buffer sign ;
Buffer.add_buffer buffer l_result ;
Ok (buffer))
else if (valid_uint32_positive l_length) then (
Buffer.add_char buffer (char_of_int tag_large_big_ext) ;
pack_uint32 l_length buffer ;
Buffer.add_char buffer sign ;
Buffer.add_buffer buffer l_result ;
Ok (buffer))
else
Error (output_error "uint32 overflow")
and float_to_binary value buffer =
Buffer.add_char buffer (char_of_int tag_new_float_ext) ;
pack_double value buffer ;
Ok (buffer)
and atom_to_binary value buffer =
(* deprecated
* (not used in Erlang/OTP 26, i.e., minor_version 2)
*)
let length = String.length value in
if length <= 255 then (
Buffer.add_char buffer (char_of_int tag_small_atom_ext) ;
Buffer.add_char buffer (char_of_int length) ;
Buffer.add_string buffer value ;
Ok (buffer))
else if length <= 65535 then (
Buffer.add_char buffer (char_of_int tag_atom_ext) ;
pack_uint16 length buffer ;
Buffer.add_string buffer value ;
Ok (buffer))
else
Error (output_error "uint16 overflow")
and atom_utf8_to_binary value buffer =
let length = String.length value in
if length <= 255 then (
Buffer.add_char buffer (char_of_int tag_small_atom_utf8_ext) ;
Buffer.add_char buffer (char_of_int length) ;
Buffer.add_string buffer value ;
Ok (buffer))
else if length <= 65535 then (
Buffer.add_char buffer (char_of_int tag_atom_utf8_ext) ;
pack_uint16 length buffer ;
Buffer.add_string buffer value ;
Ok (buffer))
else
Error (output_error "uint16 overflow")
and binary_bits_to_binary value bits buffer =
let length = String.length value in
if bits < 1 || bits > 8 then
Error (output_error "invalid OtpErlangBinaryBits")
else if valid_uint32_positive length then (
if bits <> 8 then (
Buffer.add_char buffer (char_of_int tag_bit_binary_ext) ;
pack_uint32 length buffer ;
Buffer.add_char buffer (char_of_int bits))
else (
Buffer.add_char buffer (char_of_int tag_binary_ext) ;
pack_uint32 length buffer) ;
Buffer.add_string buffer value ;
Ok (buffer))
else
Error (output_error "uint32 overflow")
and pid_to_binary node_tag node id serial creation buffer =
let creation_size = String.length creation in
let tag =
if creation_size = 4 then
tag_new_pid_ext
else
tag_pid_ext
in
Buffer.add_char buffer (char_of_int tag) ;
Buffer.add_char buffer (char_of_int node_tag) ;
Buffer.add_string buffer node ;
Buffer.add_string buffer id ;
Buffer.add_string buffer serial ;
Buffer.add_string buffer creation ;
Ok (buffer)
and port_to_binary node_tag node id creation buffer =
let id_size = String.length id
and creation_size = String.length creation in
let tag =
if id_size = 8 then
tag_v4_port_ext
else if creation_size = 4 then
tag_new_port_ext
else
tag_port_ext
in
Buffer.add_char buffer (char_of_int tag) ;
Buffer.add_char buffer (char_of_int node_tag) ;
Buffer.add_string buffer node ;
Buffer.add_string buffer id ;
Buffer.add_string buffer creation ;
Ok (buffer)
and reference_to_binary node_tag node id creation buffer =
let length = (String.length id) / 4 in
if length = 0 then (
Buffer.add_char buffer (char_of_int tag_reference_ext) ;
Buffer.add_char buffer (char_of_int node_tag) ;
Buffer.add_string buffer node ;
Buffer.add_string buffer id ;
Buffer.add_string buffer creation ;
Ok (buffer))
else if length <= 65535 then (
let creation_size = String.length creation in
let tag =
if creation_size = 4 then
tag_newer_reference_ext
else
tag_new_reference_ext
in
Buffer.add_char buffer (char_of_int tag) ;
pack_uint16 length buffer ;
Buffer.add_char buffer (char_of_int node_tag) ;
Buffer.add_string buffer node ;
Buffer.add_string buffer creation ;
Buffer.add_string buffer id ;
Ok (buffer))
else
Error (output_error "uint16 overflow")
and function_to_binary tag value buffer =
Buffer.add_char buffer (char_of_int tag) ;
Buffer.add_string buffer value ;
Ok (buffer)
let binary_to_term (binary : string) : (t, string) result =
let size = String.length binary in
if size <= 1 then
Error (parse_error "null input")
else if int_of_char binary.[0] <> tag_version then
Error (parse_error "invalid version")
else
try
match binary_to_term_ 1 binary with
| Error2 error ->
Error error
| Ok2 (i, _) when i <> size ->
Error (parse_error "unparsed data")
| Ok2 (_, term) ->
Ok term
with
Invalid_argument(_) ->
Error (parse_error "missing data")
let term_to_binary (term : t) : (string, string) result =
let buffer = Buffer.create buffer_size in
Buffer.add_char buffer (char_of_int tag_version) ;
match term_to_binary_ term buffer with
| Ok (data_uncompressed) ->
Ok (Buffer.contents data_uncompressed)
| Error (error) ->
Error (error)
let rec t_to_string (term : t) : string =
match term with
| OtpErlangInteger (value) ->
"OtpErlangInteger(" ^ (string_of_int value) ^ ")"
| OtpErlangIntegerBig (value) ->
"OtpErlangIntegerBig(" ^ (Big_int_Z.string_of_big_int value) ^ ")"
| OtpErlangFloat (value) ->
"OtpErlangFloat(" ^ (string_of_float value) ^ ")"
| OtpErlangAtom (value) ->
"OtpErlangAtom('" ^ value ^ "')"
| OtpErlangAtomUTF8 (value) ->
"OtpErlangAtomUTF8('" ^ value ^ "')"
| OtpErlangAtomCacheRef (value) ->
"OtpErlangAtomUTF8('atom(" ^ (string_of_int value) ^ ")')"
| OtpErlangAtomBool (value) ->
if value then
"OtpErlangAtomBool('true')"
else
"OtpErlangAtomBool('false')"
| OtpErlangString (value) ->
"OtpErlangString(\"" ^ value ^ "\")"
| OtpErlangBinary (value) ->
"OtpErlangBinary(\"" ^ value ^ "\")"
| OtpErlangBinaryBits (value, bits) ->
"OtpErlangBinaryBits(\"" ^ value ^ "\"," ^ (string_of_int bits) ^ ")"
| OtpErlangList (value) ->
"OtpErlangList(" ^ (sequence_to_string value) ^ ")"
| OtpErlangListImproper (value) ->
"OtpErlangListImproper(" ^ (sequence_to_string value) ^ ")"
| OtpErlangTuple (value) ->
"OtpErlangTuple(" ^ (sequence_to_string value) ^ ")"
| OtpErlangMap (value) ->
"OtpErlangMap(" ^ (map_to_string value) ^ ")"
| OtpErlangPid ({Pid.node_tag; node; id; serial; creation}) ->
"OtpErlangPid(" ^
"node_tag:" ^ (string_of_int node_tag) ^ "; " ^
"node:\"" ^ (String.escaped node) ^ "\"; " ^
"id:\"" ^ (String.escaped id) ^ "\"; " ^
"serial:\"" ^ (String.escaped serial) ^ "\"; " ^
"creation:" ^ (String.escaped creation) ^ ")"
| OtpErlangPort ({Port.node_tag; node; id; creation}) ->