public override void LoadData(BinaryReader reader, StructInstance instance) { int offset = (int)reader.BaseStream.Position; int len; try { len = GetExpressionAttribute("len").EvaluateInt(instance); } catch (OverflowException) { throw new LoadDataException("Blob size is larger than Int32"); } if (offset + len > reader.BaseStream.Length) throw new LoadDataException("Blob size " + len + " exceeds stream length"); if (len < 0) throw new LoadDataException("Blob size " + len + " is negative"); var decodedSizeExpr = GetExpressionAttribute("decodedsize"); int decodedSize = decodedSizeExpr != null ? decodedSizeExpr.EvaluateInt(instance) : -1; string encoding = GetStringAttribute("encoding"); BlobDecoder blobDecoder = FindBlobEncoding(instance, encoding); BlobCell cell = new BlobCell(this, reader.BaseStream, offset, len, blobDecoder, decodedSize); instance.AddCell(cell, _hidden); instance.RegisterCellSize(cell, len); reader.BaseStream.Position += len; StructDef structDef = GetStructAttribute("struct"); if (structDef != null) instance.AddChildSeed(new BlobChildSeed(structDef, cell)); }
public override void LoadData(BinaryReader reader, StructInstance instance) { int offset = (int)reader.BaseStream.Position; int len; try { len = GetExpressionAttribute("len").EvaluateInt(instance); } catch (OverflowException) { throw new LoadDataException("Blob size is larger than Int32"); } if (offset + len > reader.BaseStream.Length) { throw new LoadDataException("Blob size " + len + " exceeds stream length"); } if (len < 0) { throw new LoadDataException("Blob size " + len + " is negative"); } var decodedSizeExpr = GetExpressionAttribute("decodedsize"); int decodedSize = decodedSizeExpr != null?decodedSizeExpr.EvaluateInt(instance) : -1; string encoding = GetStringAttribute("encoding"); BlobDecoder blobDecoder = FindBlobEncoding(instance, encoding); BlobCell cell = new BlobCell(this, reader.BaseStream, offset, len, blobDecoder, decodedSize); instance.AddCell(cell, _hidden); instance.RegisterCellSize(cell, len); reader.BaseStream.Position += len; StructDef structDef = GetStructAttribute("struct"); if (structDef != null) { instance.AddChildSeed(new BlobChildSeed(structDef, cell)); } }
public override void LoadData(BinaryReader reader, StructInstance instance) { Expression valueExpr = GetExpressionAttribute("value"); if (valueExpr != null) { AddCell(instance, valueExpr); return; } int offset = (int)reader.BaseStream.Position; string value; int cellSize; int charSize = 1; if (_wide) charSize = 2; Expression lengthExpr = GetExpressionAttribute("len"); if (lengthExpr != null) { int length = lengthExpr.EvaluateInt(instance); if (length < 0) { throw new LoadDataException("Length expression " + lengthExpr + " has the result of " + length + " which is negative"); } cellSize = length * charSize; if (reader.BaseStream.Length - reader.BaseStream.Position < cellSize) { throw new LoadDataException("Length expression " + lengthExpr + " has the result of " + length + " and points outside the file"); } byte[] bytes = reader.ReadBytes(length * charSize); char[] chars = _wide ? Encoding.Unicode.GetChars(bytes) : Encoding.Default.GetChars(bytes); for (int i = 0; i < length; i++) { if (chars[i] == '\0') { length = i; break; } } value = new string(chars, 0, length); } else { StringBuilder valueBuilder = new StringBuilder(); BinaryReader charReader = _wide ? new BinaryReader(reader.BaseStream, Encoding.Unicode) : reader; while (true) { char c = charReader.ReadChar(); if (c == '\0') break; valueBuilder.Append(c); } value = valueBuilder.ToString(); cellSize = value.Length * charSize; } StructCell cell = AddCell(instance, value, offset); instance.RegisterCellSize(cell, cellSize); }
public override void LoadData(BinaryReader reader, StructInstance instance) { Expression valueExpr = GetExpressionAttribute("value"); if (valueExpr != null) { AddCell(instance, valueExpr); return; } int offset = (int)reader.BaseStream.Position; string value; int cellSize; int charSize = 1; if (_wide) { charSize = 2; } Expression lengthExpr = GetExpressionAttribute("len"); if (lengthExpr != null) { int length = lengthExpr.EvaluateInt(instance); if (length < 0) { throw new LoadDataException("Length expression " + lengthExpr + " has the result of " + length + " which is negative"); } cellSize = length * charSize; if (reader.BaseStream.Length - reader.BaseStream.Position < cellSize) { throw new LoadDataException("Length expression " + lengthExpr + " has the result of " + length + " and points outside the file"); } byte[] bytes = reader.ReadBytes(length * charSize); char[] chars = _wide ? Encoding.Unicode.GetChars(bytes) : Encoding.Default.GetChars(bytes); for (int i = 0; i < length; i++) { if (chars[i] == '\0') { length = i; break; } } value = new string(chars, 0, length); } else { StringBuilder valueBuilder = new StringBuilder(); BinaryReader charReader = _wide ? new BinaryReader(reader.BaseStream, Encoding.Unicode) : reader; while (true) { char c = charReader.ReadChar(); if (c == '\0') { break; } valueBuilder.Append(c); } value = valueBuilder.ToString(); cellSize = value.Length * charSize; } StructCell cell = AddCell(instance, value, offset); instance.RegisterCellSize(cell, cellSize); }