-
Notifications
You must be signed in to change notification settings - Fork 3
/
StructureSegment.cs
108 lines (99 loc) · 3.6 KB
/
StructureSegment.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Text;
namespace MapleShark
{
public sealed class StructureSegment
{
private byte[] mBuffer;
public StructureSegment(byte[] pBuffer, int pStart, int pLength)
{
mBuffer = new byte[pLength];
Buffer.BlockCopy(pBuffer, pStart, mBuffer, 0, pLength);
}
public byte? Byte { get { if (mBuffer.Length >= 1) return mBuffer[0]; return null; } }
public sbyte? SByte { get { if (mBuffer.Length >= 1) return (sbyte)mBuffer[0]; return null; } }
public ushort? UShort { get { if (mBuffer.Length >= 2) return BitConverter.ToUInt16(mBuffer, 0); return null; } }
public short? Short { get { if (mBuffer.Length >= 2) return BitConverter.ToInt16(mBuffer, 0); return null; } }
public uint? UInt { get { if (mBuffer.Length >= 4) return BitConverter.ToUInt32(mBuffer, 0); return null; } }
public int? Int { get { if (mBuffer.Length >= 4) return BitConverter.ToInt32(mBuffer, 0); return null; } }
public ulong? ULong { get { if (mBuffer.Length >= 8) return BitConverter.ToUInt64(mBuffer, 0); return null; } }
public long? Long { get { if (mBuffer.Length >= 8) return BitConverter.ToInt64(mBuffer, 0); return null; } }
public long? FlippedLong
{
get
{
if (mBuffer.Length >= 8)
{
long time = BitConverter.ToInt64(mBuffer, 0);
time = (long)(
((time << 32) & 0xFFFFFFFF) |
(time & 0xFFFFFFFF)
);
return time;
}
return null;
}
}
public string IpAddress {
get
{
if (mBuffer.Length == 4)
{
return string.Format("{0}.{1}.{2}.{3}", mBuffer[0].ToString(), mBuffer[1].ToString(), mBuffer[2].ToString(), mBuffer[3].ToString());
}
return null;
}
}
public DateTime? Date
{
get
{
try
{
if (mBuffer.Length >= 8)
return DateTime.FromFileTimeUtc(BitConverter.ToInt64(mBuffer, 0));
}
catch { }
return null;
}
}
public DateTime? FlippedDate
{
get
{
try
{
if (mBuffer.Length >= 8)
{
long time = BitConverter.ToInt64(mBuffer, 0);
time = (long)(
((time << 32) & 0xFFFFFFFF) |
(time & 0xFFFFFFFF)
);
return DateTime.FromFileTimeUtc(time);
}
}
catch { }
return null;
}
}
public string String
{
get
{
if (mBuffer.Length == 0) return null;
if (mBuffer[0] == 0x00) return "";
for (int index = 0; index < mBuffer.Length; ++index) if (mBuffer[index] == 0x00) return Encoding.ASCII.GetString(mBuffer, 0, index);
return Encoding.ASCII.GetString(mBuffer, 0, mBuffer.Length);
}
}
public string Length
{
get
{
return mBuffer.Length + (mBuffer.Length != 1 ? " bytes" : " byte");
}
}
}
}