Private/Utils/Get-FileEncoding.ps1

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<#
    .SYNOPSIS
        Guess encoding of text file

    .PARAMETER Path
        Path to file to examine

    .OUTPUTS
        [Encoding] object of detected encoding

    .LINK
        https://unicodebook.readthedocs.io/guess_encoding.html
#>

function Get-FileEncoding
{
    param
    (
        [string]$Path
    )

    if (-not (Test-Path -Path $Path -PathType Leaf))
    {
        throw "File not found: $Path"
    }

    $bytes = [IO.File]::ReadAllBytes((Resolve-Path -Path $Path).Path)

    try
    {
        # 1. Check BOM
        if ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF)
        {
            return New-Object System.Text.UTF8Encoding($true)
        }

        if ($bytes.Length -ge 4)
        {
            if ($bytes[0] -eq 0x00 -and $bytes[1] -eq 0x00 -and $bytes[2] -eq 0xFE -and $bytes[3] -eq 0xFF)
            {
                # UTF32-LE
                return New-Object System.Text.UTF32Encoding($false, $true)
            }

            if ($bytes[0] -eq 0xFF -and $bytes[1] -eq 0xFE -and $bytes[2] -eq 0x00 -and $bytes[3] -eq 0x00)
            {
                # UTF32-BE
                return New-Object System.Text.UTF32Encoding($true, $true)
            }
        }

        if ($bytes.Length -ge 2)
        {
            if ($bytes[0] -eq 0xFE -and $bytes[1] -eq 0xFF)
            {
                # UTF-16 LE
                return New-Object System.Text.UTF32Encoding($false, $true)
            }

            if ($bytes[0] -eq 0xFF -and $bytes[1] -eq 0xFE)
            {
                # UTF-16 BE
                return New-Object System.Text.UTF32Encoding($true, $true)
            }
        }

        # Read rest of file and guess encoding
        $isUnicode = $false

        for ($i = 0; $i -lt $bytes.Length; ++$i)
        {
            $byte = $bytes[$i]

            if ($byte -lt 32 -and (9, 10, 13) -inotcontains $byte)
            {
                # CTRL char and not whitespace
                $isUnicode = $true
            }

            if ($byte -lt 0x7F)
            {
                # 1 byte sequence: U+0000..U+007F
                continue
            }

            $isUnicode = $true

            if (0xC2 -le $byte -and $byte -le 0xDF)
            {
                # 0b110xxxxx: 2 bytes sequence
                $codeLength = 2
            }
            elseif (0xE0 -le $byte -and $byte -le 0xEF)
            {
                # 0b1110xxxx: 3 bytes sequence
                $codeLength = 3
            }
            elseif (0xF0 -le $byte -and $byte -le 0xF4)
            {
                # 0b11110xxx: 4 bytes sequence
                $codeLength = 4
            }
            else
            {
                # Unicode - going to assume LE as windows and moxt linux run on x86 architecture
                return New-Object System.Text.UTF32Encoding($false, $false)
            }

            if ($i + $codeLength - 1 -ge $bytes.Length)
            {
                # truncated string or invalid byte sequence
                throw "Invalid text file format - cannot determine encoding"
            }

            # Check continuation bytes: bit 7 should be set, bit 6 should be
            # unset (b10xxxxxx).
            for ($j = 1; $j -lt $codeLength; ++$j)
            {
                if ($bytes[$i + $j] -band 0xC0 -ne 0x80)
                {
                    # Unicode - going to assume LE as windows and moxt linux run on x86 architecture
                    return New-Object System.Text.UTF32Encoding($false, $false)
                }
            }

            if ($codeLength -eq 2)
            {
                # 2 bytes sequence: U+0080..U+07FF
                $b0 = [int]$bytes[$i]
                $b1 = [int]$bytes[$i + 1]
                $ch = (($b0 -band 0x1f) -shl 6) + ($b1 -band 0x3f)

                if ($ch -ge 0x0800)
                {
                    # Unicode - going to assume LE as windows and moxt linux run on x86 architecture
                    return New-Object System.Text.UTF32Encoding($false, $false)
                }
            }
            elseif ($codeLength -eq 3)
            {
                # 3 bytes sequence: U+0800..U+FFFF
                $b0 = [int]$bytes[$i]
                $b1 = [int]$bytes[$i + 1]
                $b2 = [int]$bytes[$i + 2]
                $ch = (($b0 -band 0x0f) -shl 12) + (($b1 -band 0x3f) -shl 6) + ($b2 -band 0x3f)

                if ($ch -lt 0x0800)
                {
                    # Unicode - going to assume LE as windows and moxt linux run on x86 architecture
                    return New-Object System.Text.UTF32Encoding($false, $false)
                }
            }
            elseif ($codeLength -eq 4)
            {
                # 4 bytes sequence: U+10000..U+10FFFF
                $b0 = [int]$bytes[$i]
                $b1 = [int]$bytes[$i + 1]
                $b2 = [int]$bytes[$i + 2]
                $b2 = [int]$bytes[$i + 3]
                $ch = (($b0 -band 0x07) -shl 18) + (($b1 -band 0x3f) -shl 12) + (($b2 -band 0x3f) -shl 6) + ($b3 -band 0x3f)

                if (($ch -lt 0x10000) -or (0x10FFFF -lt $ch))
                {
                    # Unicode - going to assume LE as windows and moxt linux run on x86 architecture
                    return New-Object System.Text.UTF32Encoding($false, $false)
                }
            }
        }

        # If we make it here, then UTF8 (unicode) no BOM or ASCII
        if ($isUnicode)
        {
            return New-Object System.Text.UTF8Encoding($false, $false)
        }

        return New-Object System.Text.ASCIIEncoding
    }
    finally
    {
        # Garbage collect
        $bytes = $null
    }
}