public static UInt32 ReadUInt32(byte[] buf, ref int offset) { UInt32 val = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToUInt32(buf, offset)); offset += 4; return(val); }
public static short ReadShort(byte[] buf, ref int offset) { short val = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToInt16(buf, offset)); offset += 2; return(val); }
public static int ReadInt(byte[] buf, ref int offset) { int val = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToInt32(buf, offset)); offset += 4; return(val); }
public static int WriteUInt32(byte[] buf, UInt32 val, ref int offset) { byte[] bytes = BitConverter.GetBytes(ByteOrderConverter.HostToNetworkOrder(val)); Array.Copy(bytes, 0, buf, offset, bytes.Length); offset += bytes.Length; return(bytes.Length); }
public void Load(string fileName) { _converter = ByteOrderConverter.LittleEndian; FileName = fileName; var con = new SerializingFile(new FileStream(fileName, FileMode.Open, FileAccess.Read)); Serialize(con); con.Memory.Close(); }
public void ReadFile() { if (_memory == Stream.Null) { return; } _converter = ByteOrderConverter.LittleEndian; _memory.Seek(0, 0); var magic = (uint)ReadInt(_memory); if (magic != 0x3AB70C13) { //DebugOutput.PrintLn("Not a SFAR File"); return; } _memory.Seek(8, 0); var count = ReadInt(_memory); _memory.Seek(0xC + 8 * count, 0); Entries = new List <Entry>(); int blocksize; var pos = (int)_memory.Position; do { var e = new Entry { Offset = pos }; _memory.Seek(pos, 0); blocksize = ReadInt16(_memory); _memory.Seek(pos + 0x4, 0); e.Size = ReadInt(_memory); _memory.Seek(pos + 0x1C, 0); e.Name = ReadString(_memory); pos += blocksize; Entries.Add(e); } while (blocksize != 0); }
private static IcoFrame ProcessIcoFrame(ByteReader reader, ParseContext context) { var bWidth = reader.NextUint8(); var bHeight = reader.NextUint8(); var bColorCount = reader.NextUint8(); var bReserved = reader.NextUint8(); var wPlanes = reader.NextUint16(); var wBitCount = reader.NextUint16(); var dwBytesInRes = reader.NextUint32(); var dwImageOffset = reader.NextUint32(); if (bWidth != bHeight) { context.Reporter.WarnLine(IcoErrorCode.NotSquare, $"Icon is not square ({bWidth}x{bHeight}).", context.DisplayedPath, context.ImageDirectoryIndex.Value); } if (bReserved != FileFormatConstants._iconEntryReserved) { throw new InvalidIcoFileException(IcoErrorCode.InvalidFrameHeader_bReserved, $"ICONDIRECTORY.bReserved should be {FileFormatConstants._iconEntryReserved}, was {bReserved}.", context); } if (wPlanes > 1) { throw new InvalidIcoFileException(IcoErrorCode.InvalidFrameHeader_wPlanes, $"ICONDIRECTORY.wPlanes is {wPlanes}. Only single-plane bitmaps are supported.", context); } if (dwBytesInRes > int.MaxValue) { throw new InvalidIcoFileException(IcoErrorCode.InvalidFrameHeader_dwBytesInRes, $"ICONDIRECTORY.dwBytesInRes == {dwBytesInRes}, which is unreasonably large.", context); } if (dwImageOffset > int.MaxValue) { throw new InvalidIcoFileException(IcoErrorCode.InvalidFrameHeader_dwImageOffset, $"ICONDIRECTORY.dwImageOffset == {dwImageOffset}, which is unreasonably large.", context); } var source = new IcoFrame { TotalDiskUsage = dwBytesInRes + /* sizeof(ICONDIRENTRY) */ 16, Encoding = new IcoFrameEncoding { ClaimedBitDepth = wBitCount, ClaimedHeight = bHeight > 0 ? bHeight : 256u, ClaimedWidth = bWidth > 0 ? bWidth : 256u, }, }; source.RawData = reader.Data.Slice((int)dwImageOffset, (int)dwBytesInRes).ToArray(); var bitmapHeader = new ByteReader(source.RawData, ByteOrder.LittleEndian); var signature = bitmapHeader.NextUint64(); bitmapHeader.SeekOffset = 0; if (PngDecoder.IsProbablyPngFile(ByteOrderConverter.To(ByteOrder.NetworkEndian, signature))) { PngDecoder.DoPngEntry(bitmapHeader, context, source); } else { BmpDecoder.DoBitmapEntry(bitmapHeader, context, source); } return(source); }
private static IcoFrame GenerateFrame(CommandLineOptions opts, ParseContext context) { var frame = new IcoFrame { Encoding = new IcoFrameEncoding(), }; var sourceFile = File.ReadAllBytes(opts.SourceImagePath); var reader = new ByteReader(sourceFile, ByteOrder.LittleEndian); var signature = reader.NextUint64(); var sourceEncoding = PngDecoder.IsProbablyPngFile(ByteOrderConverter.To(ByteOrder.NetworkEndian, signature)) ? IcoEncodingType.Png : IcoEncodingType.Bitmap; bool canSourceBePreserved = false; switch (sourceEncoding) { case IcoEncodingType.Bitmap: ReadBmpFile(opts, context, frame, sourceFile); break; case IcoEncodingType.Png: ReadPngFile(opts, context, frame, sourceFile, out canSourceBePreserved); break; } frame.Encoding.ActualHeight = (uint)frame.CookedData.Height; frame.Encoding.ClaimedHeight = (uint)frame.CookedData.Height; frame.Encoding.ActualWidth = (uint)frame.CookedData.Width; frame.Encoding.ClaimedWidth = (uint)frame.CookedData.Width; var outputEncoding = opts.EncodingOverride ?? sourceEncoding; if (outputEncoding != IcoEncodingType.Bitmap && opts.BitmapEncodingOverride.HasValue) { Reporter.WarnLine(IcoErrorCode.OnlySupportedOnBitmaps, "Ignoring bitmap encoding configuration because we're not emitting a bitmap frame"); } if (outputEncoding != sourceEncoding) { canSourceBePreserved = false; } if (canSourceBePreserved || opts.KeepRawFrameData) { frame.RawData = sourceFile; } else { switch (outputEncoding) { case IcoEncodingType.Bitmap: WriteBmpFrame(opts, context, frame); break; case IcoEncodingType.Png: WritePngFrame(opts, context, frame); break; } } if (opts.BitDepthOverride.HasValue) { frame.Encoding.ClaimedBitDepth = (uint)opts.BitDepthOverride.Value; } return(frame); }
public void HandleReceive() { lock (mutex) { do { if (offset < DataSizeOffset + DataSize) { break; } // 先读取包的size int packet_size = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToInt32(buffer, DataSizeOffset)); int proto_total_len = packet_size + WrapperLen; if (offset < proto_total_len) { break; } // 读取包id int id = ByteOrderConverter.NetworkToHostOrder(BitConverter.ToInt16(buffer, HandlerIdOfffset)); // 处理具体的包协议 object proto_data = null; int proto_data_deserialize_offset = DataOffset; switch (id) { case ResponseId.JoinRoomResult: { proto_data = JoinRoomResult.Deserialize(buffer, ref proto_data_deserialize_offset); } break; case ResponseId.BattleStart: { proto_data = BattleStart.Deserialize(buffer, ref proto_data_deserialize_offset); } break; case ResponseId.Tick: { proto_data = TickList.Deserialize(buffer, ref proto_data_deserialize_offset); } break; default: break; } ; if (proto_data != null) { ProtoData data = new ProtoData(); data.id = id; data.proto = proto_data; proto_data_queue.Enqueue(data); } // 包体读取完毕 Array.Copy(buffer, proto_total_len, buffer, 0, offset - proto_total_len); offset = offset - proto_total_len; }while(true); } }
public static void WriteInt(byte[] buf, int val, ref int offset) { byte[] bytes = BitConverter.GetBytes(ByteOrderConverter.HostToNetworkOrder(val)); Array.Copy(bytes, 0, buf, offset, bytes.Length); offset += bytes.Length; }