private bool validate(EndianBinaryReader binaryReader) { if (this.fileHeader.size == 14) { binaryReader.Seek(0); var crc = Crc.Calculate(binaryReader, 0, 12); if (crc != this.fileHeader.crc) { return(false); } binaryReader.Seek(14); } var fileCrcPosition = this.fileHeader.size + (int)this.fileHeader.dataSize; binaryReader.Seek(fileCrcPosition); var fileCrc = binaryReader.ReadUInt8() + (binaryReader.ReadUInt8() << 8); var start = this.fileHeader.size == 12 ? 0 : this.fileHeader.size; binaryReader.Seek(start); if (fileCrc != Crc.Calculate(binaryReader, start, fileCrcPosition)) { return(false); } binaryReader.Seek(this.fileHeader.size); return(true); }
public DefinitionRecord(EndianBinaryReader binaryReader, int localNum) { this.localNum = localNum; this.reserved = binaryReader.ReadUInt8(); this.architecture = binaryReader.ReadUInt8(); bool littleEndian = this.architecture == 0; this.GlobalMsgNum = binaryReader.ReadUInt16(littleEndian); byte numFields = binaryReader.ReadUInt8(); this.fieldDefinitions = new FieldDefinition[numFields]; for (int i = 0; i < numFields; i++) { this.fieldDefinitions[i] = new FieldDefinition(binaryReader); } }
public DataField(EndianBinaryReader binaryReader, Dictionary <string, byte> opts) { var baseNum = opts["baseNum"]; var size = opts["size"]; var arch = opts["arch"]; var littleEndian = arch == 1; var baseType = DataTypes.Types[baseNum]; var multiples = size / baseType.size; // TODO: These types should be an enum. switch (baseType.typeName) { case "enum": case "byte": case "uint8": case "uint8z": if (multiples > 1) { var data = new byte[multiples]; for (int i = 0; i < multiples; i++) { data[i] = binaryReader.ReadUInt8(); } this.data = data; } else { this.data = binaryReader.ReadUInt8(); } break; case "sint8": if (multiples > 1) { var data = new sbyte[multiples]; for (int i = 0; i < multiples; i++) { data[i] = binaryReader.ReadInt8(); } this.data = data; } else { this.data = binaryReader.ReadInt8(); } break; case "uint16": case "uint16z": if (multiples > 1) { var data = new ushort[multiples]; for (int i = 0; i < multiples; i++) { data[i] = binaryReader.ReadUInt16(littleEndian); } this.data = data; } else { this.data = binaryReader.ReadUInt16(littleEndian); } break; case "sint16": if (multiples > 1) { var data = new short[multiples]; for (int i = 0; i < multiples; i++) { data[i] = binaryReader.ReadInt16(littleEndian); } this.data = data; } else { this.data = binaryReader.ReadInt16(littleEndian); } break; case "uint32": case "uint32z": if (multiples > 1) { var data = new uint[multiples]; for (int i = 0; i < multiples; i++) { data[i] = binaryReader.ReadUInt32(littleEndian); } this.data = data; } else { this.data = binaryReader.ReadUInt32(littleEndian); } break; case "sint32": if (multiples > 1) { var data = new int[multiples]; for (int i = 0; i < multiples; i++) { data[i] = binaryReader.ReadInt32(littleEndian); } this.data = data; } else { this.data = binaryReader.ReadInt32(littleEndian); } break; case "float32": if (multiples > 1) { var data = new float[multiples]; for (int i = 0; i < multiples; i++) { data[i] = binaryReader.ReadFloat(littleEndian); } this.data = data; } else { this.data = binaryReader.ReadFloat(littleEndian); } break; case "float64": if (multiples > 1) { var data = new double[multiples]; for (int i = 0; i < multiples; i++) { data[i] = binaryReader.ReadFloat64(littleEndian); } this.data = data; } else { this.data = binaryReader.ReadFloat64(littleEndian); } break; case "uint64": case "uint64z": if (multiples > 1) { var data = new ulong[multiples]; for (int i = 0; i < multiples; i++) { data[i] = binaryReader.ReadUInt64(littleEndian); } this.data = data; } else { this.data = binaryReader.ReadUInt64(littleEndian); } break; case "sint64": if (multiples > 1) { var data = new long[multiples]; for (int i = 0; i < multiples; i++) { data[i] = binaryReader.ReadInt64(littleEndian); } this.data = data; } else { this.data = binaryReader.ReadInt64(littleEndian); } break; case "string": this.data = binaryReader.ReadChars((int)size); break; default: // TODO: This should never happen but we should also throw an exception.. break; } this.valid = check(baseType.invalidValue); }