| | | 1 | | using System; |
| | | 2 | | using System.Text; |
| | | 3 | | |
| | | 4 | | using Renci.SshNet.Security.Org.BouncyCastle.Math; |
| | | 5 | | |
| | | 6 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Utilities |
| | | 7 | | { |
| | | 8 | | /// <summary> General array utilities.</summary> |
| | | 9 | | internal abstract class Arrays |
| | | 10 | | { |
| | 0 | 11 | | public static readonly byte[] EmptyBytes = new byte[0]; |
| | 0 | 12 | | public static readonly int[] EmptyInts = new int[0]; |
| | | 13 | | |
| | | 14 | | public static bool AreAllZeroes(byte[] buf, int off, int len) |
| | 0 | 15 | | { |
| | 0 | 16 | | uint bits = 0; |
| | 0 | 17 | | for (int i = 0; i < len; ++i) |
| | 0 | 18 | | { |
| | 0 | 19 | | bits |= buf[off + i]; |
| | 0 | 20 | | } |
| | 0 | 21 | | return bits == 0; |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public static bool AreEqual( |
| | | 25 | | bool[] a, |
| | | 26 | | bool[] b) |
| | 0 | 27 | | { |
| | 0 | 28 | | if (a == b) |
| | 0 | 29 | | return true; |
| | | 30 | | |
| | 0 | 31 | | if (a == null || b == null) |
| | 0 | 32 | | return false; |
| | | 33 | | |
| | 0 | 34 | | return HaveSameContents(a, b); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public static bool AreEqual( |
| | | 38 | | char[] a, |
| | | 39 | | char[] b) |
| | 0 | 40 | | { |
| | 0 | 41 | | if (a == b) |
| | 0 | 42 | | return true; |
| | | 43 | | |
| | 0 | 44 | | if (a == null || b == null) |
| | 0 | 45 | | return false; |
| | | 46 | | |
| | 0 | 47 | | return HaveSameContents(a, b); |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Are two arrays equal. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="a">Left side.</param> |
| | | 54 | | /// <param name="b">Right side.</param> |
| | | 55 | | /// <returns>True if equal.</returns> |
| | | 56 | | public static bool AreEqual( |
| | | 57 | | byte[] a, |
| | | 58 | | byte[] b) |
| | 0 | 59 | | { |
| | 0 | 60 | | if (a == b) |
| | 0 | 61 | | return true; |
| | | 62 | | |
| | 0 | 63 | | if (a == null || b == null) |
| | 0 | 64 | | return false; |
| | | 65 | | |
| | 0 | 66 | | return HaveSameContents(a, b); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | [Obsolete("Use 'AreEqual' method instead")] |
| | | 70 | | public static bool AreSame( |
| | | 71 | | byte[] a, |
| | | 72 | | byte[] b) |
| | 0 | 73 | | { |
| | 0 | 74 | | return AreEqual(a, b); |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// A constant time equals comparison - does not terminate early if |
| | | 79 | | /// test will fail. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="a">first array</param> |
| | | 82 | | /// <param name="b">second array</param> |
| | | 83 | | /// <returns>true if arrays equal, false otherwise.</returns> |
| | | 84 | | public static bool ConstantTimeAreEqual( |
| | | 85 | | byte[] a, |
| | | 86 | | byte[] b) |
| | 0 | 87 | | { |
| | 0 | 88 | | int i = a.Length; |
| | 0 | 89 | | if (i != b.Length) |
| | 0 | 90 | | return false; |
| | 0 | 91 | | int cmp = 0; |
| | 0 | 92 | | while (i != 0) |
| | 0 | 93 | | { |
| | 0 | 94 | | --i; |
| | 0 | 95 | | cmp |= (a[i] ^ b[i]); |
| | 0 | 96 | | } |
| | 0 | 97 | | return cmp == 0; |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | public static bool AreEqual( |
| | | 101 | | int[] a, |
| | | 102 | | int[] b) |
| | 0 | 103 | | { |
| | 0 | 104 | | if (a == b) |
| | 0 | 105 | | return true; |
| | | 106 | | |
| | 0 | 107 | | if (a == null || b == null) |
| | 0 | 108 | | return false; |
| | | 109 | | |
| | 0 | 110 | | return HaveSameContents(a, b); |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | public static bool AreEqual(uint[] a, uint[] b) |
| | 0 | 114 | | { |
| | 0 | 115 | | if (a == b) |
| | 0 | 116 | | return true; |
| | | 117 | | |
| | 0 | 118 | | if (a == null || b == null) |
| | 0 | 119 | | return false; |
| | | 120 | | |
| | 0 | 121 | | return HaveSameContents(a, b); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | private static bool HaveSameContents( |
| | | 125 | | bool[] a, |
| | | 126 | | bool[] b) |
| | 0 | 127 | | { |
| | 0 | 128 | | int i = a.Length; |
| | 0 | 129 | | if (i != b.Length) |
| | 0 | 130 | | return false; |
| | 0 | 131 | | while (i != 0) |
| | 0 | 132 | | { |
| | 0 | 133 | | --i; |
| | 0 | 134 | | if (a[i] != b[i]) |
| | 0 | 135 | | return false; |
| | 0 | 136 | | } |
| | 0 | 137 | | return true; |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | private static bool HaveSameContents( |
| | | 141 | | char[] a, |
| | | 142 | | char[] b) |
| | 0 | 143 | | { |
| | 0 | 144 | | int i = a.Length; |
| | 0 | 145 | | if (i != b.Length) |
| | 0 | 146 | | return false; |
| | 0 | 147 | | while (i != 0) |
| | 0 | 148 | | { |
| | 0 | 149 | | --i; |
| | 0 | 150 | | if (a[i] != b[i]) |
| | 0 | 151 | | return false; |
| | 0 | 152 | | } |
| | 0 | 153 | | return true; |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | private static bool HaveSameContents( |
| | | 157 | | byte[] a, |
| | | 158 | | byte[] b) |
| | 0 | 159 | | { |
| | 0 | 160 | | int i = a.Length; |
| | 0 | 161 | | if (i != b.Length) |
| | 0 | 162 | | return false; |
| | 0 | 163 | | while (i != 0) |
| | 0 | 164 | | { |
| | 0 | 165 | | --i; |
| | 0 | 166 | | if (a[i] != b[i]) |
| | 0 | 167 | | return false; |
| | 0 | 168 | | } |
| | 0 | 169 | | return true; |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | private static bool HaveSameContents( |
| | | 173 | | int[] a, |
| | | 174 | | int[] b) |
| | 0 | 175 | | { |
| | 0 | 176 | | int i = a.Length; |
| | 0 | 177 | | if (i != b.Length) |
| | 0 | 178 | | return false; |
| | 0 | 179 | | while (i != 0) |
| | 0 | 180 | | { |
| | 0 | 181 | | --i; |
| | 0 | 182 | | if (a[i] != b[i]) |
| | 0 | 183 | | return false; |
| | 0 | 184 | | } |
| | 0 | 185 | | return true; |
| | 0 | 186 | | } |
| | | 187 | | |
| | | 188 | | private static bool HaveSameContents(uint[] a, uint[] b) |
| | 0 | 189 | | { |
| | 0 | 190 | | int i = a.Length; |
| | 0 | 191 | | if (i != b.Length) |
| | 0 | 192 | | return false; |
| | 0 | 193 | | while (i != 0) |
| | 0 | 194 | | { |
| | 0 | 195 | | --i; |
| | 0 | 196 | | if (a[i] != b[i]) |
| | 0 | 197 | | return false; |
| | 0 | 198 | | } |
| | 0 | 199 | | return true; |
| | 0 | 200 | | } |
| | | 201 | | |
| | | 202 | | public static string ToString( |
| | | 203 | | object[] a) |
| | 0 | 204 | | { |
| | 0 | 205 | | StringBuilder sb = new StringBuilder('['); |
| | 0 | 206 | | if (a.Length > 0) |
| | 0 | 207 | | { |
| | 0 | 208 | | sb.Append(a[0]); |
| | 0 | 209 | | for (int index = 1; index < a.Length; ++index) |
| | 0 | 210 | | { |
| | 0 | 211 | | sb.Append(", ").Append(a[index]); |
| | 0 | 212 | | } |
| | 0 | 213 | | } |
| | 0 | 214 | | sb.Append(']'); |
| | 0 | 215 | | return sb.ToString(); |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | public static int GetHashCode(byte[] data) |
| | 0 | 219 | | { |
| | 0 | 220 | | if (data == null) |
| | 0 | 221 | | { |
| | 0 | 222 | | return 0; |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | int i = data.Length; |
| | 0 | 226 | | int hc = i + 1; |
| | | 227 | | |
| | 0 | 228 | | while (--i >= 0) |
| | 0 | 229 | | { |
| | 0 | 230 | | hc *= 257; |
| | 0 | 231 | | hc ^= data[i]; |
| | 0 | 232 | | } |
| | | 233 | | |
| | 0 | 234 | | return hc; |
| | 0 | 235 | | } |
| | | 236 | | |
| | | 237 | | public static int GetHashCode(byte[] data, int off, int len) |
| | 0 | 238 | | { |
| | 0 | 239 | | if (data == null) |
| | 0 | 240 | | { |
| | 0 | 241 | | return 0; |
| | | 242 | | } |
| | | 243 | | |
| | 0 | 244 | | int i = len; |
| | 0 | 245 | | int hc = i + 1; |
| | | 246 | | |
| | 0 | 247 | | while (--i >= 0) |
| | 0 | 248 | | { |
| | 0 | 249 | | hc *= 257; |
| | 0 | 250 | | hc ^= data[off + i]; |
| | 0 | 251 | | } |
| | | 252 | | |
| | 0 | 253 | | return hc; |
| | 0 | 254 | | } |
| | | 255 | | |
| | | 256 | | public static int GetHashCode(int[] data) |
| | 0 | 257 | | { |
| | 0 | 258 | | if (data == null) |
| | 0 | 259 | | return 0; |
| | | 260 | | |
| | 0 | 261 | | int i = data.Length; |
| | 0 | 262 | | int hc = i + 1; |
| | | 263 | | |
| | 0 | 264 | | while (--i >= 0) |
| | 0 | 265 | | { |
| | 0 | 266 | | hc *= 257; |
| | 0 | 267 | | hc ^= data[i]; |
| | 0 | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | return hc; |
| | 0 | 271 | | } |
| | | 272 | | |
| | | 273 | | public static int GetHashCode(int[] data, int off, int len) |
| | 0 | 274 | | { |
| | 0 | 275 | | if (data == null) |
| | 0 | 276 | | return 0; |
| | | 277 | | |
| | 0 | 278 | | int i = len; |
| | 0 | 279 | | int hc = i + 1; |
| | | 280 | | |
| | 0 | 281 | | while (--i >= 0) |
| | 0 | 282 | | { |
| | 0 | 283 | | hc *= 257; |
| | 0 | 284 | | hc ^= data[off + i]; |
| | 0 | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | return hc; |
| | 0 | 288 | | } |
| | | 289 | | |
| | | 290 | | public static int GetHashCode(uint[] data) |
| | 0 | 291 | | { |
| | 0 | 292 | | if (data == null) |
| | 0 | 293 | | return 0; |
| | | 294 | | |
| | 0 | 295 | | int i = data.Length; |
| | 0 | 296 | | int hc = i + 1; |
| | | 297 | | |
| | 0 | 298 | | while (--i >= 0) |
| | 0 | 299 | | { |
| | 0 | 300 | | hc *= 257; |
| | 0 | 301 | | hc ^= (int)data[i]; |
| | 0 | 302 | | } |
| | | 303 | | |
| | 0 | 304 | | return hc; |
| | 0 | 305 | | } |
| | | 306 | | |
| | | 307 | | public static int GetHashCode(uint[] data, int off, int len) |
| | 0 | 308 | | { |
| | 0 | 309 | | if (data == null) |
| | 0 | 310 | | return 0; |
| | | 311 | | |
| | 0 | 312 | | int i = len; |
| | 0 | 313 | | int hc = i + 1; |
| | | 314 | | |
| | 0 | 315 | | while (--i >= 0) |
| | 0 | 316 | | { |
| | 0 | 317 | | hc *= 257; |
| | 0 | 318 | | hc ^= (int)data[off + i]; |
| | 0 | 319 | | } |
| | | 320 | | |
| | 0 | 321 | | return hc; |
| | 0 | 322 | | } |
| | | 323 | | |
| | | 324 | | public static int GetHashCode(ulong[] data) |
| | 0 | 325 | | { |
| | 0 | 326 | | if (data == null) |
| | 0 | 327 | | return 0; |
| | | 328 | | |
| | 0 | 329 | | int i = data.Length; |
| | 0 | 330 | | int hc = i + 1; |
| | | 331 | | |
| | 0 | 332 | | while (--i >= 0) |
| | 0 | 333 | | { |
| | 0 | 334 | | ulong di = data[i]; |
| | 0 | 335 | | hc *= 257; |
| | 0 | 336 | | hc ^= (int)di; |
| | 0 | 337 | | hc *= 257; |
| | 0 | 338 | | hc ^= (int)(di >> 32); |
| | 0 | 339 | | } |
| | | 340 | | |
| | 0 | 341 | | return hc; |
| | 0 | 342 | | } |
| | | 343 | | |
| | | 344 | | public static int GetHashCode(ulong[] data, int off, int len) |
| | 0 | 345 | | { |
| | 0 | 346 | | if (data == null) |
| | 0 | 347 | | return 0; |
| | | 348 | | |
| | 0 | 349 | | int i = len; |
| | 0 | 350 | | int hc = i + 1; |
| | | 351 | | |
| | 0 | 352 | | while (--i >= 0) |
| | 0 | 353 | | { |
| | 0 | 354 | | ulong di = data[off + i]; |
| | 0 | 355 | | hc *= 257; |
| | 0 | 356 | | hc ^= (int)di; |
| | 0 | 357 | | hc *= 257; |
| | 0 | 358 | | hc ^= (int)(di >> 32); |
| | 0 | 359 | | } |
| | | 360 | | |
| | 0 | 361 | | return hc; |
| | 0 | 362 | | } |
| | | 363 | | |
| | | 364 | | public static byte[] Clone( |
| | | 365 | | byte[] data) |
| | 12 | 366 | | { |
| | 12 | 367 | | return data == null ? null : (byte[])data.Clone(); |
| | 12 | 368 | | } |
| | | 369 | | |
| | | 370 | | public static byte[] Clone( |
| | | 371 | | byte[] data, |
| | | 372 | | byte[] existing) |
| | 0 | 373 | | { |
| | 0 | 374 | | if (data == null) |
| | 0 | 375 | | { |
| | 0 | 376 | | return null; |
| | | 377 | | } |
| | 0 | 378 | | if ((existing == null) || (existing.Length != data.Length)) |
| | 0 | 379 | | { |
| | 0 | 380 | | return Clone(data); |
| | | 381 | | } |
| | 0 | 382 | | Array.Copy(data, 0, existing, 0, existing.Length); |
| | 0 | 383 | | return existing; |
| | 0 | 384 | | } |
| | | 385 | | |
| | | 386 | | public static int[] Clone( |
| | | 387 | | int[] data) |
| | 0 | 388 | | { |
| | 0 | 389 | | return data == null ? null : (int[])data.Clone(); |
| | 0 | 390 | | } |
| | | 391 | | |
| | | 392 | | internal static uint[] Clone(uint[] data) |
| | 0 | 393 | | { |
| | 0 | 394 | | return data == null ? null : (uint[])data.Clone(); |
| | 0 | 395 | | } |
| | | 396 | | |
| | | 397 | | public static long[] Clone(long[] data) |
| | 0 | 398 | | { |
| | 0 | 399 | | return data == null ? null : (long[])data.Clone(); |
| | 0 | 400 | | } |
| | | 401 | | |
| | | 402 | | public static ulong[] Clone( |
| | | 403 | | ulong[] data) |
| | 0 | 404 | | { |
| | 0 | 405 | | return data == null ? null : (ulong[]) data.Clone(); |
| | 0 | 406 | | } |
| | | 407 | | |
| | | 408 | | public static ulong[] Clone( |
| | | 409 | | ulong[] data, |
| | | 410 | | ulong[] existing) |
| | 0 | 411 | | { |
| | 0 | 412 | | if (data == null) |
| | 0 | 413 | | { |
| | 0 | 414 | | return null; |
| | | 415 | | } |
| | 0 | 416 | | if ((existing == null) || (existing.Length != data.Length)) |
| | 0 | 417 | | { |
| | 0 | 418 | | return Clone(data); |
| | | 419 | | } |
| | 0 | 420 | | Array.Copy(data, 0, existing, 0, existing.Length); |
| | 0 | 421 | | return existing; |
| | 0 | 422 | | } |
| | | 423 | | |
| | | 424 | | public static bool Contains(byte[] a, byte n) |
| | 0 | 425 | | { |
| | 0 | 426 | | for (int i = 0; i < a.Length; ++i) |
| | 0 | 427 | | { |
| | 0 | 428 | | if (a[i] == n) |
| | 0 | 429 | | return true; |
| | 0 | 430 | | } |
| | 0 | 431 | | return false; |
| | 0 | 432 | | } |
| | | 433 | | |
| | | 434 | | public static bool Contains(short[] a, short n) |
| | 0 | 435 | | { |
| | 0 | 436 | | for (int i = 0; i < a.Length; ++i) |
| | 0 | 437 | | { |
| | 0 | 438 | | if (a[i] == n) |
| | 0 | 439 | | return true; |
| | 0 | 440 | | } |
| | 0 | 441 | | return false; |
| | 0 | 442 | | } |
| | | 443 | | |
| | | 444 | | public static bool Contains(int[] a, int n) |
| | 0 | 445 | | { |
| | 0 | 446 | | for (int i = 0; i < a.Length; ++i) |
| | 0 | 447 | | { |
| | 0 | 448 | | if (a[i] == n) |
| | 0 | 449 | | return true; |
| | 0 | 450 | | } |
| | 0 | 451 | | return false; |
| | 0 | 452 | | } |
| | | 453 | | |
| | | 454 | | public static void Fill( |
| | | 455 | | byte[] buf, |
| | | 456 | | byte b) |
| | 1 | 457 | | { |
| | 1 | 458 | | int i = buf.Length; |
| | 129 | 459 | | while (i > 0) |
| | 128 | 460 | | { |
| | 128 | 461 | | buf[--i] = b; |
| | 128 | 462 | | } |
| | 1 | 463 | | } |
| | | 464 | | |
| | | 465 | | public static void Fill(byte[] buf, int from, int to, byte b) |
| | 0 | 466 | | { |
| | 0 | 467 | | for (int i = from; i < to; ++i) |
| | 0 | 468 | | { |
| | 0 | 469 | | buf[i] = b; |
| | 0 | 470 | | } |
| | 0 | 471 | | } |
| | | 472 | | |
| | | 473 | | public static byte[] CopyOf(byte[] data, int newLength) |
| | 0 | 474 | | { |
| | 0 | 475 | | byte[] tmp = new byte[newLength]; |
| | 0 | 476 | | Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length)); |
| | 0 | 477 | | return tmp; |
| | 0 | 478 | | } |
| | | 479 | | |
| | | 480 | | public static char[] CopyOf(char[] data, int newLength) |
| | 0 | 481 | | { |
| | 0 | 482 | | char[] tmp = new char[newLength]; |
| | 0 | 483 | | Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length)); |
| | 0 | 484 | | return tmp; |
| | 0 | 485 | | } |
| | | 486 | | |
| | | 487 | | public static int[] CopyOf(int[] data, int newLength) |
| | 0 | 488 | | { |
| | 0 | 489 | | int[] tmp = new int[newLength]; |
| | 0 | 490 | | Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length)); |
| | 0 | 491 | | return tmp; |
| | 0 | 492 | | } |
| | | 493 | | |
| | | 494 | | public static long[] CopyOf(long[] data, int newLength) |
| | 0 | 495 | | { |
| | 0 | 496 | | long[] tmp = new long[newLength]; |
| | 0 | 497 | | Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length)); |
| | 0 | 498 | | return tmp; |
| | 0 | 499 | | } |
| | | 500 | | |
| | | 501 | | public static BigInteger[] CopyOf(BigInteger[] data, int newLength) |
| | 0 | 502 | | { |
| | 0 | 503 | | BigInteger[] tmp = new BigInteger[newLength]; |
| | 0 | 504 | | Array.Copy(data, 0, tmp, 0, System.Math.Min(newLength, data.Length)); |
| | 0 | 505 | | return tmp; |
| | 0 | 506 | | } |
| | | 507 | | |
| | | 508 | | /** |
| | | 509 | | * Make a copy of a range of bytes from the passed in data array. The range can |
| | | 510 | | * extend beyond the end of the input array, in which case the return array will |
| | | 511 | | * be padded with zeroes. |
| | | 512 | | * |
| | | 513 | | * @param data the array from which the data is to be copied. |
| | | 514 | | * @param from the start index at which the copying should take place. |
| | | 515 | | * @param to the final index of the range (exclusive). |
| | | 516 | | * |
| | | 517 | | * @return a new byte array containing the range given. |
| | | 518 | | */ |
| | | 519 | | public static byte[] CopyOfRange(byte[] data, int from, int to) |
| | 0 | 520 | | { |
| | 0 | 521 | | int newLength = GetLength(from, to); |
| | 0 | 522 | | byte[] tmp = new byte[newLength]; |
| | 0 | 523 | | Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from)); |
| | 0 | 524 | | return tmp; |
| | 0 | 525 | | } |
| | | 526 | | |
| | | 527 | | public static int[] CopyOfRange(int[] data, int from, int to) |
| | 0 | 528 | | { |
| | 0 | 529 | | int newLength = GetLength(from, to); |
| | 0 | 530 | | int[] tmp = new int[newLength]; |
| | 0 | 531 | | Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from)); |
| | 0 | 532 | | return tmp; |
| | 0 | 533 | | } |
| | | 534 | | |
| | | 535 | | public static long[] CopyOfRange(long[] data, int from, int to) |
| | 0 | 536 | | { |
| | 0 | 537 | | int newLength = GetLength(from, to); |
| | 0 | 538 | | long[] tmp = new long[newLength]; |
| | 0 | 539 | | Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from)); |
| | 0 | 540 | | return tmp; |
| | 0 | 541 | | } |
| | | 542 | | |
| | | 543 | | public static BigInteger[] CopyOfRange(BigInteger[] data, int from, int to) |
| | 0 | 544 | | { |
| | 0 | 545 | | int newLength = GetLength(from, to); |
| | 0 | 546 | | BigInteger[] tmp = new BigInteger[newLength]; |
| | 0 | 547 | | Array.Copy(data, from, tmp, 0, System.Math.Min(newLength, data.Length - from)); |
| | 0 | 548 | | return tmp; |
| | 0 | 549 | | } |
| | | 550 | | |
| | | 551 | | private static int GetLength(int from, int to) |
| | 0 | 552 | | { |
| | 0 | 553 | | int newLength = to - from; |
| | 0 | 554 | | if (newLength < 0) |
| | 0 | 555 | | throw new ArgumentException(from + " > " + to); |
| | 0 | 556 | | return newLength; |
| | 0 | 557 | | } |
| | | 558 | | |
| | | 559 | | public static byte[] Append(byte[] a, byte b) |
| | 0 | 560 | | { |
| | 0 | 561 | | if (a == null) |
| | 0 | 562 | | return new byte[] { b }; |
| | | 563 | | |
| | 0 | 564 | | int length = a.Length; |
| | 0 | 565 | | byte[] result = new byte[length + 1]; |
| | 0 | 566 | | Array.Copy(a, 0, result, 0, length); |
| | 0 | 567 | | result[length] = b; |
| | 0 | 568 | | return result; |
| | 0 | 569 | | } |
| | | 570 | | |
| | | 571 | | public static short[] Append(short[] a, short b) |
| | 0 | 572 | | { |
| | 0 | 573 | | if (a == null) |
| | 0 | 574 | | return new short[] { b }; |
| | | 575 | | |
| | 0 | 576 | | int length = a.Length; |
| | 0 | 577 | | short[] result = new short[length + 1]; |
| | 0 | 578 | | Array.Copy(a, 0, result, 0, length); |
| | 0 | 579 | | result[length] = b; |
| | 0 | 580 | | return result; |
| | 0 | 581 | | } |
| | | 582 | | |
| | | 583 | | public static int[] Append(int[] a, int b) |
| | 0 | 584 | | { |
| | 0 | 585 | | if (a == null) |
| | 0 | 586 | | return new int[] { b }; |
| | | 587 | | |
| | 0 | 588 | | int length = a.Length; |
| | 0 | 589 | | int[] result = new int[length + 1]; |
| | 0 | 590 | | Array.Copy(a, 0, result, 0, length); |
| | 0 | 591 | | result[length] = b; |
| | 0 | 592 | | return result; |
| | 0 | 593 | | } |
| | | 594 | | |
| | | 595 | | public static byte[] Concatenate(byte[] a, byte[] b) |
| | 0 | 596 | | { |
| | 0 | 597 | | if (a == null) |
| | 0 | 598 | | return Clone(b); |
| | 0 | 599 | | if (b == null) |
| | 0 | 600 | | return Clone(a); |
| | | 601 | | |
| | 0 | 602 | | byte[] rv = new byte[a.Length + b.Length]; |
| | 0 | 603 | | Array.Copy(a, 0, rv, 0, a.Length); |
| | 0 | 604 | | Array.Copy(b, 0, rv, a.Length, b.Length); |
| | 0 | 605 | | return rv; |
| | 0 | 606 | | } |
| | | 607 | | |
| | | 608 | | public static byte[] ConcatenateAll(params byte[][] vs) |
| | 0 | 609 | | { |
| | 0 | 610 | | byte[][] nonNull = new byte[vs.Length][]; |
| | 0 | 611 | | int count = 0; |
| | 0 | 612 | | int totalLength = 0; |
| | | 613 | | |
| | 0 | 614 | | for (int i = 0; i < vs.Length; ++i) |
| | 0 | 615 | | { |
| | 0 | 616 | | byte[] v = vs[i]; |
| | 0 | 617 | | if (v != null) |
| | 0 | 618 | | { |
| | 0 | 619 | | nonNull[count++] = v; |
| | 0 | 620 | | totalLength += v.Length; |
| | 0 | 621 | | } |
| | 0 | 622 | | } |
| | | 623 | | |
| | 0 | 624 | | byte[] result = new byte[totalLength]; |
| | 0 | 625 | | int pos = 0; |
| | | 626 | | |
| | 0 | 627 | | for (int j = 0; j < count; ++j) |
| | 0 | 628 | | { |
| | 0 | 629 | | byte[] v = nonNull[j]; |
| | 0 | 630 | | Array.Copy(v, 0, result, pos, v.Length); |
| | 0 | 631 | | pos += v.Length; |
| | 0 | 632 | | } |
| | | 633 | | |
| | 0 | 634 | | return result; |
| | 0 | 635 | | } |
| | | 636 | | |
| | | 637 | | public static int[] Concatenate(int[] a, int[] b) |
| | 0 | 638 | | { |
| | 0 | 639 | | if (a == null) |
| | 0 | 640 | | return Clone(b); |
| | 0 | 641 | | if (b == null) |
| | 0 | 642 | | return Clone(a); |
| | | 643 | | |
| | 0 | 644 | | int[] rv = new int[a.Length + b.Length]; |
| | 0 | 645 | | Array.Copy(a, 0, rv, 0, a.Length); |
| | 0 | 646 | | Array.Copy(b, 0, rv, a.Length, b.Length); |
| | 0 | 647 | | return rv; |
| | 0 | 648 | | } |
| | | 649 | | |
| | | 650 | | public static byte[] Prepend(byte[] a, byte b) |
| | 0 | 651 | | { |
| | 0 | 652 | | if (a == null) |
| | 0 | 653 | | return new byte[] { b }; |
| | | 654 | | |
| | 0 | 655 | | int length = a.Length; |
| | 0 | 656 | | byte[] result = new byte[length + 1]; |
| | 0 | 657 | | Array.Copy(a, 0, result, 1, length); |
| | 0 | 658 | | result[0] = b; |
| | 0 | 659 | | return result; |
| | 0 | 660 | | } |
| | | 661 | | |
| | | 662 | | public static short[] Prepend(short[] a, short b) |
| | 0 | 663 | | { |
| | 0 | 664 | | if (a == null) |
| | 0 | 665 | | return new short[] { b }; |
| | | 666 | | |
| | 0 | 667 | | int length = a.Length; |
| | 0 | 668 | | short[] result = new short[length + 1]; |
| | 0 | 669 | | Array.Copy(a, 0, result, 1, length); |
| | 0 | 670 | | result[0] = b; |
| | 0 | 671 | | return result; |
| | 0 | 672 | | } |
| | | 673 | | |
| | | 674 | | public static int[] Prepend(int[] a, int b) |
| | 0 | 675 | | { |
| | 0 | 676 | | if (a == null) |
| | 0 | 677 | | return new int[] { b }; |
| | | 678 | | |
| | 0 | 679 | | int length = a.Length; |
| | 0 | 680 | | int[] result = new int[length + 1]; |
| | 0 | 681 | | Array.Copy(a, 0, result, 1, length); |
| | 0 | 682 | | result[0] = b; |
| | 0 | 683 | | return result; |
| | 0 | 684 | | } |
| | | 685 | | |
| | | 686 | | public static byte[] Reverse(byte[] a) |
| | 0 | 687 | | { |
| | 0 | 688 | | if (a == null) |
| | 0 | 689 | | return null; |
| | | 690 | | |
| | 0 | 691 | | int p1 = 0, p2 = a.Length; |
| | 0 | 692 | | byte[] result = new byte[p2]; |
| | | 693 | | |
| | 0 | 694 | | while (--p2 >= 0) |
| | 0 | 695 | | { |
| | 0 | 696 | | result[p2] = a[p1++]; |
| | 0 | 697 | | } |
| | | 698 | | |
| | 0 | 699 | | return result; |
| | 0 | 700 | | } |
| | | 701 | | |
| | | 702 | | public static int[] Reverse(int[] a) |
| | 0 | 703 | | { |
| | 0 | 704 | | if (a == null) |
| | 0 | 705 | | return null; |
| | | 706 | | |
| | 0 | 707 | | int p1 = 0, p2 = a.Length; |
| | 0 | 708 | | int[] result = new int[p2]; |
| | | 709 | | |
| | 0 | 710 | | while (--p2 >= 0) |
| | 0 | 711 | | { |
| | 0 | 712 | | result[p2] = a[p1++]; |
| | 0 | 713 | | } |
| | | 714 | | |
| | 0 | 715 | | return result; |
| | 0 | 716 | | } |
| | | 717 | | } |
| | | 718 | | } |