• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SamboyCoding / Cpp2IL / 11860274675

15 Nov 2024 04:56PM UTC coverage: 28.447%. Remained the same
11860274675

push

github

SamboyCoding
CI: Apparently I specify dotnet-version in 3 places

1265 of 6190 branches covered (20.44%)

Branch coverage included in aggregate %.

3381 of 10142 relevant lines covered (33.34%)

126523.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.63
/Cpp2IL.Core/Extensions/StreamExtensions.cs
1
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Text;
5
using System.IO.Compression;
6

7
namespace Cpp2IL.Core.Extensions;
8

9
public static class StreamExtensions
10
{
11
    public static uint ReadUnityCompressedUint(this Stream stream)
12
    {
13
        if (stream.Position == stream.Length)
57!
14
            throw new EndOfStreamException();
×
15

16
        var b = stream.ReadByte();
57✔
17
        if (b < 128)
57✔
18
            return (uint)b;
51✔
19
        if (b == 240)
6!
20
        {
21
            //Full Uint
22
#if NET7_0_OR_GREATER
23
            Span<byte> buffer = stackalloc byte[4];
×
24
            stream.ReadExactly(buffer);
×
25
            return BitConverter.ToUInt32(buffer);
×
26
#elif NET5_0_OR_GREATER
27
            Span<byte> buffer = stackalloc byte[4];
28
            stream.Read(buffer);
29
            return BitConverter.ToUInt32(buffer);
30
#else
31
            var buffer = new byte[4];
32
            stream.Read(buffer, 0, 4);
33
            return BitConverter.ToUInt32(buffer, 0);
34
#endif
35
        }
36

37
        //Special constant values
38
        if (b == byte.MaxValue)
6!
39
            return uint.MaxValue;
×
40
        if (b == 254)
6!
41
            return uint.MaxValue - 1;
×
42

43
        if ((b & 192) == 192)
6!
44
        {
45
            //3 more to read
46
            return (uint)((b & ~192U) << 24 | (uint)(stream.ReadByte() << 16) | (uint)(stream.ReadByte() << 8) | (uint)stream.ReadByte());
×
47
        }
48

49
        if ((b & 128) == 128)
6!
50
        {
51
            //1 more to read
52
            return (uint)((b & ~128U) << 8 | (uint)stream.ReadByte());
6✔
53
        }
54

55

56
        throw new Exception($"How did we even get here? Invalid compressed int first byte {b}");
×
57
    }
58

59
    public static int ReadUnityCompressedInt(this Stream stream)
60
    {
61
        //Ref libil2cpp, il2cpp\utils\ReadCompressedInt32
62
        var unsigned = stream.ReadUnityCompressedUint();
15✔
63

64
        if (unsigned == uint.MaxValue)
15!
65
            return int.MinValue;
×
66

67
        var isNegative = (unsigned & 1) == 1;
15✔
68
        unsigned >>= 1;
15✔
69
        if (isNegative)
15!
70
            return -(int)(unsigned + 1);
×
71

72
        return (int)unsigned;
15✔
73
    }
74

75
    public static string ReadUnicodeString(this BinaryReader reader)
76
    {
77
        List<byte> bytes = [];
×
78
        var continueReading = true;
×
79
        var lastWasNull = false;
×
80
        while (continueReading)
×
81
        {
82
            var b = reader.ReadByte();
×
83

84
            if (b == 0 && lastWasNull)
×
85
                //Double null is a terminator for unicode strings
86
                continueReading = false;
×
87

88
            lastWasNull = b == 0;
×
89
            bytes.Add(b);
×
90
        }
91

92
        bytes.Add(reader.ReadByte()); //Last byte of null terminator will always be skipped - unskip it
×
93

94
        return Encoding.Unicode.GetString(bytes.ToArray()).TrimEnd('\0');
×
95
    }
96

97
    public static byte[] ReadBytes(this Stream stream)
98
    {
99
        using var ms = new MemoryStream();
×
100
        stream.CopyTo(ms);
×
101
        return ms.ToArray();
×
102
    }
×
103

104
    public static byte[] ReadBytes(this ZipArchiveEntry entry) => entry.Open().ReadBytes();
×
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc