| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Crypto.Utilities |
| | | 4 | | { |
| | | 5 | | internal sealed class Pack |
| | | 6 | | { |
| | 0 | 7 | | private Pack() |
| | 0 | 8 | | { |
| | 0 | 9 | | } |
| | | 10 | | |
| | | 11 | | internal static void UInt16_To_BE(ushort n, byte[] bs) |
| | 0 | 12 | | { |
| | 0 | 13 | | bs[0] = (byte)(n >> 8); |
| | 0 | 14 | | bs[1] = (byte)(n); |
| | 0 | 15 | | } |
| | | 16 | | |
| | | 17 | | internal static void UInt16_To_BE(ushort n, byte[] bs, int off) |
| | 0 | 18 | | { |
| | 0 | 19 | | bs[off] = (byte)(n >> 8); |
| | 0 | 20 | | bs[off + 1] = (byte)(n); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | internal static ushort BE_To_UInt16(byte[] bs) |
| | 0 | 24 | | { |
| | 0 | 25 | | uint n = (uint)bs[0] << 8 |
| | 0 | 26 | | | (uint)bs[1]; |
| | 0 | 27 | | return (ushort)n; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | internal static ushort BE_To_UInt16(byte[] bs, int off) |
| | 0 | 31 | | { |
| | 0 | 32 | | uint n = (uint)bs[off] << 8 |
| | 0 | 33 | | | (uint)bs[off + 1]; |
| | 0 | 34 | | return (ushort)n; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | internal static byte[] UInt32_To_BE(uint n) |
| | 0 | 38 | | { |
| | 0 | 39 | | byte[] bs = new byte[4]; |
| | 0 | 40 | | UInt32_To_BE(n, bs, 0); |
| | 0 | 41 | | return bs; |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | internal static void UInt32_To_BE(uint n, byte[] bs) |
| | 0 | 45 | | { |
| | 0 | 46 | | bs[0] = (byte)(n >> 24); |
| | 0 | 47 | | bs[1] = (byte)(n >> 16); |
| | 0 | 48 | | bs[2] = (byte)(n >> 8); |
| | 0 | 49 | | bs[3] = (byte)(n); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | internal static void UInt32_To_BE(uint n, byte[] bs, int off) |
| | 711 | 53 | | { |
| | 711 | 54 | | bs[off] = (byte)(n >> 24); |
| | 711 | 55 | | bs[off + 1] = (byte)(n >> 16); |
| | 711 | 56 | | bs[off + 2] = (byte)(n >> 8); |
| | 711 | 57 | | bs[off + 3] = (byte)(n); |
| | 711 | 58 | | } |
| | | 59 | | |
| | | 60 | | internal static byte[] UInt32_To_BE(uint[] ns) |
| | 0 | 61 | | { |
| | 0 | 62 | | byte[] bs = new byte[4 * ns.Length]; |
| | 0 | 63 | | UInt32_To_BE(ns, bs, 0); |
| | 0 | 64 | | return bs; |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | internal static void UInt32_To_BE(uint[] ns, byte[] bs, int off) |
| | 0 | 68 | | { |
| | 0 | 69 | | for (int i = 0; i < ns.Length; ++i) |
| | 0 | 70 | | { |
| | 0 | 71 | | UInt32_To_BE(ns[i], bs, off); |
| | 0 | 72 | | off += 4; |
| | 0 | 73 | | } |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | internal static uint BE_To_UInt32(byte[] bs) |
| | 0 | 77 | | { |
| | 0 | 78 | | return (uint)bs[0] << 24 |
| | 0 | 79 | | | (uint)bs[1] << 16 |
| | 0 | 80 | | | (uint)bs[2] << 8 |
| | 0 | 81 | | | (uint)bs[3]; |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | internal static uint BE_To_UInt32(byte[] bs, int off) |
| | 622 | 85 | | { |
| | 622 | 86 | | return (uint)bs[off] << 24 |
| | 622 | 87 | | | (uint)bs[off + 1] << 16 |
| | 622 | 88 | | | (uint)bs[off + 2] << 8 |
| | 622 | 89 | | | (uint)bs[off + 3]; |
| | 622 | 90 | | } |
| | | 91 | | |
| | | 92 | | internal static void BE_To_UInt32(byte[] bs, int off, uint[] ns) |
| | 0 | 93 | | { |
| | 0 | 94 | | for (int i = 0; i < ns.Length; ++i) |
| | 0 | 95 | | { |
| | 0 | 96 | | ns[i] = BE_To_UInt32(bs, off); |
| | 0 | 97 | | off += 4; |
| | 0 | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | internal static byte[] UInt64_To_BE(ulong n) |
| | 0 | 102 | | { |
| | 0 | 103 | | byte[] bs = new byte[8]; |
| | 0 | 104 | | UInt64_To_BE(n, bs, 0); |
| | 0 | 105 | | return bs; |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | internal static void UInt64_To_BE(ulong n, byte[] bs) |
| | 0 | 109 | | { |
| | 0 | 110 | | UInt32_To_BE((uint)(n >> 32), bs); |
| | 0 | 111 | | UInt32_To_BE((uint)(n), bs, 4); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | internal static void UInt64_To_BE(ulong n, byte[] bs, int off) |
| | 0 | 115 | | { |
| | 0 | 116 | | UInt32_To_BE((uint)(n >> 32), bs, off); |
| | 0 | 117 | | UInt32_To_BE((uint)(n), bs, off + 4); |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | internal static byte[] UInt64_To_BE(ulong[] ns) |
| | 0 | 121 | | { |
| | 0 | 122 | | byte[] bs = new byte[8 * ns.Length]; |
| | 0 | 123 | | UInt64_To_BE(ns, bs, 0); |
| | 0 | 124 | | return bs; |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | internal static void UInt64_To_BE(ulong[] ns, byte[] bs, int off) |
| | 0 | 128 | | { |
| | 0 | 129 | | for (int i = 0; i < ns.Length; ++i) |
| | 0 | 130 | | { |
| | 0 | 131 | | UInt64_To_BE(ns[i], bs, off); |
| | 0 | 132 | | off += 8; |
| | 0 | 133 | | } |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | internal static ulong BE_To_UInt64(byte[] bs) |
| | 0 | 137 | | { |
| | 0 | 138 | | uint hi = BE_To_UInt32(bs); |
| | 0 | 139 | | uint lo = BE_To_UInt32(bs, 4); |
| | 0 | 140 | | return ((ulong)hi << 32) | (ulong)lo; |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | internal static ulong BE_To_UInt64(byte[] bs, int off) |
| | 0 | 144 | | { |
| | 0 | 145 | | uint hi = BE_To_UInt32(bs, off); |
| | 0 | 146 | | uint lo = BE_To_UInt32(bs, off + 4); |
| | 0 | 147 | | return ((ulong)hi << 32) | (ulong)lo; |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | internal static void BE_To_UInt64(byte[] bs, int off, ulong[] ns) |
| | 0 | 151 | | { |
| | 0 | 152 | | for (int i = 0; i < ns.Length; ++i) |
| | 0 | 153 | | { |
| | 0 | 154 | | ns[i] = BE_To_UInt64(bs, off); |
| | 0 | 155 | | off += 8; |
| | 0 | 156 | | } |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | internal static void UInt16_To_LE(ushort n, byte[] bs) |
| | 0 | 160 | | { |
| | 0 | 161 | | bs[0] = (byte)(n); |
| | 0 | 162 | | bs[1] = (byte)(n >> 8); |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | internal static void UInt16_To_LE(ushort n, byte[] bs, int off) |
| | 0 | 166 | | { |
| | 0 | 167 | | bs[off] = (byte)(n); |
| | 0 | 168 | | bs[off + 1] = (byte)(n >> 8); |
| | 0 | 169 | | } |
| | | 170 | | |
| | | 171 | | internal static ushort LE_To_UInt16(byte[] bs) |
| | 0 | 172 | | { |
| | 0 | 173 | | uint n = (uint)bs[0] |
| | 0 | 174 | | | (uint)bs[1] << 8; |
| | 0 | 175 | | return (ushort)n; |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | internal static ushort LE_To_UInt16(byte[] bs, int off) |
| | 0 | 179 | | { |
| | 0 | 180 | | uint n = (uint)bs[off] |
| | 0 | 181 | | | (uint)bs[off + 1] << 8; |
| | 0 | 182 | | return (ushort)n; |
| | 0 | 183 | | } |
| | | 184 | | |
| | | 185 | | internal static byte[] UInt32_To_LE(uint n) |
| | 0 | 186 | | { |
| | 0 | 187 | | byte[] bs = new byte[4]; |
| | 0 | 188 | | UInt32_To_LE(n, bs, 0); |
| | 0 | 189 | | return bs; |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | internal static void UInt32_To_LE(uint n, byte[] bs) |
| | 28 | 193 | | { |
| | 28 | 194 | | bs[0] = (byte)(n); |
| | 28 | 195 | | bs[1] = (byte)(n >> 8); |
| | 28 | 196 | | bs[2] = (byte)(n >> 16); |
| | 28 | 197 | | bs[3] = (byte)(n >> 24); |
| | 28 | 198 | | } |
| | | 199 | | |
| | | 200 | | internal static void UInt32_To_LE(uint n, byte[] bs, int off) |
| | 28 | 201 | | { |
| | 28 | 202 | | bs[off] = (byte)(n); |
| | 28 | 203 | | bs[off + 1] = (byte)(n >> 8); |
| | 28 | 204 | | bs[off + 2] = (byte)(n >> 16); |
| | 28 | 205 | | bs[off + 3] = (byte)(n >> 24); |
| | 28 | 206 | | } |
| | | 207 | | |
| | | 208 | | internal static byte[] UInt32_To_LE(uint[] ns) |
| | 0 | 209 | | { |
| | 0 | 210 | | byte[] bs = new byte[4 * ns.Length]; |
| | 0 | 211 | | UInt32_To_LE(ns, bs, 0); |
| | 0 | 212 | | return bs; |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | internal static void UInt32_To_LE(uint[] ns, byte[] bs, int off) |
| | 0 | 216 | | { |
| | 0 | 217 | | for (int i = 0; i < ns.Length; ++i) |
| | 0 | 218 | | { |
| | 0 | 219 | | UInt32_To_LE(ns[i], bs, off); |
| | 0 | 220 | | off += 4; |
| | 0 | 221 | | } |
| | 0 | 222 | | } |
| | | 223 | | |
| | | 224 | | internal static uint LE_To_UInt32(byte[] bs) |
| | 0 | 225 | | { |
| | 0 | 226 | | return (uint)bs[0] |
| | 0 | 227 | | | (uint)bs[1] << 8 |
| | 0 | 228 | | | (uint)bs[2] << 16 |
| | 0 | 229 | | | (uint)bs[3] << 24; |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | internal static uint LE_To_UInt32(byte[] bs, int off) |
| | 0 | 233 | | { |
| | 0 | 234 | | return (uint)bs[off] |
| | 0 | 235 | | | (uint)bs[off + 1] << 8 |
| | 0 | 236 | | | (uint)bs[off + 2] << 16 |
| | 0 | 237 | | | (uint)bs[off + 3] << 24; |
| | 0 | 238 | | } |
| | | 239 | | |
| | | 240 | | internal static void LE_To_UInt32(byte[] bs, int off, uint[] ns) |
| | 0 | 241 | | { |
| | 0 | 242 | | for (int i = 0; i < ns.Length; ++i) |
| | 0 | 243 | | { |
| | 0 | 244 | | ns[i] = LE_To_UInt32(bs, off); |
| | 0 | 245 | | off += 4; |
| | 0 | 246 | | } |
| | 0 | 247 | | } |
| | | 248 | | |
| | | 249 | | internal static void LE_To_UInt32(byte[] bs, int bOff, uint[] ns, int nOff, int count) |
| | 0 | 250 | | { |
| | 0 | 251 | | for (int i = 0; i < count; ++i) |
| | 0 | 252 | | { |
| | 0 | 253 | | ns[nOff + i] = LE_To_UInt32(bs, bOff); |
| | 0 | 254 | | bOff += 4; |
| | 0 | 255 | | } |
| | 0 | 256 | | } |
| | | 257 | | |
| | | 258 | | internal static uint[] LE_To_UInt32(byte[] bs, int off, int count) |
| | 0 | 259 | | { |
| | 0 | 260 | | uint[] ns = new uint[count]; |
| | 0 | 261 | | for (int i = 0; i < ns.Length; ++i) |
| | 0 | 262 | | { |
| | 0 | 263 | | ns[i] = LE_To_UInt32(bs, off); |
| | 0 | 264 | | off += 4; |
| | 0 | 265 | | } |
| | 0 | 266 | | return ns; |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | internal static byte[] UInt64_To_LE(ulong n) |
| | 0 | 270 | | { |
| | 0 | 271 | | byte[] bs = new byte[8]; |
| | 0 | 272 | | UInt64_To_LE(n, bs, 0); |
| | 0 | 273 | | return bs; |
| | 0 | 274 | | } |
| | | 275 | | |
| | | 276 | | internal static void UInt64_To_LE(ulong n, byte[] bs) |
| | 28 | 277 | | { |
| | 28 | 278 | | UInt32_To_LE((uint)(n), bs); |
| | 28 | 279 | | UInt32_To_LE((uint)(n >> 32), bs, 4); |
| | 28 | 280 | | } |
| | | 281 | | |
| | | 282 | | internal static void UInt64_To_LE(ulong n, byte[] bs, int off) |
| | 0 | 283 | | { |
| | 0 | 284 | | UInt32_To_LE((uint)(n), bs, off); |
| | 0 | 285 | | UInt32_To_LE((uint)(n >> 32), bs, off + 4); |
| | 0 | 286 | | } |
| | | 287 | | |
| | | 288 | | internal static byte[] UInt64_To_LE(ulong[] ns) |
| | 0 | 289 | | { |
| | 0 | 290 | | byte[] bs = new byte[8 * ns.Length]; |
| | 0 | 291 | | UInt64_To_LE(ns, bs, 0); |
| | 0 | 292 | | return bs; |
| | 0 | 293 | | } |
| | | 294 | | |
| | | 295 | | internal static void UInt64_To_LE(ulong[] ns, byte[] bs, int off) |
| | 0 | 296 | | { |
| | 0 | 297 | | for (int i = 0; i < ns.Length; ++i) |
| | 0 | 298 | | { |
| | 0 | 299 | | UInt64_To_LE(ns[i], bs, off); |
| | 0 | 300 | | off += 8; |
| | 0 | 301 | | } |
| | 0 | 302 | | } |
| | | 303 | | |
| | | 304 | | internal static void UInt64_To_LE(ulong[] ns, int nsOff, int nsLen, byte[] bs, int bsOff) |
| | 0 | 305 | | { |
| | 0 | 306 | | for (int i = 0; i < nsLen; ++i) |
| | 0 | 307 | | { |
| | 0 | 308 | | UInt64_To_LE(ns[nsOff + i], bs, bsOff); |
| | 0 | 309 | | bsOff += 8; |
| | 0 | 310 | | } |
| | 0 | 311 | | } |
| | | 312 | | |
| | | 313 | | internal static ulong LE_To_UInt64(byte[] bs) |
| | 0 | 314 | | { |
| | 0 | 315 | | uint lo = LE_To_UInt32(bs); |
| | 0 | 316 | | uint hi = LE_To_UInt32(bs, 4); |
| | 0 | 317 | | return ((ulong)hi << 32) | (ulong)lo; |
| | 0 | 318 | | } |
| | | 319 | | |
| | | 320 | | internal static ulong LE_To_UInt64(byte[] bs, int off) |
| | 0 | 321 | | { |
| | 0 | 322 | | uint lo = LE_To_UInt32(bs, off); |
| | 0 | 323 | | uint hi = LE_To_UInt32(bs, off + 4); |
| | 0 | 324 | | return ((ulong)hi << 32) | (ulong)lo; |
| | 0 | 325 | | } |
| | | 326 | | |
| | | 327 | | internal static void LE_To_UInt64(byte[] bs, int off, ulong[] ns) |
| | 0 | 328 | | { |
| | 0 | 329 | | for (int i = 0; i < ns.Length; ++i) |
| | 0 | 330 | | { |
| | 0 | 331 | | ns[i] = LE_To_UInt64(bs, off); |
| | 0 | 332 | | off += 8; |
| | 0 | 333 | | } |
| | 0 | 334 | | } |
| | | 335 | | |
| | | 336 | | internal static void LE_To_UInt64(byte[] bs, int bsOff, ulong[] ns, int nsOff, int nsLen) |
| | 0 | 337 | | { |
| | 0 | 338 | | for (int i = 0; i < nsLen; ++i) |
| | 0 | 339 | | { |
| | 0 | 340 | | ns[nsOff + i] = LE_To_UInt64(bs, bsOff); |
| | 0 | 341 | | bsOff += 8; |
| | 0 | 342 | | } |
| | 0 | 343 | | } |
| | | 344 | | } |
| | | 345 | | } |