otp.net /Otp/OtpInputStream.cs

Language C# Lines 998
MD5 Hash e8e6af8d97d26b1cb759dd8cb280cae8 Estimated Cost $14,890 (why?)
Repository https://github.com/saleyn/otp.net.git View Raw File View Project SPDX
  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
/*``The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved via the world wide web at http://www.erlang.org/.
* 
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
* 
* The Initial Developer of the Original Code is Ericsson Utvecklings AB.
* Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
* AB. All Rights Reserved.''
* 
 * Converted from Java to C# by Vlad Dumitrescu (vlad_Dumitrescu@hotmail.com)
*/
namespace Otp
{
    using System;

    /*
    * Provides a stream for decoding Erlang terms from external format.
    *
    * <p> Note that this class is not synchronized, if you need
    * synchronization you must provide it yourself.
    **/
    public class OtpInputStream : System.IO.MemoryStream
    {
        byte[] m_buf2;
        byte[] m_buf4;
        byte[] m_buf8;
        private int m_Origin;

        /*
        * Create a stream from a buffer containing encoded Erlang terms.
        **/
        public OtpInputStream(byte[] buf)
            : this(buf, 0, buf.Length)
        {
        }

        /*
        * Create a stream from a buffer containing encoded
        * Erlang terms at the given offset and length.
        **/
        public OtpInputStream(byte[] buf, int offset, int length)
            : base(buf, offset, length, false, true)
        {
            m_buf2 = new byte[2];
            m_buf4 = new byte[4];
            m_buf8 = new byte[8];
            m_Origin = offset;
        }

        public int BufferPosition
        {
            get { return m_Origin + (int)Position; }
        }

        ///*
        //* Get the current position in the stream.
        //*
        //* @return the current position in the stream.
        //**/
        //public int getPos()
        //{
        //    return (int) base.Position;
        //}
        ///*
        //* Set the current position in the stream.
        //*
        //* @param pos the position to move to in the stream. If pos
        //* indicates a position beyond the end of the stream, the position
        //* is move to the end of the stream instead. If pos is negative, the
        //* position is moved to the beginning of the stream instead.
        //*
        //* @return the previous position in the stream.
        //**/
        //public int setPos(int pos)
        //{
        //    int oldpos = (int) base.Position;
        //    if (pos > (int) base.Length)
        //        pos = (int) base.Length;
        //    else if (pos < 0)
        //        pos = 0;

        //    base.Position = (System.Int64) pos;

        //    return oldpos;
        //}

        /*
        * Read an array of bytes from the stream. The method reads at most
        * buf.length bytes from the input stream.
        *
        * @return the number of bytes read.
        *
        * @exception OtpErlangDecodeException if the next byte cannot be
        * read.
        **/
        public int readN(byte[] buf)
        {
            try
            {
                return base.Read(buf, 0, buf.Length);
            }
            catch (System.IO.IOException)
            {
                throw new Erlang.Exception("Cannot read from input stream");
            }
        }

        /*
        * Look ahead one position in the stream without consuming the byte
        * found there.
        *
        * @return the next byte in the stream, as an integer.
        *
        * @exception Erlang.DecodeException if the next byte cannot be
        * read.
        **/
        public int peek()
        {
            int i;
            try
            {
                i = base.ReadByte();
                base.Seek(-1, System.IO.SeekOrigin.Current);
                if (i < 0)
                    i += 256;

                return i;
            }
            catch (System.Exception)
            {
                throw new Erlang.Exception("Cannot read from input stream");
            }
        }

        /*
        * Read a one byte integer from the stream.
        *
        * @return the byte read, as an integer.
        * 
        * @exception Erlang.DecodeException if the next byte cannot be
        * read.
        **/
        public int read1()
        {
            int i;
            i = base.ReadByte();

            if (i < 0)
            {
                throw new Erlang.Exception("Cannot read from input stream");
            }

            return i;
        }

        /*
        * Read a two byte big endian integer from the stream.
        *
        * @return the bytes read, converted from big endian to an integer.
        * 
        * @exception Erlang.DecodeException if the next byte cannot be
        * read.
        **/
        public int read2BE()
        {
            try
            {
                base.Read(m_buf2, 0, m_buf2.Length);
            }
            catch (System.IO.IOException)
            {
                throw new Erlang.Exception("Cannot read from input stream");
            }
            return ((((int)m_buf2[0] << 8) & 0xff00) + (((int)m_buf2[1]) & 0xff));
        }

        /*
        * Read a four byte big endian integer from the stream.
        *
        * @return the bytes read, converted from big endian to an integer.
        * 
        * @exception Erlang.DecodeException if the next byte cannot be
        * read.
        **/
        public int read4BE()
        {
            try
            {
                base.Read(m_buf4, 0, m_buf4.Length);
            }
            catch (System.IO.IOException)
            {
                throw new Erlang.Exception("Cannot read from input stream");
            }
            return read4BE(m_buf4);
        }

        public static int read4BE(byte[] b)
        {
            System.Diagnostics.Debug.Assert(b.Length == 4);
            return (int)((((int)b[0] << 24) & 0xff000000) + (((int)b[1] << 16) & 0xff0000) + (((int)b[2] << 8) & 0xff00) + (((int)b[3]) & 0xff));
        }

        /*
        * Read an eight byte big endian integer from the stream.
        *
        * @return the bytes read, converted from big endian to an integer.
        * 
        * @exception Erlang.DecodeException if the next byte cannot be
        * read.
        **/
        public System.UInt64 read8BE()
        {
            try
            {
                base.Read(m_buf8, 0, m_buf8.Length);
            }
            catch (System.IO.IOException)
            {
                throw new Erlang.Exception("Cannot read from input stream");
            }
            System.UInt64 i1 = (System.UInt64)((((int)m_buf8[0] << 24) & 0xff000000)
                             + (((int)m_buf8[1] << 16) & 0xff0000)
                             + (((int)m_buf8[2] << 8) & 0xff00)
                             + (((int)m_buf8[3]) & 0xff));
            System.UInt64 i2 = (i1 << 32) & 0xffffffff00000000
                             + (System.UInt64)((((int)m_buf8[4] << 24) & 0xff000000)
                             + (((int)m_buf8[5] << 16) & 0xff0000)
                             + (((int)m_buf8[6] << 8) & 0xff00)
                             + (((int)m_buf8[7]) & 0xff));
            return i2;
        }

        /*
        * Read a two byte little endian integer from the stream.
        *
        * @return the bytes read, converted from little endian to an
        * integer.
        * 
        * @exception Erlang.DecodeException if the next byte cannot be
        * read.
        **/
        public int read2LE()
        {
            try
            {
                base.Read(m_buf2, 0, m_buf2.Length);
            }
            catch (System.IO.IOException)
            {
                throw new Erlang.Exception("Cannot read from input stream");
            }
            return ((((int)m_buf2[1] << 8) & 0xff00) + (((int)m_buf2[0]) & 0xff));
        }

        /*
        * Read a four byte little endian integer from the stream.
        *
        * @return the bytes read, converted from little endian to an
        * integer.
        * 
        * @exception Erlang.DecodeException if the next byte cannot be
        * read.
        **/
        public int read4LE()
        {
            try
            {
                base.Read(m_buf4, 0, m_buf4.Length);
            }
            catch (System.IO.IOException)
            {
                throw new Erlang.Exception("Cannot read from input stream");
            }
            return (int)((((int)m_buf4[3] << 24) & 0xff000000) + (((int)m_buf4[2] << 16) & 0xff0000)
                + (((int)m_buf4[1] << 8) & 0xff00) + (((int)m_buf4[0]) & 0xff));
        }

        /*
        * Read an Erlang atom from the stream and interpret the value as a
        * boolean.
        *
        * @return true if the atom at the current position in the stream
        * contains the value 'true' (ignoring case), false otherwise.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream is not an atom.
        **/
        public bool read_boolean()
        {
            return System.Boolean.Parse(this.read_atom());
        }

        /*
        * Read an Erlang atom from the stream.
        *
        * @return a String containing the value of the atom.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream is not an atom.
        **/
        public string read_atom()
        {
            int tag = this.read1();
            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            if (tag != OtpExternal.atomTag)
            {
                throw new Erlang.Exception("wrong tag encountered, expected " + OtpExternal.atomTag + ", got " + tag);
            }

            int len = this.read2BE();
            int n = len > OtpExternal.maxAtomLength ? OtpExternal.maxAtomLength : len;
            string s = System.Text.Encoding.ASCII.GetString(base.GetBuffer(), BufferPosition, len);
            base.Position += len;
            if (n != len)
                s = s.Substring(0, n);
            return s;
        }

        /*
        * Read an Erlang binary from the stream.
        *
        * @return a byte array containing the value of the binary.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream is not a binary.
        **/
        public byte[] read_binary()
        {
            int tag;
            int len;
            byte[] bin;

            tag = this.read1();
            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            if (tag != OtpExternal.binTag)
            {
                throw new Erlang.Exception("Wrong tag encountered, expected " + OtpExternal.binTag + ", got " + tag);
            }

            len = this.read4BE();

            bin = new byte[len];
            this.readN(bin);

            return bin;
        }

        /*
        * Read an Erlang float from the stream.
        *
        * @return the float value.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream is not a float.
        **/
        public float read_float()
        {
            double d = this.read_double();
            float f = (float)d;
            if (System.Math.Abs(d - f) >= 1.0E-20)
                throw new Erlang.Exception("Value cannot be represented as float: " + d);
            return f;
        }

        /*
        * Read an Erlang float from the stream.
        *
        * @return the float value, as a double.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream is not a float.
        *
        **/
        public double read_double()
        {
            return getFloatOrDouble();
        }

        private double getFloatOrDouble()
        {

            // parse the stream
            int tag = this.read1();
            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            byte[] strbuf;
            double parsedValue = 0.0;

            if (tag == OtpExternal.floatTag)
            {
                // get the string
                strbuf = new byte[31];
                this.readN(strbuf);

                char[] tmpChar = new char[strbuf.Length];
                strbuf.CopyTo(tmpChar, 0);
                System.String str = new System.String(tmpChar);
                //System.Diagnostics.Debug.WriteLine("getFloatOrDouble: str = " + str);

                try
                {
                    // Easier than the java version.
                    parsedValue = System.Double.Parse(str);
                    return parsedValue;
                }
                catch
                {
                    throw new Erlang.Exception("Error parsing float format: '" + str + "'");
                }
            }
            else if (tag == OtpExternal.newFloatTag)
            {
                this.readN(m_buf8);
                // IEEE 754 decoder
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(m_buf8);
                }
                return BitConverter.ToDouble(m_buf8, 0);
            }
            else
            {
                throw new Erlang.Exception("Wrong tag encountered, expected " + OtpExternal.floatTag + ", got " + tag);
            }
        }

        /*
        * Read one byte from the stream.
        *
        * @return the byte read.
        * 
        * @exception Erlang.DecodeException if the next byte cannot be
        * read.
        **/
        public byte read_byte()
        {
            long l = this.read_long();
            byte i = (byte)l;

            if (l != i)
            {
                throw new Erlang.Exception("Value too large for byte: " + l);
            }

            return i;
        }

        /*
        * Read a character from the stream.
        *
        * @return the character value.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream is not an integer that can be represented as a char.
        **/
        public char read_char()
        {
            long l = this.read_long();
            char i = (char)l;

            if (l != i)
            {
                throw new Erlang.Exception("Value too large for byte: " + l);
            }

            return i;
        }

        /*
        * Read an unsigned integer from the stream.
        *
        * @return the integer value.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream can not be represented as a positive integer.
        **/
        public int read_uint()
        {
            long l = this.read_long();
            int i = (int)l;

            if (l != i)
            {
                throw new Erlang.Exception("Value too large for integer: " + l);
            }
            else if (l < 0)
            {
                throw new Erlang.Exception("Value not unsigned: " + l);
            }

            return i;
        }

        /*
        * Read an integer from the stream.
        *
        * @return the integer value.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream can not be represented as an integer.
        **/
        public int read_int()
        {
            long l = this.read_long();
            int i = (int)l;

            if (l != i)
            {
                throw new Erlang.Exception("Value too large for byte: " + l);
            }

            return i;
        }

        /*
        * Read an unsigned short from the stream.
        *
        * @return the short value.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream can not be represented as a positive short.
        **/
        public short read_ushort()
        {
            long l = this.read_long();
            short i = (short)l;

            if (l != i)
            {
                throw new Erlang.Exception("Value too large for byte: " + l);
            }
            else if (l < 0)
            {
                throw new Erlang.Exception("Value not unsigned: " + l);
            }

            return i;
        }

        /*
        * Read a short from the stream.
        *
        * @return the short value.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream can not be represented as a short.
        **/
        public short read_short()
        {
            long l = this.read_long();
            short i = (short)l;

            if (l != i)
            {
                throw new Erlang.Exception("Value too large for byte: " + l);
            }

            return i;
        }

        /*
        * Read an unsigned long from the stream.
        *
        * @return the long value.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream can not be represented as a positive long.
        **/
        public ulong read_ulong()
        {
            return (ulong)read_long(false);
        }

        /*
        * Read a long from the stream.
        *
        * @return the long value.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream can not be represented as a long.
        **/
        public long read_long() { return read_long(true); }

        private long read_long(bool signed)
        {
            int tag;
            int sign;
            int arity;
            long val;

            tag = this.read1();
            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            switch (tag)
            {
                case OtpExternal.smallIntTag:
                    val = this.read1();
                    break;
                case OtpExternal.intTag:
                    val = this.read4BE();
                    break;
                case OtpExternal.smallBigTag:
                    {
                        arity = this.read1();
                        sign = this.read1();

                        byte[] nb = new byte[arity];
                        if (arity != this.readN(nb))
                        {
                            throw new Erlang.Exception("Cannot read from input stream. Expected smallBigTag arity " + arity);
                        }
                        if (arity > 8)
                            throw new Erlang.Exception("Value too large for long type (arity=" + arity + ")");

                        val = 0;
                        for (int i = 0; i < arity; i++)
                        {
                            val |= (long)nb[i] << (i * 8);
                        }

                        val = (sign == 0 ? val : -val); // should deal with overflow

                        if (sign == 1 && !signed)
                            throw new Erlang.Exception("Requested unsigned, but read signed long value: " + val.ToString());

                        break;
                    }
                case OtpExternal.largeBigTag:
                default:
                    throw new Erlang.Exception("Not valid integer tag: " + tag);
            }

            return val;
        }

        /*
        * Read a list header from the stream.
        *
        * @return the arity of the list.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream is not a list.
        **/
        public int read_list_head()
        {
            int arity = 0;
            int tag = this.read1();

            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            switch (tag)
            {
                case OtpExternal.nilTag:
                    arity = 0;
                    break;
                case OtpExternal.stringTag:
                    arity = this.read2BE();
                    break;
                case OtpExternal.listTag:
                    arity = this.read4BE();
                    break;
                default:
                    throw new Erlang.Exception("Not valid list tag: " + tag);
            }

            return arity;
        }

        /*
        * Read a tuple header from the stream.
        *
        * @return the arity of the tuple.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream is not a tuple.
        **/
        public int read_tuple_head()
        {
            int arity = 0;
            int tag = this.read1();

            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            // decode the tuple header and get arity
            switch (tag)
            {
                case OtpExternal.smallTupleTag:
                    arity = this.read1();
                    break;
                case OtpExternal.largeTupleTag:
                    arity = this.read4BE();
                    break;
                default:
                    throw new Erlang.Exception("Not valid tuple tag: " + tag);

            }

            return arity;
        }

        /*
        * Read an empty list from the stream.
        *
        * @return zero (the arity of the list).
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream is not an empty list.
        **/
        public int read_nil()
        {
            int arity = 0;
            int tag = this.read1();

            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            switch (tag)
            {
                case OtpExternal.nilTag:
                    arity = 0;
                    break;
                default:
                    throw new Erlang.Exception("Not valid nil tag: " + tag);
            }

            return arity;
        }

        /*
        * Read an Erlang PID from the stream.
        *
        * @return the value of the PID.
        * 
        * @exception Erlang.DecodeException if the next term in the
        * stream is not an Erlang PID.
        **/
        public Erlang.Pid read_pid()
        {
            System.String node;
            int id;
            int serial;
            int creation;
            int tag;

            tag = this.read1();
            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            if (tag != OtpExternal.pidTag)
            {
                throw new Erlang.Exception("Wrong tag encountered, expected " + OtpExternal.pidTag + ", got " + tag);
            }

            node = this.read_atom();
            id = this.read4BE() & 0x7fff; // 15 bits
            serial = this.read4BE() & 0x07; // 3 bits
            creation = this.read1() & 0x03; // 2 bits

            return new Erlang.Pid(node, id, serial, creation);
        }

        /*
        * Read an Erlang port from the stream.
        *
        * @return the value of the port.
        * 
        * @exception DecodeException if the next term in the
        * stream is not an Erlang port.
        **/
        public Erlang.Port read_port()
        {
            System.String node;
            int id;
            int creation;
            int tag;

            tag = this.read1();
            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            if (tag != OtpExternal.portTag)
            {
                throw new Erlang.Exception("Wrong tag encountered, expected " + OtpExternal.portTag + ", got " + tag);
            }

            node = this.read_atom();
            id = this.read4BE() & 0x3ffff; // 18 bits
            creation = this.read1() & 0x03; // 2 bits

            return new Erlang.Port(node, id, creation);
        }

        /*
        * Read an Erlang reference from the stream.
        *
        * @return the value of the reference
        * 
        * @exception DecodeException if the next term in the
        * stream is not an Erlang reference.
        **/
        public Erlang.Ref read_ref()
        {
            System.String node;
            int id;
            int creation;
            int tag;

            tag = this.read1();
            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            switch (tag)
            {
                case OtpExternal.refTag:
                    node = this.read_atom();
                    id = this.read4BE() & 0x3ffff; // 18 bits
                    creation = this.read1() & 0x03; // 2 bits
                    return new Erlang.Ref(node, id, creation);



                case OtpExternal.newRefTag:
                    int arity = this.read2BE();
                    node = this.read_atom();
                    creation = this.read1() & 0x03; // 2 bits

                    int[] ids = new int[arity];
                    for (int i = 0; i < arity; i++)
                    {
                        ids[i] = this.read4BE();
                    }
                    ids[0] &= 0x3ffff; // first id gets truncated to 18 bits
                    return new Erlang.Ref(node, ids, creation);



                default:
                    throw new Erlang.Exception("Wrong tag encountered, expected ref, got " + tag);

            }
        }

        /*
        * Read a string from the stream.
        *
        * @return the value of the string.
        * 
        * @exception DecodeException if the next term in the
        * stream is not a string.
        **/
        public System.String read_string()
        {
            int tag;
            int len;
            char[] charbuf;

            tag = this.read1();
            if (tag == OtpExternal.versionTag)
            {
                tag = this.read1();
            }

            switch (tag)
            {

                case OtpExternal.stringTag:
                    len = this.read2BE();
                    string s = System.Text.Encoding.ASCII.GetString(base.GetBuffer(), BufferPosition, len);
                    base.Position += len;
                    return s;

                case OtpExternal.nilTag:
                    return "";

                case OtpExternal.listTag:
                    // List when unicode +
                    len = this.read4BE();
                    charbuf = new char[len];

                    for (int i = 0; i < len; i++)
                        charbuf[i] = this.read_char();

                    this.read_nil();
                    return new string(charbuf);

                default:
                    throw new Erlang.Exception("Wrong tag encountered, expected " + OtpExternal.stringTag + " or " + OtpExternal.listTag + ", got " + tag);

            }
        }

        /*
        * Read an arbitrary Erlang term from the stream.
        *
        * @return the Erlang term.
        *
        * @exception DecodeException if the stream does not
        * contain a known Erlang type at the next position.
        **/
        public Erlang.Object read_any()
        {
            // calls one of the above functions, depending on o
            int tag = this.peek();
            if (tag == OtpExternal.versionTag)
            {
                this.read1();
                tag = this.peek();
            }

            //System.Diagnostics.Debug.WriteLine("read_any: tag = " + tag);

            switch (tag)
            {
                case OtpExternal.smallIntTag:
                case OtpExternal.intTag:
                case OtpExternal.smallBigTag:
                    return new Erlang.Long(this);

                case OtpExternal.atomTag:
                    string s = read_atom();
                    if (s == "true")
                        return new Erlang.Boolean(true);
                    else if (s == "false")
                        return new Erlang.Boolean(false);
                    else
                        return new Erlang.Atom(s);

                case OtpExternal.floatTag:
                case OtpExternal.newFloatTag:
                    return new Erlang.Double(this);

                case OtpExternal.refTag:
                case OtpExternal.newRefTag:
                    return new Erlang.Ref(this);

                case OtpExternal.portTag:
                    return new Erlang.Port(this);

                case OtpExternal.pidTag:
                    return new Erlang.Pid(this);

                case OtpExternal.stringTag:
                    return new Erlang.String(this);

                case OtpExternal.listTag:
                case OtpExternal.nilTag:
                    return new Erlang.List(this);

                case OtpExternal.smallTupleTag:
                case OtpExternal.largeTupleTag:
                    return new Erlang.Tuple(this);

                case OtpExternal.binTag:
                    return new Erlang.Binary(this);

                case OtpExternal.largeBigTag:
                default:
                    throw new Erlang.Exception("Uknown data type: " + tag);

            }
        }
    }
}
Back to Top