main /Languages/IronPython/IronPython/Runtime/Operations/MarshalOps.cs

Language C# Lines 715
MD5 Hash c98da99ded9f7f9d9403567bc9121d75 Estimated Cost $15,056 (why?)
Repository git://github.com/IronLanguages/main.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
using System;
using System.Collections.Generic;
using System.Text;

using IronPython.Runtime;
using IronPython.Runtime.Exceptions;
using IronPython.Runtime.Operations;
using IronPython.Runtime.Types;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

#if FEATURE_NUMERICS
using System.Numerics;
#else
using Microsoft.Scripting.Math;
using Complex = Microsoft.Scripting.Math.Complex64;
#endif

namespace IronPython.Runtime.Operations {
    public class MarshalOps {
        public static byte[] GetBytes (object o, int version) {
            MarshalWriter mw = new MarshalWriter (version);
            mw.WriteObject (o);
            return mw.GetBytes ();
        }

        public static object GetObject (IEnumerator<byte> bytes) {
            MarshalReader mr = new MarshalReader (bytes);
            return mr.ReadObject ();
        }


        //
        // Format 
        //  
        // Tuple: '(',int cnt,tuple items
        // List:  '[',int cnt, list items
        // Dict:  '{',key item, value item, '0' terminator
        // Int :  'i',4 bytes
        // float: 'f', 1 byte len, float in string
        // float: 'g', 8 bytes - float in binary form
        // BigInt:  'l', int encodingSize
        //      if the value is negative then the size is negative
        //      and needs to be subtracted from int.MaxValue
        // 
        //      the bytes are encoded in 15 bit multiples, a size of
        //      0 represents a value of zero.
        // 
        // True: 'T'
        // False: 'F'
        // Float: 'f', str len, float in str
        // string: 't', int len, bytes  (ascii)
        // string: 'u', int len, bytes (unicode)
        // string: 'R' <id> - refer to interned string
        // StopIteration: 'S'   
        // None: 'N'
        // Long: 'I' followed by 8 bytes (little endian 64-bit value)
        // complex: 'x', byte len, real str, byte len, imag str
        // Buffer (array.array too, but they don't round trip): 's', int len, buffer bytes
        // code: 'c', more stuff...
        // 
        private class MarshalWriter {
            private readonly List<byte> _bytes;
            private readonly int _version;
            private readonly Dictionary<string, int> _strings;

            public MarshalWriter (int version) {
                _bytes = new List<byte> ();
                _version = version;
                if (_version > 0) {
                    _strings = new Dictionary<string, int> ();
                }
            }

            public void WriteObject (object o) {
                List<object> infinite = PythonOps.GetReprInfinite ();

                if (infinite.Contains (o)) throw PythonOps.ValueError ("Marshaled data contains infinite cycle");

                int index = infinite.Count;
                infinite.Add (o);
                try {
                    if (o == null) _bytes.Add ((byte)'N');
                    else if (o == ScriptingRuntimeHelpers.True) _bytes.Add ((byte)'T');
                    else if (o == ScriptingRuntimeHelpers.False) _bytes.Add ((byte)'F');
                    else if (o is string) WriteString (o as string);
                    else if (o is int) WriteInt ((int)o);
                    else if (o is float) WriteFloat ((float)o);
                    else if (o is double) WriteFloat ((double)o);
                    else if (o is long) WriteLong ((long)o);
                    else if (o.GetType () == typeof (List)) WriteList (o);
                    else if (o.GetType () == typeof (PythonDictionary)) WriteDict (o);
                    else if (o.GetType () == typeof (PythonTuple)) WriteTuple (o);
                    else if (o.GetType () == typeof (SetCollection)) WriteSet (o);
                    else if (o.GetType () == typeof (FrozenSetCollection)) WriteFrozenSet (o);
                    else if (o is BigInteger) WriteInteger ((BigInteger)o);
                    else if (o is Complex) WriteComplex ((Complex)o);
                    else if (o is PythonBuffer) WriteBuffer ((PythonBuffer)o);
                    else if (o == PythonExceptions.StopIteration) WriteStopIteration ();
                    else throw PythonOps.ValueError ("unmarshallable object");
                } finally {
                    infinite.RemoveAt (index);
                }
            }

            private void WriteFloat (float f) {
                if (_version > 1) {
                    _bytes.Add ((byte)'g');
                    _bytes.AddRange (BitConverter.GetBytes ((double)f));
                } else {
                    _bytes.Add ((byte)'f');
                    WriteDoubleString (f);
                }
            }

            private void WriteFloat (double f) {
                if (_version > 1) {
                    _bytes.Add ((byte)'g');
                    _bytes.AddRange (BitConverter.GetBytes (f));
                } else {
                    _bytes.Add ((byte)'f');
                    WriteDoubleString (f);
                }
            }

            private void WriteDoubleString (double d) {
                string s = DoubleOps.__repr__ (DefaultContext.Default, d);
                _bytes.Add ((byte)s.Length);
                for (int i = 0; i < s.Length; i++) {
                    _bytes.Add ((byte)s[i]);
                }
            }

            private void WriteInteger (BigInteger val) {
                _bytes.Add ((byte)'l');
                int wordCount = 0, dir;
                if (val < BigInteger.Zero) {
                    val *= -1;
                    dir = -1;
                } else {
                    dir = 1;
                }

                List<byte> bytes = new List<byte> ();
                while (val != BigInteger.Zero) {
                    int word = (int)(val & 0x7FFF);
                    val = val >> 15;

                    bytes.Add ((byte)(word & 0xFF));
                    bytes.Add ((byte)((word >> 8) & 0xFF));
                    wordCount += dir;
                }

                WriteInt32 (wordCount);

                _bytes.AddRange (bytes);
            }

            private void WriteBuffer (PythonBuffer b) {
                _bytes.Add ((byte)'s');
                List<byte> newBytes = new List<byte> ();
                for (int i = 0; i < b.Size; i++) {
                    if (b[i] is string) {
                        string str = b[i] as string;
                        byte[] utfBytes = Encoding.UTF8.GetBytes (str);
                        if (utfBytes.Length != str.Length) {
                            newBytes.AddRange (utfBytes);
                        } else {
                            byte[] strBytes = PythonAsciiEncoding.Instance.GetBytes (str);
                            newBytes.AddRange (strBytes);
                        }
                    } else {
                        newBytes.Add ((byte)b[i]);
                    }
                }
                WriteInt32 (newBytes.Count);
                _bytes.AddRange (newBytes);
            }

            private void WriteLong (long l) {
                _bytes.Add ((byte)'I');

                for (int i = 0; i < 8; i++) {
                    _bytes.Add ((byte)(l & 0xff));
                    l = l >> 8;
                }
            }

            private void WriteComplex (Complex val) {
                _bytes.Add ((byte)'x');
                WriteDoubleString (val.Real);
                WriteDoubleString (val.Imaginary ());
            }

            private void WriteStopIteration () {
                _bytes.Add ((byte)'S');
            }

            private void WriteInt (int val) {
                _bytes.Add ((byte)'i');
                WriteInt32 (val);
            }

            private void WriteInt32 (int val) {
                _bytes.Add ((byte)(val & 0xff));
                _bytes.Add ((byte)((val >> 8) & 0xff));
                _bytes.Add ((byte)((val >> 16) & 0xff));
                _bytes.Add ((byte)((val >> 24) & 0xff));
            }

            private void WriteString (string s) {
                byte[] utfBytes = Encoding.UTF8.GetBytes (s);
                if (utfBytes.Length != s.Length) {
                    _bytes.Add ((byte)'u');
                    WriteInt32 (utfBytes.Length);
                    for (int i = 0; i < utfBytes.Length; i++) {
                        _bytes.Add (utfBytes[i]);
                    }
                } else {
                    int index;
                    if (_strings != null && _strings.TryGetValue (s, out index)) {
                        _bytes.Add ((byte)'R');
                        WriteInt32 (index);
                    } else {
                        byte[] strBytes = PythonAsciiEncoding.Instance.GetBytes (s);
                        if (_strings != null) {
                            _bytes.Add ((byte)'t');
                        } else {
                            _bytes.Add ((byte)'s');
                        }
                        WriteInt32 (strBytes.Length);
                        for (int i = 0; i < strBytes.Length; i++) {
                            _bytes.Add (strBytes[i]);
                        }

                        if (_strings != null) {
                            _strings[s] = _strings.Count;
                        }
                    }
                }
            }

            private void WriteList (object o) {
                List l = o as List;
                _bytes.Add ((byte)'[');
                WriteInt32 (l.__len__ ());
                for (int i = 0; i < l.__len__ (); i++) {
                    WriteObject (l[i]);
                }
            }

            private void WriteDict (object o) {
                PythonDictionary d = o as PythonDictionary;
                _bytes.Add ((byte)'{');
                IEnumerator<KeyValuePair<object, object>> ie = ((IEnumerable<KeyValuePair<object, object>>)d).GetEnumerator ();
                while (ie.MoveNext ()) {
                    WriteObject (ie.Current.Key);
                    WriteObject (ie.Current.Value);
                }
                _bytes.Add ((byte)'0');
            }

            private void WriteTuple (object o) {
                PythonTuple t = o as PythonTuple;
                _bytes.Add ((byte)'(');
                WriteInt32 (t.__len__ ());
                for (int i = 0; i < t.__len__ (); i++) {
                    WriteObject (t[i]);
                }
            }

            private void WriteSet (object set) {
                SetCollection s = set as SetCollection;
                _bytes.Add ((byte)'<');
                WriteInt32 (s.__len__ ());
                foreach (object o in s) {
                    WriteObject (o);
                }
            }

            private void WriteFrozenSet (object set) {
                FrozenSetCollection s = set as FrozenSetCollection;
                _bytes.Add ((byte)'>');
                WriteInt32 (s.__len__ ());
                foreach (object o in s) {
                    WriteObject (o);
                }
            }

            public byte[] GetBytes () {
                return _bytes.ToArray ();
            }
        }

        private class MarshalReader {
            private IEnumerator<byte> _myBytes;
            private Stack<ProcStack> _stack;
            private readonly Dictionary<int, string> _strings;
            private object _result;

            public MarshalReader (IEnumerator<byte> bytes) {
                _myBytes = bytes;
                _strings = new Dictionary<int, string> ();
            }

            public object ReadObject () {
                while (_myBytes.MoveNext ()) {
                    byte cur = _myBytes.Current;
                    object res;
                    switch ((char)cur) {
                        case '(': PushStack (StackType.Tuple); break;
                        case '[': PushStack (StackType.List); break;
                        case '{': PushStack (StackType.Dict); break;
                        case '<': PushStack (StackType.Set); break;
                        case '>': PushStack (StackType.FrozenSet); break;
                        case '0':
                            // end of dictionary
                            if (_stack == null || _stack.Count == 0) {
                                throw PythonOps.ValueError ("bad marshal data");
                            }
                            _stack.Peek ().StackCount = 0;
                            break;
                        // case 'c': break;
                        default:
                            res = YieldSimple ();
                            if (_stack == null) {
                                return res;
                            }

                            do {
                                res = UpdateStack (res);
                            } while (res != null && _stack.Count > 0);

                            if (_stack.Count == 0) {
                                return _result;
                            }

                            continue;
                    }

                    // handle empty lists/tuples...
                    if (_stack != null && _stack.Count > 0 && _stack.Peek ().StackCount == 0) {
                        ProcStack ps = _stack.Pop ();
                        res = ps.StackObj;

                        if (ps.StackType == StackType.Tuple) {
                            res = PythonTuple.Make (res);
                        } else if (ps.StackType == StackType.FrozenSet) {
                            res = FrozenSetCollection.Make (TypeCache.FrozenSet, res);
                        }

                        if (_stack.Count > 0) {
                            // empty list/tuple
                            do {
                                res = UpdateStack (res);
                            } while (res != null && _stack.Count > 0);
                            if (_stack.Count == 0) break;
                        } else {
                            _result = res;
                            break;
                        }
                    }
                }

                return _result;
            }

            private void PushStack (StackType type) {
                ProcStack newStack = new ProcStack ();
                newStack.StackType = type;

                switch (type) {
                    case StackType.Dict:
                        newStack.StackObj = new PythonDictionary ();
                        newStack.StackCount = -1;
                        break;
                    case StackType.List:
                        newStack.StackObj = new List ();
                        newStack.StackCount = ReadInt32 ();
                        break;
                    case StackType.Tuple:
                        newStack.StackCount = ReadInt32 ();
                        newStack.StackObj = new List<object> (newStack.StackCount);
                        break;
                    case StackType.Set:
                        newStack.StackObj = new SetCollection ();
                        newStack.StackCount = ReadInt32 ();
                        break;
                    case StackType.FrozenSet:
                        newStack.StackCount = ReadInt32 ();
                        newStack.StackObj = new List<object> (newStack.StackCount);
                        break;
                }

                if (_stack == null) _stack = new Stack<ProcStack> ();

                _stack.Push (newStack);
            }

            private object UpdateStack (object res) {
                ProcStack curStack = _stack.Peek ();
                switch (curStack.StackType) {
                    case StackType.Dict:
                        PythonDictionary od = curStack.StackObj as PythonDictionary;
                        if (curStack.HaveKey) {
                            od[curStack.Key] = res;
                            curStack.HaveKey = false;
                        } else {
                            curStack.HaveKey = true;
                            curStack.Key = res;
                        }
                        break;
                    case StackType.Tuple:
                        List<object> objs = curStack.StackObj as List<object>;
                        objs.Add (res);
                        curStack.StackCount--;
                        if (curStack.StackCount == 0) {
                            _stack.Pop ();
                            object tuple = PythonTuple.Make (objs);
                            if (_stack.Count == 0) {
                                _result = tuple;
                            }
                            return tuple;
                        }
                        break;
                    case StackType.List:
                        List ol = curStack.StackObj as List;
                        ol.AddNoLock (res);
                        curStack.StackCount--;
                        if (curStack.StackCount == 0) {
                            _stack.Pop ();
                            if (_stack.Count == 0) {
                                _result = ol;
                            }
                            return ol;
                        }
                        break;
                    case StackType.Set:
                        SetCollection os = curStack.StackObj as SetCollection;
                        os.add (res);
                        curStack.StackCount--;
                        if (curStack.StackCount == 0) {
                            _stack.Pop ();
                            if (_stack.Count == 0) {
                                _result = os;
                            }
                            return os;
                        }
                        break;
                    case StackType.FrozenSet:
                        List<object> ofs = curStack.StackObj as List<object>;
                        ofs.Add (res);
                        curStack.StackCount--;
                        if (curStack.StackCount == 0) {
                            _stack.Pop ();
                            object frozenSet = FrozenSetCollection.Make (TypeCache.FrozenSet, ofs);
                            if (_stack.Count == 0) {
                                _result = frozenSet;
                            }
                            return frozenSet;
                        }
                        break;
                }
                return null;
            }

            private enum StackType {
                Tuple,
                Dict,
                List,
                Set,
                FrozenSet
            }

            private class ProcStack {
                public StackType StackType;
                public object StackObj;
                public int StackCount;
                public bool HaveKey;
                public object Key;
            }

            private object YieldSimple () {
                object res;
                switch ((char)_myBytes.Current) {
                    // simple ops to be read in
                    case 'i': res = ReadInt (); break;
                    case 'l': res = ReadBigInteger (); break;
                    case 'T': res = ScriptingRuntimeHelpers.True; break;
                    case 'F': res = ScriptingRuntimeHelpers.False; break;
                    case 'f': res = ReadFloat (); break;
                    case 't': res = ReadAsciiString (); break;
                    case 'u': res = ReadUnicodeString (); break;
                    case 'S': res = PythonExceptions.StopIteration; break;
                    case 'N': res = null; break;
                    case 'x': res = ReadComplex (); break;
                    case 's': res = ReadBuffer (); break;
                    case 'I': res = ReadLong (); break;
                    case 'R': res = _strings[ReadInt32 ()]; break;
                    case 'g': res = ReadBinaryFloat (); break;
                    default: throw PythonOps.ValueError ("bad marshal data");
                }
                return res;
            }

            private byte[] ReadBytes (int len) {
                byte[] bytes = new byte[len];
                for (int i = 0; i < len; i++) {
                    if (!_myBytes.MoveNext ()) {
                        throw PythonOps.ValueError ("bad marshal data");
                    }
                    bytes[i] = _myBytes.Current;
                }

                return bytes;
            }

            private int ReadInt32 () {
                byte[] intBytes = ReadBytes (4);

                int res = intBytes[0] |
                    (intBytes[1] << 8) |
                    (intBytes[2] << 16) |
                    (intBytes[3] << 24);

                return res;
            }

            private double ReadFloatStr () {
                MoveNext ();

                string str = DecodeString (PythonAsciiEncoding.Instance, ReadBytes (_myBytes.Current));

                double res = 0;
                if (double.TryParse (str, out res)) {
                    return res;
                }
                return 0;
            }

            private void MoveNext () {
                if (!_myBytes.MoveNext ()) {
                    throw PythonOps.EofError ("EOF read where object expected");
                }
            }

            private string DecodeString (Encoding enc, byte[] bytes) {
                return enc.GetString (bytes, 0, bytes.Length);
            }

            object ReadInt () {
                // bytes not present are treated as being -1
                byte b1, b2, b3, b4;

                b1 = ReadIntPart ();
                b2 = ReadIntPart ();
                b3 = ReadIntPart ();
                b4 = ReadIntPart ();

                byte[] bytes = new byte[] { b1, b2, b3, b4 };
                return ScriptingRuntimeHelpers.Int32ToObject (BitConverter.ToInt32 (bytes, 0));
                //return Ops.int2object(b1 | (b2 << 8) | (b3 << 16) | (b4 << 24));
            }

            private byte ReadIntPart () {
                byte b;
                if (_myBytes.MoveNext ()) {
                    b = _myBytes.Current;
                } else {
                    b = 255;
                }
                return b;
            }

            object ReadFloat () {
                return ReadFloatStr ();
            }

            private object ReadBinaryFloat () {
                return BitConverter.ToDouble (ReadBytes (8), 0);
            }

            private object ReadAsciiString () {
                string res = DecodeString (PythonAsciiEncoding.Instance, ReadBytes (ReadInt32 ()));
                _strings[_strings.Count] = res;
                return res;
            }

            private object ReadUnicodeString () {
                return DecodeString (Encoding.UTF8, ReadBytes (ReadInt32 ()));
            }

            private object ReadComplex () {
                double real = ReadFloatStr ();
                double imag = ReadFloatStr ();

                return new Complex (real, imag);
            }

            private object ReadBuffer () {
                return DecodeString (Encoding.UTF8, ReadBytes (ReadInt32 ()));
            }

            private object ReadLong () {
                byte[] bytes = ReadBytes (8);

                long res = 0;
                for (int i = 0; i < 8; i++) {
                    res |= (((long)bytes[i]) << (i * 8));
                }

                return res;
            }

            private object ReadBigInteger () {
                int encodingSize = ReadInt32 ();
                if (encodingSize == 0) {
                    return BigInteger.Zero;
                }

                int sign = 1;
                if (encodingSize < 0) {
                    sign = -1;
                    encodingSize *= -1;
                }
                int len = encodingSize * 2;

                byte[] bytes = ReadBytes (len);

#if CLR2
                // first read the values in shorts so we can work
                // with them as 15-bit bytes easier...
                short[] shData = new short[encodingSize];
                for (int i = 0; i < shData.Length; i++) {
                    shData[i] = (short)(bytes[i * 2] | (bytes[1 + i * 2] << 8));
                }

                // then convert the short's into BigInteger's 32-bit 
                // format.
                uint[] numData = new uint[(shData.Length + 1) / 2];
                int bitWriteIndex = 0, shortIndex = 0, bitReadIndex = 0;
                while (shortIndex < shData.Length) {
                    short val = shData[shortIndex];
                    int shift = bitWriteIndex % 32;

                    if (bitReadIndex != 0) {
                        // we're read some bits, mask them off
                        // and adjust the shift.
                        int maskOff = ~((1 << bitReadIndex) - 1);
                        val = (short)(val & maskOff);
                        shift -= bitReadIndex;
                    }

                    // write the value into numData
                    if (shift < 0) {
                        numData[bitWriteIndex / 32] |= (uint)(val >> (shift * -1));
                    } else {
                        numData[bitWriteIndex / 32] |= (uint)(val << shift);
                    }

                    // and advance our indices
                    if ((bitWriteIndex % 32) <= 16) {
                        bitWriteIndex += (15 - bitReadIndex);
                        bitReadIndex = 0;
                        shortIndex++;
                    } else {
                        bitReadIndex = (32 - (bitWriteIndex % 32));
                        bitWriteIndex += bitReadIndex;
                    }
                }

                // and finally pass the data onto the big integer.
                return new BigInteger(sign, numData);
#else
                // re-pack our 15-bit values into bytes
                byte[] data = new byte[bytes.Length];
                for (int bytesIndex = 0, dataIndex = 0, shift = 0; bytesIndex < len; bytesIndex++) {
                    // write 8 bits
                    if (shift == 0) {
                        data[dataIndex] = bytes[bytesIndex];
                    } else {
                        data[dataIndex] = (byte)((bytes[bytesIndex] >> shift) | (bytes[bytesIndex + 1] << (8 - shift)));
                    }

                    //done
                    dataIndex++;
                    bytesIndex++;

                    if (shift == 7) {
                        shift = 0;
                        continue;
                    }

                    // write 7 bits
                    if (bytesIndex < len - 1) {
                        data[dataIndex] = (byte)((bytes[bytesIndex] >> shift) | (bytes[bytesIndex + 1] << (7 - shift)));
                    } else {
                        data[dataIndex] = (byte)(bytes[bytesIndex] >> shift);
                    }

                    //done
                    dataIndex++;
                    shift++;
                }

                // and finally pass the data onto the big integer.
                BigInteger res = new BigInteger (data);
                return sign < 0 ? -res : res;
#endif
            }
        }

    }
}
Back to Top