2003-03-25 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Mono.PEToolkit / LEBitConverter.cs
1 \r
2 // Auto-generated file - DO NOT EDIT!\r
3 // Please edit bitconverter.xsl if you want to make changes.\r
4 \r
5 using System;\r
6 \r
7 namespace Mono.PEToolkit {\r
8 \r
9         /// <summary>\r
10         /// Little-endian bit converter.\r
11         /// </summary>\r
12         public sealed class LEBitConverter {\r
13 \r
14                 internal interface IConverter {\r
15 \r
16                         short ToInt16(byte [] val, int idx);\r
17                         ushort ToUInt16(byte [] val, int idx);\r
18                         int ToInt32(byte [] val, int idx);\r
19                         uint ToUInt32(byte [] val, int idx);\r
20                         long ToInt64(byte [] val, int idx);\r
21                         ulong ToUInt64(byte [] val, int idx);\r
22 \r
23                 }\r
24 \r
25                 public static readonly bool Native = System.BitConverter.IsLittleEndian;\r
26 \r
27                 private static readonly IConverter impl = System.BitConverter.IsLittleEndian\r
28                                         ? new LEConverter() as IConverter\r
29                                         : new BEConverter() as IConverter;\r
30 \r
31 \r
32 \r
33 \r
34                 private LEBitConverter()\r
35                 {\r
36                         // Never instantiated.\r
37                 }\r
38 \r
39                 ///<summary></summary>\r
40                 unsafe public static short SwapInt16(short x)\r
41                 {\r
42                         short* p = stackalloc short [1];\r
43                         *p = x;\r
44                         byte* bp = (byte*) p;\r
45                         byte b = bp [0];\r
46                         bp [0] = bp [1];\r
47                         bp [1] = b;\r
48                         return *p;\r
49                 }\r
50 \r
51                 ///<summary></summary>\r
52                 unsafe public static ushort SwapUInt16(ushort x)\r
53                 {\r
54                         ushort* p = stackalloc ushort [1];\r
55                         *p = x;\r
56                         byte* bp = (byte*) p;\r
57                         byte b = bp [0];\r
58                         bp [0] = bp [1];\r
59                         bp [1] = b;\r
60                         return *p;\r
61                 }\r
62 \r
63                 ///<summary></summary>\r
64                 unsafe public static int SwapInt32(int x)\r
65                 {\r
66                         int* p = stackalloc int [1];\r
67                         *p = x;\r
68                         byte* bp = (byte*) p;\r
69                         byte b = bp [0];\r
70                         bp [0] = bp [3];\r
71                         bp [3] = b;\r
72                         b = bp [1];\r
73                         bp [1] = bp [2];\r
74                         bp [2] = b;\r
75                         return *p;\r
76                 }\r
77 \r
78                 ///<summary></summary>\r
79                 unsafe public static uint SwapUInt32(uint x)\r
80                 {\r
81                         uint* p = stackalloc uint [1];\r
82                         *p = x;\r
83                         byte* bp = (byte*) p;\r
84                         byte b = bp [0];\r
85                         bp [0] = bp [3];\r
86                         bp [3] = b;\r
87                         b = bp [1];\r
88                         bp [1] = bp [2];\r
89                         bp [2] = b;\r
90                         return *p;\r
91                 }\r
92 \r
93                 ///<summary></summary>\r
94                 unsafe public static long SwapInt64(long x)\r
95                 {\r
96                         long* p = stackalloc long [1];\r
97                         *p = x;\r
98                         byte* bp = (byte*) p;\r
99                         byte b = bp [0];\r
100                         bp [0] = bp [7];\r
101                         bp [7] = b;\r
102                         b = bp [1];\r
103                         bp [1] = bp [6];\r
104                         bp [6] = b;\r
105                         b = bp [2];\r
106                         bp [2] = bp [5];\r
107                         bp [5] = b;\r
108                         b = bp [3];\r
109                         bp [3] = bp [4];\r
110                         bp [4] = b;\r
111                         return *p;\r
112                 }\r
113 \r
114                 ///<summary></summary>\r
115                 unsafe public static ulong SwapUInt64(ulong x)\r
116                 {\r
117                         ulong* p = stackalloc ulong [1];\r
118                         *p = x;\r
119                         byte* bp = (byte*) p;\r
120                         byte b = bp [0];\r
121                         bp [0] = bp [7];\r
122                         bp [7] = b;\r
123                         b = bp [1];\r
124                         bp [1] = bp [6];\r
125                         bp [6] = b;\r
126                         b = bp [2];\r
127                         bp [2] = bp [5];\r
128                         bp [5] = b;\r
129                         b = bp [3];\r
130                         bp [3] = bp [4];\r
131                         bp [4] = b;\r
132                         return *p;\r
133                 }\r
134 \r
135 \r
136 \r
137 \r
138 \r
139                 internal sealed class LEConverter : IConverter {\r
140                         ///<summary></summary>\r
141                         public short ToInt16(byte [] val, int idx)\r
142                         {\r
143                                 return BitConverter.ToInt16(val, idx);\r
144                         }\r
145                         ///<summary></summary>\r
146                         public ushort ToUInt16(byte [] val, int idx)\r
147                         {\r
148                                 return BitConverter.ToUInt16(val, idx);\r
149                         }\r
150                         ///<summary></summary>\r
151                         public int ToInt32(byte [] val, int idx)\r
152                         {\r
153                                 return BitConverter.ToInt32(val, idx);\r
154                         }\r
155                         ///<summary></summary>\r
156                         public uint ToUInt32(byte [] val, int idx)\r
157                         {\r
158                                 return BitConverter.ToUInt32(val, idx);\r
159                         }\r
160                         ///<summary></summary>\r
161                         public long ToInt64(byte [] val, int idx)\r
162                         {\r
163                                 return BitConverter.ToInt64(val, idx);\r
164                         }\r
165                         ///<summary></summary>\r
166                         public ulong ToUInt64(byte [] val, int idx)\r
167                         {\r
168                                 return BitConverter.ToUInt64(val, idx);\r
169                         }\r
170 \r
171                 }\r
172 \r
173                 internal sealed class BEConverter : IConverter {\r
174                         ///<summary></summary>\r
175                         public short ToInt16(byte [] val, int idx)\r
176                         {\r
177                                 return SwapInt16(BitConverter.ToInt16(val, idx));\r
178                         }\r
179                         ///<summary></summary>\r
180                         public ushort ToUInt16(byte [] val, int idx)\r
181                         {\r
182                                 return SwapUInt16(BitConverter.ToUInt16(val, idx));\r
183                         }\r
184                         ///<summary></summary>\r
185                         public int ToInt32(byte [] val, int idx)\r
186                         {\r
187                                 return SwapInt32(BitConverter.ToInt32(val, idx));\r
188                         }\r
189                         ///<summary></summary>\r
190                         public uint ToUInt32(byte [] val, int idx)\r
191                         {\r
192                                 return SwapUInt32(BitConverter.ToUInt32(val, idx));\r
193                         }\r
194                         ///<summary></summary>\r
195                         public long ToInt64(byte [] val, int idx)\r
196                         {\r
197                                 return SwapInt64(BitConverter.ToInt64(val, idx));\r
198                         }\r
199                         ///<summary></summary>\r
200                         public ulong ToUInt64(byte [] val, int idx)\r
201                         {\r
202                                 return SwapUInt64(BitConverter.ToUInt64(val, idx));\r
203                         }\r
204 \r
205                 }\r
206 \r
207 \r
208 \r
209 \r
210                 ///<summary></summary>\r
211                 public static short ToInt16(byte [] val, int idx)\r
212                 {\r
213                         return impl.ToInt16(val, idx);\r
214                 }\r
215 \r
216                 ///<summary></summary>\r
217                 public static ushort ToUInt16(byte [] val, int idx)\r
218                 {\r
219                         return impl.ToUInt16(val, idx);\r
220                 }\r
221 \r
222                 ///<summary></summary>\r
223                 public static int ToInt32(byte [] val, int idx)\r
224                 {\r
225                         return impl.ToInt32(val, idx);\r
226                 }\r
227 \r
228                 ///<summary></summary>\r
229                 public static uint ToUInt32(byte [] val, int idx)\r
230                 {\r
231                         return impl.ToUInt32(val, idx);\r
232                 }\r
233 \r
234                 ///<summary></summary>\r
235                 public static long ToInt64(byte [] val, int idx)\r
236                 {\r
237                         return impl.ToInt64(val, idx);\r
238                 }\r
239 \r
240                 ///<summary></summary>\r
241                 public static ulong ToUInt64(byte [] val, int idx)\r
242                 {\r
243                         return impl.ToUInt64(val, idx);\r
244                 }\r
245 \r
246 \r
247 \r
248         }\r
249 \r
250 }\r
251 \r