-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cs
executable file
·76 lines (68 loc) · 1.94 KB
/
Util.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
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
namespace FileWalk
{
/// <summary>
/// Utility functions
/// </summary>
public static class Util
{
public static string ReadFourCC(BinaryReader reader)
{
byte[] fourcc = new byte[4];
reader.Read(fourcc, 0, fourcc.Length);
return Encoding.ASCII.GetString(fourcc);
}
public static int EndianFlip(int i, int bytes)
{
return (int) EndianFlip((uint) i, bytes);
}
public static uint EndianFlip(uint u, int bytes)
{
Debug.Assert(bytes >= 2 && bytes <= 4);
if (bytes == 4)
{
return EndianFlip32(u);
}
else if (bytes == 3)
{
return EndianFlip24(u);
}
else if (bytes == 2)
{
return EndianFlip16(u);
}
return u;
}
public static uint EndianFlip16(uint u)
{
return (((u & 0x000000ff) << 8) +
((u & 0x0000ff00) >> 8));
}
public static uint EndianFlip24(uint u)
{
return (((u & 0x000000ff) << 16) +
((u & 0x0000ff00)) +
((u & 0x00ff0000) >> 16));
}
public static uint EndianFlip32(uint u)
{
return (((u & 0x000000ff) << 24) +
((u & 0x0000ff00) << 8) +
((u & 0x00ff0000) >> 8) +
((u & 0xff000000) >> 24));
}
public static uint ReadUimsbf(BinaryReader reader, int bytes)
{
Debug.Assert(bytes >= 1 && bytes <= 4);
uint i = reader.ReadByte();
for (int j = 0; j < bytes - 1; j++)
{
i = i << 8 | reader.ReadByte();
}
return i;
}
}
}