EvaluateScript and IsCanonicalPubKey implementation, IsCanonicalSignature/CheckSig...
[NovacoinLibrary.git] / Novacoin / ByteQueue.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace Novacoin
8 {
9     public class ByteQueueException : Exception
10     {
11         public ByteQueueException()
12         {
13         }
14
15         public ByteQueueException(string message)
16             : base(message)
17         {
18         }
19
20         public ByteQueueException(string message, Exception inner)
21             : base(message, inner)
22         {
23         }
24     }
25
26     public class ByteQueue
27     {
28         private int Index;
29         private List<byte> Elements;
30
31         public ByteQueue(IList<byte> List, int Start)
32         {
33             Elements = new List<byte>(List);
34             Index = Start;
35         }
36
37         public ByteQueue(IList<byte> List)
38         {
39             Elements = new List<byte>(List);
40             Index = 0;
41         }
42
43         public byte Get()
44         {
45             if (Elements.Count <= Index)
46             {
47                 throw new ByteQueueException("No elements left.");
48             }
49
50             return Elements[Index++];
51         }
52
53         public byte GetCurrent()
54         {
55             return Elements[Index];
56         }
57
58         public byte[] Get(int Count)
59         {
60             if (Elements.Count - Index < Count)
61             {
62                 throw new ByteQueueException("Unable to read requested amount of data.");
63             }
64
65             byte[] result = Elements.Skip(Index).Take(Count).ToArray();
66             Index += Count;
67
68             return result;
69         }
70
71         public byte[] GetCurrent(int Count)
72         {
73             if (Elements.Count - Index < Count)
74             {
75                 throw new ByteQueueException("Unable to read requested amount of data.");
76             }
77
78             byte[] result = Elements.Skip(Index).Take(Count).ToArray();
79
80             return result;
81         }
82
83         /// <summary>
84         /// Current index value
85         /// </summary>
86         public int CurrentIndex
87         {
88             get { return Index; }
89         }
90
91         public IEnumerable<byte> GetEnumerable(int Count)
92         {
93             if (Elements.Count - Index < Count)
94             {
95                 throw new ByteQueueException("Unable to read requested amount of data.");
96             }
97
98             IEnumerable<byte> result = Elements.Skip(Index).Take(Count);
99             Index += Count;
100
101             return result;
102         }
103
104         public ulong GetVarInt()
105         {
106             byte prefix = Get();
107
108             switch (prefix)
109             {
110                 case 0xfd: // ushort
111                     return BitConverter.ToUInt16(Get(2), 0);
112                 case 0xfe: // uint
113                     return BitConverter.ToUInt32(Get(4), 0);
114                 case 0xff: // ulong
115                     return BitConverter.ToUInt64(Get(8), 0);
116                 default:
117                     return prefix;
118             }
119         }
120     }
121 }