public void Add(byte[] group) { if (this.buffer == null) { this.buffer = group; this.next = group.Length; return; } if (this.align != 0) { if (this.align == -1) { this.next = DataConverter.Align(this.next, group.Length); } else { this.next = DataConverter.Align(this.next, this.align); } this.align = 0; } if (this.next + group.Length > this.buffer.Length) { byte[] destinationArray = new byte[Math.Max(this.next, 16) * 2 + group.Length]; Array.Copy(this.buffer, destinationArray, this.buffer.Length); Array.Copy(group, 0, destinationArray, this.next, group.Length); this.next += group.Length; this.buffer = destinationArray; } else { Array.Copy(group, 0, this.buffer, this.next, group.Length); this.next += group.Length; } }
private static bool Prepare(byte[] buffer, ref int idx, int size, ref bool align) { if (align) { idx = DataConverter.Align(idx, size); align = false; } if (idx + size > buffer.Length) { idx = buffer.Length; return(false); } return(true); }
public static IList Unpack(string description, byte[] buffer, int startIndex) { DataConverter dataConverter = DataConverter.CopyConv; ArrayList arrayList = new ArrayList(); int num = startIndex; bool flag = false; int num2 = 0; int num3 = 0; while (num3 < description.Length && num < buffer.Length) { int num4 = num3; char c = description[num3]; switch (c) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': num2 = (int)((short)description[num3] - 48); num4 = num3 + 1; break; default: switch (c) { case '[': { int num5 = -1; int i; for (i = num3 + 1; i < description.Length; i++) { if (description[i] == ']') { break; } int num6 = (int)((short)description[i] - 48); if (num6 >= 0 && num6 <= 9) { if (num5 == -1) { num5 = num6; } else { num5 = num5 * 10 + num6; } } } if (num5 == -1) { throw new ArgumentException("invalid size specification"); } num3 = i; num2 = num5; break; } default: { switch (c) { case '!': flag = true; goto IL_683; default: switch (c) { case 'I': if (DataConverter.Prepare(buffer, ref num, 4, ref flag)) { arrayList.Add(dataConverter.GetUInt32(buffer, num)); num += 4; } goto IL_683; default: switch (c) { case 'x': num++; goto IL_683; default: if (c == '*') { num2 = int.MaxValue; goto IL_683; } if (c == 'S') { if (DataConverter.Prepare(buffer, ref num, 2, ref flag)) { arrayList.Add(dataConverter.GetUInt16(buffer, num)); num += 2; } goto IL_683; } if (c != 's') { throw new ArgumentException(string.Format("invalid format specified `{0}'", description[num3])); } if (DataConverter.Prepare(buffer, ref num, 2, ref flag)) { arrayList.Add(dataConverter.GetInt16(buffer, num)); num += 2; } goto IL_683; case 'z': break; } break; case 'L': if (DataConverter.Prepare(buffer, ref num, 8, ref flag)) { arrayList.Add(dataConverter.GetUInt64(buffer, num)); num += 8; } goto IL_683; } break; case '$': break; case '%': dataConverter = DataConverter.Native; goto IL_683; } num3++; if (num3 >= description.Length) { throw new ArgumentException("$ description needs a type specified", "description"); } char c2 = description[num3]; if (flag) { num = DataConverter.Align(num, 4); flag = false; } if (num < buffer.Length) { char c3 = c2; int num6; Encoding encoding; switch (c3) { case '3': encoding = Encoding.GetEncoding(12000); num6 = 4; break; case '4': encoding = Encoding.GetEncoding(12001); num6 = 4; break; default: if (c3 != 'b') { throw new ArgumentException("Invalid format for $ specifier", "description"); } encoding = Encoding.BigEndianUnicode; num6 = 2; break; case '6': encoding = Encoding.Unicode; num6 = 2; break; case '7': encoding = Encoding.UTF7; num6 = 1; break; case '8': encoding = Encoding.UTF8; num6 = 1; break; } int j = num; switch (num6) { case 1: while (j < buffer.Length && buffer[j] != 0) { j++; } arrayList.Add(encoding.GetChars(buffer, num, j - num)); if (j == buffer.Length) { num = j; } else { num = j + 1; } break; case 2: while (j < buffer.Length) { if (j + 1 == buffer.Length) { j++; break; } if (buffer[j] == 0 && buffer[j + 1] == 0) { break; } j++; } arrayList.Add(encoding.GetChars(buffer, num, j - num)); if (j == buffer.Length) { num = j; } else { num = j + 2; } break; case 4: while (j < buffer.Length) { if (j + 3 >= buffer.Length) { j = buffer.Length; break; } if (buffer[j] == 0 && buffer[j + 1] == 0 && buffer[j + 2] == 0 && buffer[j + 3] == 0) { break; } j++; } arrayList.Add(encoding.GetChars(buffer, num, j - num)); if (j == buffer.Length) { num = j; } else { num = j + 4; } break; } } break; } case '^': dataConverter = DataConverter.BigEndian; break; case '_': dataConverter = DataConverter.LittleEndian; break; case 'b': if (DataConverter.Prepare(buffer, ref num, 1, ref flag)) { arrayList.Add(buffer[num]); num++; } break; case 'c': goto IL_300; case 'd': if (DataConverter.Prepare(buffer, ref num, 8, ref flag)) { arrayList.Add(dataConverter.GetDouble(buffer, num)); num += 8; } break; case 'f': if (DataConverter.Prepare(buffer, ref num, 4, ref flag)) { arrayList.Add(dataConverter.GetDouble(buffer, num)); num += 4; } break; case 'i': if (DataConverter.Prepare(buffer, ref num, 4, ref flag)) { arrayList.Add(dataConverter.GetInt32(buffer, num)); num += 4; } break; case 'l': if (DataConverter.Prepare(buffer, ref num, 8, ref flag)) { arrayList.Add(dataConverter.GetInt64(buffer, num)); num += 8; } break; } break; case 'C': goto IL_300; } IL_683: if (num2 > 0) { if (--num2 > 0) { num3 = num4; } continue; } num3++; continue; IL_300: if (DataConverter.Prepare(buffer, ref num, 1, ref flag)) { char c4; if (description[num3] == 'c') { c4 = (char)((sbyte)buffer[num]); } else { c4 = (char)buffer[num]; } arrayList.Add(c4); num++; } goto IL_683; } return(arrayList); }